Braintree - Retrieve Payment Method Nonce in Xamarin Forms C#

317 views Asked by At

I'm writing an app written in C# - Xamarin Forms.

I'm simply trying to get a response from Braintree's server so I can process payment.

This response is the payment_method_nonce which is required to process payment.

Here's the client side code provided by Braintree.

<script src="https://js.braintreegateway.com/web/dropin/1.24.0/js/dropin.js"></script>

<div id="dropin-container"></div>
<button id="submit-button" class="button button--small button--green">Purchase</button>

var button = document.querySelector('#submit-button');

braintree.dropin.create({
  authorization: 'xxxxx',
  selector: '#dropin-container'
}, function (err, instance) {
  button.addEventListener('click', function () {
    instance.requestPaymentMethod(function (err, payload) {
      // Submit payload.nonce to your server
    });
  })
});

It generates the credit card form nicely however if you click on the Purchase button, a payment_method_nonce is expected to return from the Braintree server.

My question is, how do I capture this payment_method_nonce variable in C# when the client form is rendered in Javascript, inside a webview?

1

There are 1 answers

1
Khoa On BEST ANSWER

Got it working.

You have to get the payment method token first after you've processed the credit card details.

Then pass this method payment token to get the payment method nonce then you proceed with the transaction.

Here's the code:

// Get the payment method token
var paymentmethod_token = creditCard.Token.ToString();

// Generate a payment method nonce
Result<PaymentMethodNonce> paymentmethodnonce_result = gateway.PaymentMethodNonce.Create(paymentmethod_token);
var nonce = paymentmethodnonce_result.Target.Nonce;