Borja
Borja

Reputation: 3551

Using Stripe how can I pass PaymentIntent’s client secret to the form?

I would like to add Stripe in my website but I'm noticing that is very difficult to use and there aren't more sources of help in the web.

My first problem is how to Pass the PaymentIntent’s client secret to the client, so how to pass a php variable in my html form.

Before I should create a PaymentIntent object:

\Stripe\Stripe::setApiKey('sk_test_8cpWr3HupPdvL9QK9cVToa4w009OYgxfNm');

$intent = \Stripe\PaymentIntent::create([
    'amount' => 0.89,
    'currency' => 'eur',
]);

Then API Stripe suggest to pass the PaymentIntent’s client secret to the client in this way:

<?php
    $intent = # ... Fetch or create the PaymentIntent;
?>
...
<input id="cardholder-name" type="text">
<!-- placeholder for Elements -->
<div id="card-element"></div>
<button id="card-button" data-secret="<?= $intent->client_secret ?>">
  Submit Payment
</button>
...

This is page of API: https://stripe.com/docs/payments/payment-intents/quickstart#passing-to-client

But how can I pass a php variable in html page?

And someone can explain me what is this client secret?

Upvotes: 0

Views: 5563

Answers (1)

taintedzodiac
taintedzodiac

Reputation: 2908

The client_secret is a property of a PaymentIntent that you can use on the frontend with your publishable key.

Based on the example in the Quick Start guide, this is a basic example:

<?php
$intent =\Stripe\PaymentIntent::create([
    "amount" => 2000,
    "currency" => "usd",
    "payment_method_types" => ["card"],
]);
?>
...
<input id="cardholder-name" type="text">
<!-- placeholder for Elements -->
<div id="card-element"></div>
<button id="card-button" data-secret="<?= $intent->client_secret ?>">
  Submit Payment
</button>
...

The key things to note are that you're creating $intent in your code, and then echoing $intent->client_secret as the proper value in your form later on.

Upvotes: 1

Related Questions