在 Stripe 文档/Laravel 收银员上,它说它可以在创建发票后自动发送电子邮件。我尝试在 Stripe 的设置菜单上切换相关设置,但购买或订阅后我没有收到任何电子邮件。我还需要手动编码电子邮件发送吗? (我认为它应该在创建发票后自动发送)
最佳答案
根据Stripe docs , 如果您希望 Stripe 自动发送收据,您必须在创建订阅时设置客户的电子邮件参数,并确保在 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并使用 Stripe webhook :
在 Stripe dashboard 设置新端点到
https://your-domain.com/stripe/webhooks
在
VerifyCsrfToken
中间件中将 URI 列为异常(exception),或在web
中间件组之外列出路由:protected $except = [ 'stripe/*', ];
定义一个新的
WebhookController
并向 Controller 添加一个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); } }
在
routes/web.php
文件中定义到 Cashier Controller 的路由。这将覆盖默认的发货路线:Route::post('stripe/webhook', '\App\Http\Controllers\WebhookController@handleWebhook');
(可选)您可以手动设置 Stripe 的 webhook 签名以提高安全性。
见 Laravel docs形成更多信息。
关于Laravel Cashier 在发票/收据上自动发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59816797/