getPaymentsApi() not working

Hi! I am trying to use the Square PHP SDK on my website, and there is an issue I’m finding very difficult to solve. In my PHP page, the getPaymentsApi() does not seem to work(I receive a 500 error). I commented out various parts of the code, and that was the only part that seemed problematic. Here’s my full code:

`<?php
require(‘autoload.php’);
//$paymentsApi = $client->getPaymentsApi();
$amount_money = new \Square\Models\Money();
$amount_money->setAmount(35);
$amount_money->setCurrency(‘USD’);

$app_fee_money = new \Square\Models\Money();
$app_fee_money->setAmount(9);
$app_fee_money->setCurrency(‘USD’);

$billing_address = new \Square\Models\Address();
$billing_address->setAddressLine1(‘Address’);
$billing_address->setLocality(‘City’);
$billing_address->setAdministrativeDistrictLevel1(‘State’);
$billing_address->setPostalCode(‘12345’);
$billing_address->setCountry(‘US’);
$billing_address->setFirstName(‘John’);
$billing_address->setLastName(‘Doe’);

$shipping_address = new \Square\Models\Address();
$shipping_address->setAddressLine1(‘Address’);
$shipping_address->setLocality(‘City’);
$shipping_address->setAdministrativeDistrictLevel1(‘State’);
$shipping_address->setPostalCode(‘12345’);
$shipping_address->setCountry(‘US’);
$shipping_address->setFirstName(‘John’);
$shipping_address->setLastName(‘Doe’);

$body = new \Square\Models\CreatePaymentRequest(
‘cnon:card-nonce-ok’,
‘idempotency_key_washerebutIputthisjustincaseIshouldntshareit’,
$amount_money
);
$body->setAppFeeMoney($app_fee_money);
$body->setAutocomplete(true);
$body->setLocationId(‘MYLOCATIONHERE’);
$body->setReferenceId(‘234567890dfgvb fghj’);
$body->setBuyerEmailAddress(‘[email protected]’);
$body->setBillingAddress($billing_address);
$body->setShippingAddress($shipping_address);
$body->setStatementDescriptionIdentifier(‘Testing’);

$api_response = $paymentsApi->createPayment($body); // THIS LINE HERE is the one that is problematic.

// if ($api_response->isSuccess()) {
// $result = $api_response->getResult();
// } else {
// $errors = $api_response->getErrors();
// }
?>`

Thanks in advance!

Apologies for the delay here. You’re saying you receive a 500 error from Square? Can you provide your Square application id? I also do not see you setting your access token anywhere in the above code, so have you already done that elsewhere?

Hi

The 500 error is on my website, caused by the getPaymentsApi() function not working. The access token is included in the JavaScript SDK in the front end.

The application ID is sandbox-sq0idb-mhw-MKpT3J0YSK9dwL0pSA.

Thanks!

I’m not seeing any 500 errors from us. I do see one error:

response=errors { code: MISSING_REQUIRED_PARAMETER detail: "Field must be set" field: "amount_money" category: INVALID_REQUEST_ERROR }

from the CreatePayment request, but it does look like you’ve successfully added the money in the above code, so I’m assuming you got past this error. I’m also not sure what you mean by you set the access token in the frontend; it should never be set in the frontend, as that means anyone would be able to view your secret access token.

Since this came from CreatePayment, that means you got past the getPaymentsApi() function.