


Analysis on the callback problem of the thinkPHP framework docking with Alipay's instant payment interface
这篇文章主要介绍了thinkPHP框架对接支付宝即时到账接口回调操作,结合实例形式分析了thinkPHP针对支付宝接口回调操作的原理与具体操作步骤,需要的朋友可以参考下
本文实例讲述了thinkPHP框架对接支付宝即时到账接口回调操作。分享给大家供大家参考,具体如下:
关于支付宝即时收款接口的对接过程,很简单,也有很多人发过,我这里就不在啰嗦了,对接完成后,在线支付成功后的回调,相对来说,是个难点,,我重点分享下我的经验。
我在开发二代旅游CMS的时候,在回调的时候,也花了不少时间。
不管是支付宝接口好是微信支付接口,回调都分为跳转回调和异步通知回调,跳转回调是不保险的,加入客人支付完成后马上把支付页面关闭,没跳转,就通知不到你这个订单已经支付了,所以我们要用异步通知回调:
$alipay_config['notify_url'] = "".$ss['web_url']."/v.php/Index-alipay_notify_url.html";
首先设置介绍异步回调的地址
异步回调的具体处理函数,我这里也贴处理,供参考:
/* 支付宝异步通知*/ public function alipay_notify_url() { vendor('Alipay.Corefunction'); vendor('Alipay.Md5function'); vendor('Alipay.Notify'); vendor('Alipay.Submit'); $info=M('rewrite')->where(array('name'=>'alipay'))->find(); $info=json_decode($info['content'],true);; //↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ //合作身份者ID,签约账号,以2088开头由16位纯数字组成的字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm $alipay_config['partner'] = $info['alipay_pid']; //收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号 $alipay_config['seller_id'] = $info['alipay_pid']; // MD5密钥,安全检验码,由数字和字母组成的32位字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm $alipay_config['key'] = $info['alipay_key']; $ss=S('config'); // 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 $alipay_config['notify_url'] = "".$ss['web_url']."/v.php/Index-alipay_notify_url.html"; // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 $alipay_config['return_url'] = "".$ss['web_url']."/member.php"; //签名方式 $alipay_config['sign_type'] = strtoupper('MD5'); //字符编码格式 目前支持 gbk 或 utf-8 $alipay_config['input_charset']= strtolower('utf-8'); //ca证书路径地址,用于curl中ssl校验 //请保证cacert.pem文件在当前文件夹目录中 $alipay_config['cacert'] = getcwd().'\\cacert.pem'; //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http $alipay_config['transport'] = 'http'; // 支付类型 ,无需修改 $alipay_config['payment_type'] = "1"; // 产品类型,无需修改 $alipay_config['service'] = "create_direct_pay_by_user"; //↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ //↓↓↓↓↓↓↓↓↓↓ 请在这里配置防钓鱼信息,如果没开通防钓鱼功能,为空即可 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ // 防钓鱼时间戳 若要使用请调用类文件submit中的query_timestamp函数 $alipay_config['anti_phishing_key'] = ""; // 客户端的IP地址 非局域网的外网IP地址,如:221.0.0.1 $alipay_config['exter_invoke_ip'] = ""; $alipayNotify = new \AlipayNotify($alipay_config); $verify_result = $alipayNotify->verifyNotify(); if($verify_result) {//验证成功 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //请在这里加上商户的业务逻辑程序代 //——请根据您的业务逻辑来编写程序(以下代码仅作参考)—— //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表 //商户订单号 $out_trade_no = $_POST['out_trade_no']; //支付宝交易号 $trade_no = $_POST['trade_no']; //交易状态 $trade_status = $_POST['trade_status']; $total_fee=$_POST['total_fee']; //if( $info['alipay_pid']!=$seller_id) return ''; //收款账户不一致 $info=M('order')->where(array('no'=>$out_trade_no))->find(); if(!$info) return ''; //订单号不存在 if($_POST['trade_status'] == 'TRADE_FINISHED') { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的 //如果有做过处理,不执行商户的业务程序 $data=''; $data['status']=2; $data['pay_price']=$total_fee; $data['pay_type']='支付宝'; $data['buyer']=$_POST['buyer_email']; $data['trade_no']=$trade_no; $data['pay_time']=time(); M('order')->where(array('no'=>$out_trade_no))->save($data); $c=M('smtp_templates')->where(array('id'=>5))->find(); if($c['status']==1) { $content = str_replace('{title}', $info['goods_name'], $c['content']); $content = str_replace('{id}', $info['goods_id'], $content); $content = str_replace('{price}',$total_fee, $content); $content = str_replace('{time}',date('Y-m-d H:i:s',time()), $content); $c=M('email_note')->where(array('pay_id'=>$out_trade_no))->find(); if(!$c) { $e=''; $e['email']=$info['c_email']; $e['content']=$content; $e['pay_id']=$out_trade_no; M('email_note')->add($e); $m=explode('|',$c['ather']); foreach($m as $mail) { if(validate_email($mail)) { $e['email']=$mail; M('email_note')->add($e); } } } } //注意: //退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知 //调试用,写文本函数记录程序运行情况是否正常 //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录"); } else if ($_POST['trade_status'] == 'TRADE_SUCCESS') { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的 //如果有做过处理,不执行商户的业务程序 $data=''; $data['status']=2; $data['pay_price']=$total_fee; $data['pay_type']='支付宝'; $data['buyer']=$_POST['buyer_email']; $data['trade_no']=$trade_no; $data['pay_time']=time(); M('order')->where(array('no'=>$out_trade_no))->save($data); $c=M('smtp_templates')->where(array('id'=>5))->find(); if($c['status']==1) { $content = str_replace('{title}', $info['goods_name'], $c['content']); $content = str_replace('{id}', $info['goods_id'], $content); $content = str_replace('{price}',$total_fee, $content); $content = str_replace('{time}',date('Y-m-d H:i:s',time()), $content); $c=M('email_note')->where(array('pay_id'=>$out_trade_no))->find(); if(!$c) { $e=''; $e['email']=$info['c_email']; $e['content']=$content; $e['pay_id']=$out_trade_no; M('email_note')->add($e); $m=explode('|',$c['ather']); foreach($m as $mail) { if(validate_email($mail)) { $e['email']=$mail; M('email_note')->add($e); } } } } //注意: //付款完成后,支付宝系统发送该交易状态通知 //调试用,写文本函数记录程序运行情况是否正常 //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录"); } //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— echo "success"; //请不要修改或删除 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// } else { //验证失败 echo "fail"; //调试用,写文本函数记录程序运行情况是否正常 //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录"); } }
因为是异步通知,所以调试是个麻烦事,不能所见即所得,也就是说,一般情况下没办法echo或者print_r打印输出结果,没办法知道他执行到哪里或者执行结果,这个时候,我们就用到log文件输出,代码:
$file = './log.txt';//要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个 $content = "支付成功".$bdata['total_fee']."\n"; //要写入的内容 file_put_contents($file, $content,FILE_APPEND);//写入文件
以上代码会在根目录下自动生成一个log.txt的文件,这样,就可以轻松知道执行结果,方便调试了
支付宝的异步通知接口,会多次通知,直到你返回success为止,也就是说,你提交一个测试订单,可以测试很多次,因为你没有返回success的话,他会每隔一定时间,通知一次
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of Analysis on the callback problem of the thinkPHP framework docking with Alipay's instant payment interface. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Problem Description When calling Alipay EasySDK using PHP, after filling in the parameters according to the official code, an error message was reported during operation: "Undefined...

ThinkPHP6 routing parameters are processed in Chinese and complete acquisition. In the ThinkPHP6 framework, URL parameters containing special characters (such as Chinese and punctuation marks) are often processed...

The OKX trading platform offers a variety of rates, including transaction fees, withdrawal fees and financing fees. For spot transactions, transaction fees vary according to transaction volume and VIP level, and adopt the "market maker model", that is, the market charges a lower handling fee for each transaction. In addition, OKX also offers a variety of futures contracts, including currency standard contracts, USDT contracts and delivery contracts, and the fee structure of each contract is also different.

Using the ThinkPHP6 framework combined with elasticsearch-php client to operate Elasticsearch...

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.
