简体   繁体   中英

Generating multiple Braintree payment method nonce using one hosted form

My application is using Braintree to collect payments on my application. I want to have a checkbox on payment form which can be ticked to store the credit card details to the braintree customer record

However, I cannot reuse the same nonce that is generated to make the payment (I am getting error saying I cannot use the same nonce multiple times).

This is what I am trying to do:

...
$paymentMethodNonce = $this->input->post("payment_method_nonce");

//make payment
Braintree_Transaction::sale(['paymentMethodNonce'       => $paymentMethodNonce,
                                'orderId'               => $orderId,
                                'merchantAccountId'     => $merchantAccountId,
                                'amount'                => $amount,
                                "options"               => ["submitForSettlement" => true]
                               ]);


//create card for existing customer
Braintree_PaymentMethod::create(['paymentMethodNonce' => $paymentMethodNonce,
                                 'customerId' => $customerId,
                                 'options' => ['verifyCard' => true]
                                ]);
...

I do not want to force user to re enter their credit card details again to be able to save it.

Is there a way in Braintree to generate more than one nonce for one hosted form? Or is there a better way to save card than having a checkbox?

Many thanks

Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support .

If you are using version 3 of Braintree's javascript SDK , you can create multiple nonces from the same credit card information by calling hostedFieldsInstance.tokenize() multiple times. Each call to tokenize will create a new nonce from the information present in the payment form.

If you are using the Drop-In form, or version 2 of the javascript SDK , you cannot create multiple nonces from the same set of credit card information. However, you can still use a single nonce to create a credit card for the customer and create a transaction. To do this, create a credit card record in the Braintree Vault for the customer with the nonce. The nonce is consumed by the operation, so you cannot use it again to create the transaction. Instead, use the payment method you just created to create the transaction. Note that you can use this workflow with a single nonce generated from version 3 of the JS SDK as well.

$paymentMethodResult = Braintree_PaymentMethod::create([
    'paymentMethodNonce' => $paymentMethodNonce,
    'customerId' => $customerId,
    'options' => ['verifyCard' => true]
]);

// now use the payment method you just created to run the transaction
$newPaymentMethodToken = $paymentMethodresult->paymentMethod->token;

$transactionResult = Braintree_Transaction::create([
    'paymentMethodToken' = $newPaymentMethodToken,
    'amount' = '20.00',
    'options' => ['submitForSettlement' => true]
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM