kaoken/veritrans-jp-air-web-laravel

用于Laravel5的VeritransJp Air-Web支付系统

1.5.2 2018-01-09 09:43 UTC

This package is auto-updated.

Last update: 2024-08-29 04:09:14 UTC


README

Travis branch composer version licence laravel version

这是一个用于在Laravel中使用日本贝里特拉斯(Veritrans)的AirWeb支付系统的工具。有关AirWeb的详细实现方法等,请参考https://www.veritrans.co.jp/developer/air/

  • 由于这是一个实验性的项目,因此可能会在没有通知的情况下突然删除此库。
  • 此库与日本贝里特拉斯无关,因此请勿提问或联系。
  • 根据规范,卡片和便利店支付不能同时使用。'00'支付方式就是指这一点。
  • 目前不提供详细的安装(构建)说明。

内容列表

安装

composer: composer.json 中添加以下内容:

  "require": {
    "kaoken/veritrans-jp-air-web-laravel":"1.5.*"
  },

初始设置

队列

队列使用,请在 config/queue.php 中确保其已启用!

例 php artisan queue:work --queue=payment,default --sleep=3 --tries=3

这部分需要根据您的环境进行设置!

app\Console\Kernel.php 中添加以下内容:

class Kernel extends ConsoleKernel
{
    protected $commands = [
        // 追加
        \Kaoken\VeritransJpAirWeb\Console\MakeVeritransJpAirWebCommand::class,
    ];
}

config\app.php 中添加以下内容:

    'providers' => [
        // 追加
        Kaoken\VeritransJpAirWeb\VeritransJpAirWebServiceProvide::class
    ],
    'aliases' => [
        // 追加
       'WebAir' => Kaoken\VeritransJpAirWeb\Facades\VeritransJpAirWeb::class
    ],
];

命令执行

$ php artisan veritrans-jp:web-air:install

以下四个文件将被添加到 database/migrations 中。

  • 2017_04_24_000000_create_air_web_payment_table.php
    • AirWeb支付信息表
  • 2017_04_24_000001_create_air_web_commodity_table.php
    • 单个商品信息表
  • 2017_04_24_000002_create_air_web_payment_notification_table.php
    • 支付完成通知信息表
  • 2017_04_24_000003_create_air_web_cvs_payment_notification_table.php
    • 便利店入金通知信息表

根据每个Web应用程序进行调整。之后

$ php artisan migrate

※ 在目录的 config 中创建名为 veritrans-jp-air-web.php 的配置文件。

配置

config\veritrans-jp-air-web.php

<?php
return [
    // マーチャントID
    'aw_merchant_id' => env('AW_MERCHANT_ID'),
    // AWへ送信するデータの検証用ハッシュキー
    'aw_merchant_hash_key' => env('AW_MERCHANT_HASH_KEY'),
    // ダミー取引フラグ ダミー取引フラグ 0 = 本番; 1 = テスト
    'aw_dummy_payment_flag' => env('AW_DUMMY_PAYMENT_FLAG', 0),

    // 売り上げフラグ:1:与信・売上、0:与信のみ。指定が無い場合は、0
    'aw_card_capture_flag' => env('AW_CARD_CAPTURE_FLAG', 1),
    // コンビニ決済の支払期限(当日からX日後)
    'aw_cvs_payment_limit' => env('AW_CVS_PAYMENT_LIMIT', 60),

    // 商品情報の商品ID未入力時に設定するダミー値
    'aw_dummy_commodity_id' => env('AW_DUMMY_COMMODITY_ID', 0),
    // 品情報のJAN_CODE未入力時に設定するダミー値
    'aw_dummy_commodity_jan_code' => env('AW_DUMMY_COMMODITY_JAN_CODE', '0'),
    // デフォルト決済方式。01 = クレジットカード、02 = コンビニ
    'aw_settlement_type' => env('AW_SETTLEMENT_TYPE', '01'),

    /**
     * 派生した場合は、クラスを変更すること
     */
    // 決済クラス
    'aw_payment_class' =>  \Kaoken\VeritransJpAirWeb\VeritransJpAirWebPayment::class,
    // 単体の商品クラス
    'aw_commodity_class' => \Kaoken\VeritransJpAirWeb\VeritransJpAirWebCommodity::class,
    // 決済完了通知クラス
    'aw_payment_notification_class' =>  \Kaoken\VeritransJpAirWeb\VeritransJpAirWebPaymentNotification::class,
    // コンビニ入金通知クラス
    'aw_cvs_payment_notification_class' =>  \Kaoken\VeritransJpAirWeb\VeritransJpAirWebCVSPaymentNotification::class,
    // 決済完了通知ジョブクラス
    'aw_payment_notification_job_class' =>  \Kaoken\VeritransJpAirWeb\Jobs\PaymentNotificationJob::class,
    // コンビニ入金通知ジョブクラス
    'aw_cvs_payment_notification_job_class' =>  \Kaoken\VeritransJpAirWeb\Jobs\CVSPaymentReceivedNotificationJob::class,
    // コンビニ決済期日を過ぎたジョブクラス
    'aw_cvs_due_date_has_passed_job_class' =>  \Kaoken\VeritransJpAirWeb\Jobs\CVSDueDateHasPassedJob::class
];

