<?php

/**
 * 应用宝, 创建订单
 */
// 导入应用宝支付库
require_once __DIR__ . '/ysdks/Api.php';
require_once __DIR__ . '/ysdks/Ysdk.php';
require_once __DIR__ . '/ysdks/Payments.php';

include_once __DIR__ . '/../../../main.php';                                    # 导入game utils
include_once __DIR__ . '/../Mo/payResp.php';                                    # 导入通用返回值结构
include_once __DIR__ . '/../Mo/payRequest.php';                                 # 导入通用订单请求结构
require_once 'config.yyb.php';                                                  # 配置文件, Ps. 里面用到了object

use loyalsoft\CLog;
use loyalsoft\JsonUtil;
use loyalsoft\HttpUtil;

$tag = "应用宝";
HttpUtil::PostOnly();                                                           # 限定客户端用post
$str = HttpUtil::getQueryString();                                              # 提取参数串
Clog::pay($tag . " 请求串 " . $str);
// $str=gzinflate($str);                                                         # 编码方案
$data = JsonUtil::decode($str);                                                 # 提取数据
$req = new PayRequest($data);                                                   # 创建订单
#
//$resp = $req->AntiAdditionCheck();                                              # 防沉迷金额校验
//if ($resp->err != 0) {
//    exit($resp);                                                                # 返回
//}
CLog::pay("平台字符串" . $req->getPlatStr());

function encodeParams($params) {
    $arr = array();
    foreach ($params as $k => $value) {
        $arr[urlencode($k)] = urlencode($value);
    }
    return $arr;
}

if ($req->InserDataBase()) {                                                    # 订单数据入库
    CLog::pay($tag . ' 创建订单' . $req);
    $params = JsonUtil::decode($str);                                           # 提取参数
    $plat = $params->plat;                                                      # "qq" or "wx"
    $openid = $params->openid;
    $openkey = $params->openkey;                                                # (手Q的paytoken,微信的accessToken)
    $pf = $params->pf;
    $pfkey = $params->pfkey;
    $params->uid = $openid;
//    $pay_token = $params->pay_token;
//    $accessToken = $params->access_token;
    $account_type = $plat;                                                       # "qq" or "wx"
    $zoneId = 1;                                                                # 固定为1,不是游戏内的, 是传给腾讯的.
    $tsnow = loyalsoft\now();
    $baseParam = array(
        'openid' => $openid,
        'openkey' => $openkey,
        'ts' => $tsnow,
        'pf' => $pf,
        'pfkey' => $pfkey,
        'zoneid' => $zoneid,
    );
    $ysdkparams = $baseParam;
    $ysdkparams['payitem'] = $req->product_id . "*" . $req->product_price * 10 . "*" . $req->product_count;
    $ysdkparams['goodsmeta'] = str_replace('*', 'X', strlen($req->product_name) > 0 ? $req->product_name : "道具名称")
            . "*" . str_replace('*', 'X', strlen($req->product_desc) > 0 ? $req->product_desc : "道具描述缺失");
    $ysdkparams['goodsurl'] = "";
    $ysdkparams['appmode'] = 1;                                                 # 设置模式: 1不可修改数量, 2可以修改数量.
    $ysdkparams['app_metadata'] = $req->callbackInfo;                           # 透传参数
    encodeParams($ysdkparams);
    CLog::pay("[$tag]直购下单" . json_encode($ysdkparams));

    $cfg = config_for_yyb::Inst($plat);                                         # 配置信息
    $sdk = new Api($cfg->appid, $cfg->appkey);                        // 创建YSDK实例
    $sdk->setPay($cfg->pay_appid, $cfg->pay_appkey);                  // 设置支付信息
    $sdk->setServerName($cfg->server_name);                           // 设置YSDK调用环境(沙箱地址/现网地址)

    $ret = buy_goods_m($sdk, $ysdkparams, $account_type);                       # 向YSDK后台发起统一订单请求, 返回值是asoc_array
    CLog::pay($ret);
    if ($ret->ret) {                                                            # 后台下单失败
        echo payResp::err($ret->ret, $ret->msg);
        CLog::pay("[$tag]向$tag 后台推送订单操作失败." . $ret->msg);
    } else {
        $arr = $req->retData();
        $r = array_merge($arr, (array) $ret);                                   # 扩展下返回值
        echo payResp::ok($r);
        CLog::pay("[$tag]向$tag 后台推送订单成功" . json_encode($r));
    }
} else {
    CLog::pay($tag . ' 创建订单失败' . $req);
    echo payResp::err(1, '后台创建订单失败');                                     # 返回值
}

