From 5e9782dbdaf873163ba0b3f0f36b03a3c32f42c8 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 21 Jan 2022 13:14:33 +0100 Subject: [PATCH 1/5] Implement invoice renderer --- config/cashier.php | 13 +++++++---- src/CashierServiceProvider.php | 16 +++++++++++++ src/Contracts/InvoiceRenderer.php | 18 +++++++++++++++ src/Invoice.php | 19 +++++----------- src/Invoices/DompdfInvoiceRenderer.php | 31 ++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 src/Contracts/InvoiceRenderer.php create mode 100644 src/Invoices/DompdfInvoiceRenderer.php diff --git a/config/cashier.php b/config/cashier.php index f2883c5b..5e51e437 100644 --- a/config/cashier.php +++ b/config/cashier.php @@ -100,18 +100,23 @@ /* |-------------------------------------------------------------------------- - | Invoice Paper Size + | Invoice Settings |-------------------------------------------------------------------------- | | This option is the default paper size for all invoices generated using | Cashier. You are free to customize this settings based on the usual | paper size used by the customers using your Laravel applications. - | - | Supported sizes: 'letter', 'legal', 'A4' + + | Supported paper sizes: 'letter', 'legal', 'A4' | */ - 'paper' => env('CASHIER_PAPER', 'letter'), + 'invoices' => [ + 'renderer' => env('CASHIER_INVOICE_RENDERER', Laravel\Cashier\Invoices\DompdfInvoiceRenderer::class), + 'options' => [ + 'paper' => env('CASHIER_PAPER', 'letter'), + ], + ], /* |-------------------------------------------------------------------------- diff --git a/src/CashierServiceProvider.php b/src/CashierServiceProvider.php index d3c7604f..2b9c2922 100644 --- a/src/CashierServiceProvider.php +++ b/src/CashierServiceProvider.php @@ -5,6 +5,7 @@ use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; use Laravel\Cashier\Console\WebhookCommand; +use Laravel\Cashier\Contracts\InvoiceRenderer; use Stripe\Stripe; use Stripe\Util\LoggerInterface; @@ -39,6 +40,7 @@ public function boot() public function register() { $this->configure(); + $this->bindInvoiceRenderer(); $this->bindLogger(); } @@ -54,6 +56,20 @@ protected function configure() ); } + /** + * Bind the default invoicer renderer. + * + * @return void + */ + protected function bindInvoiceRenderer() + { + $this->app->bind(InvoiceRenderer::class, function ($app) { + return $app->make( + config('cashier.invoices.renderer') + ); + }); + } + /** * Bind the Stripe logger interface to the Cashier logger. * diff --git a/src/Contracts/InvoiceRenderer.php b/src/Contracts/InvoiceRenderer.php new file mode 100644 index 00000000..12453aab --- /dev/null +++ b/src/Contracts/InvoiceRenderer.php @@ -0,0 +1,18 @@ +setChroot(base_path()); + $options = config('cashier.invoices.options', []); - $dompdf = new Dompdf($options); - $dompdf->setPaper(config('cashier.paper', 'letter')); - $dompdf->loadHtml($this->view($data)->render()); - $dompdf->render(); + if ($paper = config('cashier.paper')) { + $options['paper'] = $paper; + } - return $dompdf->output(); + return app(InvoiceRenderer::class)->render($this, $data, $options); } /** diff --git a/src/Invoices/DompdfInvoiceRenderer.php b/src/Invoices/DompdfInvoiceRenderer.php new file mode 100644 index 00000000..f9cfc5c2 --- /dev/null +++ b/src/Invoices/DompdfInvoiceRenderer.php @@ -0,0 +1,31 @@ +setChroot(base_path()); + + $dompdf = new Dompdf($dompdfOptions); + $dompdf->setPaper($options['paper'] ?? 'letter'); + $dompdf->loadHtml($invoice->view($data)->render()); + $dompdf->render(); + + return (string) $dompdf->output(); + } +} From 228233a7479a29e77d60b99038b66ef4dec83fa9 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 21 Jan 2022 13:18:33 +0100 Subject: [PATCH 2/5] wip --- config/cashier.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/cashier.php b/config/cashier.php index 5e51e437..45af8e65 100644 --- a/config/cashier.php +++ b/config/cashier.php @@ -103,9 +103,9 @@ | Invoice Settings |-------------------------------------------------------------------------- | - | This option is the default paper size for all invoices generated using - | Cashier. You are free to customize this settings based on the usual - | paper size used by the customers using your Laravel applications. + | These options are the defaults for all invoices generated + | by Cashier. You are free to customize these based on + | the renderer used by your Laravel application. | Supported paper sizes: 'letter', 'legal', 'A4' | From 05bcc9a0e2925cf55a7b83e1bca6fc9aab03ce00 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 21 Jan 2022 13:25:54 +0100 Subject: [PATCH 3/5] wip --- config/cashier.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/cashier.php b/config/cashier.php index 45af8e65..2e44e282 100644 --- a/config/cashier.php +++ b/config/cashier.php @@ -106,14 +106,13 @@ | These options are the defaults for all invoices generated | by Cashier. You are free to customize these based on | the renderer used by your Laravel application. - - | Supported paper sizes: 'letter', 'legal', 'A4' | */ 'invoices' => [ 'renderer' => env('CASHIER_INVOICE_RENDERER', Laravel\Cashier\Invoices\DompdfInvoiceRenderer::class), 'options' => [ + // Supported dompdf paper sizes: 'letter', 'legal', 'A4' 'paper' => env('CASHIER_PAPER', 'letter'), ], ], From 5afb3bd53c527a7d82e8b89f99b8f029ece23fb1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 24 Jan 2022 11:42:02 -0600 Subject: [PATCH 4/5] formatting --- config/cashier.php | 13 ++++++++----- src/CashierServiceProvider.php | 22 ++++++++++------------ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/config/cashier.php b/config/cashier.php index 2e44e282..1dee3b3c 100644 --- a/config/cashier.php +++ b/config/cashier.php @@ -1,5 +1,7 @@ [ - 'renderer' => env('CASHIER_INVOICE_RENDERER', Laravel\Cashier\Invoices\DompdfInvoiceRenderer::class), + 'renderer' => env('CASHIER_INVOICE_RENDERER', DompdfInvoiceRenderer::class), + 'options' => [ - // Supported dompdf paper sizes: 'letter', 'legal', 'A4' + // Supported: 'letter', 'legal', 'A4' 'paper' => env('CASHIER_PAPER', 'letter'), ], ], diff --git a/src/CashierServiceProvider.php b/src/CashierServiceProvider.php index 2b9c2922..cb234c78 100644 --- a/src/CashierServiceProvider.php +++ b/src/CashierServiceProvider.php @@ -40,8 +40,8 @@ public function boot() public function register() { $this->configure(); - $this->bindInvoiceRenderer(); $this->bindLogger(); + $this->bindInvoiceRenderer(); } /** @@ -57,30 +57,28 @@ protected function configure() } /** - * Bind the default invoicer renderer. + * Bind the Stripe logger interface to the Cashier logger. * * @return void */ - protected function bindInvoiceRenderer() + protected function bindLogger() { - $this->app->bind(InvoiceRenderer::class, function ($app) { - return $app->make( - config('cashier.invoices.renderer') + $this->app->bind(LoggerInterface::class, function ($app) { + return new Logger( + $app->make('log')->channel(config('cashier.logger')) ); }); } /** - * Bind the Stripe logger interface to the Cashier logger. + * Bind the default invoice renderer. * * @return void */ - protected function bindLogger() + protected function bindInvoiceRenderer() { - $this->app->bind(LoggerInterface::class, function ($app) { - return new Logger( - $app->make('log')->channel(config('cashier.logger')) - ); + $this->app->bind(InvoiceRenderer::class, function ($app) { + return $app->make(config('cashier.invoices.renderer')); }); } From c55f938507a5f50fed89be2f859669cad1c79d12 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Tue, 25 Jan 2022 12:10:36 +0100 Subject: [PATCH 5/5] Add default renderer in service provider --- src/CashierServiceProvider.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/CashierServiceProvider.php b/src/CashierServiceProvider.php index cb234c78..a4b11b37 100644 --- a/src/CashierServiceProvider.php +++ b/src/CashierServiceProvider.php @@ -6,6 +6,7 @@ use Illuminate\Support\ServiceProvider; use Laravel\Cashier\Console\WebhookCommand; use Laravel\Cashier\Contracts\InvoiceRenderer; +use Laravel\Cashier\Invoices\DompdfInvoiceRenderer; use Stripe\Stripe; use Stripe\Util\LoggerInterface; @@ -78,7 +79,7 @@ protected function bindLogger() protected function bindInvoiceRenderer() { $this->app->bind(InvoiceRenderer::class, function ($app) { - return $app->make(config('cashier.invoices.renderer')); + return $app->make(config('cashier.invoices.renderer', DompdfInvoiceRenderer::class)); }); }