PHP服务端生成支付宝infostr

    /**
     * InfoStr APP登录需要的的infostr
     * @return String
     */
    public function infoStr()
    {
        $infoStr = http_build_query([
            'apiname' => 'com.alipay.account.auth',
            'method' => 'alipay.open.auth.sdk.code.get',
            'app_id' => '',//应用id
            //'app_id' => '2016102800773326',//沙箱 应用id
            'app_name' => 'mc',
            'biz_type' => 'openservice',
            'pid' => '', //商户id,2088开头的
            'product_id' => 'APP_FAST_LOGIN',
            'scope' => 'kuaijie',
            'target_id' => mt_rand(999, 99999), //商户标识该次用户授权请求的ID,该值在商户端应保持唯一
            'auth_type' => 'AUTHACCOUNT', // AUTHACCOUNT代表授权;LOGIN代表登录
            'sign_type' => 'RSA2',
        ]);
        $infoStr .= '&sign=' . $this->enRSA2($infoStr);
        JXReturn_Json(0, $infoStr, '请求成功');
    }

    /**
     * enRSA2 RSA加密
     * @param String $data
     * @return String
     */
    private function enRSA2($data)
    {
        $key = "支付宝开放平台后台配置的MD5密钥";
        $str = chunk_split(trim($key), 64, "\n");
        $key = "-----BEGIN RSA PRIVATE KEY-----\n$str-----END RSA PRIVATE KEY-----\n";
        // $key = file_get_contents(storage_path('rsa_private_key.pem')); 为文件时这样引入
        $signature = '';
        $signature = openssl_sign($data, $signature, $key, OPENSSL_ALGO_SHA256) ? base64_encode($signature) : NULL;
        return $signature;
    }

你可能感兴趣的:(PHP)