条带-使用手动确认的相同付款意向

时间:2019-07-09 10:04:03

标签: php stripe-payments

我正在尝试将付款流程从费用API迁移到付款意向。

条带迁移文档指出,在第2步中,您需要在服务器https://stripe.com/docs/payments/payment-intents/quickstart#creating-with-manual-confirmation上创建付款意图

如果付款失败,并且用户尝试再次付款,那么我将为同一订单创建多个付款方式。

如果说我知道金额后立即创建了付款意图,然后使用手动确认方法将该订单的付款意图存储在数据库表中,是否可以重用相同的付款意图?< / p>

是这样的:

 // create intent
 $intent = \Stripe\PaymentIntent::create([
   'amount' => 1099,
   'currency' => 'usd',
   'confirmation_method' => 'manual',
 ],[
   'idempotency_key' => $orderId,
 ]);

 // Update orders table with payment intent id and set order status to unpaid

然后,当用户付款时:

 # vendor using composer
 require_once('vendor/autoload.php');

 \Stripe\Stripe::setApiKey(getenv('STRIPE_SECRET_KEY'));

 header('Content-Type: application/json');

 # retrieve json from POST body
 $json_str = file_get_contents('php://input');
 $json_obj = json_decode($json_str);

 #$intent = null;
 $order = // get from database
 $intent = \Stripe\PaymentIntent::retrieve($order->payment_intent_id);

 try {
   if (isset($json_obj->payment_method_id)) {

      # Create the PaymentIntent
      #$intent = \Stripe\PaymentIntent::create([
      #   'payment_method' => $json_obj->payment_method_id,
      #   'amount' => 1099,
      #   'currency' => 'usd',
      #   'confirmation_method' => 'manual',
      #   'confirm' => true,
      #]);

     # Instead of creating a new payment intent we update the previously saved PaymentIntent
     \Stripe\PaymentIntent::update($intent->id,
       [
         'payment_method' => $json_obj->payment_method_id,
         'confirm' => true,
       ]
     );
  }
  if (isset($json_obj->payment_intent_id)) {
     $intent = \Stripe\PaymentIntent::retrieve(
        $json_obj->payment_intent_id
     );
     $intent->confirm();
   }
   generatePaymentResponse($intent);
} catch (\Stripe\Error\Base $e) {
  # Display error on client
   echo json_encode([
      'error' => $e->getMessage()
   ]);
}

function generatePaymentResponse($intent) {
   # Note that if your API version is before 2019-02-11, 'requires_action'
   # appears as 'requires_source_action'.
   if ($intent->status == 'requires_action' &&
       $intent->next_action->type == 'use_stripe_sdk') {
        # Tell the client to handle the action
       echo json_encode([
         'requires_action' => true,
         'payment_intent_client_secret' => $intent->client_secret
      ]);
   } else if ($intent->status == 'succeeded') {
       # The payment didn’t need any additional actions and completed!
       # Handle post-payment fulfillment
       echo json_encode([
          "success" => true
      ]);
   } else {
      # Invalid status
      http_response_code(500);
      echo json_encode(['error' => 'Invalid PaymentIntent status']);
   }
 }

感谢任何提示和建议。

0 个答案:

没有答案
相关问题