Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: @escaping STPErrorBlock) {
- ProgressHUD.show("Processing Payment")
- preferredPaymentMethod = paymentResult.paymentMethod.stripeId
- APIClient.sharedClient.createAndConfirmPaymentIntent(paymentResult,
- amount: paymentContext.paymentAmount,
- returnURL: "payments-example://stripe-redirect",
- shippingAddress: self.paymentContext.shippingAddress,
- shippingMethod: self.paymentContext.selectedShippingMethod,
- withUserStripeId: withUserStripeId!) { (clientSecret, error) in
- guard let clientSecret = clientSecret else {
- completion(error ?? NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "Unable to parse clientSecret from response"]))
- return
- }
- //doing automatic confirmation I think the below is for manual confirmation
- STPPaymentHandler.shared().handleNextAction(forPayment: clientSecret, with: self, returnURL: nil) { (status, handledPaymentIntent, actionError) in
- switch (status) {
- case .succeeded:
- guard let handledPaymentIntent = handledPaymentIntent else {
- completion(actionError ?? NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "Unknown failure"]))
- return
- }
- if (handledPaymentIntent.status == .requiresConfirmation) {
- // Confirm again on the backend
- APIClient.sharedClient.confirmPaymentIntent(handledPaymentIntent) { clientSecret, error in
- guard let clientSecret = clientSecret else {
- completion(error ?? NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "Unable to parse clientSecret from response"]))
- return
- }
- // Retrieve the Payment Intent and check the status for success
- STPAPIClient.shared().retrievePaymentIntent(withClientSecret: clientSecret) { (paymentIntent, retrieveError) in
- guard let paymentIntent = paymentIntent else {
- completion(retrieveError ?? NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "Unable to parse payment intent from response"]))
- return
- }
- if paymentIntent.status == .succeeded {
- completion(nil)
- }
- else {
- print("Payment intent fail and is")
- print(paymentIntent.status)
- completion(NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "Authentication failed."]))
- }
- }
- }
- } else {
- // Success
- completion(nil)
- }
- case .failed:
- print("Status is: \(status)")
- print("Handled Payment Intent is: \(handledPaymentIntent!.status)")
- completion(actionError)
- case .canceled:
- completion(NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "User canceled authentication."]))
- }
- }
- }
- // completion(nil)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement