Skip to content

Instantly share code, notes, and snippets.

@snipe
Last active May 26, 2022 17:46
Show Gist options
  • Save snipe/f4af01244c141c9fd56522541e3ec261 to your computer and use it in GitHub Desktop.
Save snipe/f4af01244c141c9fd56522541e3ec261 to your computer and use it in GitHub Desktop.
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use PDF;
// This example uses Laravel 5.5, Cashier 7.0.13, and requires the barryvdh/laravel-dompdf package.
class InvoiceChargeSucceeded extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($invoice, $user)
{
$this->invoice = $invoice;
$this->user = $user;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$invoice = $this->invoice;
$total = $invoice->total();
$user_name = $this->user->name;
$company_name = $this->user->company;
// Make sure you have barryvdh/laravel-dompdf installed, and have set the PDF facade up in your config/app.php
// facades section, per the laravel-dompdf documentation.
$pdf = PDF::loadview('cashier.receipt', compact('invoice','company_name','user_name', 'total'));
return (new MailMessage)->markdown('mail.account.invoice-succeeded', [
'invoice' => $invoice,
'total' => $total,
'user_name' => $user_name,
'company_name' => $company_name
])
->attachData($pdf->output(), $invoice->id . '-'.date('Y-m-d').'.pdf', ['mime' => 'application/pdf'])
->subject('Payment Processed');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
@fideloper
Copy link

ty, top of google search for sending cashier email invoice :D

@snipe
Copy link
Author

snipe commented Mar 24, 2020

@fideloper - that thrills me to no end. Always nice to be able to help out folks who have contributed so much. <3

@Robertkeli
Copy link

Robertkeli commented Feb 23, 2021

Hi @snipe , this is really excellent. I wanted something like this. In this case what would the $invoice and $user parameters represent?

This is how my Stripe WebhookController looks like:

<?php

namespace App\Http\Controllers;

use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
use App\Notifications\InvoiceChargeSucceeded;

class WebhookController extends CashierController
{
    /**
     * Handle payment succeeds.
     *
     * @param  array $payload
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function handleInvoicePaymentSucceeded(array $payload)
    {
        $invoice = $payload['data']['object'];
        $user = $this->getUserByStripeId($invoice['customer']);

        if ($user) {
            $user->notify(new InvoiceChargeSucceeded($invoice));
        }

        return new Response('Webhook Handled', 200);
    }
}

@fideloper
Copy link

fideloper commented Feb 23, 2021

@Robertkeli

  1. Invoice (technically, the Payload) is the JSON given by the webhook (sent by Stripe). You can find that in the docs or examples of the JSON/webhooks sent in your Stripe dashboard under the Developer settings
  2. User is the user/team/billable thing - a User Eloquent model that's related to the subscription

In test mode in Stripe, you can have Stripe send you webhooks again to test it out and see what the data is.

I'll often log it to my log file to inspect the variables, via something like this:

Log::info('stripe webhook', ['payload' => $payload, 'user' => $user->toArray()])'

@hmreumann
Copy link

I found that Cashier under the hood uses Dompdf so it's already been installed. We can generate the pdf like this:

$this->pdf = (new DompdfInvoiceRenderer())->render($invoice, $data);

@snipe
Copy link
Author

snipe commented May 26, 2022

@hmreumann The question was never how to generate a PDF, it was to send the PDF as an attachment in email via notifications. Also, this thread is from 2018. A lot has changed in cashier and stripe since then.

Regardless, Dompdf is what we're calling when we say use PDF, as the installation of Dompdf has you set that as an alias in your app config.

Glad you found a solution that works for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment