phpstripe-paymentssubscription

Stripe Subscription Schedules Charge Instantly instead of waiting one hour


For some reason, when you create a "Subscription Schedules" stripe, it has very odd behavior where instead of trying to charge the customer, it keeps the invoice in draft for 1 hour and then closes the invoice and charges the customer.

I already have the card in the customer. I wonder if there is any way I can force the first phase of the subscription to be charged immediately.

My code:

  $phases = [
    [
      'items' => [
                     [
                       'price_data' => [
                       'currency' => 'usd',
                       'product' => $product->stripe_product_id,
                       'recurring' => [
                           'interval' => $payment_plan['frequency'],
                       ],
                       'unit_amount' => $payment_plan['stripe_amount']
                     ],
                     'quantity' => 1,
                ],
             ],
             'iterations' => (int) $payment_plan['total_payments']
            ],
        ];

       $subscription = $stripe->subscriptionSchedules->create([
                    'customer' => $customer->stripe_customer_id,
                    'start_date' => 'now',
                    'end_behavior' => 'cancel',
                    'phases' => $phases,
       ]);

Solution

  • Here is the solution:

    $subscription = $stripe->subscriptionSchedules->create([
                        'customer' => $customer->stripe_customer_id,
                        'start_date' => 'now',
                        'end_behavior' => 'cancel',
                        'phases' => $phases,
    ]);
    
      // now the invoice is a draft so we go get this invoice
    $invoice = $stripe->invoices->all([
                        'limit' => 3, 
                        'status' => 'draft',
                        'subscription' => $subscription->subscription,
                        'customer' => $customer->stripe_customer_id,
    ]);
    
    //get the most recent draft
    $invoice = $invoice->data[0];
    
    //finalize the invoice (but this don't generate the payment for some reason)
    $stripe->invoices->finalizeInvoice(
    $invoice->id, [
    'auto_advance' => true
    ]
    );
    
    //finaly pay the invoice
    $invoice = $stripe->invoices->pay($invoice->id,[
    'payment_method' => $token
    ]);