Stripe SDK integration using swift and flutter

1,006

It looks like you're following the Stripe Custom iOS Integration, using the native PKPaymentAuthorizationViewController.

You should read through the integration steps here: https://stripe.com/docs/mobile/ios/custom#apple-pay

Basically, your next steps would be

  • instantiate a PKPaymentAuthorizationViewController with the paymentRequest
  • Set yourself to its delegate
  • present the PKPaymentAuthorizationViewController
  • implement the relevant delegate methods to get back an Apple Pay token (PKToken)
  • convert PKToken to STPToken (a Stripe token)

All these steps and more are detailed in the link above.

Share:
1,006
Sajad Jaward
Author by

Sajad Jaward

Updated on December 09, 2022

Comments

  • Sajad Jaward
    Sajad Jaward over 1 year

    I am trying to integrate Apple Pay in iOS using flutter. I am using method channels to communicate with swift and get the payment process completed. I have followed the documentation which is in this link

    However, I believe I have stuck in the very ending part which I don't understand how to continue the flow. Since I am using flutter UIs, I don't need iOS ViewControllers.

    This is the code that I have tried so far in the AppDelegate.swift:

    func handleApplePayButtonTapped(result: FlutterResult){
        let merchantIdentifier = "my.apple.merchant.id"
        let paymentRequest = Stripe.paymentRequest(withMerchantIdentifier:merchantIdentifier, country:"US", currency:"USD")
        paymentRequest.paymentSummaryItems = [
        PKPaymentSummaryItem(label:"Fancy Hat", amount:50.00),
        PKPaymentSummaryItem(label:"iHats, Inc", amount:50.00),
        ]
    
        if Stripe.canSubmitPaymentRequest(paymentRequest){
            //next steps ???
            result(String("Can submit payment request"))
        }else{
            result(String("Can't submit payment request"))
        }
    }
    

    I am calling this code in flutter UI using this code:

    Future<void> _doPayment() async {
    String returnMsg;
    try {
      final bool result = await platform.invokeMethod('checkIfDeviceSupportsApplePay');
      if(result){
        final String status = await platform.invokeMethod('handleApplePayButtonTapped');
        print(status);
      }
      returnMsg = '$result';
    } on PlatformException catch (e) {
      returnMsg = "Failed: '${e.message}'.";
    }
    print(returnMsg);}
    

    I already have a Stripe publishable key as well as a Heroku deployed backend. If you checked my swift code, you will see where I am stuck at the moment.

    As I have understood the flow, what is remaining to be done is

    • send the card details to the backend and get a token
    • using the token, send the payment details to the Stripe server

    I am very new to swift language and code samples will be greatly helpful for me to continue with.

    Thank you.

  • Sajad Jaward
    Sajad Jaward about 5 years
    thank you for the clarification. Would you be able to provide a sample implementation for the submitTokenToBackend() method? that would be greatly helpful.
  • hmunoz
    hmunoz over 3 years
    @SajadJaward that entirely depends on how your client<>server communication works. But basically you would post your token ID over to your server-side endpoint. Your server-side endpoint would parse the token and create a Charge on Stripe. And then return a response to your client's request, for your app to display on the client-side