PHP code example of mr-saman / universal-payment

1. Go to this page and download the library: Download mr-saman/universal-payment 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/ */

    

mr-saman / universal-payment example snippets


use MrSaman\UniversalPayment\DTO\MoneyDTO;
use MrSaman\UniversalPayment\DTO\PaymentRequestDTO;
use MrSaman\UniversalPayment\Enums\Currency;
use MrSaman\UniversalPayment\Services\PaymentService;

$result = app(PaymentService::class)->initiate(new PaymentRequestDTO(
    money: new MoneyDTO(250000, Currency::IRR), // ۲۵۰٬۰۰۰ ریال
    gateway: 'zarinpal',
    orderId: 1001,
    description: 'پرداخت سفارش ۱۰۰۱',
    email: '[email protected]',
    meta: ['invoice' => 'INV-1001'],
    idempotencyKey: 'order:1001:attempt:1',
));

return response()->json($result->toArray());

if ($result->redirectUrl !== null && $result->redirectMethod === 'GET') {
    return redirect()->away($result->redirectUrl);
}

$result = app(PaymentService::class)->handleCallback(
    gateway: $gateway,
    paymentId: $paymentId,
    payload: request()->all(),
);

$result = app(PaymentService::class)->verify('zarinpal', [
    'payment_id' => $paymentId,
    'Authority' => $authority,
]);

$status = app(PaymentService::class)->status($paymentId);
$enabled = app(PaymentService::class)->availableGateways();

'callback' => [
    'success' => '/checkout/success',
    'failed' => '/checkout/failed',
    'pending' => '/checkout/pending',
],

new MoneyDTO(250000, Currency::IRR); // ریال
new MoneyDTO(25000, Currency::IRT);  // تومان
new MoneyDTO(1250000, Currency::USDT); // 1.25 USDT، دقت ۶ رقم

'fx' => [
    // مثال آموزشی: 12,000,000 ریال = 1,000,000 واحد خرد USDT
    // این عدد نرخ بازار نیست؛ نرخ معتبر کسب‌وکار خودتان را تأمین کنید.
    'IRR_USDT' => [1, 12],
    'USDT_IRR' => [12, 1],
],

use MrSaman\UniversalPayment\Contracts\CryptoTransactionVerifierInterface;

// داخل ServiceProvider برنامه؛ کلاس زیر را با اتصال واقعی خودتان پیاده‌سازی کنید.
$this->app->bind(CryptoTransactionVerifierInterface::class, YourTronVerifier::class);

public function verify(
    \MrSaman\UniversalPayment\DTO\PaymentVerifyDTO $request,
    string $walletAddress,
): \MrSaman\UniversalPayment\DTO\PaymentResultDTO;

use Illuminate\Support\Facades\Event;
use MrSaman\UniversalPayment\Events\PaymentSucceeded;

Event::listen(PaymentSucceeded::class, function (PaymentSucceeded $event) {
    $payment = $event->payment();
    // تحویل سفارش را با قید یکتای order/payment در برنامه خودتان idempotent کنید.
});

public function initiate(PaymentRequestDTO $request): PaymentResultDTO;
public function verify(PaymentVerifyDTO $request): PaymentResultDTO;
public function getName(): string;
public function getCurrency(): Currency;

config(['payment.gateways.sample_redirect.enabled' => true]);
// سپس PaymentService را resolve و از gateway: 'sample_redirect' استفاده کنید.
bash
php artisan config:clear
# پس از آماده‌سازی تنظیمات محیط عملیاتی:
php artisan config:cache
blade
<form method="POST" action="{{ $result->redirectUrl }}">
    @foreach ($result->redirectFields as $name => $value)
        <input type="hidden" name="{{ $name }}" value="{{ $value }}">
    @endforeach
    <button type="submit">ادامه پرداخت</button>
</form>
text
/payments/callback/{gateway}/{payment}?payment_id=...&upay_token=...