简体   繁体   中英

How can I confirm the Apple Pay payment with Stripe API

So after a couple of days, I was finally able to get the token created as well the payment method, and a payment intent setup this morning. Now I'm confused on how to actually confirm the payment with Apple Pay, in the non-Apple Pay checkout flow, I had this function:

func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: @escaping STPPaymentStatusBlock) {
        guard let paymentIntentClientSecret = paymentIntentClientSecret else {
            return;
        }
        // Collect card details
        
        
        let paymentIntentParams = STPPaymentIntentParams(clientSecret: paymentIntentClientSecret)
        paymentIntentParams.configure(with: paymentResult)
        
        
        // Submit the payment
        let paymentHandler = STPPaymentHandler.shared()
        paymentHandler.confirmPayment(paymentIntentParams, with: self) { (status, paymentIntent, error) in
            switch (status) {
            case .failed:
                self.displayFailureAlert(title: "Payment Failed", message: "There was an error trying to complete the payment, please try again later.")
                self.paymentInProgress = false
                break
            case .canceled:
                self.displayCancelledAlert(title: "Payment Canceled", message: "The payment has been cancelled.")
                self.paymentInProgress = false
                break
            case .succeeded:
                self.displaySuccessAlert(title: "Payment Succeeded", message: "The payment was successful!")
                self.paymentInProgress = false
                break
            @unknown default:
                fatalError()
                break
            }
        }
    }

This works great. The parameters in the STPApplePayContext are a bit different from the STPPaymentContext and I can't use the exact same functionality for it. This is the function I have for the apple Pay checkout:

func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: STPPaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) {
    guard let paymentIntentClientSecret = paymentIntentClientSecret else {
        return;
    }

    let paymentIntentParams = STPPaymentIntentParams(clientSecret: paymentIntentClientSecret)
    
    
    let error = NSError()
    completion(paymentIntentClientSecret, error)
    
}

This is all I have so far, I tried doing STPAPIClient.shared.confirmPaymentIntent(with:) but nothing changed, any suggestions?

Basically you need to follow the instructions starting at step 7 of Stripe's documentation .

Specifically you need to create a Payment Intent on your server, pass its client secret to your app, then implement the delegate methods as follows:

extension CheckoutViewController {
    func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: STPPaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) {
        let clientSecret = ... // Retrieve the PaymentIntent client secret from your backend (see Server-side step above)
        // Call the completion block with the client secret or an error
        completion(clientSecret, error);
    }

    func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPPaymentStatus, error: Error?) {
          switch status {
        case .success:
            // Payment succeeded, show a receipt view
            break
        case .error:
            // Payment failed, show the error
            break
        case .userCancellation:
            // User cancelled the payment
            break
        @unknown default:
            fatalError()
        }
    }
}

See also the documentation for STPApplePayContext , which gives simplified high-level instructions:

  1. Initialize this class with a PKPaymentRequest describing the payment request (amount, line items, required shipping info, etc)
  2. Call presentApplePayOnViewController:completion: to present the Apple Pay sheet and begin the payment process 3 (optional): If you need to respond to the user changing their shipping information/shipping method, implement the optional delegate methods
  3. When the user taps 'Buy', this class uses the PaymentIntent that you supply in the applePayContext:didCreatePaymentMethod:completion: delegate method to complete the payment
  4. After payment completes/errors and the sheet is dismissed, this class informs you in the applePayContext:didCompleteWithStatus: delegate method

You will get the payment response with retrievePaymentIntent in the success handler and you will get all payment-related information after calling this function, Also you can fetch the data with transaction id, using Strip API .

func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPPaymentStatus, error: Error?) {
        switch status {
        case .success:
            // Payment succeeded, show a receipt view
            context.apiClient.retrievePaymentIntent(withClientSecret: clientSecret) { (paymentIntent, error) in
                
                print(paymentIntent?.allResponseFields ?? "")
            }
            break
        case .error:
            // Payment failed, show the error
            break
        case .userCancellation:
            // User cancelled the payment
            break
        @unknown default:
            fatalError()
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM