How to implement Paypal subscription in a vue application? - node.js

I am working on a project which needs to deducts a fixed amount from a user's account to a business account on monthly basis. I am trying to create a Paypal button client side (vue) for accepting subscriptions on monthly basis. The users deduction information on monthly basis shall be stored in a database. The user shall also provided an option to cancel the subscription. Any help would be greatly appreciated.

Well i found out an answer after a little digging in the documentation provided at https://developer.paypal.com/docs/subscriptions/integrate and went through the steps they provided.
Steps
Create a product through their API by providing access_token in the headers.
After the product has been created, get product-Id from response and then create plan using the product_id in request body.
After the plan has been successfully created copy Plan_id and replace the id in smart subscription button.
paypal.Buttons({
createSubscription: function(data, actions) {
return actions.subscription.create({
'plan_id': 'P-your_plan_id'
});
onApprove: function(data, actions) {
alert('You have successfully created subscription ' + data.subscriptionID);
}
}

Related

Angular/node.js Stripe Checkout Integration (1 account to facilitate payments from third party payer to third party receiver)

Man, I have been at this for some days and can't find a good solution to this. I want to use my account (via public key and private key) to facilitate payments from one account to another (nothing will be going into my account). On the front-end I have:
checkout(amount) {
const strikeCheckout = (<any>window).StripeCheckout.configure({
key: environment.PRODUCTION==='true'?stripeLiveClientId:stripeTestClientId,
locale: 'auto',
token: (stripeToken: any) => {
if(stripeToken){
console.log(stripeToken)
this.createCharge(stripeToken,amount)
}
}
});
strikeCheckout.open({
name: 'Camel Stripe',
description: 'Stripe Checkout',
amount: amount
});
}
Just a small snipet, but essentially this just captures the credit card and email and ensures it is a valid credit card and then makes a stripe token. I then pass this token to the node backend and do:
stripe.paymentIntents.create({
amount: priceInPence,
currency: 'usd',
source: stripeTokenId,
capture: false, // note that capture: false
}).then(charge => {
return res.json({ success: true, message: 'Success', result: charge })
}).catch(error => {
res.status(400).json({ success: false, status: 400, message: error });
});
//})
};
No matter how I structure it, it always end's up getting the payment to my account, the account with the public/private key. Does anyone know a way I can use this token since it has the necessary information, and send the money to another account?
You can't accept payments and move the funds into a completely different Stripe account unless you're using Connect.
To piggy back on Paul's comment, after much research on Stripe Connect I wanted to share my findings. Please note this is specific to my use case and is using angular/express.
Connect offers multi-party payments, meaning you can receive funds from 1 user, then route them into another users account. You will need a stripe connect account. After that you connect an account, aka create account. They offer 3 account types all these account types need to be
Custom: basically is if you don't want the account you're routing to to see anything stripe related, meaning it is white labeled and the user has no dashboard or view of what is going on in the background.
Standard(the one I chose): this account will have a full stripe dashboard, it has all the functionality that an account would have that you make on your own. You will get a unique account id relevant to your specific connect account environment, on their end, they will have multiple account ids depending on how many other connects they are connected to, or if it is an already existing account. You will need to use this api to get the link to have them verify information before they are integrated into your connect environment account link api
Express: they will have a slimmed down version of a dashboard, it is similar to standard with less functionality
At this point you're ready to start routing payments. I did angular on the front-end, here is an example basically this simply creates a stripeToken, which is a verification that the card data is valid. You need to pass this to a backend to make use of it, this will not create any charges.
On the backend, I used express, you use the charge api You pass the stripeToken as the source attribute. One important step is to add the stripe account id you see in your connected accounts dashboard. It should look similar to:
stripe.charges.create({
amount: priceInPence,
currency: 'usd',
source: stripeTokenId,
capture: false, // note that capture: false
},{
stripeAccount:'{{acctId}}'
})
This should be sufficient to get your payments routing to the necessary account while leveraging a simple UI checkout.
One thing to note about the charges API, this is an older API and has less functionality. For my use case (US CA only with only card payments) it is sufficient. If you have a more complicated use case, or maybe your app is for heavy prod use to many users you should look into payment intents.

How to extend a subscription after a customer submits payment

When I direct a user with an existing subscription to the Stripe payment page, Stripe automatically creates a new subscription at the time the customer submits payment.
What is the workflow that will allow me to tell Stripe that I want to extend the existing subscription and not create a new one?
I asked this question to Stripe support and this is the answer I got:
To answer your question, this will depend on your integration if you are using a third party platform to create this option, but with Stripe, this can only be done manually using the reference that we provided, this means that you have to extend the subscription without requiring a customer to make a payment first.
With regard to the third party platform, I don't understand why the API works differently for them and for me. Does this mean Stripe has a secret API and I MUST purchase a third party product to use the service effectively?
this means that you have to extend the subscription without requiring a customer to make a payment first.
This is the part that is giving me grief. This seems like an extraordinarily difficult workflow. At what point do I extend the subscription? What if the customer never pays? What triggers me to un-extend it? How can I reliably report to the customer the date their subscription ends? Extending a subscription seems like a fundamental, mundane task. Can it really be this difficult?
Edit code posted per requests:
SessionCreateOptions options = new SessionCreateOptions
{
PaymentMethodTypes = new List<string> { "card" },
LineItems = new List<SessionLineItemOptions>
{
new SessionLineItemOptions { Price = order.PaymentProviderPlanID, Quantity = 1 }
},
Customer = order.CustomerID,
CustomerEmail = string.IsNullOrEmpty(order.CustomerID)? order.UserEmail : null,
Mode = "subscription",
SuccessUrl = hostURL + "/Subscription/CreateSubscription?session_id={CHECKOUT_SESSION_ID}",
CancelUrl = hostURL + "/SubActivationFailure",
};
Per clarifications to the question in the comments, do nothing and this will happen automatically. This is the default unless you explicitly set a subscription to cancel or for invoice to not be paid automatically.

How to prevent creating duplicate subscription in stripe when trial period is set on subscription

I am encoutering a problem with stripe. let me explain my working
scenario.my requirement is do not charge user for 14 days with card up front
1)user enter card details
2)sca popup appear
3)regardless of user complete the authentication or not a subscription is created in stripe because i set trial_end_date=>now()+14 days
4)user payment fails in some reason and attempt again, another subscription created
i am worried about the duplicate subscription as the stripe will attempt to pay after the 14 days for both of these subscription as it send a Stripe-hosted link for cardholders for both of these subscription
let me give a snapshot of what i have so far
$data['customer']='customerId';
$data['items']=[];
$data['items'][0]['plan']='stripe_plan_id'
$data['default_payment_method']='pm_xxxx'
$data['trial_end']= strtotime('+14 days');
$data['prorate']=true;
$data['tax_percent']=env('VAT_PERCENTAGE');
$data['expand']= ['latest_invoice.payment_intent', 'pending_setup_intent'];
try {
$subscription=\Stripe\Subscription::create($data);
}
catch(Exception $e) {
return response()->json(['success' => false,'message'=>$e->getMessage()]);
}
what i am missing? how to prevent the duplicate subscription scenario.please expain with the correct example which is i am missing.thanks in advance
I think the problem is in your payment flow, there is not a stripe api that explicitly detects duplicate subscription. You are after all allowed to assign more then 1 subscription per customer id. You can create idempotent keys, but that isn't for the same thing you're talking about, idempotent keys are for accidently hitting the submit button twice within the same timeframe.
The solution would be to attach a payment method to your stripe customer id before your subscription trial is over. For example if you are using stripe elements you would call
Node/JS Example below :
const result = await stripe.createPaymentMethod({
type: 'card',
card: card
})
then pass that result to your backend
const result = await stripe.paymentMethods.attach(paymentMethodId, {customerId})
You do not need to create a new subscription, as one has already been created for that user. Credit cards are assigned to customer Ids, and not to subscriptions. Stripe will do the rest.
You do also need to update the customer with the default payment method as follows :
const customer_update = await stripe.customers.update(stripeCustomerId,{invoice_settings: {default_payment_method:paymentMethodId}});
Now when you visit your dashboard you will see a default card assigned to your customer. Once the subscriptions falls out of the trail period, the default card will be charged.
So in this case there wont be a duplicate subscription created, as you are not calling stripe.subscriptions.create again.

