phpstripe-paymentslaravel-cashier

Stripe API (PHP) - Create a charge with default payment method card


I am using Strip PHP API and trying to create a charge:

$chargeData = [
    "amount" => $amountTaxed,
    "currency" => $this->settings->currency_code,
    "source" => 'tok_visa', // Card token
    //'customer' => $this->user->stripe_id,
    "description" => __('general.add_funds') . ' (Auto-recharge) @' . $this->user->username,
    'metadata' => [
        'user' => $this->user->stripe_id,
        'amount' => $amountTaxed,
        'taxes' => $this->settings->tax_on_wallet ? $this->user->taxesPayable() : null,
        'type' => 'deposit'
    ]
];
$charge = \Stripe\Charge::create($chargeData);

Below is payment method card detail added in customer account

enter image description here

I know how to get card token via card number

$token = \Stripe\Token::create([
    'card' => [
        'number' => '4242424242424242',
        'exp_month' => 7,
        'exp_year' => 2024,
        'cvc' => '314',
    ],
]);

If I get token manually using the card number \Stripe\Token::create, stripe's charge method \Stripe\Charge::create works fine. So my only problem is to get the source/token.

My intention is to get the token from existing payment method's card.

If there is any other way to charge from customer's existing payment methods's card, please let me know.

Thank you!


Solution

  • You're using an older integration path. Stripe's Charges API still work but are not recommended. I recommend starting with their guide for building an integration that charges a customer and saves their payment details for later charges.