PHP code example of amyavari / iran-payment-laravel
1. Go to this page and download the library: Download amyavari/iran-payment-laravel 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/ */
amyavari / iran-payment-laravel example snippets
use AliYavari\IranPayment\Facades\Payment;
// Using the default gateway (uses the callback URL from config)
$payment = Payment::create(int $amount, ?string $description = null, ?string|int $phone = null);
// Using the default gateway (define the callback URL at runtime)
$payment = Payment::callbackUrl(string $callbackUrl)->create(...);
// Using a specific gateway (uses the callback URL from config)
$payment = Payment::gateway(string $gateway)->create(...);
// Using a specific gateway (define the callback URL at runtime)
$payment = Payment::gateway(string $gateway)->callbackUrl(string $callbackUrl)->create(...);
$payment->successful(); // bool
$payment->failed(); // bool
// Get the error message (returns `null` if successful)
$payment->error(); // string|null
// Get the raw gateway response (useful for debugging)
$payment->getRawResponse(); // string|array
use AliYavari\IranPayment\Facades\Payment;
// Store the payment and associate it with a payable Eloquent model
Payment::store(Model $payable)->create(...);
Payment::{other configurations}->store(Model $payable)->create(...);
$payment->getModel(); // \AliYavari\IranPayment\Models\Payment
// Access the associated payable model
$payment->getModel()->payable;
// Example payable model (Course)
namespace App\Models;
use AliYavari\IranPayment\Concerns\HasPayment;
use Illuminate\Database\Eloquent\Model;
final class Course extends Model
{
use HasPayment;
//
}
// Payments relationship (MorphMany)
$course->payments(); // AliYavari\IranPayment\Models\Payment
use AliYavari\IranPayment\Models\Payment as PaymentModel;
// Verified and successful payments
PaymentModel::query()->successful()->...
// Verified and failed payments
PaymentModel::query()->failed()->...
// Pending (unverified) payments
PaymentModel::query()->pending()->...
// Via a payable model using HasPayment
$course->payments()->successful()->...
$course->payments()->failed()->...
$course->payments()->pending()->...
// Data t->getGatewayPayload(); // array|null
// Gateway key
$payment->getGateway(); // string
// Unique transaction ID used for tracking in your database (`null` if payment creation failed)
$payment->getTransactionId(); // string|null
$redirectData = $payment->getRedirectData(); // `null` if payment creation failed
// Redirect URL
$redirectData->url; // string
// Redirect method (POST, GET)
$redirectData->method; // string
// Redirect payload (POST body or GET query params)
$redirectData->payload; // array
// Required HTTP headers
$redirectData->headers; // array
// Get all redirect information as an array
$redirectData->toArray(); // array
use AliYavari\IranPayment\Facades\Payment;
// Create a gateway instance from callback data
$payment = Payment::gateway(string $gateway)->fromCallback(array $callbackPayload);
// Call verify without any arguments
$payment->verify();
// To find the payment in your database
$payment->getTransactionId();
// Call verify with the stored gateway payload
$payment->verify(array $gatewayPayload);
// Reverse or refund the payment (call if verification fails)
$payment->reverse();
// Let the package handle reverse automatically when needed
$payment
->autoReverse(bool $autoReverse = true)
->verify(...); // Manual or automatic storage
// Get the reference number assigned to the transaction by the bank. (`null` if payment verification failed)
$payment->getRefNumber(); // string|null
// Get user's card number used to pay. (`null` if payment verification failed)
$payment->getCardNumber(); // string|null
declare(strict_types=1);
namespace App\Http\Controllers;
use AliYavari\IranPayment\Http\Requests\SepRequest;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
final class SepVerificationController extends Controller
{
public function update(SepRequest $request): RedirectResponse
{
$callbackData = $request->validated();
// Verification logic, product delivery, etc.
}
}
// Behpardakht
use AliYavari\IranPayment\Http\Requests\BehpardakhtRequest;
// Sep
use AliYavari\IranPayment\Http\Requests\SepRequest;
// Zarinpal
use AliYavari\IranPayment\Http\Requests\ZarinpalRequest;
// IDPay
use AliYavari\IranPayment\Http\Requests\IdpayRequest;
// Pep
use AliYavari\IranPayment\Http\Requests\PepRequest;
// Sadad
use AliYavari\IranPayment\Http\Requests\SadadRequest;
// Zibal
use AliYavari\IranPayment\Http\Requests\ZibalRequest;
// PayPing
use AliYavari\IranPayment\Http\Requests\PaypingRequest;
// NextPay
use AliYavari\IranPayment\Http\Requests\NextpayRequest;
use AliYavari\IranPayment\Facades\Payment;
$payment = Payment::gateway(string $gateway)->noCallback(string $transactionId);
$payment = $paymentModel->toGatewayPayment();
use AliYavari\IranPayment\Facades\Payment;
use AliYavari\IranPayment\Dtos\PaymentRedirectDto;
// Default gateway
$fake = Payment::fake();
// Specific gateway
$fake = Payment::fake($gateway);