cimplival / pesapal
一个集成到pesapal api的laravel包
Requires
- php: >=5.5.0
- illuminate/support: 5.*
README
Laravel 6版本的Pesapal API包
安装
使用Composer添加此包
将以下行添加到您的composer.json文件中
"nesbot/carbon": "2.24 as 2.25.1"
注意:nesbot/carbon的版本可能不同
运行以下行
composer dump-autoload
最后运行以下行
composer require cimplival/pesapal "dev-master"
更新您的配置
将服务提供者添加到config/app.php中的providers数组中
Cimplival\Pesapal\PesapalServiceProvider::class,
将外观添加到config/app.php中的aliases数组中
'Pesapal' => Cimplival\Pesapal\Facades\Pesapal::class,
发布包配置
通过运行提供的控制台命令发布配置文件和迁移
php artisan vendor:publish --provider="Cimplival\Pesapal\PesapalServiceProvider"
设置
环境变量
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 - 生产或演示环境
环境变量也可以从这里设置。
使用方法
在控制器顶部包含外观
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 you payment has gone through..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
示例路由
相关路由示例,帮助在VerifyCsrfToken中间件的Csrf检查中排除整个webhooks路由组
Route::group(['prefix' => '/webhooks'], function () { //PESAPAL Route::get('donepayment', ['as' => 'paymentsuccess', 'uses'=>'PaymentsController@paymentsuccess']); Route::get('paymentconfirmation', 'PaymentsController@paymentconfirmation'); });
全部完成
欢迎报告任何问题