集成Apple Pay时如何将Stripe的javascript paymentRequest与PHP处理服务器端混合

时间:2019-03-05 22:31:21

标签: php stripe-payments payment-request-api

我正在使用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)
            );

1 个答案:

答案 0 :(得分:0)

为了让用户知道他们的充电失败并在上面的代码中触发ev.complete('fail');,您需要在获取请求中包含response.ok才能返回false

我们首先来看一下response.ok属性的定义

  

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

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

因此,对于成功的请求,您希望PHP返回2xx状态(例如200),对于失败,则要返回非200的HTTP状态代码(比如说402) 。

在您的PHP中,您将尝试收费,然后收到Stripe的响应。根据此响应,您应该使PHP返回前端状态-200 OK或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.ok将为true。

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

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

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