Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: @escaping STPErrorBlock) {
  2. ProgressHUD.show("Processing Payment")
  3.  
  4.  
  5. preferredPaymentMethod = paymentResult.paymentMethod.stripeId
  6.  
  7. APIClient.sharedClient.createAndConfirmPaymentIntent(paymentResult,
  8. amount: paymentContext.paymentAmount,
  9. returnURL: "payments-example://stripe-redirect",
  10. shippingAddress: self.paymentContext.shippingAddress,
  11. shippingMethod: self.paymentContext.selectedShippingMethod,
  12. withUserStripeId: withUserStripeId!) { (clientSecret, error) in
  13. guard let clientSecret = clientSecret else {
  14. completion(error ?? NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "Unable to parse clientSecret from response"]))
  15. return
  16. }
  17.  
  18. //doing automatic confirmation I think the below is for manual confirmation
  19. STPPaymentHandler.shared().handleNextAction(forPayment: clientSecret, with: self, returnURL: nil) { (status, handledPaymentIntent, actionError) in
  20.  
  21.  
  22. switch (status) {
  23. case .succeeded:
  24. guard let handledPaymentIntent = handledPaymentIntent else {
  25. completion(actionError ?? NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "Unknown failure"]))
  26. return
  27. }
  28. if (handledPaymentIntent.status == .requiresConfirmation) {
  29. // Confirm again on the backend
  30. APIClient.sharedClient.confirmPaymentIntent(handledPaymentIntent) { clientSecret, error in
  31. guard let clientSecret = clientSecret else {
  32. completion(error ?? NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "Unable to parse clientSecret from response"]))
  33. return
  34. }
  35.  
  36. // Retrieve the Payment Intent and check the status for success
  37. STPAPIClient.shared().retrievePaymentIntent(withClientSecret: clientSecret) { (paymentIntent, retrieveError) in
  38. guard let paymentIntent = paymentIntent else {
  39. completion(retrieveError ?? NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "Unable to parse payment intent from response"]))
  40. return
  41. }
  42.  
  43. if paymentIntent.status == .succeeded {
  44. completion(nil)
  45. }
  46. else {
  47. print("Payment intent fail and is")
  48. print(paymentIntent.status)
  49. completion(NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "Authentication failed."]))
  50. }
  51. }
  52. }
  53. } else {
  54. // Success
  55. completion(nil)
  56. }
  57. case .failed:
  58.  
  59. print("Status is: \(status)")
  60. print("Handled Payment Intent is: \(handledPaymentIntent!.status)")
  61.  
  62. completion(actionError)
  63. case .canceled:
  64. completion(NSError(domain: StripeDomain, code: 123, userInfo: [NSLocalizedDescriptionKey: "User canceled authentication."]))
  65. }
  66. }
  67. }
  68. // completion(nil)
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement