如何使用 braintree BTDropInController 创建订阅来识别所选支付选项的支付方法令牌?

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

如何知道用户从 dropIn UI(存储在保险库中)中选择了哪张卡?

Swift代码:

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?????
    }
}

对于transaction我们可以使用result.paymentMethod?.nonce.

Node.js:

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

然而对于订阅,我们需要使用paymentMethodToken,但是我们如何知道用户使用了哪一个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)
});

通过在 customer 中使用 find 函数,我们可以获得 paymentMethods(但我们不知道用户 select 在 client/iOS 端输入了哪一个:

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' }
...
}]

完全公开,我在 Braintree 工作。如果您还有其他问题,请联系 Support

您可以使用从 Drop-in UI 返回的付款方式随机数来创建订阅。您不需要使用付款方式令牌。不要传递 paymentMethodToken,而是使用 paymentMethodNonce。来自 Braintree 开发文档:

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

例如,您的请求如下所示:

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

    res.send(result)
});