Stripe discount on upcoming payment of recurring subscription - stripe-payments

I have implemented stripe recurring subscription that billed annually. I've proposed an offer for my customers that if they refer our website to 5 of their friends and they register, they will get 50% off on their subscription.
How I'll implement that discount for that particular customer on next payment?

The simplest option would be to apply a Coupon to the customer’s subscription. Then during the next billing cycle of the subscription, the coupon will be automatically applied. That can be done is two steps (here done node.js):
// Create the new Coupon, once
// Doc: https://stripe.com/docs/api/coupons/create
const coupon = await stripe.coupons.create({
percent_off: 50,
duration: 'once', // other possible value are 'forever' or 'repeating'
});
// Then every time a customer match your criteria, update their subscription
// Doc: https://stripe.com/docs/api/subscriptions/update
const subscription = await stripe.subscriptions.update(
'sub_xxx',
{ coupon: coupon.id }
);
Another option would be to apply the coupon to the customer instead of on the subscription directly. In that case the coupon would apply to all recurring charges for that customer.
// Doc: https://stripe.com/docs/api/customers/update
const customer = await stripe.customers.update(
'cus_xxx',
{ coupon: coupon.id }
);

Related

Payment intent is null on subscription with meter based pricing

I am creating subscription using meter based pricing in stripe
const stripeRequest: Stripe.SubscriptionCreateParams = {
customer: customerId,
items: [
{
price: xxxxxx
},
],
payment_settings: {
payment_method_types: ["card"],
save_default_payment_method: "on_subscription",
},
expand: ["latest_invoice.payment_intent"],
};
const stripeSubscription = await stripe.subscriptions.create(
stripeRequest,
);
stripeSubscription.latest_invoice.payment_intent is coming as null
For metered subscriptions, the customer is billed at the end of the billing period. So when you create a new metered subscription, an invoice is immediately created with a $0 amount. And since there's nothing to pay, it's expected that the invoice will have no PaymentIntent.
However the subscription object will contain a pending_setup_intent property. You could use the client_secret of this SetupIntent to collect a payment method on the frontend.

How to test recurring payment on stripe subscription for due bill?

I have created subscription using stripe api for 1 month interval.
const subscription = await stripe.subscriptions.create(
{
customer: customer.id,
items: [
{
price: productPrice.id,
},
],
// payment_behavior: 'default_incomplete',
expand: ['latest_invoice.payment_intent'],
application_fee_percent: fee,
},
{
stripeAccount: accountId,
}
);
Subscription created successfully based on product and price.but i can't wait for 1 month (next billing) for testing so i need to test now how will work for next payment.it will auto deduct from payment method? or customer will notify about next due and have to pay? there need more code for it?
Stripe has a feature called Test Clocks that allows to move subscriptions forward in time. So you could create a subscription with a test clock, then advance the clock by one month to see exactly what happens.
But to answer your question: if the customer (or the subscription) has a default payment method set, then yes Stripe will automatically attempt to make a payment at the end of every billing cycle.

Once off payment with Stripe using the API ID

I have created a subscription service using Stripe. I can subscribe a user to use recurring payments. This is the relevant code (node):
// Create the subscription
const subscription = await stripe.subscriptions.create({
customer: req.body.customerId,
items: [{ price: req.body.priceId }],
expand: ['latest_invoice.payment_intent'],
});
This works and uses the priceId as shown in the dashboard:
However, it falls over when I sell a product which isn't recurring. I get the error:
The price specified is set to `type=one_time` but this field only accepts prices with `type=recurring`
I understand the error, but I am not sure if I can set a subscription to not be recurring.
My app has 3 tiers:
once off
monthly
yearly
Ideally, I would like not to add a whole new section of code to handle what seems like a subset of what subscriptions do, but even if I do, the paymentIntent object seems to only take an amount rather than the API ID as shown in the picture. Is there any way to do this using the infrastructure I have already built?
You can't create a subscription with a non-recurring price, instead for one-off payments you'd use a PaymentIntent.
Prices are meant for use with subscriptions and the Checkout product, but you can still use the data in them for PaymentIntents. For instance:
// get the price
const price = await stripe.prices.retrieve(req.body.priceId);
// check if the price is recurring or not
if (price.recurring !== null) {
// Create the subscription
const subscription = await stripe.subscriptions.create({
customer: req.body.customerId,
items: [{ price: req.body.priceId }],
expand: ['latest_invoice.payment_intent'],
});
// do something with the subscription
} else {
const pi = await stripe.paymentIntents.create({
customer: req.body.customerId,
currency: 'usd',
amount: price.unit_amount,
});
// do something with the PaymentIntent
}
you can also attach one-time items via the subscription.create call by placing them in add_invoice_items.
see: https://stripe.com/docs/api/subscriptions/create?lang=node#create_subscription-add_invoice_items

Stripe ignoring billing_cycle_anchor when creating a new subscription and an invoice is created straight away. Why is this happening?

I am using Stripe in NodeJS to create subscirptions. I want an invoice to be generated near the end of the billing period. For example, if the billing period is 1-30 Septmeber, I want the invoice to be generated on 30 September. The API docs says that billing_cycle_anchor should do this. But it has no effect when I use it, as an invoice is generated straight away and the user is charged. Code example below.
stripeSubscription = await stripe.subscriptions.create({
customer: id,
items: [{ plan: planId }],
billing_cycle_anchor: moment()
.add('30', 'days')
.unix();,
});
How can I get monthly subscription to generate and charge at the end of the billing period?
Can billing_cycle_anchor be set to the 1st of January, regardless of when the user starts the subscription?
e.g.
this.currentYear = (new Date()).getFullYear();
this.newYear = (new Date(this.currentYear + 1 + '/01/01 00:01')).getTime()/1000;
Then in the function call:
firebase.default
.firestore()
.collection('users')
.doc(user.uid)
.collection('checkout_sessions')
.add({
price: 'price_BLAHBLAH',
billing_cycle_anchor: this.newYear, //1672503000 01/01/2023 12:10 AM
...
})

How can I create a charge without generating an invoice for it in Recurly?

Using node-recurly. The idea is to create a charge without generating an invoice, then create a subscription and have recurly attach the charge to the subscription invoice. However, when I create a charge, the invoice gets generated automatically for it, so the user gets two separate invoices in the email: one for the charge, and one for the subscription.
This is the charge object that I use:
const shippingCharge = {
amount_in_cents: parseFloat(shippingMethod.amount) * 100,
currency: 'USD',
description: `${shippingMethod.provider} ${shippingMethod.servicelevel_name} shipping`,
account: {
account_code: activationCode,
},
};
I pass it to this function that creates a charge:
recurly.transactions.create(chargeObject, (response) => {
... blah blah blah
});
recurly.subscriptions.create is being called next (calls are being made sequentially using promises). The end result is two invoices instead of one.
Recurly's documentation is confusing. When I was trying to create a charge, I made an assumption that I have to create a transaction. After contacting support, I was provided with the link to create a charge. If you look at the code examples on the right, they reference Recurly_Adjustment, not transaction object. So to create a charge I have to create an adjustment, not a transaction. Switching to proper API call fixed the issue and I received a single invoice.
Alex is correct. You will also need to use revenue_schedule_type: at_invoice if you want the charges together. The Recurly API docs do not include NodeJS examples. Here you go:
return recurly.adjustments.create(accountId, {
unit_amount_in_cents: parseFloat(shippingMethod.amount) * 100,
currency: 'USD',
description: `${shippingMethod.provider} ${shippingMethod.servicelevel_name} shipping`,
revenue_schedule_type: 'at_invoice',
accounting_code: accountingCode,
}).then(() => {
// ...create an invoice, subscription, or whatever
});

Resources