<?php

/**
 * 华为 配置信息
 */
class config_for_huawei
{

    /**
     * @var string 华为给开发者(商)分配的唯一标识
     */
    public $cpId = '220086000021803768';

    /**
     * @var string cp在华为后台注册的应用ID
     */
    public $appid = '100218075';

    /**
     * @var string 与华为约定的秘钥
     */
    public $appkey = '715316bfecf0a6ec87fa65d670e67f31';

    /**
     * @var string 应用包名
     */
    public $packageName = 'com.loyalsoft.ylsj.huawei';
    //
    /**
     * @var string 支付时开发者的五一标识
     */
    public $pay_appid = '220086000021803768';

    /**
     * @var string RSA私钥, 用于开发者APP调用HMS SDK支付接口时对请求参数进行签名
     */
    public $pay_key = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA4F5CjdXP4qF5I7fxzegn2og6H+7CdoTuAYz+IpsCdFTBBjWpHEbHH4BqwpIzMqoTyAw12zPf1HEw8RX+rZ/YcQIDAQABAkBll+jU5mrCONFsbf8MeEGLf0GqcecV2W0XwKrWKpA68ciBcPkMgudREmqllTTrhfdfTeJstw6rkXmmSrfFujgBAiEA8v9Q6C1bfpMziJm3HA5+1B8A01+yhMcjiEh1kd2YYvECIQDsX8EGn/v+tijpgnark3+ncmimuUt6YQrXqwfGAEvNgQIgcX/Ft5yv9rAkyvIZcBZpMQUGKrSxOomVVCucFrStX9ECIQDiA/kk5YrmFbXu+cGQ87uHF/17FghW3z2n1LDdIeFZgQIgcvkvphXTECwue8F5X6q+UeScOZYSwb+0pJKi/Kn+7nI=';

    /**
     * @var string RSA公钥, 用于开发者APP收到HMS SDK返回的支付结果时, 对响应参数进行验签.
     */
    public $pay_pub = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOBeQo3Vz+KheSO38c3oJ9qIOh/uwnaE7gGM/iKbAnRUwQY1qRxGxx+AasKSMzKqE8gMNdsz39RxMPEV/q2f2HECAwEAAQ==';

    /**
     *
     * @var string 游戏私钥 用于开发者对登录返回结果进行签名并传递给华为游戏服务器
     */
    public $game_key = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIuXw1TShvCJTwWiuJEudiQ9l5qr9Fx6TS2umTni/K5ekSvQNiL2Kv0IWXojAV94kfY8tQy1vEyHqXy/gRX0iiARGEP5VcEASRIIa93q6M5oW3p/v6/szQwGKhqjvaxXKHPJ6VTscrqHLblltWbJQhaG6mjOk2YcNnjK7g8tbsWPAgMBAAECgYAzCv9l2MV21v6KNQIANFkGz27MI+xqlF38QkfNHu7+Y535w1zIU/NMKu1s97fczR2ySGh7iUidQ036S5xMBusqWEHSnW5XramfjrH9y4D/QT3BdN4CbG6628BI80VZecLn4bHk9mwRsm5Nx8Kb3KzjSafxgxOpG+gjztqKVSaf4QJBAPDhZD+CeFcwfIW2JWvGDQYFYZb529x2i/Xy7l+xn97RN5ztl9NGXN3/CC5SOJnv7rZO5wxQqYsYfilnCmnCEGMCQQCUWtKSoYiK/+pT5zKa7HU2yGp/O8XZ4va+Ln23I6DMTXmSyrK2HMSSk+tO4puKX0Y2RatuieGeen6CRCHeB3/lAkEA7Ck+fNmwnGLoIrvi0lgZGqcZ98g921bKf7bOKDngcLcUXCHA8Z2RnQDWB3wLDDllA442DifjIYFA59yjI+7k2wJAMJHLftYDrf466V035CCl/1l+BEm9bKgxv/HYlqzDaB+1Pam++IBSmupyj1EBOzwV8I6+oWHiy8+uu1cy/03VQQJBALkVyl34LCMlyfbvN/ERR0GPg+kJ3PnaxxA/FJ3sARf3sqyCpNGLysSzCXCuBdgjkHoF4SjMZgHdIH4SUNByV2c=';

