Web Payments SDK reports Payment Success even when payment failed

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:

  1. Customer enters card details into the Web Payments SDK embedded in a PHP page.

  2. 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.

  3. 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!

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Card Payments
Payment API: Process Online and In-Person Payments
Take Payments

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

What information are you capturing and storing prior to the card being charged? :slightly_smiling_face:

Nothing. I’m capturing information AFTER the card is charged. Basically the web application currently store all the customers’ invoices, I’m writing something for them to make payments on these invoices via a Square invoice, so after the payment package is sent to the Square REST endpoint to process the transaction (using a paymentID generated by the Web Payment SDK), then it returns a result reporting on the success or failure of the transaction, transaction ID etc. This is what I need to process so that I can record on the web application database whether the payment was processed successfully.

This line:

curl_exec($ccon);

…would normally output the result data. I added this line before:

curl_setopt($ccon, CURLOPT_RETURNTRANSFER, true);

…which allows me to assign the output to a variable when executing the curl_exec command instead like this:

$response = curl_exec($ccon);

Then I used the data to record a payment against the invoice on our database, if the payment was successful. But now I also need it to output that same data the way it does when CURLOPT_RETURNTRANSFER is not set, so that that data is returned to the Web Payment SDK that called square_process.php, so the customer can see if it was successful too.

Okay I solved my own problem. I was returning all the data, but not the HTTP response code, which is apparently all the Web SDK looks at. I used the PHP function http_response_code function to do that and now it works fine :slight_smile:

Glad to hear that you were able to figure it out. Thanks for sharing your findings. :slightly_smiling_face: