knox/pesapal 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

knox/pesapal

Composer 安装命令:

composer require knox/pesapal

包简介

A laravel package that integrates into the pesapal api

README 文档

README

Laravel >7 Package for the Pesapal API (uses v3 pesapal API) ######for < laravel v7 use v1 of the package

Installation

Add this package using Composer

From the command line inside your project directory, simply type:

composer require knox/pesapal

Update your config (for Laravel 5.4 and below)

Add the service provider to the providers array in config/app.php:

Knox\Pesapal\PesapalServiceProvider::class,

Add the facade to the aliases array in config/app.php:

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

Publish the package configuration (for Laravel 5.4 and below)

Publish the configuration file and migrations by running the provided console command:

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

Setup

Pesapal IPN

For the url of the route use /pesapal-ipn eg mysite.com/pesapal-ipn as the IPN on the Pesapal Merchant settings dashboard

Environmental Variables

PESAPAL_CONSUMER_KEY pesapal consumer key

PESAPAL_CONSUMER_SECRET pesapal consumer secret

PESAPAL_CURRENCY ISO code for the currency

PESAPAL_IPN controller method to call for instant notifications IPN as relative path from App\Http\Controllers\ eg "TransactionController@confirmation"

PESAPAL_CALLBACK_ROUTE route name to handle the callback eg Route::get('donepayment', ['as' => 'paymentsuccess', 'uses'=>'PaymentsController@paymentsuccess']); The route name is "paymentsuccess"

NB: The controller method accepts 4 function parameters, Example:

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

Config

live - Live or Demo environment

The ENV Variables can also be set from here.

Usage

At the top of your controller include the facade
use Pesapal;

Example Code...Better Example..Haha

Assuming you have a Payment Model

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',
                    'middle_name' => 'Mname',
                    'last_name' => 'Lname',
                    'email' => 'test@test.com',
                    'phonenumber' => '254-723232323',
                    'country_code' => 'KE',
                    'line_1' => '',
                    'line_2' => '',
                    'city' => '',
                    'state' => '',
                    'postal_code' => '',
                    'zip_code' => '',
                    'reference' => $payments -> transactionid,
                    'height'=>'400px',
                    //'currency' => 'USD'
                    //'cancellation_url' => '',
                    //'notification_id' => '',
                    //'branch' => '',
                    //'account_number' => '',  
                    //'subscription_details' => [
                    //    'start_date' => '24-01-2023',
                    //    'end_date' => '31-12-2023',
                    //    'frequency' => 'DAILY'
                    //]
                );

        $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";
    }
}

Example ENV

 PESAPAL_IPN=PaymentsController@paymentconfirmation
 PESAPAL_LIVE=true
 PESAPAL_CALLBACK_ROUTE=paymentsuccess

Example View

  {!! $iframe !!}

Example Routes

Relevant routes example, to help exclude entire webhooks route group in Csrf check in VerifyCsrfToken Middleware

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

All Done

Feel free to report any issues

knox/pesapal 适用场景与选型建议

knox/pesapal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 120.94k 次下载、GitHub Stars 达 28, 最近一次更新时间为 2015 年 10 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「payments」 「visa」 「mastercard」 「pesapal」 「mpesa」 「Orange Money」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 knox/pesapal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 knox/pesapal 我们能提供哪些服务?
定制开发 / 二次开发

基于 knox/pesapal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 120.94k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 29
  • 点击次数: 38
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 28
  • Watchers: 9
  • Forks: 40
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-24