Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hellomars's avatar

Create/Send Spark invoice for single payment does a faulty redirect

Dear sparkers,

I wanted to create invoices for single payments and after some Googling, I found out I can use some Spark code to perform this:

\Stripe\Stripe::setApiKey(\Illuminate\Support\Facades\Config::get('services.stripe.secret'));
   $invoice_item = \Stripe\InvoiceItem::create([
     'customer' => $user->stripe_id,
     'amount' => $new_credits,
     'currency' => 'usd',
     'description' => 'Single Charge'
   ]);
   $invoice = \Stripe\Invoice::create([
     'customer' => $user->stripe_id
   ]);
   $invoice = \Stripe\Invoice::retrieve($invoice->id);
   $charge = $invoice->pay();

The $charge variable is a boolean, so I use that to check to see if the payment went ok:

 if ($charge) {
   $this->sendInvoiceNotification($user, $invoice);
       return Redirect::to('credits/cc')->with('success', 'Successfully obtained ' . $new_credits . ' 
       credits');
   }

However, when I specifically call the sendInvoiceNotification method (which I also borrowed from Spark...) everything seems fine except for the fact the Redirect to /credits/cc is not performed and a GET request to the route that brought us in this method (except using a POST). So the last Redirect line is never reached..

This is the sendInvoiceNotification method I borrowed:

 function sendInvoiceNotification($billable, $invoice){
   $emailView = 'spark::settings.invoices.emails.invoice';
   $invoiceData = Spark::invoiceDataFor($billable);
   $data = compact('billable', 'invoice', 'invoiceData');
   Mail::send($emailView, $data, function ($message) use ($billable, $invoice, $invoiceData) {
     $this->buildInvoiceMessage($message, $billable, $invoice, $invoiceData);
   });
 }
 function buildInvoiceMessage($message, $billable, $invoice, array $invoiceData)
 {
     $localInvoice = $billable->localInvoices()->where('provider_id', $invoice->id)->firstOrFail();
     $invoiceData['id'] = $localInvoice->id;
     $message->to($billable->email, $billable->name)
             ->subject($invoiceData['product'].' Invoice')
             ->attachData($invoice->pdf($invoiceData), 'invoice.pdf');
 }

Anyone any idea? I can comment out the sendInvoiceNotification and the invoice is still saved in the db but no pdf is mailed to the user...

0 likes
0 replies

Please or to participate in this conversation.