I’m creating an embedded online payment facility for an existing PHP web application, and it’s ALMOST fully working, just one more challenge to fix.
The flow of the programme as I’ve set it up goes like this:
-
Customer enters card details into the Web Payments SDK embedded in a PHP page.
-
The Web Payments SDK throws the input data NOT straight to the Square REST API / payments endpoint, but instead to a PHP script (square_process.php). This is because I need to capture the response data from the endpoint before sending the response back to the Web Payments SDK.
-
square_process.php sends the payment info to the REST endpoint, then captures the response in order to record the payment against the invoice on our web app. It then outputs the data for the benefit of the Web Payments SDK that called square_process.php. As far as the SDK is concerned, I’, aiming to have square_process.php mimic the behaviour of the endpoint.
So, within square_process.php, when I test it WITHOUT capturing the response data from the endpoint, and just output it directly, like this…
$ccon = curl_init(“https://connect.squareupsandbox.com/v2/payments/”);
curl_setopt($ccon, CURLOPT_POST, true);
curl_setopt($ccon, CURLOPT_HTTPHEADER, Array(‘Square-Version: 2024-03-20’,‘Authorization: Bearer XXXXXXXXXXXXXXXXX’,‘Content-Type: application/json’));
curl_setopt($ccon, CURLOPT_POSTFIELDS, $jsonData);
curl_exec($ccon);
…then it works fine, and the Web Payments SDK correctly reports success or failure.
But when I do this, in order to capture the incoming data and work with it first (store the payment in our database), and then pass it back to the SDK:
$ccon = curl_init(“https://connect.squareupsandbox.com/v2/payments/”);
curl_setopt($ccon, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ccon, CURLOPT_POST, true);
curl_setopt($ccon, CURLOPT_HTTPHEADER, Array(‘Square-Version: 2024-03-20’,‘Authorization: Bearer XXXXXXXXXXXXXXXXX’,‘Content-Type: application/json’));
curl_setopt($ccon, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($ccon);
(preprocessing happens here)
echo $response;
…then the SDK reports a successful payment regardless of the actual outcome.
When I put in the dummy card info for a failed payment, the result that come back in $response, and the API logs both show a failed payment, but the onscreen response from the SDK is “Payment Successful”. So, everything is working fine except the passing back of info from square_process.php to the SDK.
I’ve tried writing the contents of $response to a separate file, and this proves that it’s definitely a JSON data pack for a failed transaction.
Is it correct to just echo $response? This should be in JSON form already as it comes back from the endpoint, right? Do I need to also output header info or something?
Any help or suggestions would be appreciated!