<?php
date_default_timezone_set("PRC");
error_reporting(E_ALL ^ E_DEPRECATED);
header("Content-Type: text/html;charset=utf-8");
//修改商户信息
$uid = '商户id';
$service_ip = 'http://域名:82';
//把引号里的IP地址替换为你的外网IP地址或域名,有端口号的话记得加上
$return_url = $service_ip.
'/pay/return.php';
$callback_url = $service_ip.
'/pay/paynotify.php';
$ukey = '商户key';
//修改数据库连接信息
$db_ip = "127.0.0.1"; //数据库地址
$db_name = "root"; //数据库用户
$db_psd = "de3684650b9b946f"; //数据库密码
$server = "moyu_s1";

$quarr = array(
    "1" => array(
        "name" => "1区-服务器1",
        "url" => "http://127.0.0.1:10061",
        "platformId" => 3006,
        "serverID" => 32801,
        "zs" => 1, //入群福利赠送数量
    ),
    "2" => array(
        "name" => "2区-服务器2",
        "url" => "http://127.0.0.1:10062",
        "platformId" => 3006,
        "serverID" => 32802,
        "zs" => 1, //入群福利赠送数量
    ),
   "3" => array(
        "name" => "3区-服务器3",
        "url" => "http://127.0.0.1:10063",
       "platformId" => 3006,
        "serverID" => 32803,
        "zs" => 1, //入群福利赠送数量
    ),	
    "4" => array(
        "name" => "4区-服务器4",
        "url" => "http://127.0.0.1:10064",
        "platformId" => 3006,
        "serverID" => 32804,
        "zs" => 1, //入群福利赠送数量
    ),
    "5" => array(
        "name" => "5区-服务器5",
        "url" => "http://127.0.0.1:10065",
        "platformId" => 3006,
        "serverID" => 32805,
        "zs" => 1, //入群福利赠送数量
    ),   
    "6" => array(
        "name" => "6区-服务器6",
        "url" => "http://127.0.0.1:10066",
        "platformId" => 3006,
        "serverID" => 32806,
        "zs" => 1, //入群福利赠送数量
    ), 	

);

$goods = array(
    array(
        "amount" => 100, //售价，不要有重复
        "name" => "100元=1亿魔石", //名称
        "goods" => "3#100000000;624#100000000" //格式：物品ID#数量
    ),	
    array(
        "amount" => 500, //售价，不要有重复
        "name" => "500元=6亿魔石", //名称
        "goods" => "3#600000000;624#600000000" //格式：物品ID#数量
    ),
    array(
        "amount" => 1000, //售价，不要有重复
        "name" => "1000元=14亿魔石", //名称
        "goods" => "3#1400000000;624#1400000000" //格式：物品ID#数量
    ),
    array(
        "amount" => 199, //售价，不要有重复
        "name" => "199=自选修罗伙伴礼包*45", //名称
        "goods" => "11370#45" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),	
    array(
        "amount" => 201, //售价，不要有重复
        "name" => "201=神·葛罗御魂碎片*2430", //名称
        "goods" => "84000012#2430" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),		
    array(
        "amount" => 202, //售价，不要有重复
        "name" => "202=神·狄娜亚御魂碎片*2430", //名称
        "goods" => "84000013#2430" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),	
    array(
        "amount" => 203, //售价，不要有重复
        "name" => "203=神·莱尼亚御魂碎片*2430", //名称
        "goods" => "84000014#2430" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),	
    array(
        "amount" => 204, //售价，不要有重复
        "name" => "204=神·司寇迪御魂碎片*2430", //名称
        "goods" => "84000015#2430" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),
    array(
        "amount" => 205, //售价，不要有重复
        "name" => "205=神·艾拉索御魂碎片*2430", //名称
        "goods" => "84000016#2430" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),
    array(
        "amount" => 206, //售价，不要有重复
        "name" => "206=哈儿比变身礼包", //名称
        "goods" => "84000006#2430" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),
    array(
        "amount" => 207, //售价，不要有重复
        "name" => "207=葛罗变身礼包", //名称
        "goods" => "84000007#2430" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),	
    array(
        "amount" => 208, //售价，不要有重复
        "name" => "208=狄娜亚变身礼包", //名称
        "goods" => "84000008#2430" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),
    array(
        "amount" => 209, //售价，不要有重复
        "name" => "209=莱尼亚变身礼包", //名称
        "goods" => "84000009#2430" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),
    array(
        "amount" => 210, //售价，不要有重复
        "name" => "210=司寇迪变身礼包", //名称
        "goods" => "84000010#2430" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),
    array(
        "amount" => 211, //售价，不要有重复
        "name" => "211=艾拉索变身礼包", //名称
        "goods" => "84000011#2430" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),	
    array(
        "amount" => 300, //售价，不要有重复
        "name" => "300=坐骑自选箱A*4+坐骑自选箱B*8+竹林行者·滚滚*8+剑灵艾拉索*8+诛邪*9", //名称
        "goods" => "12127#4;12166#8;244#8;219#8;500001#9" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),
    array(
        "amount" => 101, //售价，不要有重复
        "name" => "101元=弑神*1+八方*1+登峰造极*1+十方*1", //名称
        "goods" => "8920010#1;8910005#1;70610048#1;70610046#1" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),		
    array(
        "amount" => 501, //售价，不要有重复
        "name" => "501元=石纹*999+橙色神器礼盒*30000", //名称
        "goods" => "9028#999;700510#30000" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),	
    array(
        "amount" => 301, //售价，不要有重复
        "name" => "301元=高级法宝副装礼盒*60+纹章*1+太阳车*1+魔盒*1+标志*1", //名称
        "goods" => "9042#60;21400312#1;21400316#1;21400304#1;21400308#1" //格式：第一个物品ID#数量;下一个物品ID#数量
    ),	
);