What to do when Strong Customer Authentication fails?

I am trying to implement a Subscription signup and payment flow with Stripe.js V3 and Strong Customer Authentication (SCA) like this:
var stripe = Stripe('pk_test_9hA8gecxBFTY3O6kUm7hl16j');
var paymentIntentSecret = 'pi_91_secret_W9';
stripe.handleCardPayment(
paymentIntentSecret
).then(function (result) {
if (result.error) {
// Display error.message in your UI.
} else {
// The payment has succeeded. Display a success message.
}
})
Everything works great when the payment succeeds.
But what am I supposed to do when it fails?
Should I redirect the user to the initial payment screen, so s/he can start over?
When I do that I get this error:
You cannot confirm this PaymentIntent because it has a status of canceled. Only a PaymentIntent with one of the following statuses may be confirmed: requires_confirmation, requires_action.
Or should I delete everything, including the previously created stripe_customer and stripe_subscription, and then start over?
Thanks for any help.
If the payment fails entirely(maybe it was declined, or 3D Secure was attempted but not completed successfully) , the PaymentIntent from the first invoice should be in the requires_payment_method state and the subscription is incomplete.
You can choose to attempt to collect new payment information from the user and use that to complete the invoice payment and activate the subscription. You can re-use the same PaymentIntent throughout this and try as many times as you wish. For example, if you had a payment form with a Card Element for collecting details, you can have the user enter a new card and call this again :
stripe.handleCardPayment(cardElement,
paymentIntentSecret
).then(function(res){...})
Alternatively you can choose to cancel the subscription entirely if you wish. Otherwise if you do nothing, or the customer isn't able to provide a payment method that works, after 24 hours, Stripe effectively cancels the subscription for you.
Your error message seems to indicate the PaymentIntent was cancelled, which might mean you cancelled the subscription, or you're trying this more than 24 hours after the initial payment, I'm not sure.
This link goes into more detail:
https://stripe.com/docs/billing/lifecycle#incomplete

Auth0 subscription plan app_metadata

I'm developing a quiz app which requires authorization for only-subscribed members can see.
How to do that? I'm thinking of putting metadata (is_subscribed) to true for subscribed member and give the scope so he/she can gain permissions.
But, I don't know how to do it. Please help. The docs is so confusing
There are two separate concerns here.
Where to keep the subscription information. app_metadata is fine, or you might choose to do so in a backend database (application specific). A client application will probably handle subscriptions and be in charge of updating that value. If you store the value in app_metadata, you will use Management API v2 to alter the user profile from the application that handles subscriptions.
Add an authorization scope based on the subscription status. In this case, you would use a rule to add a custom scope based on the value of the is_subscribed field. I.e.:
function(user, context, callback) {
if (user.app_metadata && user.app_metadata.is_subscribed) {
context.accessToken.scope = ['read:quiz'];
} else {
// remove the ability to read a quiz if not subscribed.
context.accessToken.scope = [];
}
callback(null, user, context);
}
If you decided to store the subscription information in a backend database instead of in the app_metadata, you would simply access the database from the rule in the above code.

Resources