BrainTreePayments create new payment method - node.js

I am going through the BrainTreePayments documentation page and I came across the Payment Methods. The documentation says "To create a new payment method for an existing customer, the only required attributes are the customer ID and payment method nonce."
gateway.paymentMethod.create({
customerId: "12345",
paymentMethodNonce: nonceFromTheClient
}, function (err, result) { });
The question is
1) How do we create a new payment method for a new customer without payment method nonce?
2) How do we verify the validity of an existing payment method?

Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.
1) A payment method is like a container to store payment information in your Vault, whereas the payment method nonce is a way to deliver payment information to Braintree. You use a payment method nonce to deliver payment information to create payment methods, transactions, etc. A payment method can be referenced to generate a payment method nonce from stored payment information, but the payment method must store something. You can't create an empty payment method.
2) To verify an existing payment method, use 'PaymentMethod.update()' and include 'verify_card' in your options. If you have enabled fraud tools in your control panel, newly created payment methods for credit cards will be verified before they are stored in the Vault. Cards that are not valid will not be stored and you will receive an error response.

Related

With Stripe, how to attach a single payment method created from Setup Intent API for multiple customers

With Stripe, how to use the payment method created from setup intent for multiple customers?
According tp the Create Setup Intent API, it only let us define one customer ID.
param name: customer (optional)
description of this param:
attached to the Customer on successful setup. Payment methods attached
to other Customers cannot be used with this SetupIntent.ID of the Customer this SetupIntent belongs to, ifone exists. If present, the SetupIntent’s payment method will be
TLDR
I want to let the same user to reuse same payment method to subscribe items with different presentment currencies,
I assume I should solve this requirement by directly creating multiple customer objects for a single user, and using create Setup Intent API to attach his/her same payment method for multiple "customer objects".
But, I am not sure if this is possible, therefore I welcome any recommendation for alternative approaches, such as using attach payment method to customers API, or any workarounds.
PS: Detail info (for why I am interested to ask this question)
Business requirement:
My business requirement is that customers have to subscribe to international services that must be presented with different presentment currencies. To makes thing even harder, each customer has to use the same list of saved payment methods to subscribe to services with different presentment currencies in the future. In other words, users should not have to re-enter card info again if they subscribe to items in different presentment currencies.
Limitation of the Stripe API:
Why not just use single customer object for subscribing to services (aka "price" objects) with multiple presentment currency?
I tried and it results in an error. According to this Stripe documentation, it indeed says that each customer can only be used for transactions in single currency.
If the user in my app want to subscribe to another service with another
currency, I need to create new "Customer" object in Stripe.
Why not just use /v1/payment_methods/:pm_id/attach api to attch payment method to multiple customer object?
Because according to this Attact payment method to customer API doc, Stripe does not recommend, yet I do not fully understand the risk of not performing any necessary steps as it warns. Therefore, it is just my risk averse instinct to avoid this.
To attach a new PaymentMethod to a customer for future payments, we
recommend you use a SetupIntent or a PaymentIntent with
setup_future_usage. These approaches will perform any necessary steps
to ensure that the PaymentMethod can be used in a future payment.
This is not supported. A payment method can only be consumed once if not attached, and attaching to a customer counts as a consumption. A PM can only be attached to a single customer, so you'll need to collect the payment details for each.
Stripe recommends using that customer's currency for all presentment to optimize for their experience, though that may not be suitable for all use cases.

Stripe -- after confirmCardPayment, how to attach the new card to the customer?

I am using a setupIntent to save a card to a customer for future use. I pass the client_secret from the setupIntent to the confirmCardSetup in js.
Later, I create a paymentIntent with that card. If it is declined and the status is requires_payment_method then I email the customer and give them a form to enter a new card, and subit that using confirmCardPayment in js. This works well and the charge succeeds (or becomes requires_capture).
How do I then attach that new payment method to the customer? If I do nothing, then their old saved card is still their primary payment method.
I only want to keep one card on file for each customer. I know I can detach the old card and then attach the new one, but then it will not have been done using a setupIntent, which is apparently more appropriate if I'm going to be charging the card in the future off session.
You can also save a card for future use during a payment using setup_future_usage=off_session by following this guide. The payment method will be automatically attached to the customer provided on the Payment Intent.
Your existing flow can be used, you'd just extend it by:
Prior to confirming the Payment Intent again with the new payment details, update it to set the setup_future_usage option.
After the payment succeeds, detach the old payment method like you mentioned and, if needed, update your internal reference to the payment method to use for that customer to be the new payment method id from the Payment Intent.

Stripe later payment direct charges, create token error, The customer must have an active payment source attached

