Search code examples
phplaravelstripe-paymentslaravel-cashier

add invoice items works with stripe SDK but not Laravel cashier


Im trying to get this working with laravel so i can do a one time charge together with a subscription.

The code that works. Stripe SDK way

        \Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));

    $customer = \Stripe\Customer::create([
        'email' => $request->email,
    ]);

    $paymentMethod = \Stripe\PaymentMethod::retrieve($request->token);

    $paymentMethod->attach([
        'customer' => $customer->id,
    ]);

    $customer = \Stripe\Customer::update($customer->id, [
        'invoice_settings' => [
            'default_payment_method' => $paymentMethod->id,
        ],
    ]);

    $subscription = \Stripe\Subscription::create([
        'customer' => $customer->id,
        'items' => [
            [
                'plan' => $request->plan,
            ],
        ],
        'add_invoice_items' => [
            ['price' => 'PRICE_ID']
        ],

        'off_session' => true, //for use when the subscription renews
    ]);

The code that dont work. Laravel cashier way

        $subscription = $request->user()->newSubscription('default', $request->plan);

    $subscription->create($request->token, [
        'email' => $request->email,
    ], [
            'add_invoice_items' => [
                [
                    'price' => 'PRICE_ID'
                ]
            ]
        ]);

Conclusion

If there is no way for me to get this working with laravel cashier / Billable. Then i need to use the SDK and then i need to handle a lot of other things manually like saving the recently created customer in my own DB. Cashier handles all these things for you. Is there really not a way to add simple stripe options to cashier?


Solution

  • You can do it using Laravel Cashier.

    $subscription = $user->newSubscription('default', [
        $subscription->id,
    ])->create($request->payment_method, [], [
        'add_invoice_items' => [
            ['price' => $product->id]
        ]
    ]);