您所在的位置:小祥子 » 编程 » PHP » 正文

易宝网上支付平台的PHP接口代码

时间:2015-06-03 编辑:怪盗dark 来源:本站整理

本代码参照自韩顺平149讲视频后5讲,需要学习的朋友可以参考本代码

这是测试图片:

以下是代码部分:

 <?PHP
 
 
 function HmacMd5($data, $key)
 {
     
     //需要配置环境支撑iconv,否则中文参数不能正常处理
     $key = ("GB2312", "UTF-8", $key);
     $data = ("GB2312", "UTF-8", $data);
     $b = 64;
     if (($key) > $b) {
         $key = ("H*", ($key));
     }
     $key = ($key, $b, (0x00));
     $ipad = ('', $b, (0x36));
     $opad = ('', $b, (0x5c));
     $k_ipad = $key ^ $ipad;
     $k_opad = $key ^ $opad;
     return ($k_opad . ("H*", ($k_ipad . $data)));
 }
     //我们把易宝支付要求怎样生成一个签名串
     //把各个请求参数凭借作为$data传入: $key 就是易宝给商家分配的密钥
     
 ?>
common.php
 <html>
 <head>
 <meta http-equiv="content-type" content="text/html;charset=gb2312" />
 </head>
 <form action="payConfirm.php" method="post">
 <table>
     <tr>
         <td colspan="4">
         订单号:<input type="text" name="p2_Order" /> 
         支付金额:<input type="text" name="p3_Amt" />
         </td>
     </tr>
     <tr>
     <td colspan="4">请选择支付银行</td>
     </tr>
     <tr>
     <td><input type="radio" name="pd_FrpId" value="CMBCHINA-NET-B2C" />招商银行</td>
     <td><input type="radio" name="pd_FrpId" value="ICBC-NET-B2C" />工商银行</td>
     <td><input type="radio" name="pd_FrpId" value="ABC-NET-B2C" />农业银行</td>
     <td><input type="radio" name="pd_FrpId" value="CCB-NET-B2C" />建设银行</td>
     </tr>
     <tr>
     <td colspan="4"><input type="submit" value="确认支付" /></td>
     </tr>
 </table>
 </form>
 </html>
pay.php
 <html>
 <head>
 <meta http-equiv="content-type" content="text/html;charset=gb2312" />
 </head>
 <?php
     include 'common.php';
     // 这里我们获取用户提交的信息
 
     // 1.获取订单号
     $p0_Cmd = "Buy";
     $p1_MerId = "10001126856";
     $p2_Order = $_REQUEST['p2_Order'];
     $p3_Amt = $_REQUEST['p3_Amt'];
     $p4_Cur = "CNY";
     // 商品名称
     $p5_Pid = "";
     $p6_Pcat = ""; // 商品种类
     $p7_Pdesc = ""; // 商品介绍
     // 只是易宝支付成功后,给url返回信息
     $p8_Url = "http://loaclhost/FUCKPHP/onlinezhifu/res.php";
     $p9_SAF = "0"; // 送货地址
     $pa_MP = ""; // 额外介绍
     $pd_FrpId = $_REQUEST['pd_FrpId']; // 支付通道
     $pr_NeedResponse = "1"; // 应答机制
     // 我们把请求参数一个一个拼接(拼接的时候,顺序很重要!!)
     $data="";
     $data=$data.$p0_Cmd;
     $data=$data.$p1_MerId;
     $data=$data.$p2_Order;
     $data=$data.$p3_Amt;
     $data=$data.$p4_Cur;
     $data=$data.$p5_Pid;
     $data=$data.$p6_Pcat;
     $data=$data.$p7_Pdesc;
     $data=$data.$p8_Url;
     $data=$data.$p9_SAF;
     $data=$data.$pa_MP;
     $data=$data.$pd_FrpId;
     $data=$data.$pr_NeedResponse;
 
     $merchantKey ="69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl";
     // hmac是签名串,是用于易宝和商家互相确认的关键字
     // 这里我们需要使用算法来生成(md5-hmac算法)
     $hmac = HmacMd5($data,$merchantKey);
 ?>
             你的订单号是:<?php echo $p2_Order;  ?>支付的金额是<?php echo $p3_Amt; ?>
             <!-- 把要提交的数据用隐藏域表示 -->
 <form action="https://www.yeepay.com/app-merchant-proxy/node" method="post">
     <input type="hidden" name="p0_Cmd" value="<?php echo $p0_Cmd; ?>"/>
     <input type="hidden" name="p1_MerId" value="<?php echo $p1_MerId; ?>"/>
     <input type="hidden" name="p2_Order" value="<?php echo $p2_Order; ?>"/>
     <input type="hidden" name="p3_Amt" value="<?php echo $p3_Amt; ?>"/>
     <input type="hidden" name="p4_Cur" value="<?php echo $p4_Cur; ?>"/>
     <input type="hidden" name="p5_Pid" value="<?php echo $p5_Pid; ?>"/>
     <input type="hidden" name="p6_Pcat" value="<?php echo $p6_Pcat; ?>"/>
     <input type="hidden" name="p7_Pdesc" value="<?php echo $p7_Pdesc; ?>"/>
     <input type="hidden" name="p8_Url" value="<?php echo $p8_Url; ?>"/>
     <input type="hidden" name="p9_SAF" value="<?php echo $p9_SAF; ?>"/>
     <input type="hidden" name="pa_MP" value="<?php echo $pa_MP; ?>"/>
     <input type="hidden" name="pd_FrpId" value="<?php echo $pd_FrpId; ?>"/>
     <input type="hidden" name="pr_NeedResponse" value="<?php echo $pr_NeedResponse; ?>"/>
     <input type="hidden" name="hmac" value="<?php echo $hmac; ?>"/>
     <input type="submit" value="确认网上支付"/>
 </form>
 </html>
