This example demonstrates a bare-bones PHP implementation of the OAuth flow for Square APIs. It serves a link that directs merchants to the OAuth Permissions form and handles the result of the authorization, which is sent to your application's Redirect URL (specified on the application dashboard).
For a detailed walkthrough of this example, see OAuth Walkthrough,
along with the comments included in sandbox_callback.php.
- 8.0 <= PHP
This application requires the PHP Square SDK as well as DotEnv for reading environment variables, which you install via Composer.
First, download Composer in this directory with the instructions on this page.
After you've downloaded Composer, update your Square PHP SDK version in composer.json - change REPLACE_ME to your desired version number. Latest version can be found here
install the dependencies with the following command from this directory:
composer install
-
Open the Developer Dashboard.
-
Choose Open on the card for an application.
-
At the top of the page, set the dashboard mode to the environment that you want to work with by choosing Sandbox or Production.
-
Choose OAuth in the left navigation pane. The OAuth page is shown.
-
In the Redirect URL box, enter the URL for the callback you will implement to complete the OAuth flow:
http://localhost:8000/callback.phpYou can use HTTP for localhost but an actual web server implementation must use HTTPS.
-
in the Application ID box, copy the application ID.
-
In the Application Secret box, choose Show, and then copy the application secret.
-
Click Save.
-
In your project directory, create a copy of the
.env.examplefile and name it.env -
In the newly created .env file, replace the
your-environmentwith eithersandboxorproduction -
Replace the
your-application-idandyour-application-secretplaceholders with the Sandbox or Production application ID and application secret, respectively.Note that OAuth Sandbox credentials begin with a sandbox prefix and that the base URL for calling Sandbox endpoints is https://connect.squareupsandbox.com. When you implement for production, you need production credentials and use https://connect.squareup.com as the base URL.
WARNING: Never check your credentials/access_token into your version control system. We've added
.envto the.gitignorefile to help prevent uploading confidential information.
-
Open the Developer Dashboard.
-
For testing in sandbox mode, in the Sandbox Test Accounts section, find one test acount and choose Open. For production mode, open the seller dashboard at https://squareup.com/dashboard/
-
Start the PHP server, if it is not running:
php -S localhost:8000 -
Open http://localhost:8000/request_token.php to start.
Rate this sample app here!