Skip to content
Advertisement

Stripe PaymentIntents PHP, setting price

Basically, the problem im having is between php and javascript, where my php function is createPayment() I can’t put javascript variable so I can’t enter the amount ($) of the order I’m really stuck on what to do.

Maybe a post event to the server to create payment via javascript but I don’t know how to do that

Javascript:

JavaScript

Server side:

JavaScript

Advertisement

Answer

METHOD ONE

One of the ways to tackle the problem is to add an intermediate PHP (e.g. intermediate.php)

So if your original flow is

cart.php > processpayment.php (with javascript) calling payment.php to get client_secret

then change the flow to

cart.php > intermediate.php > processpayment.php(with javascript) calling payment.php to get client_secret

So for the cart.php, first call intermediate.php with a parameter of the amount, say intermediate.php?amount=1234 :

intermediate.php

JavaScript

Now in the processpayment.php

change

JavaScript

to

JavaScript

Note: Make sure you have <?php session_start(); ?> at the top of processpayment.php too

METHOD TWO # (use ajax)

An alterative is to pass the amount value (e.g. 1234) thru ajax to the server script which will return the client secret, and the use the client secret in the stripe payment processing

Hence , use the following html/js:

  1. the HTML will trigger the JS function named trigger1() on window load

  2. the JS trigger1() will run an ajax function, passing the amount (e.g. 1234) to server2.php and wait for the return of the client secret , then put it into a hidden input field known with ID “secret”

  3. now you can get the correct client secret for the stripe payment function thru document.getElementById(“secret”).value, so can now just use:

JavaScript

So , the following is the HTML/JS:

JavaScript

and use the following php (server2.php)

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement