Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func requestPayment() {
- loading(stop: false)
- if (selectedMethod == nil) {
- let addCardViewController = STPAddCardViewController()
- addCardViewController.delegate = self
- // Present add card view controller
- let navigationController = UINavigationController(rootViewController: addCardViewController)
- present(navigationController, animated: true)
- return
- }
- print(checkoutViewController.paymentAmount*100.0)
- let user = Auth.auth().currentUser!
- var rMap = [String: Any]()
- if (categoriesTable.tableNumber == 0) {
- let fs = Firestore.firestore()
- var tmpArr = [categoriesTable.Order]()
- for order in categoriesTable.orderList {
- tmpArr.append(order.value)
- }
- let jString = try? JSONEncoder().encode(tmpArr)
- let orderString = String(data: jString!, encoding: String.Encoding.utf8) as String?
- rMap.updateValue(orderString as Any, forKey: "Items")
- let orderName = "Order Number: #"+String(Int(arc4random_uniform(100000) + 1))
- rMap.updateValue(orderName, forKey: "OrderName")
- let ownID = Auth.auth().currentUser?.uid
- rMap.updateValue(ownID as Any, forKey: "OwnerID")
- rMap.updateValue(Date(), forKey: "TimePlaced")
- rMap.updateValue(selectedMethod?.label as Any, forKey: "PaymentMethod")
- rMap.updateValue(categoriesTable.restaurantIdentifier, forKey: "RestaurantID")
- rMap.updateValue(checkoutViewController.paymentAmount, forKey: "PaidAmount")
- if let date = checkoutViewController.selectedDate {
- rMap.updateValue(date, forKey: "AcceptedTime")
- }
- var ref: DocumentReference? = nil
- ref = fs.collection("takeaway").addDocument(data: rMap) { err in
- if let err = err {
- UserResponse.showError(err.localizedDescription)
- self.loading(stop: true)
- return
- }
- print("Added takeaway")
- let tid = ref!.documentID
- checkoutViewController.takeawayID = tid
- Alamofire.request(/* <redacted>, but this code gives an paymentIntent as json*/)
- .validate(statusCode: 200...300)
- .responseJSON { (response) in
- switch response.result {
- case .success(let json):
- let piSecret = (json as? [String: AnyObject])?["client_secret"] as? String
- if piSecret == nil {
- UserResponse.showError("Failed to create payment intent, see logs for response")
- self.loading(stop: true)
- return
- } else {
- print("Got PI: " + piSecret!)
- STPPaymentHandler.shared().handleNextAction(forPayment: piSecret!, with: self, returnURL: "izumi://stripe") { (status, paymentIntent, error) in
- switch (status) {
- case .succeeded:
- self.performAction(for: paymentIntent!)
- break
- case .canceled:
- // Handle cancel
- print("Cancelled")
- break
- case .failed:
- // Handle error
- print(error!.localizedDescription)
- break
- @unknown default:
- print("Fatal error")
- }
- }
- }
- break
- case .failure(let error):
- print(response.description)
- UserResponse.showError(error.localizedDescription)
- self.loading(stop: true)
- }
- }
- }
- } else {
- Alamofire.request(/* <redacted>, but this code gives an paymentIntent as json*/)
- .validate(statusCode: 200...300)
- .responseJSON { (response) in
- switch response.result {
- case .success(let json):
- let piSecret = (json as? [String: AnyObject])?["client_secret"] as? String
- if piSecret == nil {
- UserResponse.showError("Failed to create payment intent, see logs for response")
- self.loading(stop: true)
- return
- } else {
- print("Got PI: " + piSecret!)
- STPPaymentHandler.shared().handleNextAction(forPayment: piSecret!, with: self, returnURL: "izumi://stripe") { (status, paymentIntent, error) in
- switch (status) {
- case .succeeded:
- self.performAction(for: paymentIntent!)
- break
- case .canceled:
- // Handle cancel
- print("Cancelled")
- break
- case .failed:
- // Handle error
- print(error!.localizedDescription)
- break
- @unknown default:
- print("Fatal error")
- }
- }
- }
- break
- case .failure(let error):
- UserResponse.showError(error.localizedDescription)
- self.loading(stop: true)
- print(response.description)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement