我已经为 Stripe 添加了一个 webhook 端点,并且我能够接收该 webhook,但是我如何处理从 Stripe 发送的信息?
例如我想向我的客户发送一封有关付款成功的电子邮件。我正在使用 MailerSend,我知道如何发送电子邮件,但如何启动此过程?
Webhook 控制器
public function webhook(Request $request) {
$stripe = new \Stripe\StripeClient('sk_XXX');
// This is your Stripe CLI webhook secret for testing your endpoint locally.
$endpoint_secret = 'whsec_XXX';
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
// Invalid signature
http_response_code(400);
exit();
}
// Handle the event
switch ($event->type) {
case 'checkout.session.completed':
$session = $event->data->object;
default:
echo 'Received unknown event type ' . $event->type;
}
http_response_code(200);
}
有人可以帮助我吗?
尝试在 webhook 案例中添加代码。
用于测试,不用于生产。
ngrok http 8888
。webhook(Request $request)
方法抓住它。您可能还会发现套餐很有帮助。
// See your keys here: https://dashboard.stripe.com/apikeys
\Stripe\Stripe::setApiKey('sk_test_26PHem9AhJZvU623DfE1x4sd');
$payload = @file_get_contents('php://input');
$event = null;
try {
$event = \Stripe\Event::constructFrom(
json_decode($payload, true)
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
abort(400);
}
// Handle the event
switch ($event->type) {
case 'payment_intent.succeeded':
$paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
handlePaymentIntentSucceeded($paymentIntent);
break;
case 'payment_method.attached':
$paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
handlePaymentMethodAttached($paymentMethod);
break;
// ... handle other event types
default:
echo 'Received unknown event type ' . $event->type;
}