Home > database >  PHP JSON Stripe Text string variable leading to parsing error
PHP JSON Stripe Text string variable leading to parsing error

Time:08-22

Trying to setup Stripe for a client.

In the following code, when I use the term ('Formal Trousers') directly in the code, it works perfectly fine.

However, when I try to use a variable (like, $aname='Formal Trousers';), it is throwing a parsing error (PHP Parse error: syntax error, unexpected end of file in /home/folder1/file1.php). Line no. reported in error is the last line of the PHP file (where we have ?>).

The code that works (see last line of the code where I have ( 'metadata' => ['item_name' => 'Formal Trousers','item_no' => '31'],):

// Set API key 
\Stripe\Stripe::setApiKey(STRIPE_API_KEY); 
 
$response = array( 
    'status' => 0, 
    'error' => array( 
        'message' => 'Invalid Request!'    
    ) 
); 
$aname='Formal Trousers';
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
    $input = file_get_contents('php://input'); 
    $request = json_decode($input);

} 
 
if (json_last_error() !== JSON_ERROR_NONE) { 
    http_response_code(400); 
    echo json_encode($response); 
    exit; 
} 
 
if(!empty($request->createCheckoutSession)){ 
    // Convert product price to cent 
    $stripeAmount = round($productPrice*100, 2); 
 
    // Create new Checkout Session for the order 
    try {
        $checkout_session = \Stripe\Checkout\Session::create([
            'payment_method_types' => ['card'],
            'metadata' => ['item_name' => 'Formal Trousers','item_no' => '31'],

Now, instead of a static item name as 'Formal Trousers', I want to use a PHP variable ($aname - like below)

The code that DOES NOT works with a variable (see last line of the code where I have ( 'metadata' => ['item_name' => '<?php echo $aname ?>','item_no' => '31'],):

// Set API key 
\Stripe\Stripe::setApiKey(STRIPE_API_KEY); 
 
$response = array( 
    'status' => 0, 
    'error' => array( 
        'message' => 'Invalid Request!'    
    ) 
); 
$aname='Formal Trousers';
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
    $input = file_get_contents('php://input'); 
    $request = json_decode($input);

} 
 
if (json_last_error() !== JSON_ERROR_NONE) { 
    http_response_code(400); 
    echo json_encode($response); 
    exit; 
} 
 
if(!empty($request->createCheckoutSession)){ 
    // Convert product price to cent 
    $stripeAmount = round($productPrice*100, 2); 
 
    // Create new Checkout Session for the order 
    try {
        $checkout_session = \Stripe\Checkout\Session::create([
            'payment_method_types' => ['card'],
            'metadata' => ['item_name' => '<?php echo $aname ?>','item_no' => '31'],

Also, tried only $aname (since it is all PHP code) as follows, but all those are failing:

        'metadata' => ['item_name' => '$aname','item_no' => '31'],
        'metadata' => ['item_name' => "$aname",'item_no' => '31'],
        'metadata' => ['item_name' => $aname,'item_no' => '31'],

Any idea how to use a variable in the above case?

CodePudding user response:

I think you're getting "unexpected end of file" because of the '?>' in this line:

    'metadata' => ['item_name' => '<?php echo $aname ?>','item_no' => '31'],

the compiler thinks you are leaving PHP mode and going back to plain text output. everything after that is simply output to the buffer instead of being run as PHP.

the correct line is:

    'metadata' => ['item_name' => $aname, 'item_no' => '31'],
  • Related