<?php

/**
 *  ps. 这一步其实没有跟支付宝后台交互,只是服务器自己计算的签名等信息,
 *    因为签名秘钥不适合放在客户端. (wg)
 *
 */
require_once __DIR__ . '/../../../../main.php';            # 导入游戏库
require_once __DIR__ . '/../../Mo/resp.php';               # 响应模型
require_once __DIR__ . '/../../Mo/payRequest.php';
require_once __DIR__ . '/AopSdk.php';                      # 导入alipay库
require_once __DIR__ . '/config_alipay.php';               # 导入配置文件

use loyalsoft\CLog;                                        # 添加 use
use loyalsoft\JsonUtil;
use loyalsoft\HttpUtil;

HttpUtil::PostOnly();                                       # 限定客户端用post
$str = HttpUtil::getQueryString();                          # 提取参数串
// $str=gzinflate($str);                                     # 编码方案
$data = JsonUtil::decode($str);
$req = new PayRequest($data);                               # 创建订单
$cfg = loyalsoft\config_alipay::Inst();
$req->notifyUrl = $cfg->notify_Url;                         # 给客户端传递回调URL.
$req->InserDataBase();                                      # 将订单插入数据库
CLog::pay('[alipay.pay]创建订单' . $req);          # 日志
// SDK已经封装掉了公共参数，这里只需要传入业务参数: https://docs.open.alipay.com/204/105465/
$bizcontent = "{\"body\":\"" . $req->product_name . "\","
        . "\"subject\": \"龙游科技\","
        . "\"out_trade_no\": \"" . $req->cpOrderId . "\","
        . "\"timeout_express\": \"30m\","
        . "\"total_amount\": \"" . $req->amount . "\","                         # 这里单位是元, 注意检查
        . "\"product_code\":\"QUICK_MSECURITY_PAY\","
        . "\"passback_params\":\"" . $req->callbackInfo . "\""                  # 透传参数
        . "}";

$aop = new AopClient();                                     # 客户端
$aop->appId = $cfg->appId;                                  # "app_id";
$aop->gatewayUrl = $cfg->gatewayUrl;                        # 支付宝网关
$aop->alipayrsaPublicKey = $cfg->alipayrsaPublicKey;
$aop->rsaPrivateKey = $cfg->rsaPrivateKey;
$aop->format = "json";
$aop->charset = "UTF-8";
$aop->signType = "RSA2";

//实例化具体API对应的request类,类名称和接口名称对应,
$request = new AlipayTradeAppPayRequest();                  # 当前调用接口名称：alipay.trade.app.pay
$request->setNotifyUrl($cfg->notify_Url);
$request->setBizContent($bizcontent);
$response = $aop->sdkExecute($request);                     # 这里和普通的接口调用不同，使用的是sdkExecute

$ret = array(#                                              # 返回值
    'err' => 0, #                                           # 错误码
    'platformCode' => 'zfb', #                              # 平台
    'cpOrderId' => $req->cpOrderId, #                       # 渠道订单号
    'platformInfo' => base64_encode($response)              # token串
);
echo json_encode($ret);                                     # 返回客户端
