英文:
Pay now once and, optionally (!), create a subscription too -- how to do this in Stripe?
问题
在页面上有一个文本框,客户或付款人可以通过Stripe输入要捐赠给我的金额,作为一次性付款。还有一个复选框“订阅每月5美元”,如果选中,将在之后向他收取该金额。
问题是:
无论如何,这都是一次性付款,并立即收费;客户输入任意金额。而且,如果客户选中复选框,他将自动在一个月后开始每月收取5美元的订阅费用。
我知道如何为客户创建一次性付款的会话,以下是部分代码:
stripeParams := &stripe.CheckoutSessionParams{
PaymentMethodTypes: stripe.StringSlice([]string{
"card",
}),
LineItems: []*stripe.CheckoutSessionLineItemParams{
&stripe.CheckoutSessionLineItemParams{
PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{
Currency: stripe.String(string(stripe.CurrencyUSD)),
ProductData: &stripe.CheckoutSessionLineItemPriceDataProductDataParams{
Name: stripe.String("test123"),
},
UnitAmount: stripe.Int64(amount * 100),
},
Quantity: stripe.Int64(1),
},
},
Mode: stripe.String(string(stripe.CheckoutSessionModePayment)),
//........
}
如何实现“再加上每月5美元的订阅”这个功能?
英文:
On a page there's a textbox where a customer or payer enters an amount to donate to me via Stripe, as one time payment. And there's also a checkbox "subscribe for $5/mo" which, if checked, would charge him that amount afterwards.
The thing is:
it's a one time payment in any case and charged immediatelly; a custom amount -- whaterver a customer enters. And, optionally, a customer would be charged $5/mo within a subscription, automatically, starting in a month - if, and only if, he checks a checkbox.
I know how to create a session for charging a customer one time, and here's code, partially:
stripeParams := &stripe.CheckoutSessionParams{
PaymentMethodTypes: stripe.StringSlice([]string{
"card",
}),
LineItems: []*stripe.CheckoutSessionLineItemParams{
&stripe.CheckoutSessionLineItemParams{
PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{
Currency: stripe.String(string(stripe.CurrencyUSD)),
ProductData: &stripe.CheckoutSessionLineItemPriceDataProductDataParams{
Name: stripe.String("test123"),
},
UnitAmount: stripe.Int64(amount * 100),
},
Quantity: stripe.Int64(1),
},
},
Mode: stripe.String(string(stripe.CheckoutSessionModePayment)),
//........
How would I implement the "plus a subscription of $5/mo on top" thing?
答案1
得分: 1
如果您想通过Checkout Session创建一个订阅,您应该按照这个指南操作:https://stripe.com/docs/billing/subscriptions/checkout
简要概括一下,如果您想在客户输入的一次性付款金额之上添加一个5美元的订阅,您需要执行以下步骤:
- 在仪表盘或API中创建一个产品和一个关联的5美元的重复价格。
- 将Checkout Session的
mode
从payment
更改为subscription
(参见apiref)。 - 将您在步骤1中创建的价格ID添加为Checkout Session的附加行项目(参见apiref)。
- 设置
subscription_data.trial_end
(apiref)或subscription_data.trial_period_days
(apiref),以便在试用期结束后才开始收取重复价格。即使重复价格处于试用期,一次性价格仍将预先收取。
如果您不想使用试用期,您还可以考虑将一次性的5美元折扣添加到Session中。
英文:
If you want to create a Subscription through a Checkout Session you should follow this guide: https://stripe.com/docs/billing/subscriptions/checkout
To summarize, if you want to add a $5 subscription on top of the one-time payment amount entered by your customer you would do the following:
- Create a Product and an associated $5 recurring Price in the Dashboard or the API
- Change the Checkout Session from
mode: payment
tomode: subscription
(see apiref) - Add the Price ID you created in Step 1 and add it as an additional Line Item for the Checkout Session (see apiref)
- Set either
subscription_data.trial_end
(apiref) orsubscription_data.trial_period_days
(apiref) so that the recurring price is not charged until after the trial is over. The one-time price will still be charged upfront, even if the recurring price is on a trial.
If you don't want to use trials, you could also look into adding a one-time $5 discount to the Session.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论