【问题标题】:What is the best way of handling a new PaymentIntent in Laravel Cashier在 Laravel Cashier 中处理新的 PaymentIntent 的最佳方法是什么
【发布时间】:2020-04-13 04:52:48
【问题描述】:

除非我遗漏了什么,否则我看不到任何 Cashier 包装器来提供 PaymentIntent 的创建,这是否存在?

虽然 Cashier v10 添加了一些内容来处理 SCA,但它对我不起作用,因为我正在通过 font-end 元素处理付款详细信息,因此重定向到新的 route 很麻烦。

我需要按照本指南处理它https://stripe.com/docs/payments/payment-intents/migration

这需要像这样创建一个 PaymentIntent:

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

我当然可以直接从 Stripe sdk 执行此操作,如此处所示,但考虑到我可能会添加更多功能,这些功能可能会利用 Cashier 的其他功能,它会让所有事情都通过 Cashier 更干净。

我可以通过 Cashier 创建 PaymentIntent 吗?还是错过了?如果是这样,怎么做?还是我应该以不同的方式处理?

【问题讨论】:

    标签: stripe-payments laravel-cashier


    【解决方案1】:

    我不是 Cashier 用户,但它看起来是 Stripe Billing 的接口,因此它主要处理与 Billing 相关的原语。 据我所知,它确实在后台使用了 PaymentIntents(用于验证需要身份验证的发票付款),但不直接公开 PaymentIntent 的创建。

    Cashier 文档建议在需要身份验证时将您的集成重定向到“支付页面”:https://laravel.com/docs/5.8/billing#payments-requiring-additional-confirmation(对于 PaymentIntents 处于状态 requires_action 的情况)

    对于直接创建 PaymentIntents,stripe-php API 库将是正确的方法。

    【讨论】:

    • 感谢您的回复,Laravel 文档的重定向方法对我不起作用,因为我正在使用 API 通过前端处理整个过程,这就是为什么我需要 Stripe 文档中的 paymentIntents 方法。但是 Cashier 似乎不支持 createPaymentIntent,因此我的问题是,还有办法使用 PaymentIntent 吗?
    • Cashier 似乎不是这样,您可以使用 Stripe-PHP 代替,它允许您使用所有 Stripe 资源,包括创建 PaymentIntents:stripe.com/docs/api/payment_intents/create?lang=php
    【解决方案2】:

    进一步调查后,答案是

    Laravel\Cashier\Billable->charge($amount,$method,$options)
    

    方法在幕后执行\Stripe\PaymentIntent::create($options)

    有几点需要注意:

    • 默认情况下,选项confirmation_method 将设置为automaticconfirm 设置为true
    • currency 选项将被设置并使用 Cashier 默认货币
    • amount 选项将根据传入的$amount 设置,payment_method 选项将根据传入的$method 设置。
    • 通过在 $options 参数中传递等效值,这些值都可以被替换覆盖

    最后,如果您需要SCA(我这样做了),那么您必须捕获\Laravel\Cashier\Exceptions\PaymentActionRequired 异常并为此重定向到内置的Cashier 页面,或者在前端通过您的 API 请求。

    【讨论】:

      【解决方案3】:

      扩展 Laravel\Cashier\Billable Trait; 和 Laravel\Cashier\Concerns\PerformsCharges; 并使用它来代替 User 模型中的默认 Billable trait

      <?php
      
      namespace App\Traits\Stripe;
      
      use Laravel\Cashier\Concerns\ManagesCustomer;
      use Laravel\Cashier\Concerns\ManagesInvoices;
      use Laravel\Cashier\Concerns\ManagesPaymentMethods;
      use Laravel\Cashier\Concerns\ManagesSubscriptions;
      use App\Traits\Stripe\CustomPerformCharges as PerformsCharges;
      
      trait CustomBillable
      {
          use ManagesCustomer;
          use ManagesInvoices;
          use ManagesPaymentMethods;
          use ManagesSubscriptions;
          use PerformsCharges;
      }
      
      trait CustomPerformCharges
      {
          use PerformsCharges;
        
          public function authorize(int $amount): Payment
          {
              if (!$this->hasStripeId()) {
                  throw new BadRequestException(__('User is not a stripe customer'));
              }
              
              $paymentMethod = $this->defaultPaymentMethod();
      
              $options = [
                  'capture_method' => 'manual'
              ];
      
              return $this->charge($amount, $paymentMethod, $options);
          }
      
      
          public function capture(int $amount, string $paymentIntent): void
          {
              $intent = StripePaymentIntent::retrieve($paymentIntent, $this->stripeOptions());
              $intent->capture(['amount_to_capture' => $amount]);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2022-01-11
        • 2010-09-06
        • 2018-05-27
        • 2011-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多