Process payment on server Apply Pay Stripe

Viewed 21

I am trying to process Apple Pay using Stripe on the backend however I am getting stuck with the completion block. The documentation has the following steps:

  1. Request payment intent which will return a client-secret key
  2. Pass client-secret to completion block
  3. didComplete is called

However I am taking a different approach:

  1. Pass payment method id to backend
  2. Backend runs logic and creates payment intent and captures charge
  3. Client receives 200
  4. Completion block to activate didComplete

Step 4 is where the error occurs. Calling the completion block without a client-secret key crashes the app.


STPIntentClientSecretCompletionBlock - requires optional string for first parameter which is the client-secret

func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: STPPaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) {
        sudo code:
        API.makePayment(paymentInformation.stripeId)
        completion(nil, nil)
    }
    
    func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPPaymentStatus, error: Error?) {
        switch status {
        case .success:
            break
        case .error:
            break
        case .userCancellation:
            break
        @unknown default:
            fatalError()
        }
    }
1 Answers

Take a look here in the Apple Pay iOS docs where there is a "Server-side confirmation" tab. This shows you that you should create and confirm the PaymentIntent on your server and then still pass that PaymentIntent's client secret back to the frontend to properly handle the completion block.

Related