    /**
     * @var string 游戏公钥 用于开发者验证收到的华为游戏服务器的验签结果.
     */
    public $game_pub = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLl8NU0obwiU8ForiRLnYkPZeaq/Rcek0trpk54vyuXpEr0DYi9ir9CFl6IwFfeJH2PLUMtbxMh6l8v4EV9IogERhD+VXBAEkSCGvd6ujOaFt6f7+v7M0MBioao72sVyhzyelU7HK6hy25ZbVmyUIWhupozpNmHDZ4yu4PLW7FjwIDAQAB';

    /**
     * @var string 华为后台验证登录状态的地址
     */
    public $verifySession_url = 'https://gss-cn.game.hicloud.com/gameservice/api/gbClientApi';

    /**
     * @var string 华为后台验证登录状态时的请求方式(POST)
     */
    public $verifySession_method = 'POST';

    /**
     * configs for 华为
     * @return config_for_huawei Description
     */
    static function Inst()
    {
        return new config_for_huawei();
    }

    function getPubkey()
    {
        $devPubKey = $this->pay_pub;
        $begin_public_key = "-----BEGIN PUBLIC KEY-----\r\n";
        $end_public_key = "-----END PUBLIC KEY-----\r\n";

        $str = sprintf($begin_public_key);
        $raw = strlen($devPubKey) / 64;
        $index = 0;
        while ($index <= $raw) {
            $line = substr($devPubKey, $index * 64, 64) . "\r\n";
            if (strlen(trim($line)) > 0) {
                $str .= sprintf($line);
            }
            $index++;
        }
        sprintf($end_public_key);
    }

    function getPayPrivKey()
    {
        $priKey = $this->pay_key;
        if (strpos($priKey, "BEGIN RSA PRIVATE KEY") === false) {
            $priKey = wordwrap($priKey, 64, "\n", true);
            $priKey = "-----BEGIN PRIVATE KEY-----\n" . $priKey . "\n-----END PRIVATE KEY-----";
        }
        return $priKey;
    }

    /**
     * 创建支付签名
     * @param array $data
     */
    function paySign($data)
    {
        unset($data['sign']);
        ksort($data);                                                           # 除sign以外的所有字段, 字典排序
        $arr = array();
        foreach ($data as $key => $val) {
            if ($val) {
                array_push($arr, $key . '=' . urldecode($val));                 # 排序后的数组, 需要对value进行urldecode
            }
        }
        $query_string = join('&', $arr);                                        # 重新拼接为字符串
        $key = openssl_pkey_get_private($this->getPayPrivKey());
        $str = loyalsoft\CommUtil::str2UTF8($query_string);
        \loyalsoft\CLog::warn("待签名串:$str");
        $r = openssl_sign($str, $sign, $key, 'RSA-SHA256');                     # 签名算法
        if ($r) {
            return base64_encode($sign);
        }
        die("gen signature failed!");
    }

}

//include_once __DIR__ . '/../../../main.php';             # 导入game utils
//$sign = 'S6mnrL/LC1zUlk4IGEJrzDCFSGReHRyEapQ94fKyQ9KoFXdgbdP2KiRiUwKkbzW2C+Wxd/PtwQIWfLF911dyyQ==';
//$str = 'amount=0.01&applicationID=100218075&country=CN&currency=CNY&merchantId=220086000021803768&productDesc=立刻购买尊享9折优惠&productName=60钻石&requestId=1803200947000321&sdkChannel=1&url=http://115.159.121.129/ylsj/Gameserver/Amfphp/service_call/pay/huawei/notify.php&urlver=2';
//\loyalsoft\echoLine(config_for_huawei::Inst()->test($str));
//\loyalsoft\echoLine($sign);

