Advertisement
Guest User

requestPayment

a guest
Jul 22nd, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.58 KB | None | 0 0
  1.  
  2.     func requestPayment() {
  3.         loading(stop: false)
  4.        
  5.         if (selectedMethod == nil) {
  6.             let addCardViewController = STPAddCardViewController()
  7.             addCardViewController.delegate = self
  8.            
  9.             // Present add card view controller
  10.             let navigationController = UINavigationController(rootViewController: addCardViewController)
  11.             present(navigationController, animated: true)
  12.             return
  13.         }
  14.        
  15.         print(checkoutViewController.paymentAmount*100.0)
  16.         let user = Auth.auth().currentUser!
  17.         var rMap = [String: Any]()
  18.         if (categoriesTable.tableNumber == 0) {
  19.             let fs = Firestore.firestore()
  20.            
  21.             var tmpArr = [categoriesTable.Order]()
  22.             for order in categoriesTable.orderList {
  23.                 tmpArr.append(order.value)
  24.             }
  25.            
  26.             let jString = try? JSONEncoder().encode(tmpArr)
  27.            
  28.             let orderString = String(data: jString!, encoding: String.Encoding.utf8) as String?
  29.            
  30.             rMap.updateValue(orderString as Any, forKey: "Items")
  31.             let orderName = "Order Number: #"+String(Int(arc4random_uniform(100000) + 1))
  32.             rMap.updateValue(orderName, forKey: "OrderName")
  33.             let ownID = Auth.auth().currentUser?.uid
  34.             rMap.updateValue(ownID as Any, forKey: "OwnerID")
  35.             rMap.updateValue(Date(), forKey: "TimePlaced")
  36.             rMap.updateValue(selectedMethod?.label as Any, forKey: "PaymentMethod")
  37.             rMap.updateValue(categoriesTable.restaurantIdentifier, forKey: "RestaurantID")
  38.             rMap.updateValue(checkoutViewController.paymentAmount, forKey: "PaidAmount")
  39.            
  40.             if let date = checkoutViewController.selectedDate {
  41.                 rMap.updateValue(date, forKey: "AcceptedTime")
  42.             }
  43.             var ref: DocumentReference? = nil
  44.             ref = fs.collection("takeaway").addDocument(data: rMap) { err in
  45.                 if let err = err {
  46.                     UserResponse.showError(err.localizedDescription)
  47.                     self.loading(stop: true)
  48.                     return
  49.                 }
  50.                 print("Added takeaway")
  51.                
  52.                 let tid = ref!.documentID
  53.                 checkoutViewController.takeawayID = tid
  54.                 Alamofire.request(/* <redacted>, but this code gives an paymentIntent as json*/)
  55.                     .validate(statusCode: 200...300)
  56.                     .responseJSON { (response) in
  57.                         switch response.result {
  58.                         case .success(let json):
  59.                             let piSecret = (json as? [String: AnyObject])?["client_secret"] as? String
  60.                             if piSecret == nil {
  61.                                 UserResponse.showError("Failed to create payment intent, see logs for response")
  62.                                 self.loading(stop: true)
  63.                                 return
  64.                             } else {
  65.                                 print("Got PI: " + piSecret!)
  66.                                
  67.                                 STPPaymentHandler.shared().handleNextAction(forPayment: piSecret!, with: self, returnURL: "izumi://stripe") { (status, paymentIntent, error) in
  68.                                     switch (status) {
  69.                                     case .succeeded:
  70.                                         self.performAction(for: paymentIntent!)
  71.                                         break
  72.                                     case .canceled:
  73.                                         // Handle cancel
  74.                                         print("Cancelled")
  75.                                         break
  76.                                     case .failed:
  77.                                         // Handle error
  78.                                         print(error!.localizedDescription)
  79.                                         break
  80.                                     @unknown default:
  81.                                         print("Fatal error")
  82.                                     }
  83.                                 }
  84.                             }
  85.                             break
  86.                         case .failure(let error):
  87.                             print(response.description)
  88.                             UserResponse.showError(error.localizedDescription)
  89.                             self.loading(stop: true)
  90.                         }
  91.                 }
  92.             }
  93.         } else {
  94.             Alamofire.request(/* <redacted>, but this code gives an paymentIntent as json*/)
  95.                 .validate(statusCode: 200...300)
  96.                 .responseJSON { (response) in
  97.                     switch response.result {
  98.                     case .success(let json):
  99.                         let piSecret = (json as? [String: AnyObject])?["client_secret"] as? String
  100.                         if piSecret == nil {
  101.                             UserResponse.showError("Failed to create payment intent, see logs for response")
  102.                             self.loading(stop: true)
  103.                             return
  104.                         } else {
  105.                             print("Got PI: " + piSecret!)
  106.                            
  107.                             STPPaymentHandler.shared().handleNextAction(forPayment: piSecret!, with: self, returnURL: "izumi://stripe") { (status, paymentIntent, error) in
  108.                                 switch (status) {
  109.                                 case .succeeded:
  110.                                     self.performAction(for: paymentIntent!)
  111.                                     break
  112.                                 case .canceled:
  113.                                     // Handle cancel
  114.                                     print("Cancelled")
  115.                                     break
  116.                                 case .failed:
  117.                                     // Handle error
  118.                                     print(error!.localizedDescription)
  119.                                     break
  120.                                 @unknown default:
  121.                                     print("Fatal error")
  122.                                 }
  123.                             }
  124.                         }
  125.                         break
  126.                     case .failure(let error):
  127.                         UserResponse.showError(error.localizedDescription)
  128.                         self.loading(stop: true)
  129.                         print(response.description)
  130.                     }
  131.             }
  132.         }
  133.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement