How to add ADDON in stripe susbcription? - stripe-payments

Is there a way to add multiple plans with different type in the same subscription?
I am trying to add two plans (one is recurring type and the other one single type) but stripe subscription not accepting it. Getting this error

Yes, you can. While the items property only accepts recurring prices, you can use the add_invoice_items property to add one time prices to the first invoice, when creating your subscription. Like this:
// Example in Node.js
const subscription = await stripe.subscriptions.create({
customer: `{CUSTOMER_ID}`,
items: [
{
price: ‘{RECURRING_PRICE_ID}’,
},
],
…
add_invoice_items: [{
price: `{ONE_TIME_PRICE_ID}`,
}]
})

Related

Downgrading the Stripe Subscription results in user refund

I looked around at other solutions but none of them seem to be use case for me. We're not using webhooks instead Cron jobs on the 1st of the month after payments are processed.
My service offers X hours per month.
When user subscriptions they get X hours.
When the user upgrades they immediately get X hours difference between the two, only on upgrade. This works great, the sum is correct.
My issue is that when the user downgrade stripe issues a refund on the next invoice, incorret.
Here is an example that is incorrect, apprentely the user has nothing to pay the next billing cycle which makes no sense; they should pay the downgraded cost.
The code looks like so:
If it's an upgrade:
subscription = await stripe.subscriptions.update(existingSub.id, {
cancel_at_period_end: false,
proration_behavior: "always_invoice",
default_tax_rates: [taxRate.id],
items: [
{
// We need to upgrade the existing item.
id: existingSub.items.data[0].id,
price: price.id,
quantity: 1,
},
],
});
If it's a new sub
await stripe.subscriptions.create({
customer: req.user.stripeCustomerId,
items: [
{
price: price.id,
},
],
billing_cycle_anchor: endOfMonth.unix(),
default_tax_rates: [taxRate.id],
metadata: {
productId,
paymentMethodId,
amount,
userUid: req.user.uid,
},
default_payment_method: paymentMethodId,
});
I'm not sure how to downgrade.
In your scenario above, the user has already paid for the subscription upfront. When you downgraded the subscription, the difference between the subscriptions was pro-rated and credited to the Customer credit balance [0] and applied to the next invoice.
If you don’t want the difference to be prorated when downgrading (maybe your company has a no refund policy), then you would want to apply proration_behavior='none' [1] when downgrading
You can read more about upgrading/downgrading subscriptions here : https://stripe.com/docs/billing/subscriptions/upgrade-downgrade
[0]https://stripe.com/docs/billing/customer/balance#examples
[1]https://stripe.com/docs/api/subscriptions/update#update_subscription-proration_behavior

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

PayPal subscription and webhooks

I made an website with a service that I am charging for. I want to create a PayPal subscription. I need that subscription to be connected with my backend (firebase functions - node.js) so I can change some data in the database to serve my users different content depending if they are paying or not. I wanted to use PayPal buttons for my subscription but I can't find a way to connect that button with my backend so it seems PayPal buttons aren't optimal for my problem. I can't use Stripe because it's not supported in my country. Can you offer me a different solution for my subscripton payments or show how can I use PayPal?
You can use Paypal Node SDK for your use case instead of relying to the embeddable Paypal subscribe button. The SDK will give you better integration with NodeJs.
There are basically 2 steps to do this:
1.) Define the Billing Plan Object
The billing plan object defines the subscription plan, including the number of cycles, frequency of payment, any setup fees, and so on.
var billingPlanAttribs = {
name: 'Food of the World Club Membership: Standard',
description: 'Monthly plan for getting the t-shirt of the month.',
type: 'fixed',
payment_definitions: [{
name: 'Standard Plan',
type: 'REGULAR',
frequency_interval: '1',
frequency: 'MONTH',
cycles: '11',
amount: {
currency: 'USD',
value: '19.99'
}
}],
merchant_preferences: {
setup_fee: {
currency: 'USD',
value: '1'
},
cancel_url: 'http://localhost:3000/cancel',
return_url: 'http://localhost:3000/processagreement',
max_fail_attempts: '0',
auto_bill_amount: 'YES',
initial_fail_amount_action: 'CONTINUE'
}
};
Of course, you will need to change cancel_url and return_url to your actual Firebase functions endpoints (or localhost if you are running your functions in localhost for development purposes)
2.) Create and Activate Billing Plan, so once you created or defined your billing - you will need to create that object and activate the billing plan like so:
paypal.billingPlan.create(billingPlanAttribs, function (error, billingPlan){
var billingPlanUpdateAttributes;
if (error){
console.error(JSON.stringify(error));
throw error;
} else {
// Create billing plan patch object
billingPlanUpdateAttributes = [{
op: 'replace',
path: '/',
value: {
state: 'ACTIVE'
}
}];
// Activate the plan by changing status to active
paypal.billingPlan.update(billingPlan.id, billingPlanUpdateAttributes, function(error, response){
if (error){
console.error(JSON.stringify(error));
throw error;
} else {
console.log('Billing plan created under ID: ' + billingPlan.id);
}
});
}
});
Again, all of these are documented in Paypal's Developer Section.
Here's also a link to their github example using NodeJs (which is same underlying backend as a Firebase Function)

i am trying to create order but no such sku on stripe api

i am trying to create order but no such sku on stripe api. is it possible to create order on stripe without creating a product? i just want to store the order on stripe.
const orderRes = await stripe.orders.create({
currency: 'usd',
email: 'iamaemail#gmail.com',
items: [
{type: 'sku', parent: 'sku_7hAchfCjchvSHL'},
],
shipping: {
name: 'Jenny Rosen',
address: {
line1: '1234 Main Street',
city: 'San Francisco',
state: 'CA',
country: 'US',
postal_code: '94111',
},
},
}
As you can see in the Stripe docs, the Orders API has been deprecated and is not SCA compliant (which affects if charges made via this API will succeed for European merchants or end-users).
If you look at the Stripe API reference for creating an Order object, you can see that only the currency parameter is required. If you are going to specify a value for the parent parameter for an element in the items hash, you must ensure that you are using a SKU object that was created in Test Mode or Live Mode (whichever matches the environment in which you're trying to create the Order object), and that the SKU is correct. This SKU id appears to not match any SKU id in your Stripe account (which is why you're getting the error that you mention).

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