x

PHP code always ends with caught exception

I developing a site to accept customer payments for a product, and I need to allow the customer to enter there credit card information and place an order for the product. I've downloaded sample code from the git site, along with all of the other recommended code. When I try to process a card using the sandbox, I get all o the way through to where the $transaction_api attempts to charge the card. Then I get back "Caught exception! Response body: NULL Response headers: NULL" 

 

At this point I'm stuck. I researched everything that i can find, and I don't see an answer.

Tags (1)
3,144 Views
Message 1 of 10
Report
9 REPLIES 9
Alumni

Welcome! I'm running this question by our Developers team to get more information for you. Be back soon! 😊

3,139 Views
Message 2 of 10
Report

Thank you Caty.

3,135 Views
Message 3 of 10
Report
Alumni

That example is meant to use an older version of the PHP SDK. 

 

Try using the code in this branch:https://github.com/square/connect-api-examples/blob/feature/migrate-to-2.1.0/connect-examples/v2.1/p... for the time being. 

3,126 Views
Message 4 of 10
Report

Thanks, but that doesn't appear to be a valid link.

3,121 Views
Message 5 of 10
Report
Alumni

The master branch has been updated now. 

3,119 Views
Message 6 of 10
Report

Hi i am having the same issue

 

Caught exception!
Response body:

NULL


Response headers:

NULL

 the given url is invalid. how can i resolve it using sandbox

3,077 Views
Message 7 of 10
Report

This is the code that I found that actually works:

<?php
require 'vendor/autoload.php';
# Replace these values. You probably want to start with your Sandbox credentials
# to start: https://docs.connect.squareup.com/articles/using-sandbox/
# The access token to use in all Connect API requests. Use your *sandbox* access
# token if you're just testing things out.
$access_token = '';
# Helps ensure this code has been reached via form submission
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
error_log("Received a non-POST request");
echo "Request not allowed";
http_response_code(405);
return;
}
# Fail if the card form didn't send a value for `nonce` to the server
$nonce = $_POST['nonce'];
if (is_null($nonce)) {
echo "Invalid card data";
http_response_code(422);
return;
}
\SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($access_token);
$locations_api = new \SquareConnect\Api\LocationsApi();
try {
$locations = $locations_api->listLocations();
#We look for a location that can process payments
$location = current(array_filter($locations->getLocations(), function($location) {
return !empty($location->getCapabilities()) &&
in_array('CREDIT_CARD_PROCESSING', $location->getCapabilities());
}));
} catch (\SquareConnect\ApiException $e) {
echo "Caught exception!<br/>";
print_r("<strong>Response body:</strong><br/>");
echo "<pre>"; var_dump($e->getResponseBody()); echo "</pre>";
echo "<br/><strong>Response headers:</strong><br/>";
echo "<pre>"; var_dump($e->getResponseHeaders()); echo "</pre>";
exit(1);
}
$transactions_api = new \SquareConnect\Api\TransactionsApi();
$request_body = array (
"card_nonce" => $nonce,
# Monetary amounts are specified in the smallest unit of the applicable currency.
# This amount is in cents. It's also hard-coded for $1.00, which isn't very useful.
"amount_money" => array (
"amount" => 100,
"currency" => "USD"
),
# Every payment you process with the SDK must have a unique idempotency key.
# If you're unsure whether a particular payment succeeded, you can reattempt
# it with the same idempotency key without worrying about double charging
# the buyer.
"idempotency_key" => uniqid()
);
# The SDK throws an exception if a Connect endpoint responds with anything besides
# a 200-level HTTP code. This block catches any exceptions that occur from the request.
try {
$result = $transactions_api->charge($location->getId(), $request_body);
echo "<pre>";
print_r($result);
echo "</pre>";
} catch (\SquareConnect\ApiException $e) {
echo "Caught exception!<br/>";
print_r("<strong>Response body:</strong><br/>");
echo "<pre>"; var_dump($e->getResponseBody()); echo "</pre>";
echo "<br/><strong>Response headers:</strong><br/>";
echo "<pre>"; var_dump($e->getResponseHeaders()); echo "</pre>";
}

 

3,069 Views
Message 8 of 10
Report

Thanks i have fixed.. its working on localhost now.

one thing more to confirm

 

can i run code on non ssl website for testing sandbox other then localhost?

3,066 Views
Message 9 of 10
Report

If you run it on a non-ssl url, you will see the same error as before.

I figured out what was going on by expanding the error code to show everything.

3,063 Views
Message 10 of 10
Report