-1

I have a problem using the stripe. When I create an invoice I also save the invoice_id to the MySQL database so i could reference it to retrieve that specific invoice details but it doesn't work. What am I doing wrong here?

    \Stripe\Stripe::setApiKey('my-test-key');

    $invoice = \Stripe\Invoice::retrieve(
      $invoice_id
    );

$invoice_amount = $invoice->total;

This should return the invoice total if I am not mistaken? Or should I just save everything to the database to the invoices table when creating the invoice and then get the data from the that table?

Jukka Koivu
  • 63
  • 1
  • 10

1 Answers1

0

You need to use the invoice ID to retrieve the invoice data. The customer ID won't work for this endpoint. Additionally, make sure you're including your composer autoload file (if using composer). Otherwise, include the file that has the stripe classes/functions. Lastly, ensure you're using the correct API key for your environment. In my example, I am using a test key. I mocked it up since this is the internet. Full working code below:

require '../vendor/autoload.php';

\Stripe\Stripe::setApiKey('sk_test_abc123000000000000000000');

$invoice_id = 'in_1GaEZUDvaJ1EDf9RINFgSAjA';

$invoice = \Stripe\Invoice::retrieve(
    $invoice_id
);

$invoice_amount = $invoice->total;

After that, you should be all set.

Shakima F
  • 148
  • 8
  • Hi, sorry I didn't notice I had customer_id there but even I have the $invoice_id I am not receiving anything. Do I need to have webhook as well in order to this to work? – Jukka Koivu Apr 21 '20 at 06:08
  • No additional webhooks are needed for this. Is the page returning any errors? Did you use composer for stripe? If so, also make sure you are including your autoload file for composer? What about your stripe API key? Is this included as well? I'm updating my answer to the completed code that is not returning any errors. – Shakima F Apr 22 '20 at 00:17
  • Hi, It is returning no errors and yes I have the auto composer included and having the api key also. – Jukka Koivu Apr 22 '20 at 06:49