Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bilalbasheer's avatar

Not asking consent in stripe for saving card for future usage

While using stripe, the card for future usage is saving without asking user consent. I have implemented stripe using Epheremal key. The client said he need to be sked consent before saving it, I read stripe docs and followed same steps but couldn't find the issue.

iam using flutter as frontend

   public function pay(Transaction $transaction): array 
   {
    $customerId = $this->getCustomer($transaction);

    $ephemeralKey = $this->getEphemeralKey($customerId);

    $paymentIntent = $this->api()->paymentIntents->create([
        'amount' => $transaction->amount * 100,
        'currency' => 'AED',
        'description' => 'ABCD',
        'customer' => $customerId,
        'setup_future_usage' => 'off_session',
    ]);

    return $paymentIntent->toArray() + ['ephemeral_key' => $ephemeralKey->secret, 'stripe_customer_id' => $customerId, 'customer_session_client_secret' => null];
}

public function getCustomer(Transaction $transaction)
{
    $transactionModel = $transaction->model;

    if ($transactionModel instanceof Order) {
        $model = $transactionModel->customer;
    }
    if ($transactionModel instanceof Subscription) {
        $model = $transactionModel->merchant;
    }

    if ($model->stripe_customer_id) {
        return $model->stripe_customer_id;
    }

    $customer = $this->api()->customers->create([
        'name' => $model->name,
        'email' => $model->email,
    ]);

    $model->stripe_customer_id = $customer->id;
    $model->save();

    return $customer->id;
}


private function getEphemeralKey(mixed $customerId): EphemeralKey
{
    return $this->api()->ephemeralKeys->create(
        ['customer' => $customerId],
        ['stripe_version' => '2024-09-30.acacia']
    );
}
1 like
5 replies
vincent15000's avatar

Is it for a single payment or for a subscription ?

Not sure, but I think that the card is necessarily saved if it's a subscription.

vincent15000's avatar

@bilalbasheer What front are you using for the payment form ?

Is it your own form or just a link from Stripe ?

Have you checked the configuration for the payment form ?

bilalbasheer's avatar
bilalbasheer
OP
Best Answer
Level 1
$paymentIntent = $this->api()->paymentIntents->create([
        'amount' => $transaction->amount * 100,
        'currency' => 'INR',
        'description' => 'ABCD',
        'customer' => $customerId,
        'automatic_payment_methods'=> [
            "enabled" => true
        ]
    ]);

    $customer_session = $this->api()->customerSessions->create([
        'customer' => $customerId,
        'components' => [
            'payment_element' => [
                'enabled' => true,
                'features' => [
                    'payment_method_save' => 'enabled',
                    'payment_method_save_usage' => 'on_session',
                ],
            ],
        ],
    ]);

issue fixed

1 like

Please or to participate in this conversation.