stalinko / laravel-mpay24
关于此包最新版本(dev-master)没有可用的许可证信息。
dev-master
2016-03-08 12:48 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-28 17:38:30 UTC
README
此包是Laravel 5的插件,用于与mpay24.com支付工作。
安装
使用composer安装:composer require stalinko/laravel-mpay24
然后将其添加到config/app.php中的providers列表
'LaravelMPay24\LaravelMPay24ServiceProvider',
在config/services.php中为新服务提供设置
'mpay24' => [ 'merchantId' => 'XXXXXXX', //required 'password' => 'XXXXXXXX', //required 'test' => true, //optional 'debug' => true, //optional 'successUrl' => 'WelcomeController@anySuccess', //optional 'errorUrl' => 'WelcomeController@anyError', //optional 'confirmationUrl' => 'WelcomeController@anyConfirmation', //optional ],
使用
现在您有一个名为app()->mpay24的服务,它是\LaravelMPay24\Shop类的对象。此对象使用配置中给出的设置为您初始化了MPay24Shop类。要定义商店的逻辑,您必须创建一个继承自\LaravelMPay24\Models\AbstractShop的自己的商店类,实例化该类的对象并将其传递给app()->mpay24服务
$shopDelegator = new BasicShop(); app()->mpay24->setShopDelegator($shopDelegator);
因此,mpay24将所有操作委托给您的商店。"BasicShop"是委托商店的示例实现,您可以在包中找到它。
app()->mpay24使用服务设置将回调URL设置到ORDER对象。但您可以在自己的自定义商店类中设置它,app()->mpay24不会覆盖这些设置。
工作示例
示例控制器(WelcomeController.php)
namespace App\Http\Controllers; use LaravelMPay24\Models\BasicShop; use LaravelMPay24\ORDER; use LaravelMPay24\PaymentResponse; use LaravelMPay24\Transaction; class WelcomeController extends Controller { /** * Show the application welcome screen to the user. * * @return \Illuminate\Http\Response */ public function getIndex() { return view('welcome'); } /** * Create a test transaction and redirect user to mpay24 page */ public function postIndex() { $transaction = new Transaction('test transaction'); $transaction->PRICE = 100.11; $order = new ORDER(); $order->Order->Tid = $transaction->TID; $order->Order->Price = $transaction->PRICE; $shopDelegator = new BasicShop(); $shopDelegator->setTransaction($transaction); $shopDelegator->setOrder($order); /** @var \LaravelMPay24\Shop $mpay24 */ $mpay24 = app()->mpay24; $mpay24->setShopDelegator($shopDelegator); /** @var PaymentResponse $result */ $result = app()->mpay24->pay(); if($result->getGeneralResponse()->getStatus() == 'OK') { $url = $result->getLocation(); header('Location: '.$url); } else { echo "Return Code: " . $result->getGeneralResponse()->getReturnCode(); } } public function anySuccess() { echo 'Success'; } public function anyError() { echo 'Error'; } public function anyConfirmation() { echo 'Confirmation'; } }
welcome.blade.php的代码
<html> <head> <title>Payment Test</title> </head> <body> <form method="POST"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <input type="submit" value="Try it" /> </form> </body> </html>