Connected accounts and creating a subscription as oppossed to one time payment

Viewed 98

I'm a small market place. There're Referrals/Connected-accounts (A) and Users (B). Referrals bring new users, and users are those who pay for some services. And a payment will get split: 90% will go to a Referral who's brought a particular User, and 10% to as a market place.

There're 2 options for a User: make a one time payment or create subscription. I've already implemented the former and it works fine, and here's a part of code:

    //for one time payments - all is good

    if md == stripe.CheckoutSessionModePayment {

        stripeParams.PaymentIntentData = &stripe.CheckoutSessionPaymentIntentDataParams{
            ApplicationFeeAmount: stripe.Int64(appFeeInCents), // 10% of a price, in cents
            TransferData: &stripe.CheckoutSessionPaymentIntentDataTransferDataParams{
                Destination: stripe.String(stripeRefr.StripeAccountId), //Stripe connected account id of a referral, retrieved from Db
            },

            //important!
            OnBehalfOf: stripe.String(stripeRefr.StripeAccountId), //Stripe connected account id of a referral, retrieved from Db
        }
    } else {

        //for subscriptions - this won't work

        //how to implement it?

        // stripeParams.SubscriptionData = &stripe.CheckoutSessionSubscriptionDataParams{
        //    // Items: []*stripe.SubscriptionItemsParams{
        //    //   {
        //    //     price: stripe.String("price_id_from_stripe_123"),
        //    //   },
        //    // },
        //    // TransferData: &stripe.InvoiceTransferDataParams{
        //    //   Destination: stripe.String(stripeRefr.StripeAccountId),
        //    // },
        //    // ApplicationFeePercent: stripe.Float64(cfg.Stripe.ApplicationFeePercent),
        //    ApplicationFeePercent: stripe.Float64(10),
        // }
    }


    sess, err := session.New(stripeParams)

I want to do the same thing for subscriptions. How to do it?

This code is in Go lang, but I'll be able to understand code in other language too.

1 Answers

The below code should work

params := &stripe.SubscriptionParams{
  Customer: stripe.String("cus_..."),
  Items: []*stripe.SubscriptionItemsParams{
    {
      Price: stripe.String("price_..."),
    },
  },
  ApplicationFeePercent: stripe.Float64(10),
  TransferData: &stripe.SubscriptionTransferDataParams{
    Destination: stripe.String("acct_..."),
  },
}
Related