Press "Enter" to skip to content

Braintree integration in Xamarin Forms (Part.2 )

In our previous article, we covered how to setup the braintree client-side integration to support credit card payments. Today will be extending the Braintree client-side integration to support Apple Pay and Google Pay payments in Xamarin Forms.

Before starting

In iOS, we have to do some setup configurations in the Apple Portal. (Here the steps). Then in the Entitlements.plist file, we need to enable Apple Pay and add the Merchant Id (for dev and prod).

In Android, we also need to do some setup configurations in the Braintree control panel. (Here the steps).

Let’s code

In our Forms project, we added a new method to the IPayService interface called TokenizePlatform, that will handle the tokenization based on the native platform payment offering (Apple Pay in iOS and Google Pay in Android), this method receives the payment amount and the merchant id.

Xamarin iOS project 

Add the package Naxam.BraintreeApplePay.iOS.

The iOSPayService will provide the Apple Pay Braintree implementation for TokenizePlatform method as well as implementing the PKPaymentAuthorizationViewControllerDelegate, which has handling methods for the following events: DidAuthorizePayment, PaymentAuthorizationViewControllerDidFinish and WillAuthorizePayment.

When creating the BTApplePayClient there are some important informations we need to pass:

  • CountryCode (Country where the payment will be processed)
  • CurrencyCode (Currency for processing the payment)
  • PaymentSummaryItems (A summary item description of what you are paying)

Xamarin Android project 

The AndroidPayService will provide the Google Pay Braintree implementation for TokenizePlatform method.

Xamarin Forms project 

In PaymentPageViewModel we call the TokenizePlatform method to process native platform payment option.

Since we are using the same sample for all the payments options (platform, credit card, paypal, etc). Added PaymentOptionEnum, to provide a way to select the payment option to be used.

Also added constants for the MerchantId and the total amount to process.


In the XAML view added the Platform payment option and by using the OnPlatform extension will change the icon according to the native platform option (Google Pay/Apple Pay).

Let’s test

For the sake of this demo, I’m just displaying the tokenization result once you tap on pay but when getting the event OnTokenizationSuccessful is where you should call your backend endpoint to process the payment.

Now that we are ready, the result should be the following.

To test with a valid credit card you should use one of these. In iOS you need to create a sandbox tester account, you can find more information here.

For valid testing amounts you should use one of these.

That’s all for now, check the full source code here.

Make sure to check out the third part of this series about Pay with Paypal Braintree integration (coming soon).

References

Happy Braintree integration!

4 Comments

  1. Will Will

    Great Job (Y)
    We are waiting for the last part

  2. I have been waiting since september 2018 for someone to share this code for xamarin, I just would like to say thank you very much 🙂 and am glad i didn’t just spend 4 months converting the code from Java to C# 🙂

  3. Eric Eric

    Thanks a lot! It’s a hard life without Braintree C# documentation.

    However, I am still having trouble on Google Pay. “Unrecognized app” error is displayed in Google Pay page. After searching for a while, it should related to “ENVIRONMENT_TEST” configuration (https://developers.google.com/pay/api/android/guides/test-and-deploy/integration-checklist) but I can’t find a way to set it in C#.

    Also, in Braintree documentation (https://developers.braintreepayments.com/guides/google-pay/client-side/android/v2), “googleMerchantId” have to be set in production environment, and again I can’t find a way to set it..

    Do you know how to configure those settings? Your hard work is highly appreciated.

    • Rendy Rendy

      In the AndroidManifest.xml make sure to add the wallet metadata:

Comments are closed.