godfreymakori / laravelpesapal
一个集成到 Pesapal API 的 Laravel 扩展包
Requires
- php: ^7.2
README
Laravel 5 用于 Pesapal API 的扩展包
安装
使用 Composer 添加此扩展包
在项目目录内部,只需在命令行中输入
composer require knox/pesapal
更新你的配置
在 config/app.php 的 providers 数组中添加服务提供者
Knox\Pesapal\PesapalServiceProvider::class,
在 config/app.php 的 aliases 数组中添加外观
'Pesapal' => Knox\Pesapal\Facades\Pesapal::class,
发布扩展包配置
通过运行提供的控制台命令发布配置文件和迁移
php artisan vendor:publish --provider="Knox\Pesapal\PesapalServiceProvider"
设置
Pesapal IPN
对于路由的 URL,使用 /pesapal-ipn,例如 mysite.com/pesapal-ipn 作为 Pesapal 商户设置仪表板上的 IPN
环境变量
PESAPAL_CONSUMER_KEY pesapal 消费者密钥
PESAPAL_CONSUMER_SECRET pesapal 消费者密钥
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 - 实时或演示环境
也可以从这里设置环境变量。
用法
在控制器顶部包含外观
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 }}
示例路由
相关路由示例,帮助在 VerifyCsrfToken 中间件的 Csrf 检查中排除整个 webhooks 路由组
Route::group(['prefix' => '/webhooks'], function () { //PESAPAL Route::get('donepayment', ['as' => 'paymentsuccess', 'uses'=>'PaymentsController@paymentsuccess']); Route::get('paymentconfirmation', 'PaymentsController@paymentconfirmation'); });
全部完成
请随时报告任何问题