PHP code example of getpayin / paylink

1. Go to this page and download the library: Download getpayin/paylink library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

getpayin / paylink example snippets


use GetPayin\Paylink\Core\PaylinkClient;

$paylink = new PaylinkClient(
    publicToken: getenv('PAYLINK_PUBLIC_TOKEN'),
    hashToken: getenv('PAYLINK_HASH_TOKEN'), // secret — server-side only
);

$checkout = $paylink->invoices->create([
    'firstName'   => 'John',
    'lastName'    => 'Doe',
    'email'       => '[email protected]',
    'orderTitle'  => 'Gold Plan',
    'orderAmount' => '250.00',   // pass money as strings for an exact wire form
    'currency'    => 'USD',
]);
// ['checkoutUrl' => ..., 'invoiceId' => ..., 'expiresAt' => ...]

// Redirect the payer to the returned checkout URL.
header('Location: '.$checkout['checkoutUrl']);

$checkout = $paylink->invoices->create([
    'firstName'   => 'John',
    'lastName'    => 'Doe',
    'email'       => '[email protected]',
    'orderTitle'  => 'Gold Plan',
    'orderAmount' => '250.00',
    'currency'    => 'USD',
    'iframe'      => true,       // embed the returned checkout URL in an <iframe>
]);

$paylink->payments->void(['invoiceId' => 12345]);
$paylink->payments->settle(['invoiceId' => 12345, 'amount' => '50.00']);
$paylink->payments->reverseAuthorization(['invoiceId' => 12345]);

$status = $paylink->payments->checkStatus(['invoiceId' => 12345]);
// ['invoiceId' => 12345, 'paidStatus' => '...', 'authCode' => '...']

// Refunds are idempotent when you pass an idempotency key — safe to retry:
$refund = $paylink->payments->refund(
    ['invoiceId' => 12345, 'amount' => '10.50'],
    idempotencyKey: 'refund-order-1234',
);
// ['invoiceId' => ..., 'paidStatus' => ..., 'authCode' => ..., 'refundAmount' => ...]

$result = $paylink->cards->tokenize([
    'firstName'       => 'Jane',
    'lastName'        => 'Doe',
    'cardNumber'      => '4111111111111111',
    'cardExpiryMonth' => '12',
    'cardExpiryYear'  => '2030',
    'cardCvv'         => '123',
    'country'         => 'EG',
    'address'         => '1 Main St',
    'city'            => 'Cairo',
]);
$token = $result['token'];

$paylink->cards->charge([
    'cardToken' => $token,
    'initiator' => 'merchant',
    'firstName' => 'Jane',
    'lastName'  => 'Doe',
    'currency'  => 'USD',
    'price'     => '100.00',
    'product'   => 'Monthly rebill',
    'country'   => 'EG',
    'address'   => '1 Main St',
    'city'      => 'Cairo',
]);

$paylink->cards->revoke(['cardToken' => $token]);

$mandate = $paylink->recurring->create(
    [
        'firstName'       => 'Sam',
        'lastName'        => 'Doe',
        'email'           => '[email protected]',
        'orderTitle'      => 'Gold subscription',
        'orderAmount'     => '250.00',
        'currency'        => 'USD',
        'cadenceInterval' => 'month',
        'cadenceCount'    => 1,
        'totalCycles'     => 12,
        'consentText'     => 'I authorise recurring monthly charges.',
    ],
    idempotencyKey: 'sub-signup-42',
);

$paylink->recurring->status($mandate['mandateId']);
$paylink->recurring->pause($mandate['mandateId']);
$paylink->recurring->resume($mandate['mandateId']);
$paylink->recurring->cancel($mandate['mandateId']);

$paylink->vcc->charge([/* card + order fields */], idempotencyKey: 'vcc-order-1234');
$paylink->cards->charge([/* token + order fields */], idempotencyKey: 'tok-order-1234');
$paylink->invoices->create([/* customer + order fields */], idempotencyKey: 'order-1234');

use GetPayin\Paylink\Core\Exceptions\PaylinkSignatureException;
use GetPayin\Paylink\Core\Webhook\WebhookEventType;

try {
    $event = $paylink->webhooks->verify(file_get_contents('php://input'));
    // $event->event, $event->invoiceId, $event->success, $event->raw, ...
} catch (PaylinkSignatureException) {
    http_response_code(400);
    exit;
}

if ($event->type() === WebhookEventType::InvoicePaid) {
    // Fulfil the order.
}

use GetPayin\Paylink\Core\Exceptions\PaylinkApiException;

try {
    $paylink->payments->refund(['invoiceId' => 12345, 'amount' => '10.00']);
} catch (PaylinkApiException $error) {
    if ($error->isIdempotencyConflict()) {
        // a refund with this idempotency key already exists
    }
}

$paylink->payments->refund(
    ['invoiceId' => 12345, 'amount' => '10.50'],
    idempotencyKey: 'refund-order-1234', // now retried on 429/5xx
);

new PaylinkClient(publicToken: $pub, hashToken: $secret, maxRetries: 0); // off

} catch (PaylinkApiException $error) {
    if ($error->isRateLimited()) {
        // reschedule using $error->retryAfterMs
    }
}

// config/logging.php
'channels' => [
    'paylink' => [
        'driver' => 'daily',
        'path' => storage_path('logs/paylink.log'),
        'level' => 'debug',
        'days' => 14,
    ],
],

use Illuminate\Support\Facades\Log;

$paylink = new PaylinkClient(
    publicToken: config('services.paylink.public_token'),
    hashToken: config('services.paylink.hash_token'),
    logger: Log::channel('paylink'),
);

use GetPayin\Paylink\Core\PaylinkClient;
use Illuminate\Http\RedirectResponse;

final class CheckoutController
{
    public function __construct(
        private readonly PaylinkClient $paylink,
    ) {}

    public function store(): RedirectResponse
    {
        $checkout = $this->paylink->invoices->create([
            'firstName'   => 'John',
            'lastName'    => 'Doe',
            'email'       => '[email protected]',
            'orderTitle'  => 'Gold Plan',
            'orderAmount' => '250.00',
            'currency'    => 'USD',
        ]);

        return redirect()->away($checkout['checkoutUrl']);
    }
}

use GetPayin\Paylink\Core\PaylinkClient;
use GetPayin\Paylink\Laravel\Facades\Paylink;

public function checkout(PaylinkClient $paylink)
{
    return $paylink->invoices->create([/* ... */]);
}

Paylink::invoices()->create([/* ... */]);
$event = Paylink::webhooks()->verify(request()->getContent());

use GetPayin\Paylink\Laravel\LaravelHttpTransport;

$paylink = new PaylinkClient(
    publicToken: config('paylink.public_token'),
    hashToken: config('paylink.hash_token'),
    transport: new LaravelHttpTransport(),
    logger: Log::channel('paylink'),
);
bash
php artisan vendor:publish --tag=paylink-config