aw_settlement_type 不能选择 '00',只能选择卡片或便利店。

env

根据需要向 env 文件中添加。

# Air Webへ送信するマーチャントID
AW_MERCHANT_ID=
# Air Webへ送信するデータの検証用ハッシュキー
AW_MERCHANT_HASH_KEY=
# ダミー取引フラグ ダミー取引フラグ 0 = 本番; 1 = テスト
AW_DUMMY_PAYMENT_FLAG=1
# コンビニ決済の支払期限(当日からX日後)
AW_CVS_PAYMENT_LIMIT=7

任务计划

app\Console\Kernel.php

    protected function schedule(Schedule $schedule)
    {
        ...
        $schedule->call(function(){
            AirWeb::scheduleTask()->deleteNoPaymentNotification();
            AirWeb::scheduleTask()->queueCVSDueDateHasPassed();
        })->dailyAt('00:00');
    }
  • AirWeb::deleteNoPaymentNotification($day=7) 将删除 air_web_payment 表中已过 $day 天且未收到支付完成通知的记录,或者收到通知但内容为失败的情况。
  • AirWeb::eventCVSPaymentReceivedNotification($day=0) 将将当前超过 $day 天的便利店支付期限的工作项放入队列中。之后将调用 CVSDueDateHasPassedEvent 事件,每个Web应用程序的监听器中可以进行任意处理。air_web_payment 表的记录不会被删除。

中间件

app\Http\Kernel.php 中添加以下内容:

    protected $routeMiddleware = [
        ...
        'access_via_veritrans_jp' => \Kaoken\VeritransJpAirWeb\Middleware\AccessViaVeritransJp::class
    ];

此路由中间件仅用于允许通过 VeritransJp 的支付完成通知、便利店入金通知等。
是否使用由您自行决定。

事件

  • Kaoken\VeritransJpAirWeb\Events\CVSDueDateHasPassed
    • Kaoken\VeritransJpAirWeb\Jobs\CVSDueDateHasPassedJob 调用。
    • 便利店支付,入金期限已过的事件
  • Kaoken\VeritransJpAirWeb\Events\CVSPaymentReceivedNotificationEvent
    • Kaoken\VeritransJpAirWeb\Jobs\CVSPaymentReceivedNotificationJob 调用。
    • 便利店、入金通知事件
  • Kaoken\VeritransJpAirWeb\Events\PaymentNotificationEvent
    • Kaoken\VeritransJpAirWeb\Jobs\PaymentNotificationJob 调用。
    • 支付完成通知事件

以下为使用模板示例,请添加到 app\Listeners

<?php
/**
 * 決済 リスナー
 */
namespace App\Listeners;

use AirWeb;
use Log;
use Carbon\Carbon;
use Kaoken\VeritransJpAirWeb\Events\ CVSDueDateHasPassed;
use Kaoken\VeritransJpAirWeb\Events\CVSPaymentReceivedNotificationEvent;
use Kaoken\VeritransJpAirWeb\Events\PaymentNotificationEvent;

class PaymentEventSubscriber
{
    /**
     * 購読するリスナーの登録
     * @param \Illuminate\Events\Dispatcher $events
     */
    public function subscribe($events)
    {
        // コンビニ入金期日を過ぎた
        $events->listen(
            'Kaoken\VeritransJpAirWeb\Events\CVSDueDateHasPassed',
            'App\Listeners\PaymentEventSubscriber@onCVSDueDateHasPassed'
        );
        // コンビニエンスストア、入金通知
        $events->listen(
            'Kaoken\VeritransJpAirWeb\Events\CVSPaymentReceivedNotificationEvent',
            'App\Listeners\PaymentEventSubscriber@onCVSPaymentReceivedNotification'
        );
        // 決済完了通知
        $events->listen(
            'Kaoken\VeritransJpAirWeb\Events\PaymentNotificationEvent',
            'App\Listeners\PaymentEventSubscriber@onPaymentNotification'
        );
    }

    /**
     * コンビニ入金期日を過ぎた
     * @param CVSDueDateHasPassed $event
     * @see \Kaoken\VeritransJpAirWeb\Jobs\CVSDueDateHasPassedJob::handle()
     * @throws \Exception
     */
    public function onCVSDueDateHasPassed(CVSDueDateHasPassed $event)
    {

    }

    /**
     * コンビニエンスストア、入金通知
     * @param CVSPaymentReceivedNotificationEvent $event
     * @see \Kaoken\VeritransJpAirWeb\Jobs\CVSPaymentReceivedNotificationJob::handle()
     * @throws \Exception 
     * @note 例外後、`failed_jobs`テーブルへ追加される。
     */
    public function onCVSPaymentReceivedNotification(CVSPaymentReceivedNotificationEvent $event)
    {
        $obj = $event->obj;

    }

    /**
     * 決済完了通知
     * @param PaymentNotificationEvent $event
     * @see \Kaoken\VeritransJpAirWeb\Jobs\PaymentNotificationJob::handle()
     * @throws \Exception
     */
    public function onPaymentNotification(PaymentNotificationEvent $event)
    {
        $obj = $event->obj;

    }
}

根据每个Web应用程序进行设置。例如,在收到付款后进行商品发货等处理。

失败事件时的处理示例 app\Providers\AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Queue\Events\JobFailed;
use Kaoken\VeritransJpAirWeb\Jobs\CVSDueDateHasPassedJob;
use Kaoken\VeritransJpAirWeb\Jobs\CVSPaymentReceivedNotificationJob;
use Kaoken\VeritransJpAirWeb\Jobs\PaymentNotificationJob;
use Illuminate\Support\ServiceProvider;

use Log;
use Queue;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Queue::failing(function (JobFailed $event){
            // Air Web Veritrans Jp
            if( $event->connectionName === 'payment' ){
                $e = $event->exception;
                $a['error']['msg'] = $e->getMessage();
                $a['error']['code'] = $e->getCode();
                $a['error']['file'] = $e->getFile();
                $a['error']['line'] = $e->getLine();
                $a['error']['trace'] = $e->getTrace();

                if( $event->job instanceof CVSDueDateHasPassedJob){
                    $a['obj'] = $event->job->obj;
                    Log::error("Veritrans Jp コンビニ決済で、入金期日が過ぎた",$a);
                }else if( $event->job instanceof CVSPaymentReceivedNotificationJob){
                    $a['item'] = $event->job->items;
                    Log::error("Veritrans Jp コンビニ入金通知",$a);
                }else if( $event->job instanceof PaymentNotificationJob){
                    $a['item'] = $event->job->items;
                    Log::error("Veritrans Jp 決済完了通知",$a);
                }
            }
        });
    }
}

这里仅提供日志,但也可以添加邮件发送等。

控制器

添加 Kaoken\VeritransJpAirWeb\CVSPaymentReceivedNotificationKaoken\VeritransJpAirWeb\PaymentNotification 特性,以便从 Veritans Jp Air Web 接收通知。

以下为添加到 app\Http\Controllers\AirWebController.php 的示例。

<?php
/**
 * Veritans Jp Air Web に関する処理
 * @see https://air.veritrans.co.jp/map/settings/service_settings
 */
namespace App\Http\Controllers;

use Log;
use \Illuminate\Http\Request;
use App\Library\Http\Controllers\Controller;
use Kaoken\VeritransJpAirWeb\CVSPaymentReceivedNotification;
use Kaoken\VeritransJpAirWeb\PaymentNotification;

class AirWebController extends Controller
{
    use PaymentNotification, CVSPaymentReceivedNotification;

    /**
     * 決済完了通知
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function postPaymentNotification(Request $request)
    {
        return $this->paymentNotification($request);
    }


    /**
     * コンビニ入金通知
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function postCVSPaymentReceivedNotification(Request $request)
    {
        return $this->cvsPaymentReceivedNotification($request);
    }


    /**
     * 決済完了後の移動先
     * @param Request $request
     * @param int $threadId スレッドID
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postFinishPayment(Request $request, $threadId)
    {
//        Log::info('決済完了後');
        return redirect('/');
    }

    /**
     * 未決済時の移動先
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function getUnFinishPayment(Request $request)
    {
        return redirect('/');
    }
    /**
     * 決済エラー時の移動先
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postErrorPayment(Request $request)
    {
        return redirect('/');
    }
}

路由

根据 控制器 配置创建的示例,添加到 routes/web.php

Route::group([
        'middleware' => ['access_via_veritrans_jp']
    ]
    function() {
        // 決済完了通知を受信するためのもの
        Route::post('notification/handling','AirWebController@postPaymentNotification' );
        // コンビニ入金通知を受信するためのもの
        Route::post('cvs-payment-received','AirWebController@postCVSPaymentReceivedNotification' );
    }
);
// 正常に支払い手続きが終了した購入者へ表示するURL
Route::post('payment/finish','AirWebController@postFinishPayment' );
// 決済入力画面から「戻る」をクリックした購入者へ表示する
Route::get('payment/unfinish','AirWebController@getUnFinishPayment' );
// 正常に支払い手続きが終了しなかった購入者へ表示する
Route::post('payment/error','AirWebController@postErrorPayment' );

如果不使用中间件,则将中间件 access_via_veritrans_jp 设置为空。

许可证

MIT