<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CallBack.aspx.cs" Inherits="Kesion.NET.WebSite.Api.alipay.AlipayCallBack" %>
<%@ Import Namespace="Kesion.Cache"%>
<%@ Import Namespace="Kesion.Publics"%>
<%@ Import Namespace="System.Text"%>
<%@ Import Namespace="System.Collections.Generic"%>
<%@ Import Namespace="Aop.Api"%>
<%@ Import Namespace="Aop.Api.Request"%>
<%@ Import Namespace="Aop.Api.Response"%>

<script runat="server" type="text/C#">

    //应用id
    public string app_id = "";
    //密匙
    public string merchant_private_key = "";
    //公匙
    public string alipay_public_key = "";


    protected void Page_Load(object sender, EventArgs e)
    {
        if (MyCache.GetAccountAPIConfig("alipay", "enable") != "true")
        {
            Utils.ShowError("对不起，本站没有开通支付宝快捷登录!", "error.gif", 2, KSCMS.GetInstallDir());
        }
        //alipay_callback();
        GetToken();
    }

    //支付宝登陆第二步     根据获取的用户授权码  进行获取令牌操作

    private void GetToken()
    {
        //获取授权码信息
        string app_auth_code = KSCMS.S("auth_code");
        //用户令牌
        string accessToken = "";
        //应用id
        string app_id = MyCache.GetAccountAPIConfig("alipay", "apiid");//"2019092567804706";
        //私匙信息
        string merchant_private_key = MyCache.GetAccountAPIConfig("alipay", "apikey");// "MIIEowIBAAKCAQEAnJbxWRZDJAqAaokW7w/6vkc2kISplOylC+fdoZvk8/HvwOqd8TAZMh33EIAZBv4N8zVLT4l9indq0ripmmV4fN+6IbOzP5VBi8PFND//m0z3zOsEnXDzlsy3IW5HY2sLSZfaP0oiSgmJ/qtOSET6SwCkwF/3ZMn84QsE8xRwK9h3zElzwV3FtSjV6ALZ0wEU1pxQdYFlx4gMaK5YwdFwCFw7Q27+S3uiWMVsTyIkGrMO7p+xlQghOKRjUzpsT5MaWlO1V+wCJmW5B/u7TVMda+va97tDLEwCrid3qbBWb0KbLw4d77464KbnbtoLT+7TYCILrlvOeSQ5QYyT1BW5ywIDAQABAoIBAGMhh57fYhfh8hP/v54OXq2xBZEN+xGFkqLr4boaq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OiOpBJzbK5Qm0T1P+3DceutzKysKDvU7f5TldpOpP3QUpqYF+0Gsa0Q24gj2LfxUePda/R3FJ3cjPLZlTjxsC+hUss7T9omJO";
        //公匙信息
        string alipay_public_key = MyCache.GetAccountAPIConfig("alipay", "publickey");//"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoYBo2HFtelkpupkS/BMRgSHY4QUs+0DJY3a3fJlaNjld/7KBZuCELsXYvoOt1eLsv+xn4H1DIUdwMjPrNxRJgCgPoVComrfnG3Yu1R1ecDE/LqiY9HW564BBa+CUsKLRZXvdf6DkTyr5E8FF3/+LiX979cgY2czJZtu23Nuv0Q0ti8F/d3ljLajTsWzk204JmuhuwU6T6zdZbtk5/tb3SZEedKJA7+5aB3sdcjVvL1y/OJ+cPB6uWDKaZggDk5HUR3dvflrpPeVdFNtX0SOQ0MtAMse+TYDnvFLMKpHNEJ53XZYHhh96F9ggyfDutoc1ZUyFiKJUQyXew6+TSHOpWQIDAQAB";


        try
        {

            //获取用户的授权令牌信息   用于请求用户的基本信息
            IAopClient clientAuth = new DefaultAopClient("https://openapi.alipay.com/gateway.do", app_id, merchant_private_key, "json", "1.0", "RSA2", alipay_public_key, "utf-8", false);
            AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
            request.GrantType = "authorization_code";
            request.Code = app_auth_code;
            AlipaySystemOauthTokenResponse response = clientAuth.Execute(request);

            //获取用户的授权令牌信息   用于请求用户的基本信息
            accessToken = response.AccessToken;

            //第二步  根据用户令牌获取  用户数据信息
            IAopClient client2 = new DefaultAopClient("https://openapi.alipay.com/gateway.do", app_id, merchant_private_key, "json", "1.0", "RSA2", alipay_public_key, "utf-8", false);
            AlipayUserInfoShareRequest request2 = new AlipayUserInfoShareRequest();
            AlipayUserInfoShareResponse response2 = client2.Execute(request2, accessToken);


            //用户授权的数据信息
            Session["alipay_user_id"] = response2.UserId;
            Session["alipay_token"] = response2.UserId;
            Session["real_name"] = response2.NickName;
            KSCMS.Redirect("AlipayBind.aspx");
        }
        catch {
            JS.Alert("后台配置出错！");
        }
    }


</script>