支付宝即时到账-PHP接入

发布时间:2020-06-06 21:13:14 作者:myxp
来源:网络 阅读:987

首先请到支付宝那边申请一个及时到账的接口账户,需要提交相关材料申请。然后根据即时到账的API文档进行接入。API文档提供了各种语言版本的demo,我这里是下的php版demo,然后再进行相关修改操作。你也可以将demo版本的代码重新整合,我这里暂时为了走通充值及时到账流程,就不进行代码重构了。

API文档:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7386797.0.0.eZb8FX&treeId=108&articleId=103950&docType=1


前台表单提交

   <form action="/pay/alipayapi.php" class="tm-panel-small uk-form uk-form-stacked" method="post" target="_blank">                                                          
   <div class="element" >                                                                                                                           
   </div>                                                                                                                                                                   
   <div class="uk-form-row">                                                                                                                                                
   <div class="uk-form-controls">                                                                                                                                           
           <label for="form-username" class="uk-form-label" >订单序号:</label>        
           <input  class="uk-form-width-large" type="text" name="WIDout_trade_no" id="out_trade_no" value="<?php echo trim($_GET['orderid']);?>" >                          
           <br>                                                                                                                                                             
   </div>                                                                                                                                                                   
   </div>                                                                                                                                                                   
                                                                                                                                                                            
   <div class="uk-form-row">                                                                                                                                                
   <div class="uk-form-controls">                                                                                                                                           
           <label for="form-username" class="uk-form-label" >商品名称:</label>        
           <input class="uk-form-width-large"  type="text" name="WIDsubject" value="<?php echo trim($_GET['orderid']);?>" >                                                 
           <br>                                                                                                                                                             
   </div>                                                                                                                                                                   
   </div>                                                                                                                                                                   
                                                                                                                                                                            
   <div class="uk-form-row">                                                                                                                                                
   <div class="uk-form-controls">                                                                                                                                           
           <label for="form-username" class="uk-form-label" >付款金额:</label>        
           <input class="uk-form-width-large" type="text" name="WIDtotal_fee" value="<?php echo trim($_GET['amt']);?>">                                                     
           <br>                                                                                                                                                             
   </div>                                                                                                                                                                   
   </div>                                                                                                                                                                   
                                                                                                                                                                            
   <div class="uk-form-row">                                                                                                                                                
   <div class="uk-form-controls">                                                                                                                                           
           <label for="form-username" class="uk-form-label" >商品描述:</label>        
           <input  class="uk-form-width-large" type="text" name="WIDbody" value="订单测试支付">                                                                             
           <br>                                                                                                                                                             
   </div>                                                                                                                                                                   
   </div>                                                                                                                                                                   
                                                                                                                                                                            
                                                                                                                                                                            
  <input type="hidden" name="uid" value="<?php echo $_GET['uid'];?>">                                                                                                       
  <input type="hidden" name="codes" value="<?php echo $_GET['code'];?>">                                                                                                    
  <input type="hidden" name="tags" value="<?php echo $_GET['tag'] ?>">                                                                                                      
   <div class="uk-form-row">                                                                                                                                                
     <input class="uk-button uk-button-primary uk-button-large uk-width-1-1" type="submit" class="alisubmit" value ="确认支付">                                       
   </div>                                                                                                                                                                   
  </div>                                                                                                                                                                    
  </form>

设置alipay.config.php配置文件,主要配置一下几个参数

$alipay_config['partner'] 设置签约账号ID

$alipay_config['key'] 设置MD5校验key

$alipay_config['notify_url'] 设置异步回调地址

$alipay_config['return_url'] 设置回调返回地址

$alipay_config['transport'] 设置协议类型,默认为http

$alipay_config['partner']               = 'xxxxxxxxxxxx';                                                                   
                                                                                                                            
//收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号                                        
$alipay_config['seller_id']     = $alipay_config['partner'];                                                                
                                                                                                                            
// MD5密钥,安全检验码,由数字和字母组成的32位字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm                    
$alipay_config['key']                   = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';                                               
                                                                                                                            
// 服务器异步通知页面路径  需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问                       
$alipay_config['notify_url'] = "http://xx.xx.xx.xx/pay/notify_url.php";                                                     
                                                                                                                            
// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问                      
$alipay_config['return_url'] = "http://xx.xx.xx.xx/pay/return_url.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";



交易处理完成后会发送订单信息及交易状态到你指定好的回传return_url.php中,你可以通过回传给你的订单状态判断交易是否成功,成功则进入下一步逻辑(你自己的订单处理逻辑),否则返回失败。

<?php
/* * 
 * 功能:支付宝页面跳转同步通知页面
 * 版本:3.3
 * 日期:2012-07-23 
 * 说明:
 * 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
 * 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
        
 *************************页面功能说明*************************
 * 该页面可在本机电脑测试
 * 可放入HTML等美化页面的代码、商户业务逻辑程序代码
 * 该页面可以使用PHP开发工具调试,也可以使用写文本函数logResult,该函数已被默认关闭,见alipay_notify_class.php中的函数verifyReturn
  require_once("alipay.config.php");
  require_once("lib/alipay_notify.class.php");
  ?>
  <!DOCTYPE HTML>
  <html>
      <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <?php
  //计算得出通知验证结果
  $alipayNotify = new AlipayNotify($alipay_config);
  $verify_result = $alipayNotify->verifyReturn();
  if($verify_result) {//验证成功
          /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          //请在这里加上商户的业务逻辑程序代码
  
          //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
      //获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表
  
          //商户订单号^M
          $out_trade_no = $_GET['out_trade_no'];
  
          //支付宝交易号^M
          $trade_no = $_GET['trade_no'];
  
          //交易状态
          $trade_status = $_GET['trade_status'];
  
          $total_fee = $_GET['total_fee'];
  
          //获取用户返回数据
          $user_info = $_GET['extra_common_param'];
          $user_arr = explode('.',$user_info);
          $uid = $user_arr[0];
          $code = $user_arr[1];
          $tags = $user_arr[2];
  
  
      if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
                  $types = "alipay";
                  $user_url = "/user_obj/do_orderinfo.php";
                  die("<script>;window.location='{$user_url}?uid={$uid}&code={$code}&tags={$tags}&payAmount={$total_fee}&order={$out_trade_no}&types={$types}';</script>");
  
                  //判断该笔订单是否在商户网站中已经做过处理
                  //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
                  //如果有做过处理,不执行商户的业务程序
  
  
      }
      else {
        echo "trade_status=".$_GET['trade_status'];
      }
  
  var_dump($verify_result);
          echo "验证成功<br />";
  
          //——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
  
          /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  }
  else {
      //验证失败
      //如要调试,请看alipay_notify.php页面的verifyReturn函数
      echo "验证失败";
  }
  ?>
          <title>支付宝即时到账交易接口</title>
          </head>
      <body>
      </body>
  </html>


推荐阅读:
  1. 实现php语音到账api接口的方法
  2. Android中怎么接入支付宝实现支付功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

支付宝 到账

上一篇:JavaScript 运行原理解析

下一篇:GNS3中交换机的使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》