This is my first attempt to integrate square payments into a php system. I am trying to get the sample app to work to get an understanding of its functionality. Unfortunately I get a Dotenv\Dotenv’ class not found message at the very beginning. I used the latest version of composer to download the square api.
In order for the example to work, you must create a new file .env
by copying the contents of the .env.example
file. Edit this file with your application credentials and environment configuration.
Thank you for responding. I have set up a folder square in my htdocs directory of my test browser which is firefox. I have downloaded all the sample code from the github sample for the payments api into that folder. I used the windows command prompt to position on the square folder under the htdocs. I then used composer to require square/square. I have also used composer to require vlucas/phpdotenv. That progressed me past the class not found error. Now when I invoke the localhost/square/index.php in my firefox browser I get an invalid path exception, it seems to be trying the find the .env file in the utils directory which contains the location-info.php. Reading the readme on the vlucas/phpdotenv it suggests you should use createImmutable rather that the create(dirname). When I use the create(dirname) I get Argument#1($repository) must be type repostoryinterface, string given. For a simple piece of sample code not work straight out of the box is most frustrating to get an understand how it works and how to integrate it into the working php web application. The .env file is set up in the root directory including the sandbox parameters as specified on our square account. We use the small square device to receipt attendance fess at our regular meetings.
It appears that the application is looking for the .env
file in the wrong directory. Ensure that the .env
file is in the root directory, not in the utils directory. You might want to double-check the path configuration or any specific settings related to the vlucas/phpdotenv
library.
The error “Argument#1($repository) must be type repostoryinterface, string given” suggests that there might be an issue with how you are using the create
method. Instead of using create(dirname)
, try using createImmutable
with the correct path to your .env
file. For example:
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
Make sure to replace __DIR__
with the actual path if needed.
Thank you once again for replying, much appreciated. You are obviously on the other side of the planet to myself. I did the sensible thing and went back and looked at the source material in detail. I noticed I had not included the composer.json which was in the sample, so a new one was created when I used composer. When I reused composer with the sample json in place the required material was loaded including phpdotenv which was causing my initial problems. I noticed that composer loaded the latest release of square, rather than that specified in the json. I am now working through a deprecation issue, the phpdotenv issue has been resolved. Obviously the sample I am working with has not been updated recently. It seems dynamic properties has been deprecated in php. My test system is running php 8.2. The error occurs on line 27 of the location-info.php in the payments sample on github.
Glad to hear you got it to work. We’re constantly working to improve our features based on feedback like this, so I’ll be sure to share this with the API product team.
Not quite working, but making progress. I was sidelined for some days working on another project now back on this case. I had an interesting time getting ssl security working on my windows localhost testing environment. I now do not get the message that square requires to be accessed from a htpps url, good security, but it would be good for easy testing if that requirement were relaxed when accessing sandbox. I am now getting the following error.
Fatal error: Uncaught Error: Call to a member function getLocation() on array in D:\Program Files\Apache Software Foundation\Apache2.4\htdocs\square\utils\location-info.php:31 Stack trace: #0 D:\Program Files\Apache Software Foundation\Apache2.4\htdocs\square\utils\location-info.php(50): LocationInfo->__construct() #1 D:\Program Files\Apache Software Foundation\Apache2.4\htdocs\square\index.php(4): include(‘D:\Program File…’) #2 {main} thrown in D:\Program Files\Apache Software Foundation\Apache2.4\htdocs\square\utils\location-info.php on line 31
When I look at the line of code referenced, I cannot see any syntax errors. The line is
$location = $this->square_client->getLocationsApi()->retrieveLocation(getenv(‘SQUARE_LOCATION_ID’))->getResult()->getLocation();
It is obviously access the contents of my .env file where the content for the location entry is
SQUARE_LOCATION_ID=Australia
Is this incorrect. When I accessed the sandbox credentials I did not see the term location used, just country.
My feeling is that this may be a PHP release level issue.
Guess what? It pays to read the documentation carefully. Square does not support PHP8.2. So need to go back to 8.1 for my development and testing.
I have made some progress. I have a php 8.0 environment successfully supporting ssl htpps access so I no longer get the https access error. There still seems to be an issue with establishing the location. in the index.php module. I have successfully used composer to install the required modules for the php level. I am now getting a new error message which looks extremely basic.
Fatal error: Uncaught Error: Undefined constant “Unirest\CURLAUTH_BASIC” in D:\Program Files\Apache Software Foundation\Apache2.4\htdocs\square\vendor\apimatic\unirest-php\src\Configuration.php:54 Stack trace: #0 D:\Program Files\Apache Software Foundation\Apache2.4\htdocs\square\vendor\square\square\src\SquareClient.php(160): Unirest\Configuration::init(Object(Square\SquareClient)) #1 D:\Program Files\Apache Software Foundation\Apache2.4\htdocs\square\utils\location-info.php(29): Square\SquareClient->__construct(Array) #2 D:\Program Files\Apache Software Foundation\Apache2.4\htdocs\square\utils\location-info.php(50): LocationInfo->__construct() #3 D:\Program Files\Apache Software Foundation\Apache2.4\htdocs\square\index.php(4): include(‘D:\Program File…’) #4 {main} thrown in D:\Program Files\Apache Software Foundation\Apache2.4\htdocs\square\vendor\apimatic\unirest-php\src\Configuration.php on line 54
The contents of my .env are :
Acceptable values are sandbox or production
ENVIRONMENT=sandbox
Must match the values found in the corresponding production or sandbox environment
SQUARE_APPLICATION_ID=sandbox-sq0idb-sJOYWwSgNPgkIbcB2LRJMw
SQUARE_ACCESS_TOKEN=ACCESS_TOKEN
SQUARE_LOCATION_ID=LNVATSWKMXGJD
Can you advise please as the same location function is part of the live access to square which I need to implement.
It looks like the error is related to the Unirest library and the undefined constant Unirest\CURLAUTH_BASIC
in the Configuration.php
file. This might be due to a version mismatch or an issue with the Unirest library.
-
Check Unirest Version: Ensure that you have a compatible version of the Unirest library installed. You can check the version specified in your
composer.json
file and make sure it is compatible with the Square SDK."require": { "apimatic/unirest-php": "^3.0" }
Make sure you are using a version that is supported by the Square SDK.
-
Update Unirest: If you are not using the latest version of the Unirest library, consider updating it to the latest stable version by running:
composer update apimatic/unirest-php
-
Namespace Issue: Ensure that the
Unirest\CURLAUTH_BASIC
constant is correctly defined in the Unirest library. Check theConfiguration.php
file in the Unirest library to see if the namespace and constant definitions are correct.
Again, thanks for your response. When I looked at the message more closely I noticed the term ‘curl’, part of the certificate handling. When I checked my apache/php system I found curl was not being loaded properly, once that was resolved I was able to bring up the sample page for entry of credit card numbers etc. I am now integrating into my php based system so that online credit card payments can be made. I have struck a problem, I am not very familiar with accessing classes in php. When I try and access the Uuid class to provide the idempotencykey from an include file into the main php module it cannot find the class. When I put the code in the mainline php module it works as expected. Can you advise how I can reference the class from a php include file. I have searched google without success.
Glad to hear that resolving the curl issue got the credit form to render.
In PHP, classes are typically included using the require
, require_once
, include
, or include_once
statements. The Uuid class might be part of a library or framework, so you’ll need to ensure that it’s being autoloaded or included correctly in your script.
If you’re using Composer (a dependency manager for PHP), it usually generates an autoload.php
file that you should include in your main PHP script to automatically load classes:
require 'vendor/autoload.php'; // Adjust the path to where your autoload.php is located
If you’re not using Composer, you’ll need to manually include the file that contains the Uuid class:
require_once 'path/to/Uuid.php'; // Adjust the path to the actual location of your Uuid class file
Once the class file is included, you should be able to use the Uuid class to generate an idempotency key:
$uuid = Uuid::uuid4(); // This is just an example; the actual method to generate a UUID may vary
$idempotencyKey = $uuid->toString();
If you’re still having trouble, here are a few things to check:
- Namespace: If the Uuid class is namespaced, you need to use the fully qualified name of the class or include a
use
statement at the top of your script to import the class.
use Namespace\Of\Uuid; // Use the actual namespace of the Uuid class
// Then you can use the class without the namespace prefix
$uuid = Uuid::uuid4();
- Correct Path: Ensure that the path in your
require_once
orinclude_once
statement is correct and that the file is accessible from the script where you’re trying to use the class. - Error Reporting: Enable error reporting in PHP to get more detailed information about what might be going wrong:
error_reporting(E_ALL);
ini_set('display_errors', 1);
- Class Autoloading: If the Uuid class is part of a larger library or framework, the library might provide its own autoloading mechanism. Check the documentation for the library to see how to include it properly.
Thank you very much for the obvious effort you have put into this. Your examples appear to have provided me with a way of dealing with the issue. I now will just step through the rest of the process to try to get it to work in my system environment.
I have been successful in getting the API code to work in the sandbox environment and have integrated the payment facility into our PHP application. However, I want to access the id property of the payment object to include as reconciliation information in the general ledger postings which result from the payments. It will allow reconciliation between our GL information and the information available for download from our Square account. As an old fashioned procedural programmer who started in fortran in the late 1960’s I find some of the syntax difficult to read and understand. Where can I find syntax examples of how to retrieve the id (and other information) from the payments object created by the createPayment function? It is not included in your samples.
With our examples you will get back the payment_id
for every payment that’s attempted. However if you want to retrieve payments you can use our ListPayments endpoint to list all payments taken.