Kaushal Joshi
Kaushal Joshi

Reputation: 41

How to Use Default Payment Method in Stripe Checkout Link with Laravel (Without Specifying Card Details)

I'm using Stripe as a payment gateway in my Laravel application. I want to offer customers the convenience of using their default payment method stored in their Stripe profile for subscriptions, eliminating the need to re-enter card details each time.

I'm using Stripe as a payment gateway in my Laravel application. I want to offer customers the convenience of using their default payment method stored in their Stripe profile for subscriptions, eliminating the need to re-enter card details each time.

Current Approach:

I've implemented code to create a new payment method using the Stripe\StripeClient and attach it to the user's Stripe customer:

$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]
    );

While this creates a new payment method, it's not ideal for the scenario as the user's existing default one isn't being utilized.

Desired Behavior:

I want to leverage the user's default payment method stored in their Stripe customer profile, avoiding the need for them to re-enter card details at checkout. Code for Stripe Checkout Link:

The code for generating the Stripe Checkout Link currently includes these parameters:

$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;
    }


Question:

How can I modify the code to automatically utilize the user's default payment method stored in their Stripe profile within the Checkout Link, rather than requiring the creation of a new one or specifying payment_method_types?

Additional Context:

  1. Stripe Laravel Package : "stripe/stripe-php"
  2. Stripe version : "^13.13.0"
  3. Laravel version : "^9.2"

Desired Answer: I'm seeking guidance on how to adjust the Stripe Checkout Link generation to seamlessly use the user's default payment method without the need for manual configuration or re-entering card details.

Upvotes: 0

Views: 523

Answers (1)

Nolan H
Nolan H

Reputation: 7459

You can set the customer invoice_settings.default_payment_method (API ref) to be used with Subscriptions.

You can read about the requirements and limitations of prefilling the card for mode=subscription here in the docs.

Upvotes: 0

Related Questions