Search code examples
node.jsswiftbraintree

How to identify the paymentMethod Token for selected payment option using braintree BTDropInController to create subcriptions?


How to know which card did the user choose from the dropIn UI (which is stores in the vault)?

Swift code:

let dropIn = BTDropInController(authorization: clientTokenOrTokenizationKey, request: request)
{ (controller, result, error) in
    if (error != nil) {
        print("ERROR")
    } else if (result?.isCancelled == true) {
        print("CANCELLED")
    } else if let result = result {
        // Use the BTDropInResult properties to update your UI
        // result.paymentOptionType
        // result.paymentMethod
        // result.paymentIcon
        // result.paymentDescription
        let urlString = fullURLString(baseURL: baseURL(), apiPath: CHECKOUT_API_PATH)
        let parameters: [String: Any] = [
           "payment_method_nonce" : result.paymentMethod?.nonce ?? ""
        ]

        // ??????????????????? how to get paymentMethod Token?????
    }
}

For transaction we can use result.paymentMethod?.nonce.

Node.js:

gateway.transaction.sale({
    amount: "1.00",
    paymentMethodNonce: nonceFromTheClient, // result.paymentMethod?.nonce ?? from iOS
    options: {
      submitForSettlement: true
    }
  }, function (err, result) {
});

However for subscriptions, we need to use paymentMethodToken, but how do we know which one did the user select?

Node.js:

gateway.subscription.create({
    paymentMethodToken: ???,
    planId: "goldPlanID"
}, function (err, result) {
    console.log('subscription result:', result);
    console.log('subscription err:', err);

    res.send(result)
});

By using find function in customer, we can get the paymentMethods (but we don't know which one the user selected at client/iOS side:

Node.js:

gateway.customer.find(customerBraintreeID, function(err, customer) {
...
}

PaymentMethods.token

{
"paymentMethods":
[ CreditCard {
...
token: 'jxxxxx',
uniqueNumberIdentifier: 'xxxxxxxxxx',
updatedAt: 
venmoSdk: false,
verifications: [],
maskedNumber: '411111******1111',
expirationDate: '01/2020' }
...
}]

Solution

  • Full disclosure, I work at Braintree. Contact Support if you have further questions.

    You can use the payment method nonce returned from the Drop-in UI for creating subscriptions. You do not need to use the payment method token. Rather than passing a paymentMethodToken, use paymentMethodNonce. From the Braintree dev docs:

    A payment method must be vaulted before you can associate it with a subscription, so it's usually simplest to refer to the payment method using its paymentMethodToken. However, there are 2 cases where you can pass a payment method nonce instead of a payment method token:

    • If the nonce was generated by our Drop-in UI and you passed a customerId when generating the client token, or
    • If the nonce was generated from a vaulted payment method belonging to the customer that will own the subscription

    As an example, your request would look like this:

    gateway.subscription.create({
        paymentMethodNonce: nonceFromTheClient,
        planId: "goldPlanID"
    }, function (err, result) {
        console.log('subscription result:', result);
        console.log('subscription err:', err);
    
        res.send(result)
    });