<?php

/**
 * 三星后台通知接收窗口
 * @version
 *          1.0.0 Created at 2018-3-3. by --gwang
 * @author gwang (mail@wanggangzero.cn)
 * @copyright ? 2018-3-3, SJZ LoyalSoft Corporation & gwang. All rights reserved.
 */
include_once __DIR__ . '/../../../main.php';             # 导入game utils
include_once __DIR__ . '/../Mo/payResp.php';                # 导入通用返回值结构
include_once __DIR__ . '/../Mo/payRequest.php';          # 导入通用订单请求结构
require_once __DIR__ . '/../Mo/OrderNotice.php';         # 统一订单结构
require_once __DIR__ . '/config_sanxing.php';

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

try {
    CLog::pay("[notify.三星] 收到支付回调请求: " . HttpUtil::getQueryString());
    $cfg = config_sanxing::Inst();                                                 # 配置信息
    //获取notifyData，需要添加notifyData=
    $notifyData = 'transdata=%7B%22appid%22%3A%22500000185%22%2C%22count%22%3A1%2C%22cporderid%22%3A%22lrtest5bf98ef3-6543-4d2d-9f3f-1cadfda3fc0e%22%2C%22cpprivate%22%3A%22%22%2C%22feetype%22%3A2%2C%22money%22%3A1%2C%22paytype%22%3A5%2C%22result%22%3A%220%22%2C%22transid%22%3A%2232011407211739380639%22%2C%22transtime%22%3A%222014-07-21+17%3A39%3A38%22%2C%22transtype%22%3A0%2C%22waresid%22%3A1%7D&sign=OvL34IZYWbHJXUyXXBRSgZjeqoxPK8Bh68UArbDbd9mf0LZDYFwiIuB6Vqt4MtiphCOJN9cNr4aJzY0cCaq1CxO6PABmyMf37umL5uOro58NcoImKHIhxtaHenPGlY46aaf6GgGagY4MqY%2BjEdbFlbJQh4Y%2BdvHkUKjJVyEdQsE%3D&signtype=RSA';
    $notifyData = HttpUtil::getQueryString();
    if (!$cfg->parseResp($notifyData, $notifyJson)) {
        echo "FAILURE";
        CLog::pay("[notify.三星]解析回调请求失败");
    }
//    print_r($notifyJson);
    $order = OrderNotice::Parse_samsung((array) $notifyJson);                   # 将参数归一化到order
    if ($order != null) {
        if ($order->Check()) {                                                  # 订单校验, 1代表支付成功
            if ($order->status == 1) {                                          # 订单状态是成功
                $order->UpdateOrderStatus();                                    # 更新订单状态,->已付款
                CLog::pay("[notify.三星] [发货] 订单: " . $order->cpOrderId . ", 金额: " . $order->amount);
            } else if ($order->status == 2) {                                   # status为2(failed)的情况
                $order->UpdateOrderStatus();                                    # 直接更新订单状态,->支付失败
                CLog::pay("[notify.三星] [不发货] 订单: " . $order->cpOrderId);
            } else {
                echo 'FAILURE';
                CLog::pay("[notify.三星][处理结果]: FAILURE (未知的支付状态)");   # 日志
                return;
            }
            echo 'SUCCESS'; //返回给sdk server的响应内容
            CLog::pay("[notify.三星][处理结果]:" . "SUCCESS");                   # 日志
            return;
        }
    }
    CLog::pay("[notify.三星][处理结果]:" . "FAILURE");                           # 日志
    echo 'FAILURE'; //返回给sdk server的响应内容 ,对于重复多次通知失败的订单,请参考文档中通知机制。
    return;
} catch (Exception $e) {
    CLog::pay("[notify.三星]" . $e->getMessage());                              # 日志
    throw new exception($e->getMessage());
}