Find braintree payment method token when failOnDup for paymentMethod.create is called - braintree

I'm trying to create a customer and subscription in the same workflow.
My logic requires that the email be unique so no two vaulted customers will have the same email.
My workflow is to only create a customer if the customer isn't found in the vault. The customer creation process includes the payment method. I need the paymentMethod token in order to create the subscription.
My hope was to not add duplicate paymentMethods so I'm using {failOnDuplicatePaymentMethod: true}. However, given a nonce and a customer, I can check if the customer is a dup, and I can check if the paymentMethod is a dup - but how do I get that exact paymentMethod token if the customer has N payment methods?
I assumed that the failOnDup would return the token of the dup Payment method - but that isn't the case.
What I'm trying to do is have a single subscription signup with email and payment but a customer can enter a different credit card for each subscription. I want the customer to have a single vaulted account, with N payment methods but I need the paymentMethod token of the last paymentMethod in order to create the subscription.

Full disclosure: I work as a developer for Braintree
Braintree does not support a way to retrieve the duplicate payment method after a payment method fails to create because a duplicate payment method exists.
You can retrieve the payment methods from an existing customer and present them via our Drop-in or your own custom integration. I recommend reaching out to Braintree support to help you with your integration and figure out all of your options.

Related

Laravel Cashier - Prevent duplicated Payment Methods

I'm trying to update the payment method of a subscription. I collect the card information using Stripe's js (using a provided setup intent). Then I send the payment method id, provided by Stripe's js, to Laravel to actually update the User's default payment method.
Now, the problem is I want to prevent the User to insert duplicated cards, and the only way to prevent that would be to retrieve all the user current payment methods ($user->paymentMethods();), and check if any of those has the same fingerprint of the one I'm adding as default.
To get the fingerprint of the new payment method I'd need to get the Stripe PaymentMethod object for the id provided by the Stripe's js. The Cashier method to do that would be $user->findPaymentMethod(id).
The problem is that the payment id is not yet added to the User payments methods, so $user->findPaymentMethod($request->payment_method); fails because that payment method does not belong to $user.
The only solution I can think of would be to first add the new payment method, then check for duplicates and remove them ?
It seems like you’ve already found the best approach of adding the method to the user and then looking to see if there are any duplicated fingerprints, even if it is a little inconvenient.
Reviewing the Cashier documentation doesn’t show any obvious way to access the PaymentMethod before it’s attached to the user. It may be worth reaching out to the Laravel devs to see if there is functionality that would make this flow easier to accomplish.

Laravel Cashier - Updating the Payment Method of a Subscription

I'm trying to figure out how to update the Stripe payment method associated with a subscription.
Is it fine if I simply update the default payment method of the user ?
I would use Stripe's JS to get the card information and make a payment method token with it, then update the default payment method server side. Would that be ok ?
Yep, that's correct - follow https://stripe.com/docs/payments/save-and-reuse to create and save a Payment Method to a Customer for future use. After which, pass the newly created Payment Method in invoice_settings.default_payment_method[0].
[0] https://stripe.com/docs/api/customers/update#update_customer-invoice_settings-default_payment_method

Laravel Cashier (Stripe) subscribing using the existing customer with token

I am developing a Laravel application that involves subscription payment. Now I am struggling with subscribing the user with token but using the existing customer. This is the scenario. In my application, user can update their payment/ billing information (basically card). When they update the payment info, they are just adding the card information. Then later, user can make payment or subscribe to whatever they want.
First user will add they payment method or card information. So I create the customer like this.
$user->createAsStripeCustomer($token, array_merge($options, [
'email' => 'email address',
]));
So the above method will create the stripe customer for the user along with the card. Then tomorrow, user might want to subscribe to a channel. Laravel Cashier provide the following method to subscribe.
$user->newSubscription('subscription-name', 'my-plan')->create($token);
Then issue with the above code is that, I have to pass the token again. If I have to pass the token, again, I will have to generate the token again in the Javascript. If I have to generate the token again in the javascript, I will have to ask the user to enter the card information again to get token. So what can I do to get user to subscribe using the existing customer info? How can I do that?
One method is whenever you create a stripe customer, add the user to a free subscription plan so that he doesn't have to pay anything until he subscribes. When the user starts subscription we can simply change the subscription plan to the desired plan using the following method:
$user->subscription('main')
->skipTrial()
->swap('provider-plan-id');
Another method is whenever we create a stripe customer, ask the user to select a subscription plan and put him on the trial period until he subscribes.

Setting a default payment method using the braintree dropin

I am using Braintree to charge customers for their subscriptions (annually). The customer enters his payment method using the Braintree dropin. The payment method is saved and used to pay for the service on a specific date.
Everything works fine until the customer enters another payment method. My backend uses the default payment method to pay for customers services. The problem is that you cannot change the default payment method using the Braintree dropin (setDefault parameter or similar does not exist, although it would be handy).
It has to be done using the backend SDK, where I can call "update" function on PaymentMethod object, like explained here. But that is not possible, because to update a payment method you need payment method token which is not available when changing the payment method using the dropin. With dropin only PaymentMethodNonce object is available, which according to documentation does not include payment method token. So it's basically impossible to set some payment method as default using the nonce information.
I'm looking for a way to enable the customer to change his default payment method using the Braintree dropin.

Braintree : How can i select a payment method and obtain a payment method nonce using a custom UI?

I am trying to use braintree for payments in a web application. The flow i'm after is :
user registration : create a Braintree customer, associate its id with the user
first payment : show a custom UI for the credit card information, use the Braintree tutorial for adding a credit card and make the payment.
second payment : show a list of payment methods for the customer. When he/she selects one, make a payment using the selected payment method.
I am stumped at how i'm supposed to implement the second payment part. Assuming i keep payment methods info and show the client a list of payment methods, how do i obtain a payment method nonce i need to be able to execute a sale Transaction?
To clarify, i'm not using the Dropin UI because :
i need more information than it can show when entering a credit card (like cardholder)
i need to have a custom look-and-feel ui in different languages
I've read the Braintree guides and reference and i couldn't find and resource for a custom ui where i can reuse the payment method information.
Any input is highly appreciated.
I work at Braintree. If you need more help, I suggest you email our support team.
When you store a credit card (by passing a nonce to credit card or payment method create) the response you get back contains a token. You can store this token permanently and use it in the future to make payments on the same card.
You can charge the user with his payment token or customer id: https://developers.braintreepayments.com/guides/transactions/ruby#sale-with-vaulted-payment-methods
result = Braintree::Transaction.sale(
:payment_method_token => "the_token",
:amount => "10.00"
)
# or
result = Braintree::Transaction.sale(
:customer_id => "the_customer_id",
:amount => "10.00"
)

Resources