dammyammy/lara-pay-ng
Composer 安装命令:
composer require dammyammy/lara-pay-ng
包简介
Payment Solution For Laravel 5 (Nigerian Specific Providers eg. GTPay, SimplePay, CashEnvoy, VoguePay, WebPay)
README 文档
README
Supports Multiple Gateways with a single API approach, meaning Integration is the same across board in code.
Note: Do Not Use In Production, Currently being developed to be compatible with the latest version of Laravel.
The Aim is to Integrate as many Payment Gateways As Possible.
Requirements
Currently Supported
- VoguePay
- GTPay
- CashEnvoy
- SimplePay
Gateways Currently In development
- WebPay
Installation
Simply Run
composer require dammyammy/lara-pay-ng
Next Add the Service Provider into your Providers Array in config/app.php
'providers' => [ /* * Laravel Framework Service Providers... */ 'Illuminate\Foundation\Providers\ArtisanServiceProvider', 'Illuminate\Auth\AuthServiceProvider', .... 'LaraPayNG\Providers\LaraPayNGServiceProvider', ],
Next Publish All Package Files
php artisan vendor:publish
This Would create the following:
- A PaymentController in app/Http/Controllers/PaymentController.php
- Note: This is a working Sample, you may need to change Namespace to match your Projects namespace
- Migrations would be published to folder database/migrations
- 4 Views, matching the Controller
- one with a simulated checkout out button
- one is the proceed to pay page (Pay Now Button)
- one is a success notification Url, Showing a Successful Transaction
- one is a failure notification Url, Showing a Failed Transaction
Next Add the following to your routes page in app/Http/routes.php and edit as you seem fit, But be sure specified values match Your Config file
Route::get('orders', [ 'as' => 'orders', 'uses' => 'PaymentController@orders' ]); Route::get('checkout', [ 'as' => 'checkout', 'uses' => 'PaymentController@checkout' ]); $successUrl = config('lara-pay-ng.gateways.routes.success_route'); $successName = config('lara-pay-ng.gateways.routes.success_route_name'); $failureUrl = config('lara-pay-ng.gateways.routes.failure_route'); $failureName = config('lara-pay-ng.gateways.routes.failure_route_name'); Route::post('/' . $successUrl . '/{mert_id}', [ 'as' => $successName, 'uses' => 'PaymentController@success' ]); Route::post('/' . $failureUrl . '/{mert_id}', [ 'as' => $failureName, 'uses' => 'PaymentController@failed' ]); Route::post('/payment-notification/{mert_id}', [ 'as' => 'payment-notification', 'uses' => 'PaymentController@notification' ]);
Remember to Disallow CSRF Token Verification for your payment routes
This is perhaps one of the use cases in which u need to absolutely do this. If this is not done, When a Transaction Id is been sent back your site would throw a TokenMismatchException, as the Gateway Provider is posting back, and doesn't have a token Generated from your app.
Use the appropriate route endpoints if you did change the default names and urls.
<?php namespace App\Http\Middleware; use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier; class VerifyCsrfToken extends BaseVerifier { /** * The URIs that should be excluded from CSRF verification. * * @var array */ protected $except = [ 'transaction-failed/*', 'transaction-successful/*', 'payment-notification/*', ]; }
Next Migrate the Package Tables.
php artisan migrate
Next, Test the default Controller by visiting /Orders and follow through.
The Important thing to note is, to Inject the Payment Functionality into a controller, all you need do is to Inject the PaymentGatewayManager Class into that controller, and you instantly gain access to all the methods provided by the facades below.
use LaraPayNG\Exceptions\UnknownPaymentGatewayException; use LaraPayNG\Managers\PaymentGatewayManager; class PaymentController extends Controller { /** * @var PaymentGateway */ private $paymentGateway; /** * @param PaymentGatewayManager $paymentGateway */ public function __construct(PaymentGatewayManager $paymentGateway) { $this->paymentGateway = $paymentGateway; } .... }
Facades
| Facades | Namespace | When To Use |
|---|---|---|
| Pay:: | \LaraPayNG\Facades\Pay | Use If You want To Swap Gateways Through Config. |
| GTPay:: | \LaraPayNG\Facades\GTPay | Use If You Specifically want the GTPay Gateway Implementation. |
| WebPay:: | \LaraPayNG\Facades\WebPay | Use If You Specifically want the WebPay Gateway Implementation. |
| VoguePay:: | \LaraPayNG\Facades\VoguePay | Use If You Specifically want the VoguePay Gateway Implementation. |
| CashEnvoy:: | \LaraPayNG\Facades\CashEnvoy | Use If You Specifically want the CashEnvoy Gateway Implementation. |
| SimplePay:: | \LaraPayNG\Facades\SimplePay | Use If You Specifically want the SimplePay Gateway Implementation. |
Methods Available Via Facades / PaymentGatewayManager Injection
| Method | What it is Meant For |
|---|---|
| button($transactionId, $transactionData, $class, $buttonTitle) | To create Pay Now Button For Set Gateway In a View. |
| logTransaction($transactionData) | To Store Data Transaction Being Made for future Reference. |
| receiveTransactionResponse($transactionId, $mertId) | To Get Transaction Response back from Gateway. |
| logResponse($transactionData) | To Store Transaction Response from Gateway. |
| getDefaultDriver() | To get Default Payment Gateway Driver At Runtime. |
| with($name) | To set Default Payment Gateway Driver At Runtime.. |
| config($key) | Access Config Off Set Default Payment Gateway Driver. |
Events
| Event Name | Full Event Namespace | When It is Thrown |
|---|---|---|
| TransactionSuccessful | \LaraPayNG\Events\TransactionSuccessful | When A Transaction is deemed successful |
| TransactionUnsuccessful | \LaraPayNG\Events\TransactionUnsuccessful | When A Transaction is deemed unsuccessful |
Throwing this Events are optional, but helps serve as a hook for how you handle Completed Transactions.Take these Example Scenarios When you would Like to
- Send A customer an Email to notify them of their recent Transaction Status.
- You want to Set a person's account to active in the User's Table after a successful Transaction.
- You Want to add Paying Customer's to your Paying Customer's Mailer List.
The possibilities are endless, All you have to do is to Pull in the \LaraPayNG\Traits\DetermineTransactionStatus Trait and call the dispatchAppropriateEvents($result); method within your controller. e.g.
public function notification($mert_id, Request $request) { $result = $this->handleTransactionResponse($mert_id, $request); $this->dispatchAppropriateEvents($result); return $this->determineViewToPresent($result); }
Then Simply Create Event Listeners in app/Providers/EventServiceProvider.php
<?php namespace App\Providers; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'LaraPayNG\Events\TransactionSuccessful' => [ // 'App\Listeners\SendEmailForSuccessfulTransactions', // 'App\Listeners\ActivateUser', ], 'LaraPayNG\Events\TransactionUnsuccessful' => [ // 'App\Listeners\DeactivateUser', ], ]; .... }
Finally, Create Your Listener Implementations Knowing you have access to an array result containing values needed. An example implementation is written below:
File: app/Listeners/ActivateUser.php
<?php namespace App\Listeners; use LaraPayNG\Events\TransactionSuccessful; use App\User; class ActivateUser { /** * Handle the event. * * @param ActivateUser $event * @return void */ public function handle(ActivateUser $event) { // All Values Below are available // $event['status'], // $event['items'], // $event['transaction_id'], // $event['merchant_ref'], // $event['amount'], // $event['customer_id'], // $event['payer_id'], $user = User::find($event['customer_id']); $user->activate = true; $user->save(); return true; } }
Exceptions
All Exceptions Exist Under This Namespace;
namespace \LaraPayNG\Exceptions;
| Exceptions | When It is Thrown |
|---|---|
| UnknownPaymentGatewayException | If set Driver for Default Gateway is unknown or unsupported. |
| UnspecifiedTransactionAmountException | If amount(WebPay) / gtpay_tranx_amt(GTPay) is not in $transactionData. |
| UnspecifiedPayItemIdException | If pay_item_id is unspecifed in $transactionData array (GTPay). |
| PaymentGatewayVerificationFailedException | If Hash Calculation is Wrong During Verification (Applies to GTPay/WebPay Gateway ). |
Commands
There is a Command to Help Clear Stale Records That have been logged within a set number of days. To Use Simply Type in your terminal
# php artisan lara-pay-ng:purge-database gatewayname --days=3 --with-failed=false
php artisan lara-pay-ng:purge-database
You can Pass the gatewayname attribute to the Command eg. gtpay, voguepay. This is particularly useful for Multi-gateway Setups.
In the event of not passing the option, the default gateway driver from set config would be used.
It also can be passed a --with-failed option which accepts true/false. True means all Transactions that failed should be included in the deletion, as well as a --days option specifying how many days back data is considered old.
Tips
Add Methods Like These to Your Helpers.php File then Autoload It In Your composer.json
public function generateTransactionData($dessert, $transactionId) { return 'name=' . $dessert->present()->name . ';pre=' . $dessert->present()->buyPrice . ';buyer=' . currentUserName() . '; transactionId=' . $transactionId; } public function generateTransactionMemo($product) { return 'Name: ' . $product->name . '; Price: ' . $product->price . '; Buyer: ' . Auth::user()->email; }
then You can easily Do
$product = Product::get('2'); $transactionData = [ 'ce_amount' => $product->price, 'ce_memo' => generateTransactionMemo($product), ... ]; CashEnvoy::button($product->id, $transactionData, 'btn btn-success', '<i class="fa fa-currency"></i> Pay');
TODO
- Refactor Code
- Ability to Handle Transaction From Start to Finish
- Back everything up with Tests.
dammyammy/lara-pay-ng 适用场景与选型建议
dammyammy/lara-pay-ng 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「payment」 「gateway」 「webpay」 「voguepay」 「GTPay」 「CashEnvoy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dammyammy/lara-pay-ng 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dammyammy/lara-pay-ng 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dammyammy/lara-pay-ng 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
GP WepPay payment gateway for a standard card payment
Payyo Gateway for the Omnipay payment processing library
payment gateway integration in laravel
Easy-to-use Transbank SDK for PHP.
GP WepPay integration for Nette Framework
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-13