Hướng dẫn thanh toán Gmo payment link plus với laravel

  • August 1, 2020
  • 3457

GMO là một cổng dịch vụ thanh toán số 1 của Nhật, Nó hỗ trợ nhiều hình thức thanh toán (credit card, multipayment như pay-easy convenience-store ...). Nó cung cấp đầy đủ các phương thức thanh toán chính của Nhật. Hôm nay mình sẽ hướng dẫn thanh toán GMO qua link plus sử dụng laravel.

Tạo GMOService

Tạo file app/Services/GMOService.php để xử lí các công việc liên quan đến GMO


<?php

namespace App\Services;

use Exception;
use Log;

class GMOService
{
    protected $config;

    public function __construct()
    {
        $this->config = config('payment.link_plus');
    }

    /**
     * Generate url GMO payment.
     *
     * @return null|string
     */
    public function generateUrl()
    {
        try {
            $jsonParams = json_encode($this->config['params']);
            $encodeParam = base64_encode($jsonParams);
            $hashPassword = hash('sha256', $encodeParam . $this->config['shop_password']);
            
            return sprintf(
                "%s/%s/%s/%s.%s",
                $this->config['env_url'],
                $this->config['shop_id'],
                $this->config['mode'],
                $encodeParam,
                $hashPassword
            );
        } catch (Exception $e) {
            Log::error('[ERROR_PAYMENT_GENERATE_URL]' .$e->getMessage());

            return null;
        }
    }

    /**
     * Setup params GMO payment.
     *
     * @param string $orderId
     * @param int $amount
     * @param string $retUrl
     */
    public function setParams(string $orderId, int $amount, string $retUrl = null)
    {
        $params = $this->config['params'];
        $params['transaction']['OrderID'] = $orderId;
        $params['transaction']['Amount'] = $amount;
        if ($retUrl) {
            $params['transaction']['RetUrl'] = $retUrl;
        }
        
        if ($this->config['tax']) {
            $params['transaction']['Tax'] = ($amount * $this->config['tax']) / 100;
        }

        $this->config['params'] = $params;
    }

    /**
     * Get payment result GMO payment.
     *
     * @param string $result
     *
     * @return array
     */
    public function getPaymentResult(string $result)
    {
        try {
            $paymentResult = explode('.', $result);
            $jsonPaymentResult =  base64_decode($paymentResult[0]);
            $transactionResult = json_decode($jsonPaymentResult, true);

            return $this->transformResult($transactionResult['transactionresult']);
        } catch (Exception $e) {
            Log::error('[ERROR_GET_PAYMENT_RESULT]' .$e->getMessage());

            return [];
        }
    }

    /**
     * Transform payment result.
     *
     * @param array $result
     *
     * @return array
     */
    private function transformResult(array $result)
    {
        return [
            'access_id' => $result['AccessID'],
            'access_pass' => $result['AccessPass'],
            'order_id' => $result['OrderID'],
            'result' => $result['Result'],
            'process_date' => $result['Processdate'],
            'error_code' => $result['ErrCode'],
            'error_info' => $result['ErrInfo'],
            'method' => $result['Paymethod'],
        ];
    }
}

Tạo file config

Tạo file app/config/payment.php


<?php

return [
    'link_plus' => [
        'env_url' => env('PAYMENT_LINK_PLUS_URL', ''),
        'shop_id' => env('PAYMENT_LINK_PLUS_SHOP_ID', ''),
        'shop_password' => env('PAYMENT_LINK_PLUS_SHOP_PASSWORD', ''),
        'mode' => 'checkout',
        'tax' => 10,
        'ret_url' => env('RET_URL'),
        'params' => [
            'configid' => env('PAYMENT_LINK_PLUS_CONFIG_ID', ''),
            'transaction' => [
                'PayMethods' => ['credit'],
                'RetryMax' => 10,
                'ExpireDays' => 0,
            ],
            'displaysetting' => [
                'TemplateID' => 'designA',
                'Lang' => 'ja',
            ],
            'credit' => [
                'TdFlag' => "2"
            ]
        ]
    ]
];

Cách generate link payment

Tạo function generate url trong controller


/**
     * Generate payment link plus.
     *
     * @return string
     */
    public function generateUrl(int $id)
    {
        $this->gmoService->setParams(
            $transaction_id, // Mã giao dịch
            $amount, // Số tiền thanh toán
            config('payment.link_plus.ret_url') // Callback url
        );
       
        return $this->gmoService->generateUrl()
    }

Lấy kết quả thanh toán bên GMO trả về

Kết quả của thanh toán được GMO trả về callback url với method là post


$this->gmoService->getPaymentResult($params['result']);

Lưu ý: Kết quả chỉ trả về khi người dùng click vào button trở về trang web