laravel / cashier-stripe

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services.
https://laravel.com/docs/billing
MIT License
2.37k stars 671 forks source link

create Subscription => Trying to get property 'payment_intent' of non-object #1576

Closed not82 closed 11 months ago

not82 commented 11 months ago

Cashier Stripe Version

13.17

Laravel Version

8.75

PHP Version

7.4.9

Database Driver & Version

No response

Description

Hi ,

I'm having hard pain to implement the Stripe-documented way to add a subscription in my application. I'm talking about this one : https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=elements

It recommands to create an incomplete subscription, then ask user to add payment detail.

I have no problem with the stripe version :

$subscription = $stripe->subscriptions->create([
        'customer' => $customer_id,
        'items' => [[
            'price' => $price_id,
        ]],
        'payment_behavior' => 'default_incomplete',
        'payment_settings' => ['save_default_payment_method' => 'on_subscription'],
        'expand' => ['latest_invoice.payment_intent'],
    ]);

But there is like no easy way to do this in Cashier, actually I have this :

 try {
            $subscription = $account->newSubscription(
                'default',
                $priceId
            )
                ->create(null, [], [
                    'payment_behavior' => 'default_incomplete',
                    'payment_settings' => ['save_default_payment_method' => 'on_subscription'],
                    'expand' => ['latest_invoice.payment_intent']
                ]);
        } catch (IncompletePayment $exception) {
            // https://github.com/laravel/cashier-stripe/issues/807
            $invoice = Invoice::retrieve($exception->payment->invoice);
            $subscription = Subscription::where('stripe_id', $invoice->subscription)->firstOrFail();
            $result['clientSecret'] = $subscription->asStripeSubscription()->latest_invoice->payment_intent->client_secret;
            $result['subscriptionId'] = $subscription->id;
            return $result;
        }

I have 2 minor problems with this :

It's not a hard thing, but in the end, it doesn't works ! I don't understand that the documented way to do it in Stripe, is so hard and bugged to do in Cashier. Once again, in Stripe without Cashier it works like a charm, but I want the Cashier implementation to avoid desync with Cashier.

Maybe I'm wrong in the way of implementing it, maybe with Cashier there is another way to manage it ( with all cases payment can occure , 3D sign etc.. ) in this case, where can I find it ? I readed the whole documentation.

Thanks

Steps To Reproduce

 try {
            $subscription = $account->newSubscription(
                'default',
                $priceId
            )
                ->create(null, [], [
                    'payment_behavior' => 'default_incomplete',
                    'payment_settings' => ['save_default_payment_method' => 'on_subscription'],
                    'expand' => ['latest_invoice.payment_intent']
                ]);
        } catch (IncompletePayment $exception) {
            // https://github.com/laravel/cashier-stripe/issues/807
            $invoice = Invoice::retrieve($exception->payment->invoice);
            $subscription = Subscription::where('stripe_id', $invoice->subscription)->firstOrFail();
            $result['clientSecret'] = $subscription->asStripeSubscription()->latest_invoice->payment_intent->client_secret;
            $result['subscriptionId'] = $subscription->id;
            return $result;
        }

=> Trying to get property 'payment_intent' of non-object

driesvints commented 11 months ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.