نمونه کد PHP با REST

این نمونه دو فایل است و بدون Composer به درگاه وصل می‌شود. پیش از استفاده، راهنمای اتصال به درگاه را بخوانید.

نسخه
v1.0.0
فایل‌ها
Request.php / Verify.php
توسعه‌دهنده
فینوپال

Request.php

مبلغ به ریال و حداقل ۱۰۰۰ ریال است. order_id الزامی است. بعد از کد ۱۰۰ خریدار به payment_url می‌رود.

<?php

$merchantId = 'fino-xxxx-xxxx-xxxx-xxxx';
$sandbox = false;
$base = $sandbox ? 'https://sandbox.finopal.ir' : 'https://payment.finopal.ir';

$data = [
    'merchant_id' => $merchantId,
    'amount' => 10000,
    'callback_url' => 'https://your-site.test/Verify.php',
    'description' => 'خرید تست',
    'order_id' => 'ORD-1001',
];

$jsonData = json_encode($data, JSON_UNESCAPED_UNICODE);
$ch = curl_init($base . '/api/v1/payment/request');
curl_setopt($ch, CURLOPT_USERAGENT, 'FinoPal Rest Api v1');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Accept: application/json',
    'Content-Length: ' . strlen($jsonData),
]);

$result = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
$result = json_decode($result, true);

if ($err) {
    echo 'cURL Error: ' . $err;
    exit;
}

$code = $result['code'] ?? null;
$authority = $result['data']['authority'] ?? '';

if ((int) $code === 100 && $authority !== '') {
    $url = $result['data']['payment_url'] ?? ($base . '/api/v1/payment/redirect/' . rawurlencode($authority));
    header('Location: ' . $url);
    exit;
}

echo 'Error Code: ' . $code;
echo ' message: ' . ($result['message'] ?? '');

Verify.php

فقط وقتی Status=OK است تأیید کنید. مبلغ را از دیتابیس همان سفارش بخوانید. کد ۱۰۰ تازه تأییدشده و کد ۱۰۱ قبلاً تأییدشده است.

<?php

$merchantId = 'fino-xxxx-xxxx-xxxx-xxxx';
$sandbox = false;
$base = $sandbox ? 'https://sandbox.finopal.ir' : 'https://payment.finopal.ir';

$authority = $_GET['Authority'] ?? '';
$status = $_GET['Status'] ?? '';
$amount = 10000;

if ($status !== 'OK' || $authority === '') {
    echo 'تراکنش ناموفق بود یا توسط خریدار لغو شد.';
    exit;
}

$data = [
    'merchant_id' => $merchantId,
    'amount' => $amount,
    'authority' => $authority,
];

$jsonData = json_encode($data, JSON_UNESCAPED_UNICODE);
$ch = curl_init($base . '/api/v1/payment/verify');
curl_setopt($ch, CURLOPT_USERAGENT, 'FinoPal Rest Api v1');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Accept: application/json',
    'Content-Length: ' . strlen($jsonData),
]);

$result = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
$result = json_decode($result, true);

if ($err) {
    echo 'cURL Error: ' . $err;
    exit;
}

$code = (int) ($result['code'] ?? 0);
$refId = $result['data']['ref_id'] ?? '';

if ($code === 100 || $code === 101) {
    echo 'پرداخت موفق بود. شماره پیگیری: ' . $refId;
    exit;
}

echo 'Error Code: ' . $code;
echo ' message: ' . ($result['message'] ?? '');