Advertisement
Guest User

AlexAKPasteBin

a guest
Jul 19th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.08 KB | None | 0 0
  1.     func handleAddPaymentOptionButtonTapped() {
  2.         // Setup add card view controller
  3.         let paymentConfiguration = STPPaymentConfiguration()
  4.         paymentConfiguration.requiredBillingAddressFields = .full
  5.         paymentConfiguration.publishableKey = "redacted"
  6.         let theme = STPTheme()
  7.        
  8.         let addCardViewController = STPAddCardViewController(configuration: paymentConfiguration, theme: theme)
  9.         addCardViewController.delegate = self
  10.        
  11.         // Present add card view controller
  12.         let navigationController = UINavigationController(rootViewController: addCardViewController)
  13.         present(navigationController, animated: true)
  14.     }  
  15.  
  16.  
  17.      var cardParams = STPPaymentMethodCardParams()
  18.     let billingDetails = STPPaymentMethodBillingDetails()
  19.    
  20.     func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreateToken token: STPToken, completion: @escaping STPErrorBlock) {
  21.         cardParams.token = token.tokenId
  22.         dismiss(animated: true)
  23.     }    
  24.  
  25. var clientSecret: String?
  26.     func createPaymentIntent() {
  27.         let paymentAmount = 1000
  28.         let paymentMethodTypes = ["card"]
  29.         MyAPIClient.sharedClient.createPaymentIntent(amount: paymentAmount) { (clientSecret) in
  30.             self.clientSecret = clientSecret
  31.         }
  32.     }
  33.    
  34.     func confirmPaymentIntent() {
  35.         if let clientSecret = clientSecret {
  36.             let paymentMethodParams = STPPaymentMethodParams(card: cardParams, billingDetails: nil, metadata: nil)
  37.             let paymentIntentParams = STPPaymentIntentParams(clientSecret: clientSecret)
  38.             paymentIntentParams.paymentMethodParams = paymentMethodParams
  39.            
  40.             let amount = 1000
  41.             MyAPIClient.sharedClient.createAndConfirmPaymentIntent(paymentIntentParams, paymentMethodParams: paymentMethodParams, amount: amount) { (paymentIntent, err) in
  42.                 print(err)
  43.             }
  44.         }
  45.     }
  46.  
  47.  
  48.  
  49. ///////////////// MyApiClient
  50.  
  51.     func createPaymentIntent(amount: Int, completion: @escaping (_ clientSecret: String) -> ()) {
  52.  
  53.         let url = "https://us-central1-foodfactory-813ab.cloudfunctions.net/createStripePaymentIntent"
  54.         let params: [String: Any] = [
  55.             "amount": amount,
  56.             "currency": "USD",
  57.             "payment_method_types": ["card"]
  58.         ]
  59.         Alamofire.request(url, method: .post, parameters: params)
  60.             .validate(statusCode: 200..<300)
  61.             .responseJSON { responseJSON in
  62.                 if let json = responseJSON.result.value as? [String: Any] {
  63.                     if let clientSecret = json["client_secret"] as? String {
  64.                         completion(clientSecret)
  65.                     }
  66.                 }
  67.         }
  68.     }
  69.  
  70.  
  71.     func createAndConfirmPaymentIntent(_ paymentIntentParams: STPPaymentIntentParams,
  72.                                        paymentMethodParams: STPPaymentMethodParams,
  73.                                        amount: Int,
  74.                                        completion: @escaping STPPaymentIntentCompletionBlock) {
  75.        
  76.  
  77.         let url = "https://us-central1-foodfactory-813ab.cloudfunctions.net/confirm_payment"
  78.         guard let piCheck: String = paymentIntentParams.stripeId else {return}
  79.        
  80.         var params: [String: Any] = [
  81.             "payment_intent_id": paymentIntentParams,
  82.             "amount": amount,
  83.             "currency": "gbp",
  84.             "confirm": true,
  85.         ]
  86.         //        params["shipping"] = STPAddress.shippingInfoForCharge(with: shippingAddress, shippingMethod: shippingMethod)
  87.         Alamofire.request(url, method: .post, parameters: params)
  88.             .validate(statusCode: 200..<300)
  89.             .responseJSON(completionHandler: { (response) in
  90.                 switch response.result {
  91.                 case .success(let json):
  92.                     completion(STPPaymentIntent.decodedObject(fromAPIResponse: json as? [AnyHashable: Any]), nil)
  93.                 case .failure(let error):
  94.                     completion(nil, error)
  95.                 }
  96.             })
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement