Aidvi
Aidvi

Reputation: 27

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?

Upvotes: 0

Views: 514

Answers (2)

Kritika Thakur
Kritika Thakur

Reputation: 46

You can do it using Laravel Cashier.

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

Upvotes: 3

fahim152
fahim152

Reputation: 2629

As you have not specified your error ! I guess you need to bypass the laravel's csrf protection. I've done that previously when I implemented stripe using cashier once.

go to App\Http\Middleware\VerifyCsrfToken and add 'stripe/*' inside the except array

protected $except = [
    'stripe/*',
];

check for the reference: here

Upvotes: 0

Related Questions