审查视图

public/paycenter/wechat/index.php 5.3 KB
xiaohu authored
1 2 3 4 5 6 7 8
<?php
/**
 *
 * example目录下为简单的支付样例,仅能用于搭建快速体验微信支付使用
 * 样例的作用仅限于指导如何使用sdk,在安全上面仅做了简单处理, 复制使用样例代码时请慎重
 * 请勿直接直接使用样例对外提供服务
 *
 **/
xiaohu thinkpad authored
9
date_default_timezone_set('Asia/Shanghai');
xiaohu authored
10 11 12 13
require_once "../lib/WxPay.Api.php";
require_once "WxPay.JsApiPay.php";
require_once "WxPay.Config.php";
require_once 'log.php';
xiaohu thinkpad authored
14
xiaohu authored
15 16 17 18 19 20 21 22 23 24 25
//初始化日志
$logHandler = new CLogFileHandler("../logs/" . date('Y-m-d') . '.log');
$log        = Log::Init($logHandler, 15);
Log::DEBUG("===========step1-begin-jsapi-pay=============");

//①、获取用户openid
try {

    $tools  = new JsApiPay();
//    $openId = $tools->GetOpenid();
    $deta   = $_REQUEST;
xiaohu thinkpad authored
26
xiaohu authored
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    //测试专用 0.01
    //$fee          = $deta['fe'] ? $deta['fe'] * 100 : 100;
    $fee          = 1;
    $out_trade_no = $deta['ot'] ? $deta['ot'] : date('YmdHis') . uniqid();
    $body         = $deta['sn'] ? $deta['sn'] : 'ceshi';
    $attach       = $deta['at'] ? $deta['at'] : 'ceshi';
    $openId       = $deta['op'] ? $deta['op'] : '';

    //②、统一下单
    $input = new WxPayUnifiedOrder();
    $input->SetBody($body);
    $input->SetAttach($attach);
    $input->SetOut_trade_no($out_trade_no);
    $input->SetTotal_fee($fee);
    $input->SetTime_start(date("YmdHis"));
    $input->SetTime_expire(date("YmdHis", time() + 600));
    $input->SetGoods_tag("qnbug_tag");
//    $input->SetNotify_url("http://dm161xueche.qnbug.cn/paycenter/wechat/notify.php");
    $input->SetNotify_url("http://wx.youlj.cn/paycenter/wechat/notify.php");
    $input->SetTrade_type("JSAPI");
    $input->SetOpenid($openId);
    $config = new WxPayConfig();
    $order  = WxPayApi::unifiedOrder($config, $input);
    Log::DEBUG("===========step2-begin-jsapi-pay=============");
    Log::DEBUG(json_encode($deta, true));

    $jsApiParameters = $tools->GetJsApiParameters($order);
xiaohu thinkpad authored
54
xiaohu authored
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

    //获取共享收货地址js函数参数
    $editAddress = $tools->GetEditAddressParameters();
} catch (Exception $e) {
    Log::ERROR(json_encode($e));
}
//③、在支持成功回调通知中处理成功之后的事宜,见 notify.php
/**
 * 注意:
 * 1、当你的回调地址不可访问的时候,回调通知会失败,可以通过查询订单来确认支付是否成功
 * 2、jsapi支付时需要填入用户openid,WxPay.JsApiPay.php中有获取openid流程 (文档可以参考微信公众平台“网页授权接口”,
 * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)
 */
?>

<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>微信支付跳转中</title>
    <script type="text/javascript">
        //调用微信JS api 支付
        function jsApiCall() {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                <?php echo $jsApiParameters; ?>,
                function (res) {
                    WeixinJSBridge.log(res.err_msg);
                    //alert(res.err_code);
                    if (res.err_msg == 'get_brand_wcpay_request:ok') {
                        window.location.href = 'http://wx.youlj.cn/user/order/pay_success.html';
                    } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                        window.history.go(-1);
                    }

                }
            );
        }

        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>
    <script type="text/javascript">
        //获取共享地址
        function editAddress() {
            WeixinJSBridge.invoke(
                'editAddress',
                <?php echo $editAddress; ?>,
                function (res) {
                    var value1 = res.proviceFirstStageName;
                    var value2 = res.addressCitySecondStageName;
                    var value3 = res.addressCountiesThirdStageName;
                    var value4 = res.addressDetailInfo;
                    var tel = res.telNumber;

                    //alert(value1 + value2 + value3 + value4 + ":" + tel);
                }
            );
        }

        window.onload = function () {
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', editAddress, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', editAddress);
                    document.attachEvent('onWeixinJSBridgeReady', editAddress);
                }
            } else {
                editAddress();
            }
        };
        callpay()

    </script>
</head>
<body>

</body>
</html>