I am trying to make a Stripe later payment direct charge on a connected account in Node.js. This is all in test mode using the paymentMethods API.
I create a customer and attach a payment method with stripe.setupIntents.create() and am able to make later payments to my stripe account/the platform without any issues. However I would like to make a direct payment to a connected account using stripe.paymentIntents.create().
Following this guide, https://stripe.com/docs/connect/cloning-saved-payment-methods, I am under the impression I can clone a payment method used for my platform and use it to make a direct payment for a connected account. I attempt to create a token for the customer but receive the error "The customer must have an active payment source attached." despite the customer having an active default payment method that works for my platform, 'pm_1HFjvsLIOnOaY98HTxPugN5i'.
const token = await stripe.tokens.create({customer: 'cus_HpOf9y6TJ5XYlA'}, { stripeAccount: 'acct_1HDuHbBwYBNGN1ir'}
I am further confused by this guide https://stripe.com/docs/payments/payment-methods/connect#cloning-payment-methods, is this just an alternative method to accomplish essentially the same thing? Or do I need to create a new payment method onto the connected account, but wouldn't that defeat the purpose of creating a token?
Great question! In the API call you shared, you're passing only the customer parameter. Cloning a Payment Method requires passing both customer and payment_method like so:
const paymentMethod = await stripe.paymentMethods.create({
customer: 'cus_HpOf9y6TJ5XYlA',
payment_method: 'pm_1HFjvsLIOnOaY98HTxPugN5i',
}, {
stripeAccount: 'acct_1HDuHbBwYBNGN1ir',
});
With Payment Methods there's no concept of a "default source" for a customer—When creating a charge or sharing a payment method you must always specify both the customer ID and payment method ID. The exception to this is Subscriptions, which will look at the invoice_settings.default_payment_method property on the customer and use that for subscription and invoice payments.

Stripe - Create subscriptions with a customer with PaymentMethod instead of Source (Token)

I'm integrating Stripe Subscriptions in our workflow, but when creating a subscription with a Customer created with a paymentMethod instead of source I receive the error This customer has no attached payment source.
If I call in the browser createToken() and attach it to the customer in the source field it works.
We integrated one-time payments using the new docs with SCA that use createPaymentMethod() and not createToken(), so our customers are saved with paymentMethod, not source, like explained here:
https://stripe.com/docs/payments/cards/saving-cards-after-payment#save-payment-method
curl https://api.stripe.com/v1/customers \
-u sk_test_secret_token \
-d payment_method="{{PAYMENT_METHOD_ID}}"
Furthermore, in the migration guide it says to replace createToken() with createPaymentMethod().
In Scenario 2: Charging customers off-session for their initial payment, I saw the following:
To create subscriptions and charge customers off-session for their
initial payment, you need to:
1) Use CreatePaymentMethod to collect payment information
2) Create a customer using the ID of the PaymentMethod you created
3) Create the subscription
4) Set up error handling using handleCardSetup for authentication failures and handleCardPayment for authorization failures
I followed those steps. I don't create a SetupIntent (just like I don't create it in one-time payments and it works in these cases), and receive the error I said before when trying to create subscriptions.
Is it possible to create subscriptions with a customer with paymentMethod instead of source?
(We reuse cards using the customers created with paymentMethod for one-time payments, so it would be very important to be able to reuse the same customer/card for subscriptions, without the need for the user input data in stripe elements or anything of the sort, because it would break our flow for reusing cards)
I was able to solve it by including the payment method in the field default_payment_method.
This field is described as follows:
ID of the default payment method for the subscription. It must belong
to the customer associated with the subscription. If not set, invoices
will use the default payment method in the customer’s invoice
settings.
So I assumed that it would use the payment method I associated with the customer when not defined. It seems it is not happening, tough, so I needed to pass it explicitly (is it a Stripe bug? or creating the customer with paymentMethod doesn't make it the default payment method in the customer’s invoice settings? I will contact Stripe to make sure).
Update (2019-09-23)
I contacted Stripe asking if this was a bug and they replied:
[...] With that being said though, this wouldn't be a bug on our end, rather
expected behavior.
If you're wanting for the Payment Method that you're adding to the
customer object to be used on subscriptions, or invoices, by default
without specifying the default_payment_method when creating the
subscription then you would want to attach the Payment Method and
specify the invoice_settings.default_payment_method parameter when
updating the customer. This information can be found within our API
reference here:
https://stripe.com/docs/api/payment_methods/attach
The parameter to use when updating this can be found on the customer
object itself, here:
https://stripe.com/docs/api/customers/update#update_customer-invoice_settings-default_payment_method
This can also be specified upon customer creation, which can be seen
here:
https://stripe.com/docs/api/customers/create#create_customer-invoice_settings-default_payment_method
Specifying this parameter would indicate that the card being added
would be the default for subscriptions, as well as invoices, so that
the default_payment_method wouldn't need to be specified upon the
subscription, or invoice, creation.

Braintree API: How can I tell which payment_method is associated with my nonce?

Background:
I create a braintree customer with firstName, lastName and email
I use the dropin ui in checkout form and it creates a payment_method and token and sends me a nonce. Good so far.
I need to add a billing address to the payment_method before I charge the nonce...
Question:
How can I discover which payment_method is associated with my nonce?
Edit to add:
There is a paradoxical reference at the bottom of the javascript+PHP page to an otherwise undocumented [paymentMethodNonce] parameter which uses an also undocumented [options][verifyCard] parameter. I suppose I could run [paymentMethodNonce] sans [options] against each payment_method token associated the user and inspect the errors... lol.
I work at Braintree. If you have more questions, I suggest you reach out to our support team.
When you use the Drop-In UI, it doesn't automatically create a payment method, just a nonce. You pass the nonce back to your server and create a payment method with it:
$result = Braintree_PaymentMethod::create(array(
'customerId' => '12345',
'paymentMethodNonce' => 'nonce-from-the-client'
));
If the nonce points to an already-vaulted payment method for that customer, you'll get back the existing payment method rather than a duplicate.
You can then update that payment method to add a billing address before using it to create a transaction.

Resources