gostripe-payments

Pay now once and, optionally (!), create a subscription too -- how to do this in Stripe?


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?


Solution

  • 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:

    1. Create a Product and an associated $5 recurring Price in the Dashboard or the API
    2. Change the Checkout Session from mode: payment to mode: subscription (see apiref)
    3. Add the Price ID you created in Step 1 and add it as an additional Line Item for the Checkout Session (see apiref)
    4. Set either subscription_data.trial_end (apiref) or subscription_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.