webhook 收到"payment_intent.successed"事件后,如何重定向到成功屏幕?
・使用付款意向付款后如何显示成功屏幕?
・结帐新允许您指定"success_url",如下所示,但是如果您不使用结帐,如何指定"success_url"?
$checkoutSession = StripeCheckoutSession::create([
'customer_email' => 'customer@example.com',
'success_url' => 'https://xxxx/thanks.php',
已尝试的代码 (PHP(
・我可以通过网络钩子接收"payment_intent.成功"事件
・我不重定向到成功屏幕
▼索引.php
<?php
StripeStripe::setApiKey("sk_test_xxxx");
$paymentintent = StripePaymentIntent::create([
"amount" => 1099,
"currency" => "jpy",
]);
?>
<input id="cardholder-name" type="text">
<div id="card-element"></div>
<button id="card-button" data-secret="<?php echo $paymentintent->client_secret; ?>">
Submit Payment
</button>
<script src="https://js.stripe.com/v3/"></script>
<script>
const stripe = Stripe('pk_test_xxxx');
const elements = stripe.elements();
const cardElement = elements.create('card');
cardElement.mount('#card-element');
const cardholderName = document.getElementById('cardholder-name');
const cardButton = document.getElementById('card-button');
const clientSecret = cardButton.dataset.secret;
cardButton.addEventListener('click', async (ev) => {
const {paymentIntent, error} = await stripe.handleCardPayment(
clientSecret, cardElement, {
payment_method_data: {
billing_details: {name: cardholderName.value}
}
}
);
if (error) {
// Display error.message in your UI.
console.log(error)
} else {
// The payment has succeeded. Display a success message.
console.log("ok")
}
});
</script>
2019/4/27 添加
・案例1.在index.php中编写的"标头位置"示例。失败
if (error) {
console.log(error)
} else {
<?php
header('Location: http://example.com/');
exit();
?>
}
・案例2.用webhook.php编写的"标头位置"示例。失败
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
try {
$event = StripeWebhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(UnexpectedValueException $e) {
// Invalid payload
http_response_code(400); // PHP 5.4 or greater
exit();
} catch(StripeErrorSignatureVerification $e) {
// Invalid signature
http_response_code(400); // PHP 5.4 or greater
exit();
}
if ($event->type == "payment_intent.succeeded") {
$intent = $event->data->object;
header('Location: http://example.com/');
exit();
您无法在 webhook 中重定向用户,因为它是由 Stripe 服务器触发的。
您应该做的是,当付款成功时(在您的javascript中(,在等待事件处理的同时将用户重定向到页面。
当您收到 webhook 时,更改购物车/订单/等的"状态",然后等待页面会收到 websocket 事件或更简单的拉取请求,指示付款已处理并显示订单确认页面。