Esempio n. 1
0
        public static async Task <Models.ApiViewModels.ApiAccountKycRequest> CheckKycRequest(ILogger logger, ApplicationDbContext context,
                                                                                             UserManager <ApplicationUser> userManager, KycSettings kycSettings, string applicationUserId, string token)
        {
            var jsonBody = JsonConvert.SerializeObject(new { token = token });
            var response = RestUtils.ServiceRequest(kycSettings.KycServerUrl, "status", jsonBody);

            if (response.IsSuccessful)
            {
                var json = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.Content);
                if (json.ContainsKey("status"))
                {
                    var status = json["status"];
                    // update kyc level if complete
                    if (status.ToLower() == viafront3.Models.ApiViewModels.ApiRequestStatus.Completed.ToString().ToLower())
                    {
                        var newLevel = 2;
                        var user     = await userManager.FindByIdAsync(applicationUserId);

                        if (user == null)
                        {
                            return(null);
                        }
                        var userKyc = user.Kyc;
                        if (userKyc == null)
                        {
                            userKyc = new Kyc {
                                ApplicationUserId = user.Id, Level = newLevel
                            };
                            context.Kycs.Add(userKyc);
                        }
                        else if (userKyc.Level < newLevel)
                        {
                            userKyc.Level = newLevel;
                            context.Kycs.Update(userKyc);
                        }
                        context.SaveChanges();
                    }
                    // return to user
                    var model = new viafront3.Models.ApiViewModels.ApiAccountKycRequest
                    {
                        Token      = token,
                        ServiceUrl = $"{kycSettings.KycServerUrl}/request/{token}",
                        Status     = status,
                    };
                    return(model);
                }
            }
            return(null);
        }
Esempio n. 2
0
        public static async Task <Models.ApiViewModels.ApiAccountKycRequest> CreateKycRequest(ILogger logger, ApplicationDbContext context,
                                                                                              UserManager <ApplicationUser> userManager, KycSettings kycSettings, string applicationUserId, string email)
        {
            // check request does not already exist
            var kycReq = context.KycRequests.Where(r => r.ApplicationUserId == applicationUserId).FirstOrDefault();

            if (kycReq != null)
            {
                return(await CheckKycRequest(logger, context, userManager, kycSettings, applicationUserId, kycReq.Token));
            }
            // call kyc server to create request
            var token    = Utils.CreateToken();
            var jsonBody = JsonConvert.SerializeObject(new { api_key = kycSettings.KycServerApiKey, token = token, email = email });
            var response = RestUtils.ServiceRequest(kycSettings.KycServerUrl, "request", kycSettings.KycServerApiSecret, jsonBody);

            if (response.IsSuccessful)
            {
                var json = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.Content);
                if (json.ContainsKey("status"))
                {
                    var status = json["status"];
                    // save to database
                    var date = DateTimeOffset.Now.ToUnixTimeSeconds();
                    kycReq = new KycRequest {
                        ApplicationUserId = applicationUserId, Date = date, Token = token
                    };
                    context.KycRequests.Add(kycReq);
                    context.SaveChanges();
                    // return to user
                    var model = new viafront3.Models.ApiViewModels.ApiAccountKycRequest
                    {
                        Token      = token,
                        ServiceUrl = $"{kycSettings.KycServerUrl}/request/{token}",
                        Status     = status,
                    };
                    return(model);
                }
            }
            else
            {
                logger.LogError($"kyc request ({kycSettings.KycServerUrl}) failed with http statuscode: {response.StatusCode}");
            }
            return(null);
        }