參考文檔:
官方掃碼支付文檔:https://docs.open.alipay.com/194
官方demo下載地址:http://aopsdkdownload.cn-hangzhou.alipay-pub.aliyun-inc.com/demo/F2FPay_Demo_php.zip
沙箱配置信息地址(沒有的自己申請):https://openhome.alipay.com/platform/appDaily.htm?tab=info
首先我們把下載好的demo放到tp5的extend下面,如下圖
然後我們在新建一個控制器,以下是我寫的部分代碼
<?php
namespace app\index\controller;
use think\Controller;
use think\Loader;
class Index extends Controller
{
public function index()
{
$config = array (
//簽名方式,默認爲RSA2(RSA2048)
'sign_type' => "RSA2",
//支付寶公鑰
'alipay_public_key' => "",
//商戶私鑰
'merchant_private_key' => "",
//編碼格式
'charset' => "UTF-8",
//支付寶網關,因爲用的沙箱環境,記得把網關換成以下這個,正式的環境的時候記得換回去
'gatewayUrl' => "https://openapi.alipaydev.com/gateway.do",
//應用ID
'app_id' => "2016xxxxxxxxx",
//異步通知地址,只有掃碼支付預下單可用
'notify_url' => "http://www.baidu.com",
//最大查詢重試次數
'MaxQueryRetry' => "10",
//查詢間隔
'QueryDuration' => "3"
);
Loader::import('alipay.f2fpay.service.AlipayTradeService');
Loader::import('alipay.f2fpay.model.builder.AlipayTradePayContentBuilder');
$payRequestBuilder = new \AlipayTradePayContentBuilder();
$payRequestBuilder->setBody('測試');
$payRequestBuilder->setSubject('測試');
$payRequestBuilder->setTotalAmount(100);
$payRequestBuilder->setOutTradeNo('1233j56776445');
$qrPay = new \AlipayTradeService($config);
$qrPayResult=$qrPay->qrPay($payRequestBuilder);
switch ($qrPayResult->getTradeStatus()){
case "SUCCESS":
echo "支付寶創建訂單二維碼成功:"."<br>---------------------------------------<br>";
$response = $qrPayResult->getResponse();
$qrcode = $qrPay->create_erweima($response->qr_code);
$this->assign('img',$qrcode);
return $this->fetch();
// exit();
// print_r($response);
break;
case "FAILED":
echo "支付寶創建訂單二維碼失敗!!!"."<br>--------------------------<br>";
if(!empty($qrPayResult->getResponse())){
print_r($qrPayResult->getResponse());
}
break;
case "UNKNOWN":
echo "系統異常,狀態未知!!!"."<br>--------------------------<br>";
if(!empty($qrPayResult->getResponse())){
print_r($qrPayResult->getResponse());
}
break;
default:
echo "不支持的返回狀態,創建訂單二維碼返回異常!!!";
break;
}
}
}
然後我們會發現,我們不能生成二維碼,找到AlipayTradeService.php裏面二維碼生成的方法
function create_erweima($content, $size = '200', $lev = 'L', $margin= '0') {
$content = urlencode($content);
$image = '<img src="http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$lev.'|'.$margin.'&chl='.$content.'" widht="'.$size.'" height="'.$size.'" />';
return $image;
}
這是使用了谷歌的api生成二維碼,使用需要翻牆。
我們可以使用phpqrcode,使用qrcode必須開啓支持GD2擴展庫支持(一般情況下都是開啓狀態)
phpqrcode類文件下載,下載地址:https://sourceforge.net/projects/phpqrcode/
下載好了之後我們只需要裏面的phpqrcode.php,這裏我把它改成qrcode去了
然後我們修改AlipayTradeService.php下的create_erweima方法如下
function create_erweima($content, $size = '200', $lev = 'L', $margin= '0') {
\think\Loader::import('alipay.qrcode');
// $content = urlencode($content);
// $image = '<img src="http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$lev.'|'.$margin.'&chl='.$content.'" widht="'.$size.'" height="'.$size.'" />';
$outfile=false; $level=QR_ECLEVEL_L; $size=10;$margin=4;
$image =QRencode::factory($level,$size,$margin);
ob_end_clean();
return $image->encodePNG($content,$outfile,$saveandprint=false);
}
然後運行,我們就可以發現二維就可以生成啦,有可能會遇到生成的二維碼會是亂碼,我的解決方法是在create_erweima加入ob_end_clean()方法。