andreikainer/laravel-mpay24

此包的最新版本(dev-master)没有提供许可信息。

dev-master 2018-10-14 02:00 UTC

This package is auto-updated.

Last update: 2024-09-14 19:53:24 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>