PHP: Create Payment Link Example

Hi

I have tried and tried to get an example to create a payment link on my account via php.

Can anyone provide the correct connecting script and an example to create a payment link.

Thanks would be much appreciated.

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Download, Configure, and Run the Client Sample
Postman
Take a Credit Card Payment

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

To create a payment link using PHP, you would use the Square PHP SDK and the CreatePaymentLink endpoint. Here’s a basic example:

First, you need to install the Square PHP SDK. You can do this using Composer:

composer require square/square

Then, you can use the following PHP script to create a payment link:

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

use Square\SquareClient;
use Square\Models\CreatePaymentLinkRequest;
use Square\Models\Money;

$client = new SquareClient([
    'accessToken' => 'YOUR_ACCESS_TOKEN',
    'environment' => 'sandbox', // Use 'production' for production environment
]);

$api = $client->getPaymentsApi();

$money = new Money();
$money->setAmount(100); // Amount in smallest currency unit (e.g. cents for USD)
$money->setCurrency('USD');

$request = new CreatePaymentLinkRequest('Payment for services', $money);
$request->setDescription('Description of the payment');

try {
    $response = $api->createPaymentLink($request);

    if ($response->isSuccess()) {
        $result = $response->getResult();
        $paymentLink = $result->getLink();
        echo "Payment Link URL: " . $paymentLink->getUrl();
    } else {
        $errors = $response->getErrors();
        print_r($errors);
    }
} catch (Exception $e) {
    echo "Exception when calling PaymentsApi->createPaymentLink: ", $e->getMessage(), PHP_EOL;
}
?>

Replace 'YOUR_ACCESS_TOKEN' with your actual access token. This script creates a payment link for $1.00 USD with the description ‘Description of the payment’. If successful, it will print the URL of the payment link.

Please note that this is a basic example. In a real application, you would need to handle errors and edge cases appropriately.

This answer was reviewed by @Bryan-Square.

This is my code

require ‘square/autoload.php’;
echo ‘Connected
’;

?>

<?php use Square\SquareClient; use Square\Models\CreatePaymentLinkRequest; use Square\Models\Money; $client = new SquareClient([ 'accessToken' => 'ACCESS CODE REMOVED', 'environment' => 'production', // Use 'production' for production environment ]); $api = $client->getPaymentsApi(); $money = new Money(); $money->setAmount(100); // Amount in smallest currency unit (e.g. cents for USD) $money->setCurrency('USD'); $request = new CreatePaymentLinkRequest('Payment for services', $money); $request->setDescription('Description of the payment'); if ($api->isSuccess()) { $result = $api->getResult(); } else { $errors = $api->getErrors(); } echo $result; echo $errors;

Great! what’s the error your getting? Also have you tried the above example? :slightly_smiling_face:

Am getting a blank screen and using the above code