PHP code example of scottchayaa / allpay

1. Go to this page and download the library: Download scottchayaa/allpay library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

scottchayaa / allpay example snippets


return [
    'ServiceURL' => 'http://payment-stage.allpay.com.tw/Cashier/AioCheckOut',
    'HashKey'    => '5294y06JbISpM5x9',
    'HashIV'     => 'v77hoKGq4kWxNNIS',
    'MerchantID' => '2000132',
];

use Allpay;

public function Demo()
{
    //Official Example : 
    //https://github.com/allpay/PHP/blob/master/AioSDK/example/sample_Credit_CreateOrder.php
    
    //基本參數(請依系統規劃自行調整)
    Allpay::i()->Send['ReturnURL']         = "http://www.allpay.com.tw/receive.php" ;
    Allpay::i()->Send['MerchantTradeNo']   = "Test".time() ;           //訂單編號
    Allpay::i()->Send['MerchantTradeDate'] = date('Y/m/d H:i:s');      //交易時間
    Allpay::i()->Send['TotalAmount']       = 2000;                     //交易金額
    Allpay::i()->Send['TradeDesc']         = "good to drink" ;         //交易描述
    Allpay::i()->Send['ChoosePayment']     = \PaymentMethod::ALL ;     //付款方式

    //訂單的商品資料
    array_push(Allpay::i()->Send['Items'], array('Name' => "歐付寶黑芝麻豆漿", 'Price' => (int)"2000",
               'Currency' => "元", 'Quantity' => (int) "1", 'URL' => "dedwed"));

    //Go to AllPay
    echo "歐付寶頁面導向中...";
    echo Allpay::i()->CheckOutString();
}

static function CheckOutString($paymentButton,$target = "_self",$arParameters = array(),$arExtend = array(),$HashKey='',$HashIV='',$ServiceURL=''){
    
    $arParameters = self::process($arParameters,$arExtend);
    //產生檢查碼
    $szCheckMacValue = CheckMacValue::generate($arParameters,$HashKey,$HashIV,$arParameters['EncryptType']);
    
    $szHtml =  '<!DOCTYPE html>';
    $szHtml .= '<html>';
    $szHtml .=     '<head>';
    $szHtml .=         '<meta charset="utf-8">';
    $szHtml .=     '</head>';
    $szHtml .=     '<body>';
    $szHtml .=         "<form id=\"__allpayForm\" method=\"post\" target=\"{$target}\" action=\"{$ServiceURL}\">";
    foreach ($arParameters as $keys => $value) {
        $szHtml .=         "<input type=\"hidden\" name=\"{$keys}\" value='{$value}' />";
    }
    $szHtml .=             "<input type=\"hidden\" name=\"CheckMacValue\" value=\"{$szCheckMacValue}\" />";
    $szHtml .=             "<input type=\"submit\" id=\"__paymentButton\" value=\"{$paymentButton}\" />";
    $szHtml .=         '</form>';
    $szHtml .=     '</body>';
    $szHtml .= '</html>';
    return  $szHtml ;
}

static function CheckOutString($paymentButton,$target = "_self",$arParameters = array(),$arExtend = array(),$HashKey='',$HashIV='',$ServiceURL=''){
    
    $arParameters = self::process($arParameters,$arExtend);
    //產生檢查碼
    $szCheckMacValue = CheckMacValue::generate($arParameters,$HashKey,$HashIV,$arParameters['EncryptType']);
    
    $szHtml =  '<!DOCTYPE html>';
    $szHtml .= '<html>';
    $szHtml .=     '<head>';
    $szHtml .=         '<meta charset="utf-8">';
    $szHtml .=     '</head>';
    $szHtml .=     '<body>';
    $szHtml .=         "<form id=\"__allpayForm\" method=\"post\" target=\"{$target}\" action=\"{$ServiceURL}\">";

    foreach ($arParameters as $keys => $value) {
        $szHtml .=         "<input type=\"hidden\" name=\"{$keys}\" value='{$value}' />";
    }

    $szHtml .=             "<input type=\"hidden\" name=\"CheckMacValue\" value=\"{$szCheckMacValue}\" />";
    if (!isset($paymentButton)) {
        $szHtml .=  '<script type="text/javascript">document.getElementById("__allpayForm").submit();</script>';
    }
    else{
        $szHtml .=  "<input type=\"submit\" id=\"__paymentButton\" value=\"{$paymentButton}\" />";
    }
    $szHtml .=         '</form>';
    $szHtml .=     '</body>';
    $szHtml .= '</html>';
    return  $szHtml ;
}

function CheckOutFeedback() {
    return $arFeedback = CheckOutFeedback::CheckOut($_POST,$this->HashKey,$this->HashIV,0);   
}

function CheckOutFeedback($allPost = null) {
    if($allPost == null) $allPost = $_POST;
    return $arFeedback = CheckOutFeedback::CheckOut($allPost,$this->HashKey,$this->HashIV,0);   
}

public function PayReturn(Request $request)
{
    /* 取得回傳參數 */
    $arFeedback = Allpay::i()->CheckOutFeedback($request->all());
    //...
}

php artisan vendor:publish