using System;
using System.Collections.Generic;
using System.Linq;
using Aop.Api;
using Library;
using LitJson;
using Microsoft.AspNetCore.Http;
using Aop.Api.Request;
using Aop.Api.Response;
using Aop.Api.Util;
using System.Collections;
using MySystem.Models.Main;
using Newtonsoft.Json;

namespace MySystem
{
    public class AlipayFunction
    {
        private HttpContext _context;
        public AlipayFunction(HttpContext context)
        {
            _context = context;
        }
        public AlipayFunction()
        {
        }



        //小程序
        string MiniAppId = "2021004100642805";
        //应用公钥
        // string PublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnTchC1mU0rFnRv8/eqDz1i/gS5Wd5fDD/TobKDBlj1UgQPRnT9g8wy/uTtqMnImR09ZQ5C/Ekw8B2GlN6bLbza0+7Fym7rNNKw3EvEL8tQ6pMe1GbIu68tCEjIslPXswGCgGRI30LeSj0BElaSAaqbvvyTHp+vxNLwav/hSGokPa8C76SzgCpvvzjXk9oGiNV5eutdoWEKbwyvw45kv70JHragNANTqU8DouzPLHenoD5V+QqJw1He9mZSb08QgSHCFW1dau8Hmxy0oXsfUVXXHJfzkhoyJOerYiR+oVuRqaIIF9xFlUPsNdpsAnJCP+HaAqRJpvuKfV4hNdg7uvIQIDAQAB";
        //支付宝公钥
        string MiniAlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxuS3Edgrruau8SO2DsK0f2KcE+A6pGJ1f8CdL395qwoX5RnEx9yRUotkwN0ZF6qMUojFKu/J/X2DpxBWBc6jIa+cnFwPUykefXcXj/AQsgiIbFzNXBH9NSz+hngXP6xDs8D1u9h+fJB+TYD+WFXKwOcGwcx23uHLHUDi1UIEZOhT2Eui+aMCYVygUWH7JynTtbFQZyZcSsD2Dxj5OJNJlK110jarFgvqY98WxqtaRtB9Y8whwRRlGfa5GP2r/Vt3xbGeAflRl5khFgRLZgeFIfxJpEj1TZV2tCtpFS0uhXJGEQbMnzFynx8UaOx/0hBCyik1MQuu3flym6nx7XxPSQIDAQAB";
        string MiniPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmIK2EVxir9uspaS96y2giySdjv8jpekg8ZaLUluTv5DxbYbacAAVIxPs19yT2saFVhb3Qhwcx/03913J0v8DjEDQqiySZqV5y9z+C5NXGTGp5PyQK4q1wdAtg2TKkHlMRPrN/nhaFDEu6Hehoub1xEPm06qQU44A77yMnrZ5YJcS/Cv22lvcikVp6sThPmBMmmA+BF/v6hufYGWRlnWoAhl2lfmR9kSfuH1dGp/OT0WiDF7WD8ZR4iPAM8bpvBuq/UDiHBaZMJogtqkzBDH4EqiD+Tzg0ALn5zbSRiTk+GebjZNPkaW68v0LpyWOt8wbOmjh9EyaW4gEMOHxrJzZHwIDAQAB";
        //应用私钥
        string MiniPrivateKey = "MIIEowIBAAKCAQEAmIK2EVxir9uspaS96y2giySdjv8jpekg8ZaLUluTv5DxbYbacAAVIxPs19yT2saFVhb3Qhwcx/03913J0v8DjEDQqiySZqV5y9z+C5NXGTGp5PyQK4q1wdAtg2TKkHlMRPrN/nhaFDEu6Hehoub1xEPm06qQU44A77yMnrZ5YJcS/Cv22lvcikVp6sThPmBMmmA+BF/v6hufYGWRlnWoAhl2lfmR9kSfuH1dGp/OT0WiDF7WD8ZR4iPAM8bpvBuq/UDiHBaZMJogtqkzBDH4EqiD+Tzg0ALn5zbSRiTk+GebjZNPkaW68v0LpyWOt8wbOmjh9EyaW4gEMOHxrJzZHwIDAQABAoIBAARdmYCduGV4UFTdhmI5CCNzhN8XBpo1NjA0MwDEhlIxf/e2qIIUPoamh10Wf1T/4ck5FxLZYymGYhe+o4xaqGtXX6G/bOFrUzU2uj7ylMev2EEpyaa1bcaCgwsD05iXIEvJMiRapZl2XvVgl0vmeJqJpw3gEAmUQwqZxxyz9H2DSkPMaSqJFczM5vmOUCnf+gK5BTgO9H12r+IeoxfU1inu3tyuqlUAHbPmKFrQ4hJoiqHXMZKxddBxhSwfxkJj0494WErRsXdFqcu1CFtV8g2dXdrXCGsFq0cNl3Rq0sge/Q6otHrIIFLA7rRHEPe6Ub2uodQLpn0s+QTDC/nMVmECgYEAxwM5d63vht6FVIf4G1UnIOFqaPawpUmoxO2srNQtSlU+Jqi7aEXW4XNYihgZtIotmTaCHBwHrxPkdYI7kz2jn0WQmcnDjEwnARR1phOSJ+yCPUCbWMwQcorxaQn3gYu1ndoED87rqiUhTogyslTGvMeC7NIFnoLpWEn5UN7eQFsCgYEAxC6e2e1YF7vBwHHoYFvUwF57klBA9N9VMaw80mjGLuaTJwyxeFYH2QOpbeVs3upmfmmJSuOAVzg7QBkgBw0x6Wzrz831rCWGFRNZwDLnIP5h2G7SNmeNKe+HqV7M4jQ7Yelbgvgi/U6BedEyjfM4ekldBJkRsPP8QcWaeNeG5Y0CgYEAnZb4EsUxP2NFwUJKPZqYeXkGgkeWacyCUC7E5ba+hnYp2QMUI+Xp2m00V2XuNQCWSxTWwtuPWhwudL7D3jyZLK4xaxZSOu6ylDBXWUN2IRcvqsFWQmKVQ56yu7Kgw3yWPIE/ZiU8iGcOkNlZC84F8ccq7c0BlhukG724J0nURLECgYAWXTYwo7TqJ7ZDqPiTm4RaVasCl/F/uMHugrMATX9iM1jNF85XaH9GhvtWktPhaZk7ie5jwiGMT9W5TePvydpJwOf07Ya3FCP/3Q/bMDJW+YjP8no2R4G5KRwu+6ahltC6La8H9B+Q/tUVvPplYjerARgAgS6aHIZCPTOJ2RAAtQKBgGpJtAIZ8sE4wVCDwd1zuVTF40SkUeeoU6dRNVFV1Z++S4XHbjEGLf8ee95kpCv1jL1W70kh50zqSJP+3DnqOeTOvVudhTwBdtDd9YapmJMHh6vObP1WSXStq/LzULK2N8jxWiMJo32lE4HwsO3rH9aRe2ioObqo4zd+C6ZSzfqo";

        //小程序AES密钥，目前用于解密手机号
        string Key = "CxMrVGoYbx+XCEqGqJkP/A==";
        public string GetAlipayUserId(string Auth_code)
        {
            string result = "";
            try
            {
                IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", MiniAppId, MiniPrivateKey, "json", "1.0", "RSA2", MiniAlipayPublicKey, "UTF-8", false);
                //获取access_token
                AlipaySystemOauthTokenRequest requestAccess_token = new AlipaySystemOauthTokenRequest();
                requestAccess_token.GrantType = "authorization_code";
                requestAccess_token.Code = Auth_code;
                AlipaySystemOauthTokenResponse responseAccess_token = client.Execute(requestAccess_token);
                function.WriteLog(responseAccess_token.Body, "支付宝授权");
                string AlipayUserId = responseAccess_token.AlipayUserId; //20881043611849171819285350010300
                string UserId = responseAccess_token.UserId; //2088222145274001
                result = UserId + "|" + AlipayUserId;
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "支付宝授权报错日志");
            }
            return result;
        }
    }
}
