<?php

/**
 * Description of notify.php
 *  九游充值结果回调接口
 * @version
 *          1.0.0 Created at 2017-12-21. by --gwang
 * @author gwang (mail@wanggangzero.cn)
 * @copyright ? 2017-12-21, SJZ LoyalSoft Corporation & gwang. All rights reserved.
 */
/**
 * 1. 提取参数
 * 2. 校验
 * 3. 转成订单数据
 * 4. 分析订单状态
 * 5. 依状态处理对应逻辑
 * 6. 入库mysql+日志
 * 7. 返回
 */
// 导入game库
include_once __DIR__ . '/../../../main.php';
require_once __DIR__ . '/service/SDKServerService.php';
require_once __DIR__ . '/model/SDKException.php';
require_once __DIR__ . '/util/ConfigHelper.php';
require_once __DIR__ . '/util/LoggerHelper.php';

require_once __DIR__ . '/../Mo/OrderNotice.php';

use loyalsoft\CLog;
use loyalsoft\HttpUtil;
use loyalsoft\pay\OrderNotice;

try {

    $request = HttpUtil::getQueryString();       // 接收HTTP POST信息
    CLog::pay('[uc.notify]收到的支付回调请求:' . $request);
    $responseData = json_decode($request, true); // 处理支付回调请求
    if ($responseData != null) {
        CLog::pay('[uc.notify]' . var_export($responseData, true));
        $baseService = new BaseSDKService();
        $signSource = $baseService->getSignData($responseData['data'])
                . ConfigHelper::getStrVal("sdkserver.game.apikey");             //组装签名原文
        $sign = md5($signSource);                                               //MD5加密签名
        CLog::pay("[uc.notify][签名原文]:" . $signSource);
        CLog::pay("[uc.notify][签名结果]:" . $sign);
        if ($sign == $responseData['sign']) {
            //游戏需根据orderStatus参数的值判断是否给玩家过账虚拟货币。（S为充值成功、F为充值失败，避免假卡、无效卡充值成功）
            $order = OrderNotice::Parse_UcOrder($responseData);
            if ($order->Check()) {                                              # 校验订单
                if ($order->status == 1) {                                      # 订单状态是成功
                    $order->UpdateOrderStatus();                                # 更新订单状态,->已付款
                    CLog::pay("[uc.notify][发货] 订单ID: " . $order->cpOrderId . ", 金额(分): " . $order->amount);
                } else if ($order->status == 2) {                               # status为2(failed)的情况
                    $order->UpdateOrderStatus();                                # 直接更新订单状态,->支付失败
                    CLog::pay("[uc.notify][不发货] 订单ID: " . $order->cpOrderId);
                } else {
                    echo 'FAILURE';
                    CLog::pay("[uc.notify][处理结果]: FAILURE (未知的支付状态)");      # 日志
                    return;
                }
                CLog::pay("[uc.notify][处理结果]:" . "SUCCESS");
                echo 'SUCCESS';                                             //返回给sdk server的响应内容
                return;
            }
        }
        CLog::pay("[uc.notify][处理结果]:" . "FAILURE");
        echo 'FAILURE'; //返回给sdk server的响应内容 ,对于重复多次通知失败的订单,请参考文档中通知机制。
        return;
    } else {
        CLog::pay("[uc.notify] 提取参数失败:" . $request);
        throw new exception('json_decode()异常');
    }
} catch (SDKException $e) {
    CLog::pay("[uc.notify] 程序执行异常:" . $e->getMessage());
    throw new exception($e->getMessage());
}