Square Payment Form Migration - PHP Help

Hi All,

I’m trying to migrate a payment form from the Square Payments Form to the new method as advised.

I’m working off the example at:

However, when adding those to my webserver, pulling in all the relevant vendor libraries hitting index.php, I simply get the following error:

Fatal error: Uncaught TypeError: Argument 1 passed to Square\ApiHelper::appendUrlWithTemplateParameters() must be of the type string, null given, called in /home/customer/www/[mydomain.com]/public_html/php_payment/vendor/square/square/src/SquareClient.php on line 302 and defined in /home/customer/www/[mydomain.com]/public_html/php_payment/vendor/square/square/src/ApiHelper.php:25

My .env credentials are correct (sandbox currently)

Not a hardcore developer, so need any help I can get with this, as the current payment form (which I have working happily) is being killed off in a few months.

Thanks to anyone who can assist!

Ben

:wave: According to the error an argument was pass to the ApiHelper when it should have been a string in your SquareClient.php. The line it references is attempting to pull the server_url for the environment, but given that’s an invalid environment you’re passing, it will fail and return null. You’ll want to do this instead:

$client = new SquareClient([
    'accessToken' => $square_access_token,
    'environment' => $upper_case_environment
]);

:slightly_smiling_face:

Thanks Bryan,

This is my current code, which appears to be correct?

	$access_token =  getenv('SQUARE_ACCESS_TOKEN');
	$environment = strtoupper(getenv('ENVIRONMENT'));
	
	 $this->square_client = new SquareClient([
		'accessToken' => $access_token,  
		'environment' => $environment
	]);

This is in the file provided by square under

utils/location-info.php

Am I missing something?

Ben

If you print the client do you get the correct values? Also do you have the link to the file utils/location-info.php that you say is provided by Square? I don’t see it in the SDK or examples.

Hu Bryan - it’s in the GitHub files linked to in my first post:

“Code samples demonstrating the functionality of the Square Connect API - connect-api-examples/connect-examples/v2/php_payment”

This is in Square’s own GitHub, so presume it’s official?

Thanks, our example has the environment setting coming from the env and it looks like what you provided doesn’t. It should be:

'environment' => getenv('ENVIRONMENT')

Look two lines above it - exactly the same value being passed, no?

(it doesn’t work either way)

I totally missed that. You’re right. I haven’t been able to replicate the error. Did you make any other adjustments to the sample prior to getting this error and was it working as expected prior?

I literally downloaded it, ran composer to pull in the necessary components, uploaded it to my web server and added my .env info.

No other changes made.

The SqPaymentForm method worked - can’t believe I’ve gotta spend days I don’t have battling with this now!

If you run it locally first does it work?

Nope. Full Error:

Fatal error: Uncaught TypeError: Argument 1 passed to Square\ApiHelper::appendUrlWithTemplateParameters() must be of the type string, null given, called in /Users/ben/Desktop/Square Update/connect-api-examples-master/connect-examples/v2/php_payment/vendor/square/square/src/SquareClient.php on line 302 and defined in /Users/ben/Desktop/Square Update/connect-api-examples-master/connect-examples/v2/php_payment/vendor/square/square/src/ApiHelper.php:25 

Stack trace: 

#0 /Users/ben/Desktop/Square Update/connect-api-examples-master/connect-examples/v2/php_payment/vendor/square/square/src/SquareClient.php(302): Square\ApiHelper::appendUrlWithTemplateParameters(NULL, Array, false) 

#1 /Users/ben/Desktop/Square Update/connect-api-examples-master/connect-examples/v2/php_payment/vendor/square/square/src/Apis/LocationsApi.php(181): Square\SquareClient->getBaseUri() 

#2 /Users/ben/Desktop/Square Update/connect-api-examples-master/connect-examples/v2/php_payment/utils/location-info.php(37): Square\Apis\LocationsApi->retrieveLocation('LPMGH....'.... in /Users/ben/Desktop/Square Update/connect-api-examples-master/connect-examples/v2/php_payment/vendor/square/square/src/ApiHelper.php on line 25

OK, finally getting somewhere - I can load the form, and process a hard coded amount, but of course, that is useless, as amounts will change.

Form has the client name, invoice number, and a field for hem to enter the invoice amount (invoices are generated outside of square by necessity)

Any pointers on how I access the value of form fields from index.php from within process-payment.php

Have tried via the normal $_POST that I’m accustomed to, but get nothing.

Does the JS-created credit card form post the entire form to process-payment.php, or just it’s own details.

This is painful!

The story so far:

https://invoice.thymeonline.com/invoice2/public_html/

Glad to hear you got it to work. Since the amount is hard coded on the backend you can pass it in as a variable since the amount charged will be different for each customer. If your passing it to the backend from the form your customers putting the card information in you can include it in the POST to the payment request with the source_id. :slightly_smiling_face:

Hi Bryan,

the issue I’m having is that the data doesn’t seem to be reaching the process-payment.php page.

I am attempting to access the field via POST (this doesn’t work as it’s the result of a javascript ‘fetch’ action), and by the $data->amount option (where $data is the contents of the file_get_contents(php://input) that is used to retrieve the token), i.e. this part of Square’s sample code:

$json = file_get_contents('php://input');
$data = json_decode($json);
$token = $data->token;
$amount = $data->amount;

(‘token’ is arriving correctly, but ‘amount’ doesn’t seem to exist)

How do I access the other form variables (along with the amount, I will need address, etc for SCA once I finally get the basics working)

Totally lost as to how to access the form fields, as it seems the sample code is only submitting the nested form in the iframe generated by square.js, ignoring the parent form fields.

$client = new SquareClient ([
            'accessToken' => getenv('ACCESS_TOKEN'),
            'environment' => Environment::SANDBOX,
        ]);

Hello can you help me pass the environment please, i got error : “TypeError: Argument 1 passed to Square\BearerAuthManager::__construct() must be of the type string, bool given, called in C:\Users\User\Documents\projet-laravel\divineTransportation\vendor\square\square\src\SquareClient.php on line 121 in file C:\Users\User\Documents\projet-laravel\divineTransportation\vendor\square\square\src\BearerAuthManager.php on line 21”