助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
>[danger] 注意:如果支付失败,请到TP日志中查看错误信息,支付错误提示格式如下: > JSAPI支付错误 =======pay(WxPayJs) err: > Native扫码支付错误 =======pay(WxNative) err: > H5支付错误 =======pay(WxMWEB_H5) err: > Micropay 刷卡支付错误 =======pay(WxMICROPAY) err: >[info]支付API,使用连接调起微信接口 微信支付: ``` {:url('accounts/Pay/pay',['payType'=>'WX','body'=>'cowcms','attach'=>'cowcms','out_trade_no'=>"1111",'total_fee'=>1])} ``` 微信H5支付: ``` {:url('accounts/Pay/pay',['payType'=>'WX_H5','body'=>'cowcms','attach'=>'cowcms','out_trade_no'=>"1111",'total_fee'=>1])} ``` 微信扫码支付: ``` {:url('accounts/Pay/pay',['payType'=>'WX_Native','body'=>'cowcms','attach'=>'cowcms','out_trade_no'=>"1111",'total_fee'=>1,'product_id'=>0])} ``` >[info]【智能识别支付】在手机网页端使用H5支付,直接调起微信支付,在手机以外,如电脑端使用扫描支付,显示二维码支付码,在微信中调起微信支付,智能识别当前所在的支付环境,选择对应的支付方式 * [x] 第一步 :在你需要支付的页面中调用调起微信支付的 JS-SDK如果不是在微信环境下支付,可以跳过这步 ``` <script type="text/javascript" src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script> ``` * [x] 第二步 :在你需要支付的页面中编写如下代码 ``` <script> $(function($) { var payTypeUrl = "{:url('accounts/Pay/get_pay_type')}";//获取支付方式的连接,固定不变 /*==========================编写支付方式的选择框===========================*/ $.post(payTypeUrl,{},function(data){ if(data) { var payTypeStr = "",checkedStr = ""; for(var i=0 ;i < data.length; i++) { checkedStr = i==0 ? "checked" : ""; payTypeStr += "<input type='radio' name='payType' value='"+data[i].payType+"' title='"+data[i].title+"' "+checkedStr+">" +data[i].payType } $('#payType').html(payTypeStr); } },"json"); </script> ``` >{payType:payType,body:BODY,attach:ATTACH,out_trade_no:OUT_TRADE_NO,total_fee:TOTAL_FEE} 参数说明 | 参数 | 类型 | 默认 | 描述 | | --- | --- |--- |--- | | payType(**必填**) | String(128) | WX:微信支付, WX_H5:微信H5支付,WX_Native:微信扫描支付| 支付类型| | body | String(128) | 产品消费__会员 | 商品描述| | attach | String(128) | PRO_1 | 附加数据,在查询API和支付通知中原样返回,可作为自定义参数使用 可以附带产品id或者其它| | out_trade_no | String(32) | 20150806125346 | cowcms系统产生的订单号, 且唯一 | | total_fee | INT | 100| 订单总金额,单位为分 >[info]【微信支付】如何使用JSAPI支付 * [x] 第一步 :控制器的php代码 ``` $param = array( 'body'=>'产品消费__会员充值', 'attach'=>'recharge', 'out_trade_no'=>"cowcms_123456", 'total_fee'=>1 ); $wxPayJs = pay('WX',$param); $this->assign('jsApiParameters',$wxPayJs[0]); ``` >$param 参数说明 | 参数 | 类型 | 默认 | 描述 | | --- | --- |--- |--- | | body(**必填**) | String(128) | 产品消费__会员 | 商品描述| | attach (**必填**)| String(128) | PRO_1 | 附加数据,在查询API和支付通知中原样返回,可作为自定义参数使用 可以附带产品id或者其它| | out_trade_no(**必填**) | String(32) | 20150806125346 | cowcms系统产生的订单号, 且唯一 | | total_fee (**必填**)| INT | 100| 订单总金额,单位为分 | time_start | String(14) | 20091225091010 | 订单生成时间,格式为yyyyMMddHHmmss,默认为当前时间| | time_expire | String(14) | 20091225091010 | 订单失效时间,格式为yyyyMMddHHmmss,默认为下单时间加5分钟| | goods_tag| String(14) | WXG | 订单优惠标记,使用代金券或立减优惠功能时需要的参数| | openid| String(32) | oUpF8uMuAJO_M2pxb1Q9zNjWeS6o | 此参数为微信用户在商户对应appid下的唯一标识,可以不填| * [x] 第二步 :模板中JS代码 ``` //调用微信JS api 支付 function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', <?php echo $jsApiParameters; ?>, function(res){ if(res.err_msg == "get_brand_wcpay_request:ok" ){ alert("支付成功") } else { alert("错误码:"+res.err_desc+"错误信息"+res.err_msg); } } ); } function callpay() { if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } ``` * [x] 第三步 :在你想触发支付的按钮上,加入 onClick="callpay()" 属性,如: ``` <button type="button" onClick="callpay()" >立即支付</button> ``` 等同于打开以下连接 ``` {:url('systems/Pay/pay',['payType'=>'WX','body'=>'产品消费__会员充值','attach'=>'recharge','out_trade_no'=>"cowcms_123456",'total_fee'=>1])} ``` >[info]【微信支付】如何使用Native(扫码)支付 * [x] 第一步 :控制器的php代码 ``` $param = array( 'body'=>'产品消费__会员充值', 'attach'=>'recharge', 'out_trade_no'=>"cowcms_123456", 'total_fee'=>1, 'product_id'=>3, ); $url = pay('WX_Native',$param); $qrcodeUrl = Request()->domain().url("systems/Qrcode/qrcode",['text'=>urlencode($url) ] ); $this->assign('qrcodeUrl', $qrcodeUrl ); ``` >$param 参数说明 | 参数 | 类型 | 默认 | 描述 | | --- | --- |--- |--- | | body(**必填**) | String(128) | 产品消费__会员 | 商品描述| | attach (**必填**)| String(128) | PRO_1 | 附加数据,在查询API和支付通知中原样返回,可作为自定义参数使用 可以附带产品id或者其它| | out_trade_no(**必填**) | String(32) | 20150806125346 | cowcms系统产生的订单号, 且唯一 | | total_fee (**必填**)| INT | 100| 订单总金额,单位为分 | | product_id (**必填**)| String(32) | cowcms_0| 此id为二维码中包含的商品ID,商户自行定义| | time_start | String(14) | 20091225091010 | 订单生成时间,格式为yyyyMMddHHmmss,默认为当前时间| | time_expire | String(14) | 20091225091010 | 订单失效时间,格式为yyyyMMddHHmmss,默认为下单时间加5分钟| | goods_tag| String(14) | WXG | 订单优惠标记,使用代金券或立减优惠功能时需要的参数| * [x] 第二步 :模板中显示二维码图片 ``` <img src="{$qrcodeUrl}l" style="width:150px;height:150px;"/> ``` 等同于打开以下连接 ``` {:url('systems/Pay/pay',['payType'=>'WX_Native','body'=>'产品消费__会员充值','attach'=>'recharge','out_trade_no'=>"cowcms_123456",'total_fee'=>1,,'product_id'=>3,'qrcode_width'=>150])} ``` 注意:url("systems/Qrcode/qrcode",['text'=>urlencode($url) ] ),该连接可以生成一个临时二维码,连接所传参数为 text:生成的二维码所包含的内容,可以是连接也可以是文本内容 size:生成二维码的大小,,默认为 4 level:二维码的容错率,默认"L", L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%); margin:二维码周围边框空白区域间距值,默认 2 >[info]【微信支付】如何使用MWEB(H5)支付 * [x] 第一步 :控制器的php代码 ``` $param = array( 'body'=>'产品消费__会员充值', 'attach'=>'recharge', 'out_trade_no'=>"cowcms_123456", 'total_fee'=>1, 'redirect_url'=>"http://tp.tybxkj.cc/public/index.php", ); $wxPayH5 = pay('WX_H5',$param); if($wxPayH5 ) { $mweb_url= $wxPayH5 ; $this->assign('mweb_url', $mweb_url ); } ``` >$param 参数说明 | 参数 | 类型 | 默认 | 描述 | | --- | --- |--- |--- | | body(**必填**) | String(128) | 产品消费__会员 | 商品描述| | attach (**必填**)| String(128) | PRO_1 | 附加数据,在查询API和支付通知中原样返回,可作为自定义参数使用 可以附带产品id或者其它| | out_trade_no(**必填**) | String(32) | 20150806125346 | cowcms系统产生的订单号, 且唯一 | | total_fee (**必填**)| INT | 100| 订单总金额,单位为分 | | scene_info (**必填**)| String(256) | IOS移动应用{"h5_info": {"type":"IOS","app_name": "王者荣耀","bundle_id": "com.tencent.wzryIOS"}} 安卓移动应用{"h5_info": {"type":"Android","app_name": "王者荣耀","package_name": "com.tencent.tmgp.sgame"}}}WAP网站应用{"h5_info": {"type":"Wap","wap_url": "http://tp.tybxkj.cc","wap_name": "COWCMS"}} | 该字段用于上报支付的场景信息,针对H5支付有以下三种场景,请根据对应场景上报,H5支付不建议在APP端使用,针对场景1,2请接入APP支付,不然可能会出现兼容性问题,1,IOS移动应用{"h5_info":"h5_info" {"type": "","app_name": "","bundle_id": ""}| | redirect_url| String(256) | 当前支付页面 | H5支付完成或者取消后,跳转到那个页面,默认跳转到当前支付页面| | time_start | String(14) | 20091225091010 | 订单生成时间,格式为yyyyMMddHHmmss,默认为当前时间| | time_expire | String(14) | 20091225091010 | 订单失效时间,格式为yyyyMMddHHmmss,默认为下单时间加5分钟| | goods_tag| String(14) | WXG | 订单优惠标记,使用代金券或立减优惠功能时需要的参数| * [x] 第二步 :模板中触发 ``` <a href="{$mweb_url}">确认支付</a> ``` 等同于打开以下连接 ``` {:url('systems/Pay/pay',['payType'=>'WX_H5'','body'=>'产品消费__会员充值','attach'=>'recharge','out_trade_no'=>"cowcms_123456",'total_fee'=>1, 'redirect_url'=>"http://tp.tybxkj.cc/public/index.php")} ``` >[danger]注意:模版中主要是实现跳转$mweb_url连接,目的是调起微信支付面板输入密码,可以在代码中实现跳转, 如果wxPayH5($param)执行成功会返回['mweb_url'=>'URL'], 如果执行中微信有错误信息返回 ['return_code'=>'FAIL','return_msg'=>'错误信息'], 如果wxPayH5($param)失败返回false. >[info]【微信支付】如何使用MICROPAY(商家刷卡)支付 * [x] 第一步 :创建一个表单 ``` <form action="#" method="post"> <div style="margin-left:2%;">支付金额:</div><br/> <input type="text" style="width:96%;height:35px;margin-left:2%;" readonly value="1" name="Total_fee" /><br /><br /> <div style="margin-left:2%;">授权码:</div><br/> <input type="text" style="width:96%;height:35px;margin-left:2%;" name="auth_code" /><br /><br /> <div align="center"> <input type="submit" value="提交刷卡" type="button" /> </div> </form> ``` * [x] 第二步 :在授权码的输入框,用扫码器获取用户收付款二维码或条形码的授权码,然后提交表单到PHP页面,代码如下: ``` if(request()->isPost()) { $Total_fee = request()->param('Total_fee') ? request()->param('Total_fee') : 1; $auth_code = request()->param('auth_code') ? request()->param('auth_code') : false; if($auth_code) { $param = array( 'body'=>'产品消费__会员充值', 'attach'=>'recharge', 'out_trade_no'=>"cowcms_".date("YmdHis"), 'total_fee'=>$Total_fee, 'auth_code'=>$auth_code, ); $returnData = pay('WX_Micropay',$param); ;//返回真表示支付成功 } } else { return $this->fetch(); } ``` >$param 参数说明 | 参数 | 类型 | 默认 | 描述 | | --- | --- |--- |--- | | body(**必填**) | String(128) | 产品消费__会员 | 商品描述| | attach (**必填**)| String(128) | PRO_1 | 附加数据,在查询API和支付通知中原样返回,可作为自定义参数使用 可以附带产品id或者其它| | out_trade_no(**必填**) | String(32) | 20150806125346 | cowcms系统产生的订单号, 且唯一 | | total_fee (**必填**)| INT | 100| 订单总金额,单位为分 | | auth_code (**必填**)| String(32) | 120061098828009406| 扫码支付授权码,设备读取用户微信中的条码或者二维码信息(注:用户付款码条形码规则:18位纯数字,以10、11、12、13、14、15开头)| | time_start | String(14) | 20091225091010 | 订单生成时间,格式为yyyyMMddHHmmss,默认为当前时间| | time_expire | String(14) | 20091225091010 | 订单失效时间,格式为yyyyMMddHHmmss,默认为下单时间加5分钟| | goods_tag| String(14) | WXG | 订单优惠标记,使用代金券或立减优惠功能时需要的参数| >[info]查询支付订单 ``` //$param['transaction_id']="4200000295201905289397268000";//微信支付订单号 $param['out_trade_no']="cowcms_1559112635";//COWCMS系统支付时传递过去的订单号 $order = pay('WX_PayOrder',$param);; dump($order); ``` >查询订单请求参数 | 参数 | 类型 | 默认 | 描述 | | --- | --- |--- |--- | | transaction_id | String(32) | 1217752501201407033233368018 | 微信支付订单号,支付成功后在回调中或者支付碎片中返回| | out_trade_no| String(32) | cowcms_20190528192231 | COWCMS系统支付时传递过去的订单号,transaction_id和out_trade_no二选一,推荐使用out_trade_no| >返回示例代码 ``` array(20) { ["appid"] => string(18) "wxc0d7ec4863c12ab9" ["attach"] => string(8) "recharge" ["bank_type"] => string(3) "CFT" ["cash_fee"] => string(1) "1" ["fee_type"] => string(3) "CNY" ["is_subscribe"] => string(1) "N" ["mch_id"] => string(10) "1225084602" ["nonce_str"] => string(16) "KaqrYV88aB1VXkpz" ["openid"] => string(28) "o4KFnuLOwsQR9tDusUk0ZBhe5b4Q" ["out_trade_no"] => string(21) "cowcms_20190528192231" ["result_code"] => string(7) "SUCCESS" ["return_code"] => string(7) "SUCCESS" ["return_msg"] => string(2) "OK" ["sign"] => string(64) "558D972A61DA690C172E0F19BE14EEAA85BAEF738AD07C78FDE69F67615966BF" ["time_end"] => string(14) "20190528192532" ["total_fee"] => string(1) "1" ["trade_state"] => string(7) "SUCCESS" ["trade_state_desc"] => string(12) "支付成功" ["trade_type"] => string(6) "NATIVE" ["transaction_id"] => string(28) "4200000295201905289397268000" } ``` 参数说明[查看](https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_2) >[info]微信退款 ``` //$param['transaction_id']="4200000295201905289397268000";//微信支付订单号 $param['out_trade_no']="cowcms_1559112635";//COWCMS系统支付时传递过去的订单号 $param['refund_fee']=1;//退款金额 $param['total_fee']=1;//总金额 $order = pay('WX_Refund',$param);; dump($order); ``` >微信退款请求参数 | 参数 | 类型 | 默认 | 描述 | | --- | --- |--- |--- | | transaction_id | String(32) | 1217752501201407033233368018 | 微信支付订单号,支付成功后在回调中或者支付碎片中返回| | out_trade_no| String(32) | cowcms_20190528192231 | COWCMS系统支付时传递过去的订单号,transaction_id和out_trade_no二选一,推荐使用out_trade_no| | out_refund_no| String(32) | cowcms_1559112635| 商户系统内部的退款单号,商户系统内部唯一,由退款时生成| | refund_fee| int | 1 | 退款金额| | total_fee| int | 1 | 总金额| >返回示例代码 ``` array(18) { ["appid"] => string(18) "wxc0d7ec4863c12ab9" ["cash_fee"] => string(1) "1" ["cash_refund_fee"] => string(1) "1" ["coupon_refund_count"] => string(1) "0" ["coupon_refund_fee"] => string(1) "0" ["mch_id"] => string(10) "1225084602" ["nonce_str"] => string(16) "TnqNAo748YYIWzJe" ["out_refund_no"] => string(27) "cowcms_Es52hE20190528200815" ["out_trade_no"] => string(21) "cowcms_20190528200640" ["refund_channel"] => array(0) { } ["refund_fee"] => string(1) "1" ["refund_id"] => string(29) "50000000342019052809759991995" ["result_code"] => string(7) "SUCCESS" ["return_code"] => string(7) "SUCCESS" ["return_msg"] => string(2) "OK" ["sign"] => string(64) "CD668E1F9C80D6E5B117D3FA81D32313EE563DF31DC288891A5C620982219B7F" ["total_fee"] => string(1) "1" ["transaction_id"] => string(28) "4200000294201905282699566424" } ``` 参数说明[查看](https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_4) >[info]微信退款订单查询 ``` //$param['transaction_id']="4200000295201905289397268000";//微信支付订单号 $param['out_trade_no']="cowcms_1559112635";//COWCMS系统支付时传递过去的订单号 $order = pay('WX_RefundOrder',$param);; dump($order); ``` >微信退款请求参数 | 参数 | 类型 | 默认 | 描述 | | --- | --- |--- |--- | | transaction_id | String(32) | 1217752501201407033233368018 | 微信支付订单号,支付成功后在回调中或者支付碎片中返回refund_id > out_refund_no > transaction_id > out_trade_no 四选一,推荐使用out_trade_no| | out_trade_no| String(32) | cowcms_Es52hE20190528200815| COWCMS系统支付时传递过去的订单号refund_id > out_refund_no > transaction_id > out_trade_no 四选一,推荐使用out_trade_no| | out_refund_no| String(32) | cowcms_1559112635| 商户系统内部的退款单号,商户系统内部唯一,由退款时生成refund_id > out_refund_no > transaction_id > out_trade_no 四选一,推荐使用out_trade_no| | refund_id| String(32) | 50000000342019052809759991995 | 微信退款单号,由退款生成refund_id > out_refund_no > transaction_id > out_trade_no 四选一,推荐使用out_trade_no | >返回示例代码 ``` array(21) { ["appid"] => string(18) "wxc0d7ec4863c12ab9" ["cash_fee"] => string(1) "1" ["mch_id"] => string(10) "1225084602" ["nonce_str"] => string(16) "mIhD7yHURDURQMap" ["out_refund_no_0"] => string(27) "cowcms_Es52hE20190528200815" ["out_trade_no"] => string(21) "cowcms_20190528200640" ["refund_account_0"] => string(29) "REFUND_SOURCE_UNSETTLED_FUNDS" ["refund_channel_0"] => string(8) "ORIGINAL" ["refund_count"] => string(1) "1" ["refund_fee"] => string(1) "1" ["refund_fee_0"] => string(1) "1" ["refund_id_0"] => string(29) "50000000342019052809759991995" ["refund_recv_accout_0"] => string(21) "支付用户的零钱" ["refund_status_0"] => string(7) "SUCCESS" ["refund_success_time_0"] => string(19) "2019-05-28 20:10:30" ["result_code"] => string(7) "SUCCESS" ["return_code"] => string(7) "SUCCESS" ["return_msg"] => string(2) "OK" ["sign"] => string(64) "845BBD20C1C13A92FDAF2108A3119C4C01956CF6D4D50A1E20BF8DFA3E74B9E5" ["total_fee"] => string(1) "1" ["transaction_id"] => string(28) "4200000294201905282699566424" } ```参数说明[查看](https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_5)