jwn25/

用于集成esewa与php/Laravel的Omnipay支付库的包

1.3.0 2020-08-13 05:17 UTC

This package is auto-updated.

Last update: 2024-09-13 14:06:04 UTC


README

php-esewa 是一个开发包,旨在实现esewaOmnipay的支持。Omnipay 是一个PHP的支付处理库。

安装

由于此包是 Omnipay 的 esewa 支持,因此必须安装 Omnipay 库。

composer require league/omnipay

composer require jwn25/php-esewa

Laravel 使用

配置

在 config/services.php 中添加以下行

'esewa' => [  
  'merchant_code' => env('ESEWA_MERCHANT_CODE', 'epay_payment'),  
  'test_mode' => env('ESEWA_TEST_MODE', true)  
]

ENV

使用 esewa 提供的凭据更新您的 .env 文件。

ESEWA_MERCHANT_CODE=YOUR_MERCHANT_CODE
ESEWA_TEST_MODE=false

路由

Route::post('/order/{order_id}/payment-process', 'PaymentController@processPayment');

Route::get('/order/{order_id}/payment-failed', 'PaymentController@paymentFailed')->name('payment-failed');  
  
Route::get('/order/{order_id}/payment-completed', 'PaymentController@paymentCompleted')->name('payment-completed');

控制器

<?php  
  
namespace App\Http\Controllers;  
  
use Illuminate\Http\Request;  
use Omnipay\Omnipay;  
  
class PaymentController extends Controller {
  protected $payment_gateway;
    
  public function __construct() {
    $this->payment_gateway = Omnipay::create('PhpEsewa_Secure');
    $this->payment_gateway->setScd(config('services.esewa.merchant_code'));
    $this->payment_gateway->setTestMode(config('services.esewa.test_mode'));
  }

  public function processPayment($order_id) {
    try {  
      $response = $this->payment_gateway->purchase([  
          'amt' => 100,  
          'txAmt' => 0,  
          'psc' => 0,  
          'pdc' => 0,  
          'tAmt' => 100,  
          'pid' => rand(10, 10000), //Your Purchase Unique ID
          'su' => route('payment-completed', $order_id),  
          'fu' => route('payment-failed', $order_id),  
      ])->send();  
    } catch (Exception $e) {  
      //return back with some proper payment somehow failed message.
    }
    if ($response->isRedirect()) {  
      $response->redirect();  
    } else {  
      //return back with some proper payment somehow failed message.
    }
  }
	
  public function paymentCompleted($order_id, Request $request)  
  {  
    $response = $this->payment_gateway->verifyPayment([  
      'amt' => $request->get(amt),  
      'rid' => $request->get('refId'),  
      'pid' => $request->get('oid'),  
     ])->send();  
      
      
    if ($response->isSuccessful()) {
       // Update your order payment status using $order_id
       //redirect users to show some congratulations message (To make them feel good)
     } else {
       //IF SOMEHOW SOMETHING WENT WRONG INTERNALLY. Redirect users to route with proper message.
       // return redirect()->route('YOUR_ROUTE')->with('message', 'Your payment has been declined. Please retry.')
     }
    }
    
  public function paymentFailed($order_id) {  
    //Redirect user back with payment failed message  
  }
}  

简单的PHP使用

创建购买

use Omnipay\Omnipay;
use Exception;

$payment_gateway = Omnipay::create('PhpEsewa_Secure');

$payment_gateway->setScd('MERCHANT_CODE');
$payment_gateway->setTestMode(true); //set it false if you want live mode.

try {
    $resp = $payment_gateway->purchase([
      'amt' => 100,  
      'txAmt' => 0,  
      'psc' => 0,  
      'pdc' => 0,  
      'tAmt' => 100,  
      'pid' => rand(10,10000),  //Your Purchase Unique ID
      'su' => 'https://yoursite.com/payment/success,  
      'fu' => 'https://yoursite.com/payment/failed',
    ])->send();

    if ($resp->isRedirect()) {
        $resp->redirect();
    }
} catch (Exception $e) {
    return $e->getMessage();
}

支付验证

$payment_gateway = Omnipay::create('PhpEsewa_Secure');

$payment_gateway->setScd('MERCHANT_CODE');
$payment_gateway->setTestMode(true); //set it false if you want live mode.

$resp = $payment_gateway->verifyPayment([
	'amt' => '100',  
	'rid' => 'ABCD', //rid from url (attached by esewa) 
	'pid' => '1234-5678', //pid from url (attached by esewa)
])->send();

if($resp->isSuccessful()) {
 //DO WHATEVER YOU WANT AFTER PAYMENT SUCCESS
}
//DO SOMETHING IF FAILED

ESEWA 文档

访问并查看Esewa 官方开发者指南,以详细了解参数和支付流程。

困惑?

如果您感到困惑,请随时通过jeewandhakal25@gmail.com联系我。