0

我正在使用 Stripe 元素进行异步 paymentRequest 以向客户收费。无论收费结果如何,请求都会返回“成功”,那么返回状态的最佳方法是什么,以便我可以处理收费状态客户端并报告失败的收费?我需要使用端点吗?在没有指导的情况下,Stripe 似乎让我们在收费后悬而未决。

听众:

<script>    
    paymentRequest.on('token', function(ev) {

        fetch('https://example.com/pub/apple.php', {
        method: 'POST',
        body: JSON.stringify({token: ev.token.id , email: ev.token.email}),
        headers: {'content-type': 'application/json'},
    })
      .then(function(response) {
        if (response.ok) {

          // Report to the browser that the payment was successful, prompting
          // it to close the browser payment interface.

          ev.complete('success');

        } else {

          // Report to the browser that the payment failed, prompting it to
          // re-show the payment interface, or show an error message and close
          // the payment interface.

          ev.complete('fail');
        }
  });
});
    </script>

https://example.com/pub/apple.php

require_once('../_stripe-php-4.9.0/init.php');

// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$json = json_decode($input);

// get the token from the returned object

$token = $json->token;
$email = $json->email;

$skey = 'sk_test_Y********************F';  

\Stripe\Stripe::setApiKey($skey);  

//  create the customer

$customer = \Stripe\Customer::create(array(
          "source" => $token,
           "description" => 'Device',
           "email" => $email)
        );  



    //charge the card    

    $charge = \Stripe\Charge::create(array(
              "amount" => 1000, 
              "currency" => 'GBP',
              "statement_descriptor" => 'MTL',
              "description" => 'Device - '.$customer->id,
              "customer" => $customer->id)
            );
4

1 回答 1

0

为了让用户知道他们的收费失败,并ev.complete('fail');在上面的代码中触发,你需要response.ok在你的 fetch 请求中 return false

我们先来看看那个response.ok属性的定义,

Response 接口的 ok 只读属性包含一个布尔值,说明响应是否成功(状态在 200-299 范围内)。

通过https://developer.mozilla.org/en-US/docs/Web/API/Response/ok

因此,对于成功的请求,您希望 PHP 返回一个2xx状态(假设200为 ),而对于失败的请求,则返回一个非 200 的 HTTP 状态代码(假设为 a 402)。

在您的 PHP 中,您将尝试进行 Charge,然后收到来自 Stripe 的响应。根据这个响应,您应该让 PHP 向您的前端返回一个状态 --- 一个200OK 或一个402错误。

您可以使用函数来执行此操作http_response_code(),例如http_response_code(200)http_response_code(402)

http://php.net/manual/en/function.http-response-code.php

我们来看一个简化的例子

try {
  // make a charge
  $charge = \Stripe\Charge::create(array(
    "amount" => 1000, 
    "currency" => 'GBP',
    "source" => $token
  ));

  // send a 200 ok
  http_response_code(200);
  print("Charge successful");

} catch(\Stripe\Error\Card $e) {
  // Since it's a decline, \Stripe\Error\Card will be caught
  $body = $e->getJsonBody();
  $err  = $body['error'];

  // this charge declined, return a 402
  // response.ok should then be false
  http_response_code(402);
  print('Error:' . $err['message']);
}

此处的 Charge 调用被包装在一个try-catch块中。如果收费成功,200则会发送一个 HTTP 响应代码,并且该代码为response.oktrue。

如果用户提供了拒绝的卡,则会捕获该错误,并402返回 HTTP 响应代码(以及错误消息)。在那种情况下response.ok会是false,所以ev.complete('fail');会被调用。

还有一些其他类型的 Stripe 错误你可能想要捕获,完整的参考在这里,

https://stripe.com/docs/api/errors https://stripe.com/docs/api/errors/handling

于 2019-03-05T23:53:35.073 回答