我在 Laravel 应用程序中使用 Stripe 作为支付网关。我希望为客户提供便利,让他们可以使用 Stripe 个人资料中存储的默认付款方式进行订阅,而无需每次重新输入银行卡详细信息。
我在 Laravel 应用程序中使用 Stripe 作为支付网关。我希望为客户提供便利,让他们可以使用 Stripe 个人资料中存储的默认付款方式进行订阅,而无需每次重新输入银行卡详细信息。
目前的方法:
我已经实现了使用 Stripe\StripeClient 创建新付款方式的代码,并将其附加到用户的 Stripe 客户:
$stripeKey = setting(setting('cashier_mode', 'sandbox') . '_stripe_secret_key');
$stripe = new \Stripe\StripeClient($stripeKey);
$paymentDetail = $stripe->paymentMethods->create([
'type' => 'card',
'card' => [
'number' => $request->card_number,
'exp_month' => $request->exp_month,
'exp_year' => $request->exp_year,
'cvc' => $request->cvc,
],
'billing_details' => [
'address' => [
'line1' => $request->line1,
'line2' => $request->line2, // Optional address line 2
'city' => $request->city,
'state' => $request->state,
'postal_code' => $request->postal_code,
'country' => $request->country,
],
'name' => $request->name,
],
]);
$stripe->paymentMethods->attach(
$paymentDetail->id,
['customer' => auth()->user()->stripe_customer_id]
);
虽然这创建了一种新的付款方式,但对于该场景来说并不理想,因为用户现有的默认付款方式并未得到利用。
期望的行为:
我想利用用户在 Stripe 客户资料中存储的默认付款方式,避免他们在结帐时重新输入银行卡详细信息。 Stripe 结账链接代码:
生成 Stripe Checkout Link 的代码当前包含以下参数:
$payload = [
'success_url' => config('services.accounts.base_url') . "/plan-callback?". http_build_query([
'subscription' => $subscription->id,
'price' => $price->id
]) . '&subscription_id={CHECKOUT_SESSION_ID}',
'cancel_url' => config('services.accounts.base_url') . "/plans/".$price->id."/change",
'payment_method_types' => ['card'],
'mode' => 'subscription',
'billing_address_collection' => 'required',
'line_items' => [
[
'price' => $planId,
'quantity' => 1
]
],
'metadata' => [
'user' => $user->id
],
'customer' => $user->stripe_customer_id,
];
$response = Cashier::client('stripe')->checkout->create($payload);
if ($response) {
return $response->url ?? null;
}
问题:
如何修改代码以自动利用结帐链接内其 Stripe 配置文件中存储的用户默认付款方式,而不是要求创建新付款方式或指定 payment_method_types?
其他背景:
所需答案: 我正在寻求有关如何调整 Stripe Checkout Link 生成以无缝使用用户的默认付款方式而无需手动配置或重新输入卡详细信息的指导。