威望0
积分7946
贡献0
在线时间763 小时
UID1
注册时间2021-4-14
最后登录2024-11-21
管理员
- UID
- 1
- 威望
- 0
- 积分
- 7946
- 贡献
- 0
- 注册时间
- 2021-4-14
- 最后登录
- 2024-11-21
- 在线时间
- 763 小时
|
PHP调用支付宝APP支付(Thinkphp)
PHP调用支付宝APP支付(Thinkphp),伸手党可直接修改使用, 注意:以下代码为本人在实际开发中的代码,只是写了写简单的实现逻辑代码,详细代码就不罗列出来啦,若你需要使用请结合自身业务修改即可 - <?php
- #调用支付宝APP支付
- public function pay(){
- Vendor('alisdk.AopSdk');//载入sdk类
- $aop = new \AopClient;
- $idCode = I('idCode');
- $price = M('Bill')->where(array('idCode'=>$idCode))->getField('price');
- $out_trade_no = time().mt_rand(10000,99999);
- $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";#此处为真实支付网关,若用沙箱环境记得使用沙箱测试的网关
- $aop->appId = "20171*******787064";
- $aop->rsaPrivateKey = '*************LsnY/LF2ImP94EpPYkjS0HJcIp8iqPMzJjk5AwZB2txnbkG6ULl6Hn/nKrLixxfbFiXwKDfw7blj0zP5gmHKb45NnKvhYZVkuf2aRm409oy3KJC+J2EPjeAjj8pA+Uz9lKR8YmjzIOxqZ9YCzsgNXd7dw4ARo95yK8r1IlOYRX24dz/mhgFtMoPWcOzRLXMDhUhR0lxplU5m1PEbh5+mDFss7GyUxvOrbwZoeik14Chc5BJirL9kA2C8sXkEI0+xTJPXXjQc0iMr3tvXgQCh/+dtw2Q99IaK5b06kCXPHIzI9LKCVOitk8kVwo5E2eDXvRdyovLs3LzlQSSQIDAQABAoIBAQDRrR4eP1hxGtgEDNExvvd2AndoBi92CaBHaWFg9DzOCPT6isO3moTtQh1ltwxeE6ngwQobkSOi9ObmDhGdqh6wQfPylFjJrr4B25E4l3Qt+nWDDOYUVOajGYFG/XhtZWyfjn8cLLtfiNkSYClvRsy5+C/EgOgqSDnGtvlDN4pnUI5UPNAgZkPDqpH0sjwh4rykxU6Lti5YN2Lp6ttw64nMP0lGt2snu5CYwL5jwUphWobPusd4MBqAxy0fNhHkKaOUDk5uRc9vDs9UZw6MIgGvO/UCK4sDTTwxzcHqc7************l9gMWpSUAgKHawH77736na1ACaGa6/RAoGBAPqDknwP5IURNu7SptW8zYtSye7EEa0sqJ/CJnMCSC/KDk4K4dbcwmPsnZWAChlaHm9kQPRAwZFSC9S83ZgG3GGB/SOO0Vq6wulhlyv572Sy1REaUl9RNH+mMFxSSpb9gVM4L82/0aS0QkfzgxAH8LtU97JSgBPJISTmOkwwjDWvAoGBAN26YONTQBfPPTkREBq8XS1+g1xZLp3YQyGnEScklObqoBN/Fnml+9xBfWTLcYY+E+SUHQKuT1b6SeLXhNu10hPLnVLCLugYm1gHmrrSNrgvRLsh7JR/HaqfY6hjVJKcbuE8sMmWwNXun4w/eO04lR4hWUqaKWPIarzFyqsRTC2HAoGBAKpiHplevdB7et9ISsyIk0w1TL6mgFdGTpIKHNiQLsTCKqSN5jibAWOxJSTvZu8DSjv89eiquny6Tu2sXxLtZ6qyHJe4pU+OQs803vq8DURyFVSCkI/fzVMclzvThIPZe8I8LtdDpbTeHU6Oow+eNu9SxP11+0J5w58zL2duhY5lAoGANwWUG3VCN2ZW8/1W+Lt7h9mYMWd0GPlcl1JFAGikhrT8ajokUqWs3VhnDIcrsbtf5l4W4N5uk8/iW+dNcdQpwH3LmYjUaQYKLosci3W03HnmXWWqtqLgssGA0n1rtEIbhcswt6WwTqY7c2sg0AU1g9eUt92PndJTImsWwuK0+6sCgYAUvrIyQ4+aDOu1Hrol/vVGoaIzkibVOYeJCvg1zeKS5VHcpm32sW066VIgAIGwT72ZIhFalB/n3MoIA0sep1ZS6fC01B/ZjogQKwMlSdvyu+adX9aQRAGHgDeadBgqbEBPXixrgZWrTD9pYStXoRmfwcHqz7iUrKi8iCA+++++++';//请填写开发者私钥去头去尾去回车,一行字符串
- $aop->format = "json";
- $aop->charset = "UTF-8";
- $aop->signType = "RSA2";
- $aop->alipayrsaPublicKey = '###########iG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtxZxk98EVMDRv7ByKgmg5f9r/Q3d2rjfUpBQ+8M448PidkqwwRtZZJk8LzwryHi2Da2bWT/n+/9aNad7R7g+lvj5lKHJ2hQywl/Dfe14ck/UUfeJ3bmRE8cScSaYu9ufO4GN/Al6pyOTGgNM9dwP5Tyy2IQAfatnhoMYKGW0QkPe9igmmFa5m6lQAl7F5dt5XQZFtO5yv4BiZdnfcNicvMPFA77WwbzbBoI8Fab+Rf3pLD9q8P8EfiX5ErpHX6xl59hfqNkzZHHa5/IPHQGKm1Iy0yxfQp/5zNjmdTiFWad6DPI+wvQwqzCeIfas/aIb/AphLO4c2jr0iwbr2bsDKQIDAQAB';//请填写支付宝公钥,一行字符串
- //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
- $request = new \AlipayTradeAppPayRequest();
- //SDK已经封装掉了公共参数,这里只需要传入业务参数
- $bizcontent = json_encode([
- // "idCode" => I('idCode'),
- "body" => I('service'),
- "subject" => I('title'),
- "passback_params" => UrlEncode(I('idCode')),
- "business" => I('name'),//商家
- "out_trade_no" => $out_trade_no,
- // "idCode" => I('idCode'),
- "timeout_express" => '10m',
- "product_code" => "QUICK_MSECURITY_PAY",
- "total_amount" => $price,
- ]);
- $request->setNotifyUrl("http://0.0.0.0/app/index.php/Home/Goods/msg");//商户外网可以访问的异步回调地址
- $request->setBizContent($bizcontent);
- //这里和普通的接口调用不同,使用的是sdkExecute
- $response = $aop->sdkExecute($request);
- //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
- // echo htmlspecialchars($response);//就是orderString 可以直接给客户端请求,无需再做处理。
- $this->shows(200,'',$response);
- }
- // 支付异步回调请求
- public function msg(){
- Vendor('alisdk.AopSdk');
- $aop = new \AopClient;
- $aop->alipayrsaPublicKey = '***********QEFAAOCAQ8AMIIBCgKCAQEAtxZxk98EVMDRv7ByKgmg5f9r/Q3d2rjfUpBQ+8M448PidkqwwRtZZJk8LzwryHi2Da2bWT/n+/9aNad7R7g+lvj5lKHJ2hQywl/Dfe14ck/UUfeJ3bmRE8cScSaYu9ufO4GN/Al6pyOTGgNM9dwP5Tyy2IQAfatnhoMYKGW0***********mmFa5m6lQAl7F5dt5XQZFtO5yv4BiZdnfcNicvMPFA77WwbzbBoI8Fab+Rf3pLD9q8P8EfiX5ErpHX6xl59hfqNkzZHHa5/IPHQGKm1Iy0yxfQp/5zNjmdTiFWad6DPI+wvQwqzCeIfas/aIb/AphLO4c2jr0iwbr2bs######B';// 填写支付宝公钥,一行字符串
- $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");// 验签
- // 判断验签
- if ($flag) {#验签通过
- $trade_status = $_POST['trade_status'];
- $idCode = $_POST['passback_params'];
- $price = $_POST['total_amount'];
- $subject = json_decode($_POST['subject']);
- // 判断交易状态
- if ($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {
- #把回调信息写入文档记录起来,方便查看回调信息
- #$files = fopen("file.txt", "w");
- #fwrite($files, json_encode($_POST));
- #fclose($files);
- $res = M('Order')->where(array('idCode'=>$idCode))->setField('staus',1);
- if ($res) {
- echo "success";// 告知支付宝支付成功 请勿修改,否则支付宝会一直回调!!!
- } else {
- echo "fail";
- }
- } else {
- echo "fail";
- }
- } else {
- echo "fail";
- }
- }
复制代码 |
|