|
- <?php
- /**
- *微信商城的支付
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 张帅
- * PhpStorm weChatShop.php
- * Create By 2016/11/21 11:55 $
- */
-
- header("Content-type:text/html;charset=utf-8");
- ini_set('date.timezone', 'Asia/Shanghai');
- //error_reporting(E_ERROR);
- require_once "../lib/WxPay.Api.php";
- require_once "WxPay.JsApiPay.php";
- require_once 'log.php';
- require_once '../../Common/Function.php';
-
- //支付url
- $pay_url = 'http://wx.zhizhuchuxing.com/wechat/WxPay/realpay/success_notify_wechat_shop.php';
-
- //url数组
- $url_arr = array(
- 'success' => 'http://wx.zhizhuchuxing.com/weixin/pay_success.html',
- 'fail' => 'http://wx.zhizhuchuxing.com/weixin/order_detail.html',
- 'order_detail' => 'http://wx.zhizhuchuxing.com/module/order/orderlist/getDetaile',
- 'update_order' => 'http://wx.zhizhuchuxing.com/module/order/order/updateOrderStatus'
- );
- //获取参数
- //获取订单号
- if (!empty($_GET['order_id']) && $_GET['order_id'] !== false) {
- $order_id = $_GET['order_id'];
- } else {
- echo "<script>alert('订单号有误')</script>";
- exit;
- }
-
- //初始化日志
- $logHandler = new CLogFileHandler("../logs/" . date('Y-m-d') . '.log');
- $log = Log::Init($logHandler, 15);
-
- //打印输出数组信息
- function printf_info($data)
- {
- foreach ($data as $key => $value) {
- echo "<font color='#00ff55;'>$key</font> : $value <br/>";
- }
- }
-
- //①、获取用户openid
- $tools = new JsApiPay();
- $open_id = $tools->GetOpenid();
-
- //支付
- $jsApiParameters = pay($order_id,$url_arr,$pay_url,$open_id,$tools);
-
- //打log方法
- function write_log1($result)
- {
- $dir = __DIR__ . '/Log/';
- if (!is_dir($dir)) {
- mkdir($dir);
- }
- $filename = $dir . date("Y-m-d") . ".log";
- file_put_contents($filename, date("Y-m-d H:i:s") . " " . $result . PHP_EOL, FILE_APPEND);//订单详细日志
- }
-
- //判断支付支付
- function CheckWeChatPay($order_id,$url_arr){
- $order_result = WeChatShopOrderDetail($order_id,$url_arr);
- //已支付,跳转支付成功页面
- if(!$order_result){
- header('Location:' . $url_arr['success'] . '?order_id=' . $order_id);
- }
- //未支付
- $price = (int)($order_result['price']*100);
- //测试价格
- //$price = 1;
- //如果价格为0,修改订单状态
- if($price == 0){
- //1.修改订单状态
- $data = array('order_id'=>$order_id);
- $update_order = http_request($url_arr['update_order'],$data);
- $update_order = json_decode($update_order,true);
- if(!$update_order['flag']){
- echo "<script>alert('网络错误')</script>";
- exit;
- }
- //2.跳转支付成功页面
- header('Location:' . $url_arr['success'] . '?order_id=' . $order_id);
- }
- $prod_name = $order_result['prod_name'];
- return array(
- 'order_id' => $order_result['order_id'],
- 'price' => $price,
- 'prod_name' => $prod_name
- );
- }
-
- //微信商城订单详情
- function WeChatShopOrderDetail($order_id,$url_arr){
- //获取订单详情
- $data = array('order_id'=>$order_id,
- 'code'=>md5('zhizhuchuxing')
- );
- $order_detail = http_request($url_arr['order_detail'],$data);
- $order_detail = json_decode($order_detail,true);
-
- //订单号无效
- if(!$order_detail['flag']){
- echo "<script>alert('订单号无效')</script>";
- exit;
- }
- //判断是否已支付
- $order_detail = $order_detail['data'];
- if($order_detail['order_status'] == 145){
- return array(
- 'order_id' => $order_detail['order_status'],
- 'price' => $order_detail['price'],
- 'prod_name' => $order_detail[0]['line_name']
- );
- }else{
- return false;
- }
- }
-
- //支付方法
- function pay($order_id,$url_arr,$pay_url,$open_id,$tools){
- //验证支付
- $order_info = CheckWeChatPay($order_id,$url_arr);
- //产品名
- $prod_name = $order_info['prod_name'];
- $price = $order_info['price'];
- //支付
- $input = new WxPayUnifiedOrder();
- $SetBody = $prod_name . ": " . $order_id;
- $input->SetBody($SetBody);
- $SetOut_trade_no = $order_id . "-" . date("ymdHis"); //支付串号(商户单号)
- $input->SetOut_trade_no($SetOut_trade_no);
- $input->SetTotal_fee($price);
- $SetTime_start = date("YmdHis");
- $input->SetTime_start($SetTime_start);
- $SetTime_expire = date("YmdHis", time() + 600);
- $input->SetTime_expire($SetTime_expire);
- $SetGoods_tag = "代金券";
-
- $input->SetGoods_tag($SetGoods_tag);
- //打Log
- write_log1($pay_url);
-
- $input->SetNotify_url($pay_url); //回掉地址
- $SetTrade_type = "JSAPI";
- $input->SetTrade_type($SetTrade_type);
- $input->SetOpenid($open_id);
-
- //打Log
- write_log1($SetBody . "^" . $SetOut_trade_no . "^" . $price . "^" . $SetTime_start . "^" . $SetTime_expire . "^" . $SetGoods_tag . "^" . $pay_url . "^" . $SetTrade_type . "^" . $open_id);
- $order = WxPayApi::unifiedOrder($input, 15);
- //printf_info($order);
- $jsApiParameters = $tools->GetJsApiParameters($order);
- //print_r($jsApiParameters);
- //获取共享收货地址js函数参数
- $editAddress = $tools->GetEditAddressParameters();
-
- //打Log
- write_log1(json_encode($jsApiParameters));
- return $jsApiParameters;
- }
-
- ?>
-
-
- <script type="text/javascript">
- var order_id = <?php echo $order_id;?>;
- var success_url = '<?php echo $url_arr['success']; ?>';
- var fail_url = '<?php echo $url_arr['fail']; ?>';
- var update_order = '<?php echo $url_arr['update_order']; ?>';
-
-
- window.onload = function () {
- 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();
- }
- };
- //调用微信JS api 支付
- function jsApiCall() {
- WeixinJSBridge.invoke(
- 'getBrandWCPayRequest',
- <?php echo $jsApiParameters; ?>,
- function (res) {
- WeixinJSBridge.log(res.err_msg);
- if (res.err_msg == 'get_brand_wcpay_request:ok') {
- alert('支付成功');
- window.location.href = success_url+'?order_id='+order_id;
- } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
- alert('支付取消');
- window.location.href = fail_url+'?order_id='+order_id;
- } else {
- alert('支付失败');
- window.location.href = fail_url+'?order_id='+order_id;
- }
- }
- );
- }
-
- 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();
- }
- }
-
- </script>
|