0

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?

1 Answer 1

1

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'],
Sign up to request clarification or add additional context in comments.

3 Comments

THX @KaeVerens - but as I already mentioned in the question, your answer does not work. (Mentioned in 2nd last line of my q)
I understand. however, $aname is definitely correct, and '<?php echo $aname ?>' is definitely incorrect. I'd suggest that you have some other errors in the file. You only showed the one error message (to do with the ?>) , and didn't show the error messages from the other attempts. check the line numbers given by the error messages.
You are right. I was using $aname with quotes, which was also a cause of error. Using it without any quotes (like you answered) fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.