Custom Payment Flow Stripe - CalculateOrderAmount

Viewed 280

Hi to everybody who may answer.

I'm struggling with Stripe custom payment flow more precisely with the function CalculateOrderAmount() and mostly struggling because of my PHP knowledge.

Basically I retrieve the price of the item from my database and when a customer add it to the cart like this:

$dbh = new PDO( DB_DSN, DB_USER, DB_PASS );

$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );


$sql = 'SELECT *

        FROM card

        where orderRef =' . $_SESSION['orderRef'];

$cards = dbSelectAll( $dbh, $sql );

$price = count( $cards ) * 10;

When I put this kind of silly code into the function

function CalculateOrderAmount()
{
global $price;

$items = [

    'price' => $price

];

return $items['price'];
}

I have an error message: Uncaught (in promise) SyntaxError: Unexpected end of JSON input at client.js:19 because of those lines of code:

// retrieve JSON from POST body

$json_str = file_get_contents('php://input');

$json_obj = json_decode($json_str);



$paymentIntent = \Stripe\PaymentIntent::create([

    'amount' => calculateOrderAmount($json_obj->items),

    'currency' => 'eur',

    'payment_method_types' => ['card']

]);

I don't know what I'm doing wrong but it is completely wrong, can someone give me a hint on what should I do and did someone have dealt with stripe custom payment flow and have figured it out.

Many thanks in advance for your answers.

0 Answers
Related