Home  >  Article  >  Backend Development  >  PHP interface code of Yibao online payment platform

PHP interface code of Yibao online payment platform

WBOY
WBOYOriginal
2016-08-08 09:23:201597browse

This code refers to the last 5 lectures from Han Shunping’s video lecture 149. Friends who need to learn can refer to this code

This is the test picture:

The following is the code part:

<span> 1</span> <?<span>php
</span><span> 2</span><span> 3</span><span> 4</span><span>function</span> HmacMd5(<span>$data</span>, <span>$key</span><span>)
</span><span> 5</span><span>{
</span><span> 6</span><span> 7</span><span>//</span><span>需要配置环境支撑iconv,否则中文参数不能正常处理</span><span> 8</span><span>$key</span> = <span>iconv</span>("GB2312", "UTF-8", <span>$key</span><span>);
</span><span> 9</span><span>$data</span> = <span>iconv</span>("GB2312", "UTF-8", <span>$data</span><span>);
</span><span>10</span><span>$b</span> = 64<span>;
</span><span>11</span><span>if</span> (<span>strlen</span>(<span>$key</span>) > <span>$b</span><span>) {
</span><span>12</span><span>$key</span> = <span>pack</span>("H*", <span>md5</span>(<span>$key</span><span>));
</span><span>13</span><span>    }
</span><span>14</span><span>$key</span> = <span>str_pad</span>(<span>$key</span>, <span>$b</span>, <span>chr</span>(0x00<span>));
</span><span>15</span><span>$ipad</span> = <span>str_pad</span>('', <span>$b</span>, <span>chr</span>(0x36<span>));
</span><span>16</span><span>$opad</span> = <span>str_pad</span>('', <span>$b</span>, <span>chr</span>(0x5c<span>));
</span><span>17</span><span>$k_ipad</span> = <span>$key</span> ^ <span>$ipad</span><span>;
</span><span>18</span><span>$k_opad</span> = <span>$key</span> ^ <span>$opad</span><span>;
</span><span>19</span><span>return</span><span>md5</span>(<span>$k_opad</span> . <span>pack</span>("H*", <span>md5</span>(<span>$k_ipad</span> . <span>$data</span><span>)));
</span><span>20</span><span>}
</span><span>21</span><span>//</span><span>我们把易宝支付要求怎样生成一个签名串
</span><span>22</span><span>    //把各个请求参数凭借作为$data传入: $key 就是易宝给商家分配的密钥</span><span>23</span><span>24</span> ?>
common.php

<span> 1</span> <html>
<span> 2</span> <head>
<span> 3</span> <meta http-equiv="content-type" c />
<span> 4</span> </head>
<span> 5</span> <form action="payConfirm.php" method="post">
<span> 6</span> <table>
<span> 7</span>     <tr>
<span> 8</span>         <td colspan="4">
<span> 9</span>         订单号:<input type="text" name="p2_Order" /> 
<span>10</span>         支付金额:<input type="text" name="p3_Amt" />
<span>11</span>         </td>
<span>12</span>     </tr>
<span>13</span>     <tr>
<span>14</span>     <td colspan="4">请选择支付银行</td>
<span>15</span>     </tr>
<span>16</span>     <tr>
<span>17</span>     <td><input type="radio" name="pd_FrpId" value="CMBCHINA-NET-B2C" />招商银行</td>
<span>18</span>     <td><input type="radio" name="pd_FrpId" value="ICBC-NET-B2C" />工商银行</td>
<span>19</span>     <td><input type="radio" name="pd_FrpId" value="ABC-NET-B2C" />农业银行</td>
<span>20</span>     <td><input type="radio" name="pd_FrpId" value="CCB-NET-B2C" />建设银行</td>
<span>21</span>     </tr>
<span>22</span>     <tr>
<span>23</span>     <td colspan="4"><input type="submit" value="确认支付" /></td>
<span>24</span>     </tr>
<span>25</span> </table>
<span>26</span> </form>
<span>27</span> </html>
pay.php

<span> 1</span> <html>
<span> 2</span> <head>
<span> 3</span> <meta http-equiv="content-type" c />
<span> 4</span> </head>
<span> 5</span> <?<span>php
</span><span> 6</span><span>include</span> 'common.php'<span>;
</span><span> 7</span><span>//</span><span> 这里我们获取用户提交的信息
</span><span> 8</span><span> 9</span><span>    // 1.获取订单号</span><span>10</span><span>$p0_Cmd</span> = "Buy"<span>;
</span><span>11</span><span>$p1_MerId</span> = "10001126856"<span>;
</span><span>12</span><span>$p2_Order</span> = <span>$_REQUEST</span>['p2_Order'<span>];
</span><span>13</span><span>$p3_Amt</span> = <span>$_REQUEST</span>['p3_Amt'<span>];
</span><span>14</span><span>$p4_Cur</span> = "CNY"<span>;
</span><span>15</span><span>//</span><span> 商品名称</span><span>16</span><span>$p5_Pid</span> = ""<span>;
</span><span>17</span><span>$p6_Pcat</span> = ""; <span>//</span><span> 商品种类</span><span>18</span><span>$p7_Pdesc</span> = ""; <span>//</span><span> 商品介绍
</span><span>19</span><span>    // 只是易宝支付成功后,给url返回信息</span><span>20</span><span>$p8_Url</span> = "http://loaclhost/FUCKPHP/onlinezhifu/res.php"<span>;
</span><span>21</span><span>$p9_SAF</span> = "0"; <span>//</span><span> 送货地址</span><span>22</span><span>$pa_MP</span> = ""; <span>//</span><span> 额外介绍</span><span>23</span><span>$pd_FrpId</span> = <span>$_REQUEST</span>['pd_FrpId']; <span>//</span><span> 支付通道</span><span>24</span><span>$pr_NeedResponse</span> = "1"; <span>//</span><span> 应答机制
</span><span>25</span><span>    // 我们把请求参数一个一个拼接(拼接的时候,顺序很重要!!)</span><span>26</span><span>$data</span>=""<span>;
</span><span>27</span><span>$data</span>=<span>$data</span>.<span>$p0_Cmd</span><span>;
</span><span>28</span><span>$data</span>=<span>$data</span>.<span>$p1_MerId</span><span>;
</span><span>29</span><span>$data</span>=<span>$data</span>.<span>$p2_Order</span><span>;
</span><span>30</span><span>$data</span>=<span>$data</span>.<span>$p3_Amt</span><span>;
</span><span>31</span><span>$data</span>=<span>$data</span>.<span>$p4_Cur</span><span>;
</span><span>32</span><span>$data</span>=<span>$data</span>.<span>$p5_Pid</span><span>;
</span><span>33</span><span>$data</span>=<span>$data</span>.<span>$p6_Pcat</span><span>;
</span><span>34</span><span>$data</span>=<span>$data</span>.<span>$p7_Pdesc</span><span>;
</span><span>35</span><span>$data</span>=<span>$data</span>.<span>$p8_Url</span><span>;
</span><span>36</span><span>$data</span>=<span>$data</span>.<span>$p9_SAF</span><span>;
</span><span>37</span><span>$data</span>=<span>$data</span>.<span>$pa_MP</span><span>;
</span><span>38</span><span>$data</span>=<span>$data</span>.<span>$pd_FrpId</span><span>;
</span><span>39</span><span>$data</span>=<span>$data</span>.<span>$pr_NeedResponse</span><span>;
</span><span>40</span><span>41</span><span>$merchantKey</span> ="69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl"<span>;
</span><span>42</span><span>//</span><span> hmac是签名串,是用于易宝和商家互相确认的关键字
</span><span>43</span><span>    // 这里我们需要使用算法来生成(md5-hmac算法)</span><span>44</span><span>$hmac</span> = HmacMd5(<span>$data</span>,<span>$merchantKey</span><span>);
</span><span>45</span> ?>
<span>46</span>             你的订单号是:<?php <span>echo</span><span>$p2_Order</span>;  ?>支付的金额是<?php <span>echo</span><span>$p3_Amt</span>; ?>
<span>47</span>             <!-- 把要提交的数据用隐藏域表示 -->
<span>48</span> <form action="https://www.yeepay.com/app-merchant-proxy/node" method="post">
<span>49</span>     <input type="hidden" name="p0_Cmd" value="<?php echo <span>$p0_Cmd</span>; ?>"/>
<span>50</span>     <input type="hidden" name="p1_MerId" value="<?php echo <span>$p1_MerId</span>; ?>"/>
<span>51</span>     <input type="hidden" name="p2_Order" value="<?php echo <span>$p2_Order</span>; ?>"/>
<span>52</span>     <input type="hidden" name="p3_Amt" value="<?php echo <span>$p3_Amt</span>; ?>"/>
<span>53</span>     <input type="hidden" name="p4_Cur" value="<?php echo <span>$p4_Cur</span>; ?>"/>
<span>54</span>     <input type="hidden" name="p5_Pid" value="<?php echo <span>$p5_Pid</span>; ?>"/>
<span>55</span>     <input type="hidden" name="p6_Pcat" value="<?php echo <span>$p6_Pcat</span>; ?>"/>
<span>56</span>     <input type="hidden" name="p7_Pdesc" value="<?php echo <span>$p7_Pdesc</span>; ?>"/>
<span>57</span>     <input type="hidden" name="p8_Url" value="<?php echo <span>$p8_Url</span>; ?>"/>
<span>58</span>     <input type="hidden" name="p9_SAF" value="<?php echo <span>$p9_SAF</span>; ?>"/>
<span>59</span>     <input type="hidden" name="pa_MP" value="<?php echo <span>$pa_MP</span>; ?>"/>
<span>60</span>     <input type="hidden" name="pd_FrpId" value="<?php echo <span>$pd_FrpId</span>; ?>"/>
<span>61</span>     <input type="hidden" name="pr_NeedResponse" value="<?php echo <span>$pr_NeedResponse</span>; ?>"/>
<span>62</span>     <input type="hidden" name="hmac" value="<?php echo <span>$hmac</span>; ?>"/>
<span>63</span>     <input type="submit" value="确认网上支付"/>
<span>64</span> </form>
<span>65</span> </html>
payConfirm.php