payConfirm.php
 <?php
     include 'common.php';
     //获取从易宝支付网关返回的信息
     $p1_MerId = "10001126856";
     $r0_Cmd = $_REQUEST['r0_Cmd'];
     $r1_Code = $_REQUEST['r1_Code'];
     $r2_TrxId = $_REQUEST['r2_TrxId'];
     $r3_Amt = $_REQUEST['r3_Amt'];
     $r4_Cur = $_REQUEST['r4_Cur'];
     $r5_Pid = $_REQUEST['r5_Pid'];
     $r6_Order = $_REQUEST['r6_Order'];
     $r7_Uid = $_REQUEST['r7_Uid'];
     $r8_MP = $_REQUEST['r8_MP'];
     $r9_BType = $_REQUEST['r9_BType'];
     $hmac = $_REQUEST['hmac'];
     
     // 拼接
     $res_src = "";
     $res_src = $res_src . $p1_MerId;
     $res_src = $res_src . $r0_Cmd;
     $res_src = $res_src . $r1_Code;
     $res_src = $res_src . $r2_TrxId;
     $res_src = $res_src . $r3_Amt;
     $res_src = $res_src . $r4_Cur;
     $res_src = $res_src . $r5_Pid;
     $res_src = $res_src . $r6_Order;
     $res_src = $res_src . $r7_Uid;
     $res_src = $res_src . $r8_MP;
     $res_src = $res_src . $r9_BType;
     $merchantKey = "69c1522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4P1";
     // 对返回的结果进行MD5-hmac加密处理,和返回的hmac签名串比较
     if (HmacMd5($res_src, $merchantKey) == $hmac) {
         if ($r1_Code == 1) {
             if ($r9_BType == 1) {
                 echo '交易成功!';
                 echo '订单号为' . $r6_Order . '支付成功!' . '所付金额是' . $r3_Amt . '易宝支付订单号' . $r2_TrxId;
                 echo '<br/>浏览器重定向';
             } elseif ($r9_BType == 2) {
                 echo 'success';
                 echo '<br/>交易成功!';
                 echo '<br/>服务器点对点通讯';
             }
         }
     } else {
         echo '签名被篡改了';
     }
 
 ?>
res.php
关键词:支付 PHP 代码