<?php
date_default_timezone_set("Asia/Taipei");
ini_set('display_errors',true);
error_reporting(E_ALL);
    
include(__DIR__.'/../data/common.inc.php');
include(__DIR__.'/zlinePayRequest.php');
try{
    $fullsqlLink = new \PDO("mysql:host=".$cfg_dbhost.";dbname=".$cfg_dbname, $cfg_dbuser, $cfg_dbpwd,array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"));
    $fullsqlLink->setAttribute(\PDO::ATTR_ERRMODE,\PDO::ERRMODE_EXCEPTION);
} catch(\PDOException $e){
    echo $e->getMessage(); 
}

$userData = new \stdClass();
$userData->user_id = 126;
$userData->mobile_phone = '1234';
$zpay = new zlinePayRequest($fullsqlLink);
if(!isset($_SESSION['sendid'])){ $_SESSION['sendid'] = false; }
if(isset($_POST['sendid']) AND $_SESSION['sendid'] != $_POST['sendid']){
    switch($_POST['api']){
        case 'regist':
            $res = $zpay->apply2BRegist($userData);
            break;
            
    }
    $_SESSION['sendid'] = $_POST['sendid'];
}
?>
<!DOCUTYPE html>
<html lang = "zh">
    <head>
    <title>中联信2B测试</title>
    <style>
        body {
            font-family: monospace;
        }
        hr {
            border:0;
            height:0;
            border-bottom:1px solid gray;
        }
    </style>
    </head>
    <body>
    <script>
        function charge(){
            document.getElementById('T').value = 'C';
            document.getElementById('F').submit();
        }
        function charge2(){
            document.getElementById('T').value = 'D';
            document.getElementById('F').submit();
        }
        document.getElementById('F').onsubmit = function(){
            document.getElementById('T').value = 'W';
            document.getElementById('F').submit();
        }
    </script>
    <form action = "" method = "POST" id = "F">
        <input type = "hidden" value = "W" id = "T" name = "T">
        <input type = "hidden" value = "<?php echo time();?>" name = "sendid">
        <?php 
            $res = $zpay->queryWalletBalance($userData);
            $balance = ($res->CURAMT / 100);
        ?>
        <hr>
        <input type = "button" value = "支付并充值100RMB" onclick = "charge()">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type = "button" value = "不支付直接充值100RMB" onclick = "charge2()"><br>
        
        当下余额：<?php echo $balance;?><br>
        消费金额：<input type = "text" name = "amount">(单位：RMB元)
        <br>
        <input type = "submit" value = "消费">
    </form>
    </body>
</html>

