Jay1
September 29, 2023, 5:18pm
1
Hello, If anyone could help it would be greatly appreciated Im running the following code on my mamp server
$access_token="p";
$ch= curl_init();
$url = “https://connect.squareup.com/v2/online-checkout/payments ”;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array(
‘Square-Version: 2023-08-16’,
‘Authorization: Bearer’.$access_token,
‘Content-Type: application/json’,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = array(
‘description’ => ‘Clothes’,
‘quick_pay’ => array(
‘location_id’ => 'L 0R8’,
‘name’ => ‘clothes’,
),
‘quick_pay’ => array(
‘amount’ => 250,
‘currency’ => ‘USD’,
),
‘idempotency_key’ => 'a32 ********2a73’,
‘payment_note’ => ‘large’,
);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$resp = curl_exec($ch);
if ($e = curl_error($ch)) {
echo $e;
} else {
$decode = json_decode($resp);
print_r($decode);
}
the output I am receiving is
stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [category] => INVALID_REQUEST_ERROR [code] => NOT_FOUND [detail] => Resource not found. ) ) )
When I change
$url = “https://connect.squareup.com/v2/payments ”;
the output I received is
stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [category] => AUTHENTICATION_ERROR [code] => UNAUTHORIZED [detail] => This request could not be authorized. ) ) )
I am using the example from https://developer.squareup.com/explorer/square/checkout-api/create-payment-link
There’s a few issues in the code you’ve provided that need to be corrected.
Incorrect Quotes: The double quotes around the URL and the headers are using the wrong character. Replace the smart quotes (“ ”) with regular double quotes (").
Typo in cURL resource: You’ve defined the cURL resource as $ch
, but later you mistakenly use $curl
when setting the POSTFIELDS option. Change $curl
to $ch
.
Quick Pay Array: It seems that you are trying to send an array within an array for the ‘quick_pay’ field. To do this correctly, you should define ‘quick_pay’ as an array containing the sub-arrays. Additionally, it’s important to use the correct array keys.
If you try the following does it work:
<?php
$access_token = "p";
$ch = curl_init();
$url = "https://connect.squareup.com/v2/online-checkout/payments";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array(
'Square-Version: 2023-08-16',
'Authorization: Bearer ' . $access_token, // Added space after Bearer
'Content-Type: application/json',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = array(
'description' => 'Clothes',
'quick_pay' => array(
array(
'location_id' => 'L0R8', // Changed single quotes to double quotes
'name' => 'clothes',
),
array(
'amount' => 250,
'currency' => 'USD',
),
),
'idempotency_key' => 'a32********2a73',
'payment_note' => 'large',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // Used $ch instead of $curl
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);
?>