Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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

This comment has been minimized.

Copy link

@fideloper fideloper commented Mar 20, 2020

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

@snipe

This comment has been minimized.

Copy link
Owner Author

@snipe 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

This comment has been minimized.

Copy link

@Robertkeli 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

This comment has been minimized.

Copy link

@fideloper 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()])'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment