0

Fatal error: Uncaught Error: Using $this when not in object context in E:...\Invoice.php:237 Stack trace: #0 E:...\subscription.php(67): Stripe\Invoice::sendInvoice('in_1M4MW6ABdULn...', Array) #1 {main} thrown in E:...\Invoice.php on line 237

getting this error when sending invoice after subscription in stripe.

$invoice = \Stripe\Invoice::sendInvoice(
  $stripe_subscription->latest_invoice,
  []
);


$stripe_subscription = \Stripe\Subscription::create(
     [ 'customer' => $stripe_customer->id, 
       'payment_behavior' => 'allow_incomplete',
       'items' => [ [ 'price' => $price->id, ], ],
        'metadata' => ['Address' => "address"], 
     ]
     );
Martin
  • 22,212
  • 11
  • 70
  • 132
  • THIS: [PHP Fatal error: Using $this when not in object context](https://stackoverflow.com/questions/2350937/php-fatal-error-using-this-when-not-in-object-context) – Martin Nov 15 '22 at 11:16
  • No that's different question I have issues with stripe – zohaib irshad Nov 15 '22 at 11:18
  • The cause is the same. It doesn't matter what you're using, in PHP the reason for this error is these causes: using `$this` when you're using a non-object or a static-object. If your problem is Stripe then double and triple check the Stripe documentation and be sure to use the correct documentation for your correct version of Stripe. You may need to download and install the later version of Stripe. – Martin Nov 15 '22 at 11:20

1 Answers1

0

There's no such method as sendInvoice like that in the Stripe library.

You get this error because sendInvoice is an instance method that requires you to have an Invoice object, you can't call it statically like that.

The correct way to do this in the latest version of the library is

$stripe = new \Stripe\StripeClient(
  'sk_test_xxxx'
);

$stripe_subscription = $stripe->subscriptions->create([.......]);

$invoice = $stripe->invoices->sendInvoice(
  $stripe_subscription->latest_invoice,
  []
); 

If you're using an old version of the library, I think you have to retrieve the object first to be able to call sendInvoice on it. But you should not use this old legacy approach.

$invoice = \Stripe\Invoice::retrieve($stripe_subscription->latest_invoice);
$invoice->sendInvoice([]);
karllekko
  • 5,648
  • 1
  • 15
  • 19