0
votes

I know that when I create a payment method I can use failOnDuplicatePaymentMethod to block duplicate cards. But when I use the storeInVaultOnSuccess option with Braintree_Transaction::sale, what is the best way to not save the card if it is already saved?

Edit:

Let me clarify my situation to make sure. On my checkout page I am currently using this JavaScript:

braintree.setup(
    myToken,
    'custom',
    {
        id: 'my-form-id',
        hostedFields: {
            ...
        },
        onPaymentMethodReceived: function(obj) {
            ...
        },
        onError: function(obj) {
            ...
        }
    }
);

The customer fills in their CC number, CVV and expiration date and clicks submit and then that onPaymentMethodReceived callback fires. In that JS callback I make an AJAX call to my back-end and pass the nonce. On the back-end I call Braintree_Transaction::sale in order to charge the customer.

I always need Braintree_Transaction::sale to successfully complete so that the sale goes through. And then in addition so this sale, I want the card to be saved if the customer has checked "save my card" and the card isn't already saved.

On this checkout page, the customer does have the option to select a saved card instead of inputting all their card info again, but they may type all the card info in again(for an already saved card) instead of selecting the saved card.

How would you do it given this setup? Does your below setup still apply? If so how exactly would I integrate the below with my above setup? Or do I need to rearrange my UI/UX for this(I think this is a pretty standard checkout flow)?

1

1 Answers

0
votes

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

There isn't a way to prevent duplicate payment methods when making a Braintree_Transaction::sale API call. However, you can still achieve your goal with some settings on your client. Here are those steps:

  1. On your server, create a client token and include the customer_ID and the failOnDuplicatePaymentMethod parameters:

```

$clientToken = $gateway->clientToken()->generate([
    "customerId" => "aCustomerId",
    "options" => [
        "failOnDuplicatePaymentMethod" => true
        ] ]);

```

  1. Use this client token as your authorization when creating the Braintree client instance:

```

var createClient = require('braintree-web/client').create;

createClient({
  authorization: CLIENT_AUTHORIZATION
}, function (createErr, clientInstance) {
  // ...
});

Per Braintree's docs regarding generating a client Token,

If [the failOnDuplicatePaymentMethod] option is passed and the same payment method has already been added to the Vault for any customer, the request will fail. This can only be passed if a $customerId is passed as well. If the check fails, this option will stop the Drop-in from returning a $paymentMethodNonce. This option will be ignored for PayPal, Pay with Venmo, Apple Pay, and Google Pay payment methods.