Как перенаправить на экран успеха после получения события "payment_intent.succeeded" через webhook?
・ Как я могу отобразить экран успеха после совершения платежа с помощью PaymentIntent?
・ Checkout new позволяет вам указать «success_url», как показано ниже, но как указать «success_url», если вы не используете Checkout?
$checkoutSession = \Stripe\Checkout\Session::create([
'customer_email' => 'customer@example.com',
'success_url' => 'https://xxxx/thanks.php',
Кодпопробовал (PHP)
・ Я могу получить событие «payment_intent.succeeded» через webhook
・ Я не перенаправляю на экран успеха
▼ index.php
<?php
\Stripe\Stripe::setApiKey("sk_test_xxxx");
$paymentintent = \Stripe\PaymentIntent::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 add
・ case1.Пример «местоположения заголовка» записан в index.php.Ошибка
if (error) {
console.log(error)
} else {
<?php
header('Location: http://example.com/');
exit();
?>
}
・ case2.Пример «местоположения заголовка» записан в webhook.php.Отказ
$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); // PHP 5.4 or greater
exit();
} catch(\Stripe\Error\SignatureVerification $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();