Search code examples
laravellaravel-5stripe-paymentslaravel-cashier

Select card from saved cards, if not create a new card Before Payment in Laravel Cashier


Select card from saved cards, if not create a new card Before Payment in Laravel Cashier.

Note: Just Charging the $N amount, N can be different for next time checkout.

Step 1: List of Saved Cards, if not create a new one.

Step 2: Choose Card

Step 3: Checkout the payment.


Solution

  • $defaultCard = $user->defaultCard();  // Default Card
    
    $cards = $user->cards();   //List of Cards
    
    
    //Make current source as default
    $customer = \Stripe\Customer::retrieve(($request->stripe_id));
    $customer->default_source = $request->card_token;
    $customer->save();
    
    // Now charge customer
    $charge = \Stripe\Charge::create([
                    'amount'   => $amount,
                    'currency' => 'usd',
                    'customer' => $customer->id,
                ]);