<span> 1</span> <?<span>php
</span><span> 2</span><span>include</span> 'common.php'<span>;
</span><span> 3</span><span>//</span><span>获取从易宝支付网关返回的信息</span><span> 4</span><span>$p1_MerId</span> = "10001126856"<span>;
</span><span> 5</span><span>$r0_Cmd</span> = <span>$_REQUEST</span>['r0_Cmd'<span>];
</span><span> 6</span><span>$r1_Code</span> = <span>$_REQUEST</span>['r1_Code'<span>];
</span><span> 7</span><span>$r2_TrxId</span> = <span>$_REQUEST</span>['r2_TrxId'<span>];
</span><span> 8</span><span>$r3_Amt</span> = <span>$_REQUEST</span>['r3_Amt'<span>];
</span><span> 9</span><span>$r4_Cur</span> = <span>$_REQUEST</span>['r4_Cur'<span>];
</span><span>10</span><span>$r5_Pid</span> = <span>$_REQUEST</span>['r5_Pid'<span>];
</span><span>11</span><span>$r6_Order</span> = <span>$_REQUEST</span>['r6_Order'<span>];
</span><span>12</span><span>$r7_Uid</span> = <span>$_REQUEST</span>['r7_Uid'<span>];
</span><span>13</span><span>$r8_MP</span> = <span>$_REQUEST</span>['r8_MP'<span>];
</span><span>14</span><span>$r9_BType</span> = <span>$_REQUEST</span>['r9_BType'<span>];
</span><span>15</span><span>$hmac</span> = <span>$_REQUEST</span>['hmac'<span>];
</span><span>16</span><span>17</span><span>//</span><span> 拼接</span><span>18</span><span>$res_src</span> = ""<span>;
</span><span>19</span><span>$res_src</span> = <span>$res_src</span> . <span>$p1_MerId</span><span>;
</span><span>20</span><span>$res_src</span> = <span>$res_src</span> . <span>$r0_Cmd</span><span>;
</span><span>21</span><span>$res_src</span> = <span>$res_src</span> . <span>$r1_Code</span><span>;
</span><span>22</span><span>$res_src</span> = <span>$res_src</span> . <span>$r2_TrxId</span><span>;
</span><span>23</span><span>$res_src</span> = <span>$res_src</span> . <span>$r3_Amt</span><span>;
</span><span>24</span><span>$res_src</span> = <span>$res_src</span> . <span>$r4_Cur</span><span>;
</span><span>25</span><span>$res_src</span> = <span>$res_src</span> . <span>$r5_Pid</span><span>;
</span><span>26</span><span>$res_src</span> = <span>$res_src</span> . <span>$r6_Order</span><span>;
</span><span>27</span><span>$res_src</span> = <span>$res_src</span> . <span>$r7_Uid</span><span>;
</span><span>28</span><span>$res_src</span> = <span>$res_src</span> . <span>$r8_MP</span><span>;
</span><span>29</span><span>$res_src</span> = <span>$res_src</span> . <span>$r9_BType</span><span>;
</span><span>30</span><span>$merchantKey</span> = "69c1522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4P1"<span>;
</span><span>31</span><span>//</span><span> 对返回的结果进行MD5-hmac加密处理,和返回的hmac签名串比较</span><span>32</span><span>if</span> (HmacMd5(<span>$res_src</span>, <span>$merchantKey</span>) == <span>$hmac</span><span>) {
</span><span>33</span><span>if</span> (<span>$r1_Code</span> == 1<span>) {
</span><span>34</span><span>if</span> (<span>$r9_BType</span> == 1<span>) {
</span><span>35</span><span>echo</span> '交易成功!'<span>;
</span><span>36</span><span>echo</span> '订单号为' . <span>$r6_Order</span> . '支付成功!' . '所付金额是' . <span>$r3_Amt</span> . '易宝支付订单号' . <span>$r2_TrxId</span><span>;
</span><span>37</span><span>echo</span> '<br/>浏览器重定向'<span>;
</span><span>38</span>             } <span>elseif</span> (<span>$r9_BType</span> == 2<span>) {
</span><span>39</span><span>echo</span> 'success'<span>;
</span><span>40</span><span>echo</span> '<br/>交易成功!'<span>;
</span><span>41</span><span>echo</span> '<br/>服务器点对点通讯'<span>;
</span><span>42</span><span>            }
</span><span>43</span><span>        }
</span><span>44</span>     } <span>else</span><span> {
</span><span>45</span><span>echo</span> '签名被篡改了'<span>;
</span><span>46</span><span>    }
</span><span>47</span><span>48</span> ?>
res.php

The above introduces the PHP interface code of Yibao online payment platform, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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