private IHttpActionResult CreateDonorForUnauthenticatedUser(CreateDonorDTO dto) { MpContactDonor donor; try { donor = _donorService.GetContactDonorForEmail(dto.email_address); } catch (Exception e) { var msg = "Error getting donor for email " + dto.email_address; logger.Error(msg, e); var apiError = new ApiErrorDto(msg, e); throw new HttpResponseException(apiError.HttpResponseMessage); } int existingDonorId = (donor == null) ? 0 : donor.DonorId; try { donor = _donorService.CreateOrUpdateContactDonor(donor, String.Empty, dto.first_name, dto.last_name, dto.email_address, dto.stripe_token_id, DateTime.Now); } catch (PaymentProcessorException e) { return(e.GetStripeResult()); } catch (Exception e) { var msg = "Error creating donor for email " + dto.email_address; logger.Error(msg, e); var apiError = new ApiErrorDto(msg, e); throw new HttpResponseException(apiError.HttpResponseMessage); } var responseBody = new DonorDTO { Id = donor.DonorId, ProcessorId = donor.ProcessorId, RegisteredUser = false, Email = donor.Email }; // HTTP StatusCode should be 201 (Created) if we created a donor, or 200 (Ok) if returning an existing donor var statusCode = (existingDonorId == donor.DonorId) ? HttpStatusCode.OK : HttpStatusCode.Created; return(ResponseMessage(Request.CreateResponse(statusCode, responseBody))); }
public MpContactDonor CreateDonor(CheckScannerCheck checkDetails) { MpContactDonor mpContactDonorById = null; // If scanned check has a donor id, try to use it to lookup the donor if (checkDetails.DonorId != null && checkDetails.DonorId > 0) { mpContactDonorById = _donorService.GetContactDonorForDonorId(checkDetails.DonorId.Value); } // Get the MpContactDonor and info based off of the account and routing number to see if we need to create a new one var contactDonorByAccount = _donorService.GetContactDonorForDonorAccount(checkDetails.AccountNumber, checkDetails.RoutingNumber) ?? new MpContactDonor(); // if find by mpContact donor id is used then mpContact donor found by id matches mpContact donor // found by account and account has stripe token if (mpContactDonorById != null && mpContactDonorById.ContactId == contactDonorByAccount.ContactId && contactDonorByAccount.Account.HasPaymentProcessorInfo()) { return(contactDonorByAccount); } // if find by mpContact donor id is not used then mpContact donor // found by account has stripe token else if (mpContactDonorById == null && contactDonorByAccount.Account != null && contactDonorByAccount.Account.HasPaymentProcessorInfo()) { return(contactDonorByAccount); } var contactDonor = mpContactDonorById ?? contactDonorByAccount; var account = _mpDonorService.DecryptCheckValue(checkDetails.AccountNumber); var routing = _mpDonorService.DecryptCheckValue(checkDetails.RoutingNumber); var token = _paymentService.CreateToken(account, routing, checkDetails.Name1); var encryptedKey = _mpDonorService.CreateHashedAccountAndRoutingNumber(account, routing); contactDonor.Details = new MpContactDetails { DisplayName = checkDetails.Name1, Address = new MpPostalAddress { Line1 = checkDetails.Address.Line1, Line2 = checkDetails.Address.Line2, City = checkDetails.Address.City, State = checkDetails.Address.State, PostalCode = checkDetails.Address.PostalCode } }; var newDonor = _donorService.CreateOrUpdateContactDonor(contactDonor, string.Empty, string.Empty, string.Empty, string.Empty, null, DateTime.Now); newDonor.Account = new MpDonorAccount { AccountNumber = account, RoutingNumber = routing, Type = AccountType.Checking, EncryptedAccount = encryptedKey, Token = token.Id }; return(newDonor); }