ThinkPHP 支付宝 扫码支付
2016-12-11 本文已影响0人
yieldHL
ThinkPHP3.2.3
集成支付宝的扫码支付
1.先下载官方Demo,下载后将整个 aop
文件夹放到ThinkPHP/Library/Vendor中

2.修改文件,将AopClient.php
中的 new SignData();
更改为 new \SignData();
;
将aop/request/AlipayTradePrecreateRequest
放到aop
目录下(这个随意,我是打算删掉request目录,所以才提出来)
3.使用方式

public function testAction(){
Vendor('aop.AopClient');
Vendor('aop.signData');
Vendor('aop.AlipayTradePrecreateRequest'); #如果你没有把这个文件提出来,就应该是aop.request.AlipayTradePrecreateRequest
$aop = new \AopClient();
$aop->gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';
$aop->appId = '2016072300102600';
$aop->rsaPrivateKeyFilePath = './rsa_private_key.pem';
$aop->alipayPublicKey='./alipay_public_key.pem';
$aop->apiVersion = '1.0';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new \AlipayTradePrecreateRequest();
$data = json_encode(array(
"out_trade_no" => "wx2016120951519992",
"total_amount" => "1",
"subject" => "测试",
"body" => "测试商品100"
),JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
$request->setBizContent($data);
$request->setNotifyUrl("http://pay.moblm.com/Todo/notify");
$result = $aop->execute ( $request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if(!empty($resultCode)&&$resultCode == 10000){
$resp = (array)$result->$responseNode;
Vendor('phpqrcode.phpqrcode'); # 这里开始是生成二维码
$url = $resp['qr_code'];
$errorCorrectionLevel =intval(4) ;//容错级别
$matrixPointSize = intval(8);//生成图片大小
$object = new \QRcode();
$object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
} else {
echo "失败";
exit;
}
}
小tip:我只改了new \SignData()
过后放到Linux服务器中的时候,提示找不到SignData Class,你可以把SignData提出来放到AopClient.php
最后面

公钥,私钥,支付宝公钥
进入沙箱调试网址,生成公钥、私钥、设置公钥、查看支付宝公钥



然后将私钥放到一个文件夹下(ras_private_key.pem
是非JAVA语言的,ras.private_key_pkcs8.pem
是JAVA语言的),并创建一个alipay_public_key.pem
(名字随便你)

这里注意了(看到没,字都特别大,所以一定要注意)
在
alipay_public_key.pem
中切记要把KEY分三行,windows和mac下都没问题,到了Linux上就有问题了!!!至于三行怎么断,随便你怎么断,因为我也是乱断的

就这样