Skip to content

Instantly share code, notes, and snippets.

@justincone
Forked from gilbitron/InvoicePaid.php
Created August 13, 2019 16:23
Show Gist options
  • Save justincone/0fc329c88adff62a1000f168bc4ad561 to your computer and use it in GitHub Desktop.
Save justincone/0fc329c88adff62a1000f168bc4ad561 to your computer and use it in GitHub Desktop.
Convert the Laravel Spark invoice email to a notification email
<?php // routes/custom.php
/*
|--------------------------------------------------------------------------
| Custom Routes
|--------------------------------------------------------------------------
|
| These routes have no middleware or namespace set.
|
*/
Route::post('/webhook/stripe', 'App\Extensions\StripeWebhookController@handleWebhook');
<?php // app/Notifications/InvoicePaid.php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Laravel\Cashier\Invoice;
class InvoicePaid extends Notification
{
use Queueable;
protected $billable;
protected $invoice;
/**
* Create a new notification instance.
*
* @param mixed $billable
* @param Invoice $invoice
*/
public function __construct($billable, Invoice $invoice)
{
$this->billable = $billable;
$this->invoice = $invoice;
}
/**
* 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)
{
$invoiceData = \Spark::invoiceDataFor($this->billable);
$mailMessage = (new MailMessage)->subject($invoiceData['product'] . ' Invoice')
->greeting('Hi ' . explode(' ', $this->billable->name)[0] . '!')
->line('Thanks for your continued support. We\'ve attached a copy of your invoice for your records. Please let us know if you have any questions or concerns!')
->attachData($this->invoice->pdf($invoiceData), 'invoice.pdf');
return $mailMessage;
}
}
<?php // app/Providers/RouteServiceProvider.php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* ...
*/
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
// ...
$this->mapCustomRoutes();
}
protected function mapCustomRoutes()
{
require base_path('routes/custom.php');
}
}
<?php // app/Extensions/StripeWebhookController.php
namespace App\Extensions;
use App\Notifications\InvoicePaid;
use Laravel\Spark\Http\Controllers\Settings\Billing\StripeWebhookController as SparkStripeWebhookController;
class StripeWebhookController extends SparkStripeWebhookController
{
/**
* Send an invoice notification e-mail.
*
* @param mixed $billable
* @param \Laravel\Cashier\Invoice
* @return void
*/
protected function sendInvoiceNotification($billable, $invoice)
{
$billable->notify(new InvoicePaid($billable, $invoice));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment