Square Payment Gateway Integration in PHP – Step-by-Step Guide

If you are planning to accept online payments on your website using PHP, then Square is a trusted and secure payment gateway you can use. In this guide, you will learn how to integrate the Square Payment Gateway in PHP with proper setup, sample code, and clear instructions.

What is Square Payment Gateway?

Square is a popular payment platform that allows businesses to accept payments online and in person. It fully supports multiple payment methods such as credit/debit cards, Google Pay, and Apple Pay. Square also provides a secure and user-friendly environment for developers to build payment solutions.

Why Choose Square for Payment Integration?

Here are a few reasons to use Square for your PHP application:

  • Secure and PCI-compliant payment process
  • Easy to set up with clear documentation
  • Free developer sandbox for testing
  • No monthly fees – pay per transaction
  • Supports multiple payment methods

Requirements Before You Start

Before you begin integrating Square with PHP, make sure you have the following:

  1. A Square Developer Account (free signup at developer.squareup.com)
  2. PHP 7.1 or higher
  3. Composer installed
  4. An HTTPS server (SSL required for live payments)
  5. Basic understanding of PHP

Step 1: Install Square PHP SDK

To use Square APIs in PHP, you need to install the Square SDK using Composer.

Open your terminal or command prompt and run:-

composer require square/square

This will download and install the Square SDK into your project.

Step 2: Get Your Square API Credentials

  1. Go to your Square Developer Dashboard.
  2. Create a new application.
  3. Get the following details:
    • Access Token (Sandbox for testing, Production for live)
    • Application ID
    • Location ID

Use sandbox credentials during development.

Step 3: Set Up Square Payment in PHP

Now write the PHP code to create a payment using Square.

PHP Code Example:

<?php
require 'vendor/autoload.php';

use Square\SquareClient;
use Square\Models\Money;
use Square\Models\CreatePaymentRequest;

$client = new SquareClient([
    'accessToken' => 'YOUR_SANDBOX_ACCESS_TOKEN', // replace with your sandbox token
    'environment' => 'sandbox' // or 'production' for live
]);

$payments_api = $client->getPaymentsApi();

// Create money object - amount is in cents (e.g., 500 = $5.00)
$money = new Money();
$money->setAmount(1000); // $10.00
$money->setCurrency('USD');

// Create unique request
$payment_request = new CreatePaymentRequest(
    'CARD_NONCE_HERE', // Replace with the nonce from frontend form
    uniqid(), // Unique ID for request
    $money
);

try {
    $response = $payments_api->createPayment($payment_request);
    
    if ($response->isSuccess()) {
        $result = $response->getResult();
        echo "Payment successful. Payment ID: " . $result->getPayment()->getId();
    } else {
        $errors = $response->getErrors();
        foreach ($errors as $error) {
            echo "Error: " . $error->getDetail() . "<br>";
        }
    }
} catch (Exception $e) {
    echo "Exception: " . $e->getMessage();
}
?>

Important Note:

You must replace 'CARD_NONCE_HERE' With the card nonce/token that is generated from Square’s Web Payments SDK in the frontend.

Step 4: Collect Card Details on the Frontend

Square does not allow you to handle raw card data directly. You must use their Web Payments SDK or Square Payment Form to collect card information securely.

In your HTML frontend, use this script:

<script type="text/javascript" src="https://sandbox.web.squarecdn.com/v1/square.js"></script>

You can follow the official Web Payments SDK documentation to set up the form. Once a user submits the form, a nonce (token) is generated, which you send to the PHP backend.

Best Practices

  • Always use HTTPS for payment pages.
  • Do not store raw card data on your server.
  • Test everything in sandbox mode before going live.
  • Handle and log errors carefully.
  • Set the proper currency (e.g. USD, INR) as per your business.

Conclusion:

Integrating the Square payment gateway in PHP is extremely easy and developer-friendly. In just a few steps, you can safely accept payments on your website or web application if you want. Also, it is always better to start with a sandbox environment and follow all security best practices when going live.

Read also:-

Share this post :