I am trying to integrate square online payments with my website. (UK GBP).
I am using the Php version of the scripts etc and trying to connect with the sandbox.
I have set the JS to be ‘https://js.squareupsandbox.com/v2/paymentform.js’ and have set the location id, access code and applications ids for the sandbox env.
In case this is relevant, the php file has points to the following spot for the apis - $transactions_api = new \SquareConnect\Api\TransactionsApi();
When i try a test transaction , I keep getting the error below.
I would like some help to diagnose the cause please. I have read the other threads on the same issue and while i see they suggest a mismatch between sandbox and live, I don’t know where this mismatch could be occuring.
TransactionApi->charge:object(stdClass)#7 (1) { [“errors”]=> array(1) { [0]=> object(stdClass)#6 (3) { [“category”]=> string(20) “AUTHENTICATION_ERROR” [“code”]=> string(12) “UNAUTHORIZED” [“detail”]=> string(352) "The Authorization http header of your request was incorrect or expired. The header value is expected to be of the format “Bearer TOKEN” (without quotation marks), where TOKEN is a valid access token (e.g. “Bearer ABC123def456GHI789jkl0”).
So wherever you’re setting the access token before creating the transactionApi, that access token is either incorrect or expired. If you’ve used OAuth, then you would need to refresh the token, or just make sure you’re using the sandbox access token from your developer dashboard.
Also, I would like to point out that the Transactions API is going to be fully retired later this year in September, and is currently expired. You should definitely move to the Payments API at your earliest convenience.
Hi Stephen
Thanks for this. I have checked and double checked (and even generated a new access token) and it still results in the same error. (the access token matches the environment i’m pointing to).
I am happy to move to the Payments API but as a very novice developer I really need a copy of a fully coded php/html file to implement it from scratch - rather than just migration info Migrate from Transactions API.
Alternatively, could you have a look at my current php file below and let me know what i need to change?
Do you have anything like that? perhaps in connect-api-examples.
I have been liaising with a member of your API team but frankly am making very little progress now. Happy to have a phone conversation.
thanks Aparna
<?php
require 'connect-php-sdk-master/vendor/autoload.php';
$access_token = '{{redacted}}';
# setup authorization
\SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($access_token);
# create an instance of the Transaction API class
$transactions_api = new \SquareConnect\Api\TransactionsApi();
$location_id = 'LGT9ET3MFFYP9';
$nonce = $_POST['nonce'];
$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" => (int) $_POST['amount'],
"currency" => "GBP"
),
# 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()
);
try {
$result = $transactions_api->charge($location_id, $request_body);
// print_r($result);
// echo '';
if($result['transaction']['id']){
echo 'Payment success!';
echo "Transation ID: ".$result['transaction']['id']."";
}
} catch (\SquareConnect\ApiException $e) {
echo "Exception when calling TransactionApi->charge:";
var_dump($e->getResponseBody());
}
?>
Please do not share your access token with anyone; I’ve removed it but that’s a big security concern.
No where in your code have you set the sandbox host domain, which is why you’re seeing this error (you’re using sandbox access token + location id but hitting production urls).
You would need to do something like \SquareConnect\Configuration::getDefaultConfiguration()->setHost("https://connect.squareupsandbox.com"); to fix the error.