I keep getting this error, Error: Must supply amount_money don’t know what wrong with this code. Any help would be appreciated.
<?php
require 'vendor/autoload.php'; // Include the Square PHP SDK
use Square\SquareClient;
use Square\LocationsApi;
use Square\Exceptions\ApiException;
use Square\Http\ApiResponse;
use Square\Models\ListLocationsResponse;
use Square\Environment;
use Square\Models\Money;
use Square\Models\CreatePaymentRequest;
// Replace with your Sandbox credentials
$squareAccessToken = 'mytokenxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// Initialize Square API client
$client = new \Square\SquareClient([
'accessToken' => $squareAccessToken,
'environment' => \Square\Environment::SANDBOX, // Use Sandbox environment
]);
// Create a payment request
$paymentsApi = $client->getPaymentsApi();
// Create the amountMoney object
$total=10;
$money = new \Square\Models\Money();
$money->setAmount(floatval($total)*100); // Multiply the total by 100 to represent cents
$money->setCurrency('USD');
$cardNonce = 'cnon:card-nonce-ok'; // Replace with the actual card nonce
$paymentRequest = new CreatePaymentRequest($cardNonce, uniqid(), null, $money);
try {
// Send the payment request
$response = $paymentsApi->createPayment($paymentRequest);
// Handle the response
if ($response->isSuccess()) {
$payment = $response->getResult()->getPayment();
echo "Payment successful. Payment ID: " . $payment->getId();
} else {
$errors = $response->getErrors();
foreach ($errors as $error) {
echo "Error: " . $error->getDetail() . "\n";
echo "Total amount: " . $total . " USD<br>";
echo "Card Nonce: " . $cardNonce . "<br>";
}
}
} catch (\Square\Exceptions\ApiException $e) {
echo "Exception: " . $e->getMessage();
}
?>