Search code examples
braintree

Handle Different Cases Using Drop-In UI


I am trying to use the Braintree Drop-In UI with a subscription service on my website. How do I tell my server how to create the subscription?

For Instance:

A user already has a payment method in the vault so when they hit submit my server just uses the payment nonce given to create the subscription since they used a vaulted method.

BUT, what if a user adds a new payment method? How does my server know to create a new payment method for them since it is not vaulted and I cannot just use the nonce to make a new subscription?

Really what I need to know is how can my server know the user is adding a new method versus using one already vaulted in the Drop-In UI.


Solution

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

    To create a subscription using an existing payment method, first generate a client_token, passing in a customer_id.

    @client_token = Braintree::ClientToken.generate(
      :customer_id => a_customer_id
    )
    

    After passing this client_token into your Drop-in UI, the customer's previous payment methods will be within the form. The customer can then select one of these payment methods, or create a new one, and the new one will be selected.

    After form submission, a payment_method_nonce that corresponds to the selected payment_method will be sent to your server. You can then call Subscription.create, passing in the payment_method_nonce. See our payment_method_nonce documentation for more information.

    result = Braintree::Subscription.create(
      :payment_method_nonce => "payment_method_nonce",
      :plan_id => "silver_plan"
    )