payum for Laravel 線上支付程式

payum 是一套和 omnipay 類似的程式,但它提供了更多的東西

可參考原作者在stackoverflow上的回覆

在測試Payum的過程中,發現它已經將交易過程用檔案的方式存入檔案內,這部份就比Omnipay來得更加便利

看上了這一點,所以將它整合入Laravel,並且將交易利用Eloquent的方式存入資料庫內(也可以設定存入檔案)

分享給大家使用 Github

Payum for Laravel 5

Installing

To get the latest version of Laravel Exceptions, simply require the project using Composer:

composer require recca0120/laravel-payum

Instead, you may of course manually update your require block and run composer update if you so choose:

{
    "require": {
        "recca0120/laravel-payum": "^0.0.1"
    }
}

Include the service provider within config/app.php. The service povider is needed for the generator artisan command.

'providers' => [
    ...
    Recca0120\LaravelPayum\ServiceProvider::class,
    ...
];

Config

return [
    'router' => [
        'prefix'     => 'payment',
        'as'         => 'payment.',
        // if laravel 5.1 remove web
        'middleware' => 'web',
    ],

    'storage' => [
        // optioins: eloquent, filesystem
        'token' => 'filesystem',

        // optioins: eloquent, filesystem
        'gatewayConfig' => 'filesystem',
    ],

    // 'customFactoryName' => [
    //     'factory'  => 'FactoryClass',
    //     'username' => 'username',
    //     'password' => 'password',
    //     'sandbox'  => false
    // ],
    'gatewayConfigs' => [
        'offline' => []
    ],
];

Controller


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Payum\Core\GatewayInterface;
use Payum\Core\Model\PaymentInterface;
use Payum\Core\Payum;
use Payum\Core\Request\GetHumanStatus;
use Payum\Core\Security\TokenInterface;
use Payum\Core\Storage\StorageInterface;
use Recca0120\LaravelPayum\Service\Payment;

class PaymentController extends BaseController
{
    public function prepare(Payment $payment)
    {
        return $payment->prepare('offline', function (PaymentInterface $payment, $gatewayName, StorageInterface $storage, Payum $payum) {
            $payment->setNumber(uniqid());
            $payment->setCurrencyCode('TWD');
            $payment->setTotalAmount(100);
            $payment->setDescription('A description');
            $payment->setClientId('anId');
            $payment->setClientEmail('foo@example.com');
            $payment->setDetails([]);
        });
    }

    public function done(Payment $payment, Request $request, $payumToken)
    {
        return $payment->done($request, $payumToken, function (GetHumanStatus $status, PaymentInterface $payment, GatewayInterface $gateway, TokenInterface $token) {
            return response()->json([
                'status' => $status->getValue(),
                'client' => [
                    'id'    => $payment->getClientId(),
                    'email' => $payment->getClientEmail(),
                ],
                'number'        => $payment->getNumber(),
                'description'   => $payment->getCurrencyCode(),
                'total_amount'  => $payment->getTotalAmount(),
                'currency_code' => $payment->getCurrencyCode(),
                'details'       => $payment->getDetails(),
            ]);
        });
    }
}

Router

Route::get('payment', [
    'as'   => 'payment',
    'uses' => 'PaymentController@prepare',
]);

Route::any('payment/done/{payumToken}', [
    'as'   => 'payment.done',
    'uses' => 'PaymentController@done',
]);

Eloquent

If you want use eloquent you need change config.php and create database

Migrate

publish vendor

artisan vendor:publish --provider="Recca0120\LaravelPayum\ServiceProvider"

migrate

artisan migrate

modify config


return [
    'router' => [
        'prefix'     => 'payment',
        'as'         => 'payment.',
        // if laravel 5.1 remove web
        'middleware' => 'web',
    ],

    'storage' => [
        // optioins: eloquent, eloquent
        'token' => 'filesystem',

        // optioins: eloquent, filesystem
        'gatewayConfig' => 'filesystem',
    ],

    // 'customFactoryName' => [
    //     'factory'  => 'FactoryClass',
    //     'username' => 'username',
    //     'password' => 'password',
    //     'sandbox'  => false
    // ],
    'gatewayConfigs' => [
        'offline' => []
    ],
];
本帖已被设为精华帖!
本帖由 Summer 于 7年前 加精
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 5
Summer

楼主开源了好多高质量扩展包,点个赞 :+1:

7年前 评论

@Summer 有些好東西不整合進來真是太可惜了

7年前 评论
zoroo

Star 支持一下

7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!