首页 > 解决方案 > Stripe sca 订阅 php 并自动收费

问题描述

我已经实现了 Stripe SCA 迁移,我的服务端代码创建支付意图如下

      $intent = \Stripe\PaymentIntent::create([
            'payment_method' => $requestData['payment_method_id'],
            'amount' => $requestData->amount,
            'currency' => $requestData->currencyIso,
            'payment_method_types' => ['card'],
            'confirmation_method' => "manual",
            'confirm' => true,
            'setup_future_usage'=>"off_session",
        ]);
        return $intent;

我的js代码如下创建卡支付和处理响应

 stripe.createPaymentMethod('card', cardElement, {
        billing_details: {name: cardholderName.value}
 })

并处理响应:

function handleServerResponse(response) {
  if (response.error) {

  } else if (response.requires_action) {

    stripe.handleCardAction(
      response.payment_intent_client_secret
    ).then(function(result) {
      if (result.error) {

      } else {
        // The card action has been handled
        // The PaymentIntent can be confirmed again on the server
        fetch('https://test.com/server.php', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ 
            payment_method_id: result.paymentMethod.id, 
             amount: amount


          })
        }).then(function(confirmResult) {
            console.log(confirmResult);
          return confirmResult.json();
        }).then(handleServerResponse);
      }
    });
  } 


  }
}

$plan = Plan::create([
                'currency' => $currency,
                'interval' => 'month',
                'product' => $product->id,
                'nickname' => 'Payment Plan for order - ' . $order_id,
                'amount' => $price,
                'metadata' => [
                    'order_number' => $order_id,
                    'bgt_customer_number' => $customer_id,
                ],
            ]);
            $schedule = SubscriptionSchedule::create([
                'customer' => $customer->id,
                'start_date' => 'now',
                'end_behavior' => 'cancel',
                'metadata' => [
                    'local_id' => $order_id,
                    'local_account_id' => $account_id,
                    'local_currency_id' => $currency_id,
                    'local_user_id'=> $user_id,
                    'local_installments_total' => $plan_length,
                    'bgt_customer_number' => $customer_id,
                    'order_number' => $order_id,
                    'invoice_description' => 'Payment Plan for order - ' . $order_id
                ],
                'phases' => [
                    [
                        'items' => [
                            [
                                'plan' => $plan->id,
                            ],
                        ],
                        'collection_method'=> 'charge_automatically',
                        'iterations'=>$plan_length
                    ],
                ],
            ]);
            $metadata = [
                'installments_paid' => '0',
                'installments_total' => $plan_length,
                'local_id' =>$order_id,
                'local_account_id' =>$account_id,
                'local_currency_id' =>$currency_id,
                'subscription_schedule_id' => $schedule->id
            ];

            $subscription_id=$schedule->subscription;
            $result=Subscription::update($subscription_id,
                [
                    'metadata' => $metadata,
                    'default_payment_method' => $paymentMethodParams['payment_method'],
                    'proration_behavior' =>'always_invoice',
                    //Create only after adding payment method id in customer
                ]
            );

我正在获取 sca 卡的 SCA 模式,并且工作流程是正确的。但我担心的是测试使用 sca 创建的订阅

我使用 4000000000003220 和 424242424242424 进行了测试,订阅分 3 期创建。

订阅是使用正确的分期付款创建的:但我担心的是订阅第一期不会立即收费。发票显示为:

此发票草稿由订阅生成。它可以被编辑,直到它在 1 小时内自动完成。

.

当我尝试从条纹仪表板完成充电(作为测试的一部分)时,它显示

发票已完成,但客户需要完成 3D Secure 身份验证才能成功付款。他们的银行需要此额外步骤以防止欺诈。您的设置中禁用了 3D 安全电子邮件,因此您需要手动通知客户

未能捕获发票。问题:1:我希望第一期或第一期发生。我如何在登台环境中实现这一点。我在创建订阅或 SCA 方法时是否遗漏了任何要点?

2:什么是真正的“确认方法”=>“手动”。真的很迷茫。

标签: phpstripe-paymentslaravel-cashier

解决方案


使用 Stripe 订阅时,您通常不会自己创建付款意图。相反,您将创建订阅和发票等计费对象并与之交互。发票将为您创建付款意向。Stripe 有一个指南,可以引导您获取付款信息​​和创建订阅。如果您通读并遵循该指南,您应该能够构建您想要的东西。

要回答您的具体问题:

问题:1:我希望第一期或第一期发生。我如何在登台环境中实现这一点。我在创建订阅或 SCA 方法时是否遗漏了任何要点?

这可能是因为您正在创建一个付款意向并支付与属于您的订阅发票的付款意向无关的款项。

2:什么是真正的“确认方法”=>“手动”。真的很迷茫。

confirmation_method属性控制如何确认付款意图。但是,如果您使用订阅并希望支持 SCA,则应忽略此属性,而应遵循上面链接的指南。


推荐阅读