scottchayaa / allpay
Allpay - Laravel 5 版本
This package is auto-updated.
Last update: 2024-09-06 09:39:39 UTC
README
步骤 1 : 下载包
使用composer命令安装
composer require scottchayaa/allpay dev-master
或者将package添加到composer.json中
"require": {
"scottchayaa/allpay": "dev-master"
},
然后更新安装
composer update
或全新安装
composer install
步骤 2 : 修改配置文件
在config/app.php
中添加providers
和aliases
的参数
'providers' => [
// ...
ScottChayaa\Allpay\AllpayServiceProvider::class,
]
'aliases' => [
// ...
'Allpay' => ScottChayaa\Allpay\Facade\Allpay::class,
]
步骤 3 : 将配置发布到你的项目
执行以下命令,将包的配置文件配置到你的项目中
php artisan vendor:publish
可至config/allpay.php中查看
默认是Allpay的测试环境设置
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(); }
对于使用Laravel的开发者,建议使用CheckOutString()
返回String的方法
当然使用CheckOut()
也是可以的
但如果使用的话,我猜后面可能会遇到获取不到特定Session的问题
PS : PaymentMethod前面一定要加反斜线 \ → 目前我也无法,如果有人知道如何不添加,请告诉我
需要在PaymentMethod前面添加反斜线 '' → 我不知道如何去除它。如果有人知道如何去除,请告诉我如何操作。谢谢~
示例(本地环境)
示例链接:https:///[your-project]/public/allpay_demo_201608
错误修复记录
AllPay.Payment.Integration.php : (最新提交 e9278b9)
https://github.com/allpay/PHP/commit/e9278b9cad76e6a71608bee3f5f4289982cfe16f
1) 修正 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 ; }
主要是针对$paymentButton
进行调整
如果有比较现在版本和以前版本的人会发现这个错误
缺少if (!isset($paymentButton))
的判断
如果官方的工程师发现了这个问题就赶快解决吧
2) 修正CheckOutFeedback
原文件
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()); //... }
注意要传入$request->all()
因为官方原始的方法是传入$_POST
→ Laravel 5 不认这个,所以会出错
进行此修正
不过这部分没有多做说明,留给大家试试看