knox/pesapal

一个集成到 Pesapal API 的 Laravel 扩展包

v1.5.0 2023-02-23 09:57 UTC

This package is auto-updated.

Last update: 2024-09-06 20:27:34 UTC


README

Laravel 5,6,7,8,9,10 的 Pesapal API 扩展包

安装

使用 Composer 添加此扩展包

在项目目录内部从命令行,只需输入

composer require knox/pesapal

更新您的配置(适用于 Laravel 5.4 及以下版本)

将服务提供者添加到 config/app.php 中的 providers 数组

Knox\Pesapal\PesapalServiceProvider::class,

将外观添加到 config/app.php 中的 aliases 数组

'Pesapal' => Knox\Pesapal\Facades\Pesapal::class,

发布扩展包配置(适用于 Laravel 5.4 及以下版本)

通过运行提供的控制台命令发布配置文件和迁移

php artisan vendor:publish --provider="Knox\Pesapal\PesapalServiceProvider"

设置

Pesapal IPN

对于路由的 URL,请使用 /pesapal-ipn,例如 mysite.com/pesapal-ipn 作为 Pesapal 商户设置仪表板上的 IPN

环境变量

PESAPAL_CONSUMER_KEY pesapal consumer key

PESAPAL_CONSUMER_SECRET pesapal consumer secret

PESAPAL_CURRENCY 货币的 ISO 代码

PESAPAL_IPN 用于即时通知 IPN 的控制器方法,相对于 App\Http\Controllers\ 的路径,例如 "TransactionController@confirmation"

PESAPAL_CALLBACK_ROUTE 处理回调的路由名称,例如 Route::get('donepayment', ['as' => 'paymentsuccess', 'uses'=>'PaymentsController@paymentsuccess']); 路由名称是 "paymentsuccess"

注意:控制器方法接受 4 个函数参数,例如

public function confirmation($trackingid,$status,$payment_method,$merchant_reference)
{
	$payments = Payments::where('tracking',$trackingid)->first();
    $payments -> payment_status = $status;
    $payments -> payment_method = $payment_method;
    $payments -> save();
}       

配置

live - 实时或演示环境

ENV 变量也可以从这里设置。

用法

在控制器顶部包含外观
use Pesapal;

示例代码...更好的示例..哈哈

假设您有一个支付模型

use Pesapal;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Auth;
use App\Payment;

class PaymentsController extends Controller
{
    public function payment(){//initiates payment
        $payments = new Payment;
        $payments -> businessid = Auth::guard('business')->id(); //Business ID
        $payments -> transactionid = Pesapal::random_reference();
        $payments -> status = 'NEW'; //if user gets to iframe then exits, i prefer to have that as a new/lost transaction, not pending
        $payments -> amount = 10;
        $payments -> save();

        $details = array(
            'amount' => $payments -> amount,
            'description' => 'Test Transaction',
            'type' => 'MERCHANT',
            'first_name' => 'Fname',
            'last_name' => 'Lname',
            'email' => 'test@test.com',
            'phonenumber' => '254-723232323',
            'reference' => $payments -> transactionid,
            'height'=>'400px',
            //'currency' => 'USD'
        );
        $iframe=Pesapal::makePayment($details);

        return view('payments.business.pesapal', compact('iframe'));
    }
    public function paymentsuccess(Request $request)//just tells u payment has gone thru..but not confirmed
    {
        $trackingid = $request->input('tracking_id');
        $ref = $request->input('merchant_reference');

        $payments = Payment::where('transactionid',$ref)->first();
        $payments -> trackingid = $trackingid;
        $payments -> status = 'PENDING';
        $payments -> save();
        //go back home
        $payments=Payment::all();
        return view('payments.business.home', compact('payments'));
    }
    //This method just tells u that there is a change in pesapal for your transaction..
    //u need to now query status..retrieve the change...CANCELLED? CONFIRMED?
    public function paymentconfirmation(Request $request)
    {
        $trackingid = $request->input('pesapal_transaction_tracking_id');
        $merchant_reference = $request->input('pesapal_merchant_reference');
        $pesapal_notification_type= $request->input('pesapal_notification_type');

        //use the above to retrieve payment status now..
        $this->checkpaymentstatus($trackingid,$merchant_reference,$pesapal_notification_type);
    }
    //Confirm status of transaction and update the DB
    public function checkpaymentstatus($trackingid,$merchant_reference,$pesapal_notification_type){
        $status=Pesapal::getMerchantStatus($merchant_reference);
        $payments = Payment::where('trackingid',$trackingid)->first();
        $payments -> status = $status;
        $payments -> payment_method = "PESAPAL";//use the actual method though...
        $payments -> save();
        return "success";
    }
}

示例环境变量

 PESAPAL_IPN=PaymentsController@paymentconfirmation
 PESAPAL_LIVE=true
 PESAPAL_CALLBACK_ROUTE=paymentsuccess

示例视图

  {!! $iframe !!}

示例路由

相关路由示例,以帮助排除整个 Webhooks 路由组在 VerifyCsrfToken 中间件中的 Csrf 检查

Route::group(['prefix' => '/webhooks'], function () {
    //PESAPAL
    Route::get('donepayment', ['as' => 'paymentsuccess', 'uses'=>'PaymentsController@paymentsuccess']);
    Route::get('paymentconfirmation', 'PaymentsController@paymentconfirmation');
});

全部完成

请随时报告任何问题