search
HomeBackend DevelopmentPHP TutorialIntroduction to the callback method of thinkPHP framework docking with Alipay's instant payment interface

这篇文章主要介绍了thinkPHP框架对接支付宝即时到账接口回调操作,结合实例形式分析了thinkPHP针对支付宝接口回调操作的原理与具体操作步骤,需要的朋友可以参考下

本文实例讲述了thinkPHP框架对接支付宝即时到账接口回调操作。分享给大家供大家参考,具体如下:

关于支付宝即时收款接口的对接过程,很简单,也有很多人发过,我这里就不在啰嗦了,对接完成后,在线支付成功后的回调,相对来说,是个难点,,我重点分享下我的经验。

我在开发二代旅游CMS(http://www.erdaicms.com)的时候,在回调的时候,也花了不少时间。

不管是支付宝接口好是微信支付接口,回调都分为跳转回调和异步通知回调,跳转回调是不保险的,加入客人支付完成后马上把支付页面关闭,没跳转,就通知不到你这个订单已经支付了,所以我们要用异步通知回调:

$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中文网!

相关推荐:

ThinkPHP写数组插入与获取最新插入数据ID的方法

关于thinkphp实现发送邮件密码找回功能的介绍

The above is the detailed content of Introduction to the callback method of thinkPHP framework docking with Alipay's instant payment interface. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version