通过Webhook收到“ payment_intent.succeeded”事件后,如何重定向到成功屏幕?

时间:2019-04-26 22:30:56

标签: php stripe-payments

在通过Webhook收到“ payment_intent.succeeded”事件后,如何重定向到成功屏幕?

・使用PaymentIntent付款后如何显示成功屏幕?

・通过Checkout new可以指定“ success_url”,如下所示,但是如果不使用Checkout,如何指定“ success_url”呢?

$checkoutSession = \Stripe\Checkout\Session::create([
'customer_email' => 'customer@example.com',
'success_url' => 'https://xxxx/thanks.php',

尝试使用代码(PHP)

・我可以通过网络挂钩收到“ payment_intent.succeeded”事件

・我没有重定向到成功屏幕

▼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添加

・ 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();

1 个答案:

答案 0 :(得分:0)

由于Stripe服务器触发了Webhook,因此无法重定向用户。

您应该做的是,当付款成功时(在您的JavaScript中),在等待事件处理时将用户重定向到页面。

当您收到Webhook时,更改购物车/订单/等的“状态”,然后等待页面将收到Websocket事件或更简单的事件,拉动指示付款已处理的请求并显示订单确认页面。 / p>