function getRoleIDByName($apiUrl, $roleName) {
    $apiUrl = $apiUrl.
    "/role/name";
    $param = array(
        "name" => $roleName,
    );
    $sign = calcSign($param);
    $param["sign"] = $sign;
    $ret = sendPost($apiUrl, $param);
    $retData = json_decode($ret, true, 512, JSON_BIGINT_AS_STRING);
    $status = $retData["status"];
    if ($status != 200) {
        return 0;
    }
    $roleid = $retData["data"]["id"];
    return $roleid;
}

function webRecharge($apiUrl, $param) {
    $apiUrl = $apiUrl.
    "/webRecharge";
    $retData = json_decode(sendPost($apiUrl, $param), true);
    return true;
}

function generalOrderID() {
    $order_id_main = date('Ymd');
    $order_id_len = strlen($order_id_main);
    $order_id_sum = 0;
    for ($i = 0; $i < $order_id_len; $i++) {
        $order_id_sum += (int)(substr($order_id_main, $i, 1));
    }
    $pro = substr(md5(uniqid(microtime(true).mt_rand())), 0, 3);
    $orderid = $order_id_main.str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT).$pro;
    return $orderid;
}

function calcRechargeSign($inArray) {
    $preStr = implode("", $inArray).
    "k0RlsOyyWMoi";
    return sha1($preStr);
}

function calcSign($inArray) {
    $preStr = implode("", $inArray).
    "0I3EaVD2WfElnw3O";
    return sha1($preStr);
}

function sendPost($url, $param) {
    $gmusername = "shenghe";
    $gmpassword = "kgiVYEh7pKu8";

    if (empty($url) || empty($param)) {
        return false;
    }
    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $postUrl);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $gmusername.
        ":".$gmpassword);
    $data = curl_exec($ch);
    curl_close($ch);
    //exit($data);
    return $data;
}

function makeItemsString($mailid, $mailnum) {
    $itemStr = "";
    $itemStr = $mailid.
    "#".$mailnum;
    return $itemStr;
}

function mailByRoleName($apiUrl, $param) {
    $apiUrl = $apiUrl.
    "/mailById";
    $param["sign"] = calcSign($param);
    $retData = json_decode(sendPost($apiUrl, $param), true);
    if ($retData["status"] == 200) {
        return true;
    } else {
        exit("个人邮件发送失败，原因为:".$retData["message"]);
        return false;
    }
}

function sendGoods($apiUrl, $param) {
    $apiUrl = $apiUrl. "/recharge";
    $retData = json_decode(sendPost($apiUrl, $param), true);
    return true;
}