<?php

/**
 * 金立 配置信息
 */
class config_for_gionee
{
//    /**
//     * @var string cp在平台注册的应用ID
//     */
//    public $appid;

    /**
     * @var string 与平台约定的秘钥
     */
    public $appkey = '6132256582774767902D65B5637EF31C';

    /**
     * @var string secretkey 密钥
     */
    public $app_secret = '9187B069633740FF9A62AF914D63D8A4';

    /**
     * @var string 支付私钥
     */
    public $pay_priKey = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL/wCIlhQZKTpriajAI6LIIFgd3j4houusaSpkufwxj1vLeiq7g/oktGYrPAamTORoK3xeuWwPTnsT4n/vGBRcjxPSzMbnHty4oE15gjRuKbLVHethrDXo2VNCdb8o/y4iVNnpQGAa1zvc5WMuO8SVIn2uwNcnTncuF/edgKaRz1AgMBAAECgYBSXFOtb7hNh2IxMNAHXKMgn/BcIJ8zEP5Am0kUhleXbEswmXMKOvQPTQKpk1WUMBd9nBInx6bA8HLT8yGozAn/MoOhMNx9vkqZkQ6pi/B0Lei5+PjFeyeCiynplwzgUWgqqkT9WYzue7jz1EzVCOdelmBHp1YahJjP/yThqPvouQJBAPaYTUiEEqxXvkzkWhv8lZrMfndAQxhRFFdaTnTNZBrJEw7CrJPSnxGl0sPRPLsDMK5Yb2cAXJQ3TQuxJD1DukMCQQDHQhHZ+uE5VEN7uKq3Eqk2qVMf67AyiRkDxLiT62h8reO+EfZaJhkscdEtCedUnn2xO0gOMBkjVnvIdDL+DWRnAkBDkQWT+gQ3zq7o2KTMCFBhouh/+k6oCgc9zBO1M1XbdP7Qy4yH0G0fY8U1PE6cm0oqazbTDSlQvzpQGtBoLPeLAkEAqRULiOk2zSsPyKC/XHJdXKD6hI+/CGMF8wivh/WuyH3WT6d2YLELk4dRdOJ+hnOnPQmM710pN69fgo3351T96QJBAMBw2p8uu1JROk4wrxiguiAXr6cs25fTVyOeZdAbCx+P81JyYYx9rbg/2QjN6qu24rsUfrSrDVGGeqMyNaJrzPk=';

    /**
     * @var string 支付公钥
     */
    public $pay_pubKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDJ92Q9HyPyjEl2dkhRvvOWz2BUIdwmv5iSyU6or3zXxU6Xw3+gRb7IeksGnS4W2xNJohRf4QV4ETmpVsYoCmCO+K4U5PFybN1kth4pktHUbAxDFN78H3qVTvwVCbG4SUmYJvsbzueEJzK2aXEGkR/w4IKohU1gtA3fUc2wTX/sXwIDAQAB';

    /**
     * @var string 平台验证登录状态并换取uid的接口地址
     */
    public $ApiURL_verifySession = "https://id.gionee.com/account/verify.do";

    /**
     * @var string 平台验证登录状态时的请求方式(get)
     */
    public $verifySession_method = 'GET';

    /**
     * @var string 平台创建订单的URL
     */
    public $ApiURL_pay_createOrder = 'https://pay.gionee.com/amigo/create/order';
    public $createOrder_method = 'POST';

    /**
     * configs for 金立
     * @return config_for_gionee Description
     */
    static function Inst()
    {
        return new config_for_gionee();
    }

    /**
     * 创建订单的签名方法
     */
    function pay_sign($post_arr)
    {
        $private_key = $this->pay_priKey;
        ksort($post_arr);
        $signature_str = '';
        foreach ($post_arr as $key => $value) {
            $signature_str .= $value;
        }
        // 【NOTE】跑通demo后替换成商户自己的private_key
        $pem = chunk_split($private_key, 64, "\n");
        $pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
        $private_key_id = openssl_pkey_get_private($pem);
        $signature = false;
        $r = openssl_sign($signature_str, $signature, $private_key_id);
        if ($r) {
            $sign = base64_encode($signature);
            return $sign;
        }
        die('gen signature failed!');
    }

    /**
     * 验证支付通知中的签名
     * @param array $post_arr
     * @return type
     */
    function notify_rsa_verify($post_arr)
    {
        $signature = base64_decode(urldecode($post_arr['sign']));
        unset($post_arr['ext_info']);
        unset($post_arr['sign']);
        ksort($post_arr);
        $arr = array();
        foreach ($post_arr as $key => $val) {
            array_push($arr, $key . '=' . $val);
        }
        $signature_str = join('&', $arr);
        $pem = chunk_split($this->pay_pubKey, 64, "\n");
        $pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
        $public_key_id = openssl_pkey_get_public($pem);
        return openssl_verify($signature_str, $signature, $public_key_id, 'RSA-SHA1');
    }

}
