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

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.

Related

How can I validate if card has insufficient balance or card is declined in stripe payment using payment intent in Laravel?

I have implemented stripe payment using Laravel. In which I am making payment using payment intent.
When I enter wrong card details, it shows me the error message at the time of creating the token. But when I try to make payment with cards like insufficient balance, lost card, blocked card, etc. from stripe test cards. It accepts payment from all cards instead of showing me the error message and shows me entry in my stripe account.
First I have created token, then created payment intent and then confirmed the payment intent.
Can anyone help me out here?
You can use try catch. In the catch you need to catch paymnet exception. You will get exception list in the stripe documentation.

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.

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

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.

Specifiy Stripe subscription start date using Laravel Cashier

I'm developing an application that uses Stripe, through Laravel Cashier to handle user subscriptions. When the user account is made, It has to be manually verified (has to be done this way due to the business logic) however the subscription starts immediately. Is there a way to pause the subscription, and only start it once each account has been verified? I'm using Laravel 5.
I think something like the following could work:
Create a boolean variable in your "users" table that you could call something like "verify_subscription".
Whenever a new user account is created, set "verify_subscription" to false.
Don't send the subscription off to Stripe (which will pause the subscription) while a new user has their "verify_subscription" set to false.
I can think of two different options at this point: Either create a confirmation email that gets sent to you (instead of or in addition to the user) where when you are ready to unpause the subscription you can click on that link for that particular user which will send the subscription off to Stripe and "unpause" their subscription OR you could create a route with a form where you can look up the user who you would like to unpause and simply search for that user, submit a form that gets processed which does the same thing (unpause the user by sending the subscription off to Stripe).
Hope that gives you some ideas to work with if you haven't already solved this issue!

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