I was interested in this process too and found this method on the stripe site, albeit with some difficulty.
Your above method was a nifty workaround but a little clunky for me so hopefully others will find the following to be a smoother way of handling the process.
To create a subscription and capture a payment at once
the flow is as follows:
- Create customer if it has not been done,
- Create a subscription with the parameters in the code below including your Price ID which has its own billing cycle associated with it,
- Use the Client Secret from the subscription created in step 2 to use with the Payment Elements form to capture the payment and store the Payment Method.
This way you do not have to manually mess around with subscription start dates, cycles etc, thereby simplifying the process and reducing room for human error.
$stripe = new \Stripe\StripeClient(
'your_secret_stripe_key'
);
// *Create customer if not already created *
$customer = $stripe->customers->create([
'description' => 'example customer',
'email' => 'example@blah.com'
]);
$customer_id = $customer->id;
//*Create subscription*
$subscription = $stripe->subscriptions->create([
'customer' => $customer_id,
'items' => [['price' => '*your_previously_created_price_id*']],
'payment_behavior' => 'default_incomplete',
'payment_settings' => ['save_default_payment_method' => 'on_subscription'],
'expand' => ['latest_invoice.payment_intent']
]);
// *Use this client secret to capture payment with the Payment Elements form*
$clientSecret = $subscription->latest_invoice->payment_intent->client_secret;
As you can see, creating the subscription generates an invoice which creates a Payment Intent, the Client Secret of which can be used to capture a card and save it as the Default Payment Method for the subscription. Your customer enters the card details, the payment is processed and the subscription becomes 'active'.