PHP code example of payriff / payriff-php

1. Go to this page and download the library: Download payriff/payriff-php 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/ */

    

payriff / payriff-php example snippets


use Payriff\Payriff;

$payriff = new Payriff(appKey: getenv('PAYRIFF_APP_KEY'));

$order = $payriff->orders->create([
    'amount' => '10.00',
    'description' => 'Order #1001',
    'callbackUrl' => 'https://shop.example/payriff/callback',
    'requestRrn' => 'order-1001',
]);

// Send the customer to the hosted payment page
header('Location: ' . $order['paymentUrl']);

use Payriff\Payriff;
use Payriff\Region;

$payriff = new Payriff(appKey: getenv('PAYRIFF_APP_KEY'), region: Region::SAUDI);

$created = $payriff->orders->create([
    'amount' => '25.00',
    'currency' => 'AZN',          // default: the region currency
    'operation' => 'PRE_AUTH',    // default PURCHASE
    'language' => 'AZ',
    'description' => 'Booking #77',
    'callbackUrl' => 'https://shop.example/payriff/callback',
    'metadata' => ['bookingId' => '77'],
]);

$info = $payriff->orders->get($created['orderId']);
$byRef = $payriff->orders->getByRequestRrn('order-1001');   // the requestRrn you sent on create

$payriff->orders->complete(['orderId' => $created['orderId'], 'amount' => '25.00']);
$payriff->orders->refund(['orderId' => $created['orderId'], 'amount' => '5.00', 'refundReason' => 'Partial return']);
$payriff->orders->expire($created['orderId']);              // cancel an unpaid order

$receiptPdf = $payriff->orders->downloadReceipt($created['orderId']);

$charge = $payriff->payments->autoPay([
    'cardUuid' => $savedCardUuid,
    'amount' => '9.99',
    'description' => 'Monthly subscription',
    'requestRrn' => 'sub-2026-10',
]);

$session = $payriff->cards->save([
    'customerRef' => 'customer-42',
    'callbackUrl' => 'https://shop.example/payriff/card-saved',
    'idempotencyKey' => bin2hex(random_bytes(16)),
]);
// Redirect the customer to $session['paymentUrl'] to verify the card.

$details = $payriff->cards->getSave($session['cardSaveId']);
if ($details['status'] === 'VERIFIED') {
    $cardUuid = $details['cardUuid'];   // store it; use with autoPay
}

$cards = $payriff->cards->list('customer-42');
$payriff->cards->delete($cards[0]['cardUuid']);

$page = $payriff->transactions->list([
    'status' => 'APPROVED',
    'from' => '2026-09-01',        // DateTimeInterface or YYYY-MM-DD
    'to' => new DateTimeImmutable('2026-09-30'),
    'page' => 0,
    'size' => 20,                  // server maximum is 20
]);

foreach ($page['content'] as $tx) {
    echo $tx['orderId'], ' ', $tx['amount'], PHP_EOL;
}

$payriff = new Payriff(appKey: getenv('PAYRIFF_APP_KEY'), merchantId: getenv('PAYRIFF_MERCHANT_ID'));

$maskedName = $payriff->payouts->checkCardholder('4169741330151979');   // e.g. "J*** D**"

$payout = $payriff->payouts->create([
    'transferAmount' => '50.00',   // minimum 1
    'description' => 'Refund for order #1001',
    'fullName' => 'JOHN DOE',
    'finCode' => '1AB2C3D',
    'cardPan' => '4169741330151979',
    'requestRrn' => 'payout-1001',
    'idempotencyKey' => 'payout-1001',
]);

$status = $payriff->payouts->getByRequestRrn('payout-1001');
$history = $payriff->payouts->list(['status' => 'SUCCESS']);
$receipt = $payriff->payouts->downloadReceipt('payout-1001');

$invoice = $payriff->invoices->create([
    'amount' => '15.00',
    'fullName' => 'JOHN DOE',
    'phoneNumber' => '+994501234567',
    'description' => 'Consultation',
    'expireDate' => new DateTimeImmutable('+7 days'),
    'sendSms' => true,
]);

$link = $invoice['paymentUrl'];   // share with the customer
$details = $payriff->invoices->get($invoice['invoiceUuid']);

use Payriff\Webhook;

$notified = Webhook::parseOrderCallback(file_get_contents('php://input'));   // also accepts a decoded array

// Callbacks are not signed: confirm the state with Payriff before fulfilling.
$confirmed = $payriff->orders->get($notified['orderId']);
if ($confirmed['paymentStatus'] === 'APPROVED') {
    // fulfil the order (make this idempotent: the same callback can arrive more than once)
}
http_response_code(200);

use Payriff\Exception\PayriffConnectionException;
use Payriff\Exception\PayriffException;
use Payriff\Exception\ValidationException;

try {
    $payriff->orders->refund(['orderId' => $orderId]);
} catch (ValidationException $e) {
    error_log("Refund rejected: {$e->getMessage()} ({$e->getResultCode()})");
} catch (PayriffConnectionException $e) {
    // Outcome unknown: check $payriff->orders->get($orderId) before retrying
} catch (PayriffException $e) {
    error_log("Payriff error {$e->getResultCode()} responseId={$e->getResponseId()}");
}
bash
composer