Laravel 收银员在发票/收据上自动发送电子邮件

Laravel Cashier auto send email on invoice / receipt

在 Stripe 文档/Laravel 收银台上,它说它可以在创建发票后自动发送电子邮件。我尝试在 Stripe 的设置菜单上切换相关设置,但我在购买或订阅后没有收到任何电子邮件。我还需要手动编写电子邮件发送代码吗? (我在想应该是创建发票后自动发送)

根据Stripe docs, if you want Stripe to automatically send receipts, You have to set customer’s email parameter when creating a subscription and ensure that the option email customers for successful payments is enabled on Stripe dashboard

$user->newSubscription('default', 'monthly')
    ->create($paymentMethod, [
        'email' => $user->email, // <= customer’s email
    ]);

请注意:

Receipts for payments created using your test API keys are not sent automatically. Instead, you can view or manually send a receipt using the Dashboard.


但是如果您想通过 Laravel 发送收据,您可以 define a new webhook event handler and use Stripe webhook:

  1. Stripe dashboardhttps://your-domain.com/stripe/webhooks

  2. 设置一个新端点
  3. 在您的 VerifyCsrfToken 中间件中将 URI 列为例外,或将路由列在 web 中间件组之外:

    protected $except = [
        'stripe/*',
    ];
    
  4. 定义一个新的WebhookController并向控制器添加一个handleInvoicePaymentSucceeded方法来处理invoice.payment_succeeded webhook:

    <?php
    
    namespace App\Http\Controllers;
    
    use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
    use App\Notifications\InvoicePaid;
    
    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 InvoicePaid($invoice));
            }
    
            return new Response('Webhook Handled', 200);
        }
    }
    
  5. routes/web.php 文件中定义到收银台控制器的路由。这将覆盖默认运送路线:

    Route::post('stripe/webhook', '\App\Http\Controllers\WebhookController@handleWebhook');
    
  6. (可选)您可以手动设置 Stripe 的 webhook 签名以提高安全性。

请参阅 Laravel docs 形成更多信息。