x

PaymentsApi->createPayment: [HTTP/1.1 401 Unauthorized]

I am completely new to Square, so please pardon if I am missing something simple, but I have been trying to test the createPayment() for days now and keep hitting the NOT AUTHORIZED response!

 

I set up the sandbox account and fetched the access token, and used the sample code to generate a nonce which I plugged into the server-side php page that will handle the payment. But I keep getting an Unauthorized response!

 

Can anyone help me with what I hope is an obvious and simple tweak to make this work? Here is my PHP code that is trying to create the payment on the nonce:

 

<?php

require_once('include/square/connect-php-sdk-master/autoload.php'); // This from the github PHP SDK
SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken('I input my sandbox ACCESS TOKEN here'); // Sandbox Access Token

$apiInstance = new SquareConnect\Api\PaymentsApi();
$body = new \SquareConnect\Model\CreatePaymentRequest(); */

$Money = new \SquareConnect\Model\Money();
$Money->setAmount(100);
$Money->setCurrency('USD');

$body->setLocationId('I input my sandbox location ID here');
$body->setSourceId('I input the nonce generated from the card form here');
$body->setIdempotencyKey('ABCDECBASEDheAIwXfvIOoWfqzKac9yY'); // just a random string
$body->setAmountMoney($Money);

try {
$result = $apiInstance->createPayment($body);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling PaymentsApi->createPayment: ', $e->getMessage(), PHP_EOL;
}

?>

771 Views
Message 1 of 2
Report
1 REPLY 1

This error looks super familiar and I swear I got it recently, but can't remember exactly what was causing it, but in general I almost always have problems with the Access Token and the Location ID... so my advice is to carefully double check those. Like really don't assume they're both correct. Look for a missing character if you have to because 8 times out of 10 one of those is always my problem!

 

 

GetPreOrder.com, Official Square Partner - Create a $250K custom SuperSite for Square free. https://getpreorder.com/supersites
719 Views
Message 2 of 2
Report