Browse Source

微信支付

master
林一峰 7 years ago
parent
commit
1f05621a1a
  1. 11
      app/Http/Controllers/Weixin/UserController.php
  2. 57
      app/Http/Controllers/Weixin/WxPayController.php
  3. 18
      resources/org/wxpay/WxPayConfig.php
  4. 7
      resources/org/wxpay/WxPayPubHelper.class.php
  5. 20
      resources/views/weixin/user/userRechargeOrderDetail.blade.php

11
app/Http/Controllers/Weixin/UserController.php

@ -78,9 +78,10 @@ class UserController extends CommonController
require_once(resource_path('org/wxpay/WxPayConfig.php')); // 导入微信配置类
require_once(resource_path('org/wxpay/WxPayPubHelper.class.php')); // 导入微信支付类
$body = '商品购买';//订单详情
$out_trade_no = '2017787878';//订单号
$body = '充值';//订单详情
$out_trade_no = '20177878738';//订单号
$total_fee = floatval(0.01*100);//价格0.01
$attach = 'pay_type=1';
$notify_url = route('weixin_wxpay_notify');//通知地址
$wxconfig= \WxPayConfig::wxconfig();
@ -101,10 +102,10 @@ class UserController extends CommonController
$unifiedOrder->setParameter("body","$body");//商品描述
$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号
$unifiedOrder->setParameter("total_fee","$total_fee");//总金额
$unifiedOrder->setParameter("notify_url","$notify_url");//通知地址
//$unifiedOrder->setParameter("attach","$attach"); //附加数据,选填,在查询API和支付通知中原样返回,可作为自定义参数使用,示例:a=1&b=2
$unifiedOrder->setParameter("notify_url","$notify_url");//通知地址
$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
//$unifiedOrder->setParameter("attach","test"); //附加数据,选填,在查询API和支付通知中原样返回,可作为自定义参数使用
$prepay_id = $unifiedOrder->getPrepayId();
$prepay_id = $unifiedOrder->getPrepayId();
//=========步骤3:使用jsapi调起支付============
$jsApi->setPrepayId($prepay_id);
$jsApiParameters = $jsApi->getParameters();

57
app/Http/Controllers/Weixin/WxPayController.php

@ -14,8 +14,61 @@ class WxPayController extends CommonController
//微信支付回调
public function wxpayNotify(Request $request)
{
require_once(resource_path('org/wxpay/WxPayPubHelper.class.php'));
file_put_contents("1.txt",$GLOBALS['HTTP_RAW_POST_DATA']);
//获取通知的数据
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
//将XML转为array
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$post_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$get_arr = explode('&',$post_data['attach']);
foreach($get_arr as $value)
{
$tmp_arr = explode('=',$value);
$post_data[$tmp_arr[0]] = $tmp_arr[1];
}
return view('weixin.cart.index', $data);
if($post_data['result_code'] == 'SUCCESS')
{
//$post_data['out_trade_no']
//$post_data['transaction_id']
//$post_data['total_fee']
file_put_contents("2.txt",$post_data['total_fee'].'--'.$post_data['out_trade_no'].'--'.$post_data['attach']);
echo "SUCCESS";
}
else
{
echo "FAILE";
}
}
/**
* 发微信红包
* @param string $openid 用户openid
*/
public function wxhbpay($re_openid,$money,$wishing='恭喜发财,大吉大利!',$act_name='赠红包活动',$remark='赶快领取您的红包!')
{
//微信支付-start
require_once(resource_path('org/wxpay/WxPayConfig.php')); // 导入微信配置类
require_once(resource_path('org/wxpay/WxPayPubHelper.class.php')); // 导入微信支付类
$wxconfig= \WxPayConfig::wxconfig();
$wxHongBaoHelper = new \Sendredpack($wxconfig);
$wxHongBaoHelper->setParameter("mch_billno", date('YmdHis').rand(1000, 9999));//订单号
$wxHongBaoHelper->setParameter("send_name", '红包');//红包发送者名称
$wxHongBaoHelper->setParameter("re_openid", $re_openid);//接受openid
$wxHongBaoHelper->setParameter("total_amount", floatval($money*100));//付款金额,单位分
$wxHongBaoHelper->setParameter("total_num", 1);//红包収放总人数
$wxHongBaoHelper->setParameter("wishing", $wishing);//红包祝福
$wxHongBaoHelper->setParameter("client_ip", '127.0.0.1');//调用接口的机器 Ip 地址
$wxHongBaoHelper->setParameter("act_name", $act_name);//活劢名称
$wxHongBaoHelper->setParameter("remark", $remark);//备注信息
$responseXml = $wxHongBaoHelper->postXmlSSL();
//用作结果调试输出
//echo htmlentities($responseXml,ENT_COMPAT,'UTF-8');
$responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
return $responseObj->result_code;
}
}

18
resources/org/wxpay/WxPayConfig.php

