public async Task <IActionResult> SignupNewMerchant([FromBody] SignupModel model) { if (!_signupService.EnableSignup) { return(Forbid()); } _log.Info($"Signup started for merchant {model.MerchantName} by {HttpContext?.Connection?.RemoteIpAddress?.ToString().SanitizeIp()}"); var merchantId = _signupService.GetIdFromName(model.MerchantName); #region Validate try { var merchant = await _payMerchantClient.Api.GetByIdAsync(merchantId); if (merchant != null) { return(BadRequest(ErrorResponse.Create(PayInvoicePortalApiErrorCodes.Signup.MerchantExist))); } } catch (ClientApiException e) when(e.HttpStatusCode == HttpStatusCode.NotFound) { } try { var employee = await _payInvoiceClient.GetEmployeeByEmailAsync(model.EmployeeEmail); if (employee != null) { return(BadRequest(ErrorResponse.Create(PayInvoicePortalApiErrorCodes.Signup.EmployeeEmailExist))); } } catch (ErrorResponseException e) when(e.StatusCode == HttpStatusCode.NotFound) { } #endregion try { var apiKey = StringUtils.GenerateId(); // create merchant var merchant = await _payMerchantClient.Api.CreateAsync(new CreateMerchantRequest { Name = merchantId, DisplayName = model.MerchantName, ApiKey = apiKey, Email = model.EmployeeEmail }); await _payAuthClient.RegisterAsync(new RegisterRequest { ApiKey = apiKey, ClientId = merchant.Id }); // create employee var employee = await _payInvoiceClient.AddEmployeeAsync(new CreateEmployeeModel { Email = model.EmployeeEmail, FirstName = model.EmployeeFirstName, LastName = model.EmployeeLastName, MerchantId = merchant.Id }); await _payAuthClient.RegisterAsync(new RegisterModel { EmployeeId = employee.Id, MerchantId = merchant.Id, Email = model.EmployeeEmail, Password = model.EmployeePassword, ForceEmailConfirmation = true }); var token = employee.Id.ToBase64(); var sent = await _emailService.SendEmailConfirmationAsync( $"{employee.FirstName} {employee.LastName}", $"{model.HostUrl.TrimEnd('/')}/signup/confirmEmail/{token}", new[] { employee.Email } ); if (!sent) { return(BadRequest(ErrorResponse.Create(PayInvoicePortalApiErrorCodes.Signup.EmailNotSent))); } } catch (ClientApiException e) when(e.HttpStatusCode == HttpStatusCode.BadRequest && e.ErrorResponse.ErrorMessage == "Merchant with the same email already exists") { return(BadRequest(ErrorResponse.Create(PayInvoicePortalApiErrorCodes.Signup.MerchantEmailExist))); } catch (Exception e) { _log.Error(e, $"Error occured for merchant {model.MerchantName}, id: {merchantId}"); return(BadRequest(ErrorResponse.Create(PayInvoicePortalApiErrorCodes.UnexpectedError))); } _log.Info($"Signup success for merchant {model.MerchantName} by {HttpContext?.Connection?.RemoteIpAddress?.ToString().SanitizeIp()}"); return(Ok()); }
public void Test_GetIdFromName_Valid(string name) { var id = _signupService.GetIdFromName(name); Assert.AreEqual("NewMerchantName", id); }