Home > php教程 > php手册 > body text

支付宝+银联统一支付功能实现

WBOY
Release: 2016-06-07 11:40:54
Original
1615 people have browsed it

这个model将支付宝和银行支付统一到一起,摆脱了对SDK的依赖,我尽量封在了这一个model里面,可根据自己项目需求修改。

应一些朋友要求,我把发起支付以及支付回调部分也放上来,我去掉了一些内容只留下了流程。

2015-8-14更新:很久没上这里了,发现好多消息说想要模板,我这里发出来。
PayAction:<?php <br /> class PayAction extends Action{<br>     //支付跳转<br>     function do_pay(){<br>         //构造参数<br>         $payment = array(<br>             'money'=>12,<br>             'deal_name'=>'充值',<br>             //支付银行,对应银行ID我在后面贴出来<br>             'bank_id'=>'',<br>             'notice_sn'=>'20140920123456',<br>         );<br>         echo D('Payment')->get_payment_code($payment);<br>     }<br>     <br>     //支付结果同步回调<br>     function response(){<br>         $request = $_GET;<br>         unset($request['_URL_']);<br>         $pay_res = D('Payment')->notify($request);<br> <br>         if($pay_res['status']){<br>             //支付成功业务逻辑<br>         }else{<br>             $this->error('支付失败');<br>         }<br>     }<br>     <br>     //支付结果异步回调<br>     function notify(){<br>         $request = $_POST;<br>         $pay_res = D('Payment')->notify($request);<br> <br>         if($pay_res['status']){<br>             //支付成功业务逻辑<br>             echo 'success';<br>         }else{<br>             echo 'fail';<br>         }<br>     }<br> }<br> <br> //银行ID<br> $bank_id = array(<br>     'ICBCB2C'    =>    '中国工商银行',<br>     'CMB'        =>    '招商银行',<br>     'CCB'        =>    '中国建设银行',<br>     'ABC'        =>    '中国农业银行',<br>     'SPDB'        =>    '上海浦东发展银行',<br>     'SDB'        =>    '深圳发展银行',<br>     'CIB'        =>    '兴业银行',<br>     'BJBANK'    =>    '北京银行',<br>     'CEBBANK'    =>    '中国光大银行',<br>     'CMBC'        =>    '中国民生银行',<br>     'CITIC'        =>    '中信银行',<br>     'GDB'        =>    '广东发展银行',<br>     'SPABANK'    =>    '平安银行',<br>     'BOCB2C'    =>    '中国银行',<br>     'COMM'        =>    '交通银行',<br>         'ALIPAY'    =>    '支付宝',<br> );<br> <br> ?>出现调试错误一般是参数传错了
1、配置model里面的config,model里其他东西根本不用看
2、构造请求参数$payment
money、deal_name和订单号notice_sn是你随便给的,bank_id在页面用 radio传过来的其值是我给的那些银行ID,比如工行就是ICBCB2C
支付宝+银联统一支付功能实现

PaymentModel:<?php <br /> <br> class PaymentModel extends Model{<br>     private $config;<br>     function _initialize(){<br>         $this->config = array(<br>             'alipay_partner'=>C('ALIPAY_PARTNER'),<br>             'alipay_account'=>C('ALIPAY_ACCOUNT'),<br>             'alipay_key'=>C('ALIPAY_KEY'),<br>             'return_url' => 'http://'.HOST_NAME.U('Pay/response'),<br>             'notify_url' => 'http://'.HOST_NAME.U('Pay/notify'),<br>         );<br>     }<br>     function get_payment_code($form){<br>         $link = $form['bank_id'] == 'ALIPAY' ? $this->alipay_link($form) : $this->bank_link($form);<br>         return $link;<br>     }<br>     // 银联接连<br>     function bank_link($form){<br>         $payment_notice = array(<br>             'money'=>$form['money'],<br>             'deal_name'=>$form['order_name'],<br>             'bank_id'=>$form['bank_id'],<br>             'notice_sn'=>$form['order_sn'],<br>         );<br>         $money = round($payment_notice['money'],2);<br>         $payment_info = $this->config;<br> <br>         $subject = $payment_notice['deal_name'];<br>         <br>         $data_return_url = $payment_info['return_url'];<br>         $data_notify_url = $payment_info['notify_url'];<br> <br>         $service = 'create_direct_pay_by_user';<br>         /* 银行类型 */<br>         $bank_type = $payment_notice['bank_id'];<br>         <br>         $parameter = array(<br>             'service'           => $service,<br>             'partner'           => $payment_info['alipay_partner'],<br>             //'partner'           => ALIPAY_ID,<br>             '_input_charset'    => 'utf-8',<br>             'notify_url'        => $data_notify_url,<br>             'return_url'        => $data_return_url,<br>             /* 业务参数 */<br>             'subject'           => $subject,<br>             'out_trade_no'      => $payment_notice['notice_sn'], <br>             'price'             => $money,<br>             'quantity'          => 1,<br>             'payment_type'      => 1,<br>             /* 物流参数 */<br>             'logistics_type'    => 'EXPRESS',<br>             'logistics_fee'     => 0,<br>             'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',<br>             'extend_param'    => 'changjianghu',<br>             /* 买卖双方信息 */<br>             'seller_email'      => $payment_info['alipay_account'],<br>             'defaultbank'    =>    $bank_type,<br>             'payment'    =>    'bankPay'<br>         );<br> <br>         $parameter = $this->argSort($parameter);<br> <br>         $param = '';<br>         $sign  = '';<br> <br>         foreach ($parameter AS $key => $val){<br>             $param .= "$key=" .urlencode($val). "&";<br>             $sign  .= "$key=$val&";<br>         }<br> <br>         $param = substr($param, 0, -1);<br>         $sign  = substr($sign, 0, -1). $payment_info['alipay_key'];<br>         $sign_md5 = md5($sign);<br> <br>         <br>         $payLinks = '<form>正在连接支付接口...</form>';<br>         $payLinks.='<script>document.getElementById("jumplink").submit();</script>';<br> <br>         return $payLinks;<br>     }<br>     // 支付宝链接<br>     function alipay_link($form){<br>         $payment_notice = array(<br>             'money'=>$form['money'],<br>             'deal_name'=>$form['order_name'],<br>             'bank_id'=>$form['bank_id'],<br>             'notice_sn'=>$form['order_sn'],<br>         );<br>         $money = round($payment_notice['money'],2);<br>         $payment_info = $this->config;<br> <br>         $subject = $payment_notice['deal_name'];<br>         <br>         $data_return_url = $payment_info['return_url'];<br>         $data_notify_url = $payment_info['notify_url'];<br>         <br>         $parameter = array(<br>             'service'           => 'create_direct_pay_by_user',<br>             'partner'           => $payment_info['alipay_partner'],<br>             //'partner'           => ALIPAY_ID,<br>             '_input_charset'    => 'utf-8',<br>             'notify_url'        => $data_notify_url,<br>             'return_url'        => $data_return_url,<br>             /* 业务参数 */<br>             'subject'           => $subject,<br>             'out_trade_no'      => $payment_notice['notice_sn'], <br>             'price'             => $money,<br>             'quantity'          => 1,<br>             'payment_type'      => 1,<br>             /* 物流参数 */<br>             'logistics_type'    => 'EXPRESS',<br>             'logistics_fee'     => 0,<br>             'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',<br>             'extend_param'        => 'changjianghu',<br>             /* 买卖双方信息 */<br>             'seller_email'      => $payment_info['alipay_account']<br>         );<br>         // print_r($parameter);exit;<br>         $parameter = $this->argSort($parameter);<br> <br>         $param = '';<br>         $sign  = '';<br> <br>         foreach ($parameter AS $key => $val)<br>         {<br>             $param .= "$key=" .urlencode($val). "&";<br>             $sign  .= "$key=$val&";<br>         }<br> <br>         $param = substr($param, 0, -1);<br>         $sign  = substr($sign, 0, -1). $payment_info['alipay_key'];<br>         $sign_md5 = md5($sign);<br> <br>         <br>         $payLinks = '<form>正在连接支付接口...</form>';<br>         $payLinks.='<script>document.getElementById("jumplink").submit();</script>';<br>         <br>         return $payLinks;<br>     }<br> <br> /*结果*/<br>     function notify($request){<br>         $return_res = array(<br>             'info'=>'',<br>             'status'=>false,<br>         );<br>         $payment = $this->config;<br>         <br>         $request = $this->argSort($request);<br> <br>         /* 检查数字签名是否正确 */<br>         $isSign = $this->getSignVeryfy($request);<br>         if (!$isSign){//签名验证失败<br>             $return_res['info'] = '签名验证失败';<br>             return $return_res;<br>         }<br> <br>         if ($request['trade_status'] == 'TRADE_SUCCESS' || $request['trade_status'] == 'TRADE_FINISHED' || $request['trade_status'] == 'WAIT_SELLER_SEND_GOODS' || $request['trade_status'] == 'WAIT_BUYER_CONFIRM_GOODS'){<br>             <br>             $return_res['status'] = true;<br>             <br>         }<br>         return $return_res;<br>     }<br> <br>     // 获取返回时的签名验证结果<br>     function getSignVeryfy($para_temp) {<br>         //除去待签名参数数组中的空值和签名参数<br>         $para_filter = $this->paraFilter($para_temp);<br>         //对待签名参数数组排序<br>         $para_sort = $this->argSort($para_filter);<br>         //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串<br>         $prestr = $this->createLinkstring($para_sort);<br>         <br>         $isSgin = false;<br>         $isSgin = $this->md5Verify($prestr, $para_temp['sign'], $this->config['alipay_key']);<br>         return $isSgin;<br>     }<br>     // 验证签名<br>     function md5Verify($prestr, $sign, $key) {<br>         $prestr = $prestr . $key;<br>         $mysgin = md5($prestr);<br> <br>         if($mysgin == $sign) {<br>             return true;<br>         }<br>         else {<br>             return false;<br>         }<br>     }<br>     // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串<br>     function createLinkstring($para) {<br>         $arg  = "";<br>         while (list ($key, $val) = each ($para)) {<br>             $arg.=$key."=".$val."&";<br>         }<br>         //去掉最后一个&字符<br>         $arg = substr($arg,0,count($arg)-2);<br>         <br>         //如果存在转义字符,那么去掉转义<br>         if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}<br>         <br>         return $arg;<br>     }<br>     // 除去数组中的空值和签名参数<br>     function paraFilter($para) {<br>         $para_filter = array();<br>         while (list ($key, $val) = each ($para)) {<br>             if($key == "sign" || $key == "sign_type" || $val == "")continue;<br>             else    $para_filter[$key] = $para[$key];<br>         }<br>         return $para_filter;<br>     }<br>     // 对数组排序<br>     function argSort($para) {<br>         ksort($para);<br>         reset($para);<br>         return $para;<br>     }<br>     <br> }<br> <br> ?>HTML模板:nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br> <br> <br> <meta> <br> <meta> <br> <meta> <br> <title>预购一个梦想 - 唱江湖音乐众筹网</title> <br> <meta> <br> <meta> <br> <link> <br> <link> <br> <link> <br> <script></script><br> <br> <br> <br>     <br> <div> <br>     <div> <br>         <div> <br>                             <div> <br>                                 <img alt="支付宝+银联统一支付功能实现" ><br> <map><br>     <area> <br>     <area> <br>     <area> <br>     <area> <br> </map>            </div> <br> <br>         </div> <br>         <ul> <br>                                     <li> <br>                         <span><br>                         <a>首页</a><br>                         </span>        <br>                     </li> <br>                                     <li> <br>                         <span><br>                         <a>音乐众筹</a><br>                         </span>        <br>                     </li> <br>                                     <li> <br>                         <span><br>                         <a>秒杀</a><br>                         </span>        <br>                     </li> <br>                                     <li> <br>                         <span><br>                         <a>帮助中心</a><br>                         </span>        <br>                     </li> <br>                                 <li> <br>                     <span><br>                             <a>发起众筹</a><br>                     </span><br>                      <span><br>                             <a>发起秒杀</a><br>                     </span><br>                 </li> <br>         </ul> <br>         <div> <br>             <div>    <br>                     <a>消息</a>  |  <br>     <a>Badri</a>  |          <br>     <a>登出</a><br>     <br>     <br>     <div> <br>         <div> <br>             <span><a>关注动态</a></span><br>             <span><a>查看评论</a></span><br>             <span><a>查看私信</a></span><br>             <span><a>查看通知</a></span><br> <br>         </div> <br>     </div> <br>     <div> <br>         <div> <br>             <span><a>我的主页</a></span><br>             <span><a>项目管理</a></span><br>             <span><a>发起项目</a></span><br>             <span><a>秒杀管理</a></span><br>             <span><a>发起秒杀</a></span><br>             <span><a>个人设置</a></span><br> <br>         </div> <br>     </div> <br>     <br>             </div>            <br>             <form> <br>             <div> <br>             <input><br>             <input><br>             <input>                <br>             </div> <br>             </form>    <br>             <br>     <br>             <br>         </div> <br>         <br>         <br> <br>         <br>     </div>        <br> </div> <br> <br> <link> <br> <script></script><br> <div></div> <br> <br> <div> <br>     <div> <br>         <div> <br>             专为音乐人设计的便携式监听音箱iLoud            <div> <br>                 <font><br>                 支持金额  ¥1990.00            <br>                         <br>                 邮费:¥15.00<br>                                 </font><br>                 应付总额 <span>¥2002.00</span> 元    <br>             </div> <br>         </div> <br>         <br>         <div></div> <br>         <br>         <div></div> <br>         <br>         <div> <br>             <br>             <form>                                    <br>                 <br>                                     <div> <br>                         <div></div> <br>                         <label>使用余额支付:</label><br>                         <input><br>                         <label>可用余额:¥82,800.00</label><br>                         <input><br>                         <input><br>                         <div></div> <br>                     </div> <br>                                         <br>                 <div> <style>.alibank_types{float:left; display:block; background:url(http://zc.changjianghu.com/system/payment/AlipayBank/banklogo.gif); font-size:0px; width:150px; height:10px; text-align:left; padding:15px 0px;}.bk_typeCMB{background-position:15px -444px; }.bk_typeICBCB2C{background-position:15px -404px; }.bk_typeCCB{background-position:15px -84px; }.bk_typeABC{background-position:15px -44px; }.bk_typeSPDB{background-position:15px -364px; }.bk_typeSDB{background-position:15px -324px; }.bk_typeCIB{background-position:15px -484px; }.bk_typeBJBANK{background-position:15px -610px; }.bk_typeCEBBANK{background-position:15px -124px; }.bk_typeCMBC{background-position:15px -164px; }.bk_typeCITIC{background-position:15px -284px; }.bk_typeGDB{background-position:15px -244px; }.bk_typeSPABANK{background-position:15px -903px; }.bk_typeBOCB2C{background-position:15px -939px; }.bk_typeCOMM{background-position:15px -204px; }.bk_typeICBCBTB{background-position:15px -782px; }.bk_typePSBC-DEBIT{background-position:15px -524px; }</style> <script>function set_bank(bank_id){$("input[name=&#039;bank_id&#039;]").val(bank_id);}</script><label><input></label><label><input></label><label><input></label><label><input></label><label><input></label><label><input></label><label><input></label><label><input></label><label><input></label><label><input></label><label><input></label><label><input></label><label><input></label><label><input></label><label><input></label><input><div></div> </div> <div> <div> <input> 支付宝即时到帐支付:</div> <div><img alt="支付宝+银联统一支付功能实现" ></div> <div></div> <div></div> </div>                <br>                 <div></div> <br>                 <div> <br>                     <div> <br>                         <div> <br>                             <span>确定,去付款</span><br>                         </div> <br>                     </div>                <br>                     <input>    <br>                     <input>    <br>                     <input><br>                     <input>    <br>                     <input>                    <br>                     <div></div> <br>                 </div> <br>                 <br>             </form> <br>             <br>         </div> <!--left--><br>         <div> <br>                 <br>                 <div> <br>                     <div> <br>                     <img alt="支付宝+银联统一支付功能实现" ><br>                     <div></div> <br>                     <a>专为音乐人设计的便携式监听音箱iLoud</a><br>                     <div></div> <br>                     <a>百音堂</a><br> <br>                     </div> <br>                     <div> <br>                         <div></div> <br>                     </div> <br>                     <div> <br>                         <div> <br>                             <span></span><br>                         </div> <br>                         <div></div> <br>                         <div> <span>90%</span><span>剩余</span> </div> <br>                         <div> <span><font>9</font>份</span><span>剩余份数</span> </div> <br>                         <div> <span><font>1990</font>元</span><span>价格</span> </div> <br>                 <br>                         <div></div> <br>                     </div> <br>                 </div> <br>                 <br>                 <br>                 <br>             </div> <br>         <br>         <div></div> <br>         <br>     </div> <br> </div> <br> <div></div> <br> <br> <div></div> <br> <div> <br>     <div> <br>         <div> <br>             <dl> <br>                 <h3>来自唱江湖的信息:</h3> <br>                 <dd><!-- --></dd> <br> </dl> </div> </div> </div>

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template