@ -61,22 +61,26 @@ class WxPayConfig
{
//=======【基本信息设置】=====================================
//微信公众号身份的唯一标识。审核通过后,在微信发送的邮件中查看
$wxconfig['APPID'] = self::APPID;
$wxconfig['APPID'] = self::APPID;
//受理商ID,身份标识
$wxconfig['MCHID'] = self::MCHID;
$wxconfig['MCHID'] = self::MCHID;
//商户支付密钥Key。审核通过后,在微信发送的邮件中查看
$wxconfig['KEY'] = self::KEY;
$wxconfig['KEY'] = self::KEY;
//JSAPI接口中获取openid,审核后在公众平台开启开发模式后可查看
$wxconfig['APPSECRET'] = self::APPSECRET;
$wxconfig['APPSECRET'] = self::APPSECRET;
//=======【JSAPI路径设置】===================================
//获取access_token过程中的跳转uri,通过跳转将code传入jsapi支付页面
$wxconfig['JS_API_CALL_URL'] = '';
//=======【证书路径设置】=====================================
//证书路径,注意应该填写绝对路径
$wxconfig['SSLCERT_PATH'] = self::SSLCERT_PATH;
$wxconfig['SSLKEY_PATH'] = self::SSLKEY_PATH;
$wxconfig['SSLCERT_PATH'] = self::SSLCERT_PATH;
$wxconfig['SSLKEY_PATH'] = self::SSLKEY_PATH;
//=======【异步通知url设置】===================================
//异步通知url,商户根据实际开发过程设定
//$wxconfig['NOTIFY_URL'] = 'http://'.$_SERVER['HTTP_HOST'].U('Wxpay/notify_url'); //放在下单时,添加
$wxconfig['NOTIFY_URL'] = ''; //放在下单时,添加
//=======【curl超时设置】===================================
//本例程通过curl使用HTTP POST方法,此处可修改其超时时间,默认为30秒

7
resources/org/wxpay/WxPayPubHelper.class.php

@ -166,7 +166,7 @@ class Common_util_pub
//初始化curl
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOP_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
//这里设置代理,如果有的话
//curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
@ -186,7 +186,6 @@ class Common_util_pub
//返回结果
if($data)
{
curl_close($ch);
return $data;
}
else
@ -194,7 +193,6 @@ class Common_util_pub
$error = curl_errno($ch);
echo "curl出错,错误码:$error"."<br>";
echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
curl_close($ch);
return false;
}
}
@ -379,7 +377,6 @@ class UnifiedOrder_pub extends Wxpay_client_pub
return $prepay_id;
}
}
/**
@ -826,7 +823,7 @@ class JsApi_pub extends Common_util_pub
//初始化curl
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);

20
resources/views/weixin/user/userRechargeOrderDetail.blade.php

@ -3,19 +3,18 @@
<link href="<?php echo env('APP_URL'); ?>/css/weixin/style.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/weixin/mobile.js"></script>
<link href="<?php echo env('APP_URL'); ?>/css/font-awesome.min.css" type="text/css" rel="stylesheet">
<script>
//调用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{$jsApiParameters},
<?php echo $jsApiParameters; ?>,
function(res){
WeixinJSBridge.log(res.err_msg);
//alert(res.err_code+res.err_desc+res.err_msg);
if(res.err_msg=='get_brand_wcpay_request:ok')
if(res.err_msg=='get_brand_wcpay_request:ok')
{
alert('支付成功!');
}
@ -23,8 +22,8 @@ function jsApiCall()
{
alert('支付失败!');
}
setTimeout("location.href = '{$returnUrl}'",2000);
//setTimeout("location.href = '<?php echo $returnUrl; ?>'",2000);
}
);
}
@ -51,21 +50,16 @@ function callpay()
</div>
<style>
.account{text-align:center;margin-top:30px;}
.account .icon{color:#FFCC00;font-size:100px;}
.account .tit{color:#000;font-size:18px;}
.bottoma{display:block;font-size:18px;padding:10px;border-radius:2px;}
</style>
<div class="floor account">
<div class="icon"><i class="fa fa-google-wallet"></i></div>
<div class="floor">
<div style="margin:10px;text-align:left;">
<p style="color:#7abd65;">订单已于 <b style="color:#fea700;"><?php echo $post['created_at']; ?></b> 提交成功,请您尽快付款!</p>
<p>订单已于 <b style="color:#fea700;"><?php echo $post['created_at']; ?></b> 提交成功,请您尽快付款!</p>
订单号:<?php echo $post['id']; ?><br>
应付金额:<strong style="color:#D03737;"><?php echo $post['money']; ?></strong> 元<br><br>
<p style="color:#999;font-size:.875em">请您在提交订单后30分钟内完成支付,否则订单会自动取消。</p>
</div>
<a style="margin:0 10px 10px 10px;background-color:#1aad19;text-align:center;color:#fff;border:1px solid #179e16;" class="bottoma" href="javascript:chongzhi();">去支付</a>
<a style="margin:0 10px 10px 10px;background-color:#1aad19;text-align:center;color:#fff;border:1px solid #179e16;" class="bottoma" href="javascript:callpay();">去支付</a>
</div>
</body></html>
Loading…
Cancel
Save