mst-ghi/larapool

一个用于连接所有伊朗支付网关的 Laravel 库

v1.1.0 2021-08-16 10:24 UTC

This package is auto-updated.

Last update: 2024-09-16 17:27:33 UTC


README

一个用于连接所有伊朗支付网关的 Laravel (+7)

活动端口 :

  • IDPay
  • Mellat
  • Sadad
  • Zarinpal
  • Payline
  • Jahanpay
  • Saderat
  • IranKish
  • Saman
  • Parsian
  • Pay
  • JiBit
  • AP
  • BitPay

安装 :

步骤 1
composer require mst-ghi/larapool
步骤 2:将以下更改应用到 config/app.php 文件中
   'providers' => [
         MstGhi\Larapool\LarapoolServiceProvider::class
   ],

  'aliases' => [
        'Larapool' => MstGhi\Larapool\Larapool::class
   ]
步骤 3:发布所需文件
php artisan vendor:publish 

选择并输入 MstGhi\Larapool\LarapoolServiceProvider 的数量以发布资源文件。发布文件包括配置和迁移文件

步骤 4:创建事务表
php artisan migrate

包配置

安装操作已完成。现在打开 config/larapool.php 中的 larapool.php 文件,并输入与您想要的银行门户相关的设置。

创建新事务

您的项目 Order 模型必须使用 TransactionAble 特性。这个特性为订单模型添加了关系 transactions()
您必须注册订单,然后为该订单创建一个事务

$order = Order::create([
    'paid_price' => 1000
    // your data
]);

$resId = \MstGhi\Larapool\LarapoolTransaction::generateResId();

// create new transaction
$order->transactions()->create([
    'user_id' => $user->id, // customer id here, this is optional
    'port_id' => \MstGhi\Larapool\Larapool::P_IDPAY,
    'price' => $order->paid_price,
    'res_id' => $resId,
    'platform' => \MstGhi\Larapool\Larapool::PLATFORM_WEB,
    'last_change_date' => strtotime(now()),
]);

// In the next step, transfer to the bank portal using resId

在下一步中,使用 $resId 转到银行门户

重定向到银行

可以使用 redirect() 直接重定向到银行门户
或者
接收相关链接,然后使用 redirectLink() 以其他方式连接到银行门户

重定向到银行的方法选择取决于您的项目

use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use MstGhi\Larapool\LarapoolTransaction;
use MstGhi\Larapool\Larapool;
use MstGhi\Larapool\IDPay\IDPay;
use MstGhi\Larapool\Exceptions\NotFoundTransactionException;

public function redirect($resId)
{
    $exception = null;
    
    $transaction = LarapoolTransaction::with(['transactionable'])->where('res_id', $resId)->first();
    
    if (!$transaction) {
    
        $exception = new NotFoundTransactionException();
        
    } else {
    
        /** @var IDPay $larapool */
        $larapool = new Larapool(Larapool::P_IDPAY);
        
        try {
        
            $refId = $larapool->setTransaction($transaction)->ready()->refId();
            // any operation on $refId here ...
        
            /**
            * redirect by php header() method automatically 
            */
            $larapool->redirect();
            
            // Or ...
            
            /**
            * relevant link and connected to the bank portal in other ways
            */
            $link = $larapool->redirectLink();
            
            return response()->json([ 'link' => $link ], Response::HTTP_OK);
            
        } catch (Exception $e) {
        
            $exception = $e;
            
        }
    }
        
    if ($exception) {
    
        Log::critical(
            "Error in getting payment url: {$exception->getMessage()} #transactionId: {$transaction->id}"
        );
        
        throw $exception;
    }
    
}

银行回调

use Illuminate\Support\Facades\Log;
use MstGhi\Larapool\Exceptions\NotFoundTransactionException;
use MstGhi\Larapool\LarapoolTransaction;
use MstGhi\Larapool\Larapool;
use MstGhi\Larapool\IDPay\IDPay;
use Carbon\Carbon;

public function callback(LarapoolTransaction $transaction, $resId)
    {
        if (($transaction && !$resId) || ($transaction && $transaction->res_id != $resId)) {
            throw new NotFoundTransactionException();
        }
        
        // load transactionable, this is use as orderId
        $transaction->load(['transactionable']);

        try {
             /** @var IDPay $larapool */
             $larapool = new Larapool(Larapool::P_IDPAY);

            /** verify transaction [bank] */
            $larapool->setOrderId($transaction->transactionable->id)->verify($transaction);

            $trackingCode = $larapool->trackingCode();
            $cardNumber = $larapool->cardNumber();

            $transaction->update([
                'tracking_code' => $trackingCode,
                'card_number' => $cardNumber,
                'status' => LarapoolTransaction::TRANSACTION_SUCCEED,
                'payment_date' => strtotime(Carbon::now()),
                'last_change_date' => strtotime(Carbon::now()),
            ]);
            
            // any operation after success payment here ...
            
        } catch (\Exception $e) {
            $message = $e->getMessage();
            Log::critical("Error in verifying payment: $message #transactionId: {$transaction->id}");
           
            $transaction->update([
                'status' => LarapoolTransaction::TRANSACTION_FAILED,
                'last_change_date' => strtotime(Carbon::now()),
            ]);
            
            // any operation after failed payment here ...
        }
    }