<?php 
ini_set('date.timezone','Asia/Shanghai');
require_once "../lib/DzPay.Api.php";
require_once "DzPay.PublicPay.php";

$input = new WXPublic();
$input->SetProductName("测试样例");
$input->SetProductDescription('公众号');
$input->SetAmount("0.01");
$input->SetMerchantOrderNo(date("YmdHis"));
$input->SetWechatAppId("");
$input->SetBuyerId("");

$publicPay = new PublicPay();
$jsApiParameters = $publicPay->pay($input,false);
?>

<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>
</head>
<body>
    <br/>
    <font color="#9ACD32"><b>该笔订单支付金额为<span style="color:#f00;font-size:50px">1分</span>钱</b></font><br/><br/>
    <?php
    if(!empty($jsApiParameters))
    {
    echo'
    <div align="center">
        <button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer;  color:white;  font-size:16px;" type="button" onclick="callpay()" >立即支付</button>
    </div>';
    }
    ?>
    <script>
    function callpay() {
        if (event)
            event.stopPropagation()
        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();
        }
    }

    function jsApiCall() {
        try {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                <?php
                echo $jsApiParameters;
                ?>,
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        alert('支付成功');
                    } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                        return;
                    } else {
                        alert("支付失败");
                    }
                }
            );
        }
        catch (e) {
            alert(e)
        }
    }
    </script>
</body>
</html>