private async Task <BonusCustomerStatus> ProcessBonusCustomer(BonusCustomerRequest contract)
        {
            var emailHash = HashHelper.ComputeSha256Hash(contract.Email);

            _log.Info($"Started processing bonus for customer with id: {contract.CustomerId}",
                      new { contract.CustomerId, emailHash, contract.PartnerId, LocationId = contract.ExternalLocationId });

            var bonusCustomerStatus = ValidateBonusCustomerRequestData(contract);

            if (bonusCustomerStatus != BonusCustomerStatus.OK)
            {
                _log.Info($"Stopped processing bonus for customer with id: {contract.CustomerId}" +
                          " due to invalid request data", new { contract.CustomerId, emailHash });
                return(bonusCustomerStatus);
            }

            LocationInfoResponse locationInfoResponse = null;

            if (!string.IsNullOrWhiteSpace(contract.ExternalLocationId))
            {
                locationInfoResponse = await _partnerManagementClient.Locations.GetByExternalId2Async(contract.ExternalLocationId);

                if (locationInfoResponse == null)
                {
                    return(BonusCustomerStatus.LocationNotFound);
                }
            }

            var partnerInfo = await _partnerAndLocationHelper.ValidatePartnerInfo(contract.PartnerId, locationInfoResponse);

            if (partnerInfo != PartnerAndLocationStatus.OK)
            {
                if (partnerInfo == PartnerAndLocationStatus.PartnerNotFound)
                {
                    return(BonusCustomerStatus.PartnerNotFound);
                }
                if (partnerInfo == PartnerAndLocationStatus.LocationNotFound)
                {
                    return(BonusCustomerStatus.LocationNotFound);
                }
            }

            var emailMissing      = string.IsNullOrWhiteSpace(contract.Email);
            var customerIdMissing = string.IsNullOrWhiteSpace(contract.CustomerId);

            CustomerProfileResponse customer;

            if (!emailMissing && !customerIdMissing)
            {
                customer = await _customerProfileClient.CustomerProfiles.GetByCustomerIdAsync(contract.CustomerId);

                if (customer.Profile == null)
                {
                    _log.Warning($"Customer not found with id: {contract.CustomerId}", null, contract.CustomerId);
                    return(BonusCustomerStatus.CustomerNotFound);
                }

                if (customer.Profile.Email != contract.Email)
                {
                    _log.Info($"Stopped processing bonus for customer with id: {contract.CustomerId}" +
                              " due to customerId and email miss-match", new { contract.CustomerId, emailHash });
                    return(BonusCustomerStatus.CustomerIdDoesNotMatchEmail);
                }
            }

            if (!customerIdMissing && emailMissing)
            {
                customer = await _customerProfileClient.CustomerProfiles.GetByCustomerIdAsync(contract.CustomerId);

                if (customer.Profile == null)
                {
                    _log.Warning($"Customer not found with id: {contract.CustomerId}", null, contract.CustomerId);
                    return(BonusCustomerStatus.CustomerNotFound);
                }

                contract.Email = customer.Profile.Email;
            }
            else if (customerIdMissing && !emailMissing)
            {
                customer = await _customerProfileClient.CustomerProfiles.GetByEmailAsync(
                    new GetByEmailRequestModel { Email = contract.Email });

                if (customer.Profile == null)
                {
                    _log.Warning($"Customer not found with email: {emailHash}", null, emailHash);
                    return(BonusCustomerStatus.CustomerNotFound);
                }

                contract.CustomerId = customer.Profile.CustomerId;
            }

            var fiatAmountHash = HashHelper.ComputeSha256Hash(contract.FiatAmount.Value.ToString());

            _log.Info($"Using hotel referral for email {emailHash}", new
            {
                contract.CustomerId,
                contract.PartnerId,
                contract.Currency,
                LocationId = contract.ExternalLocationId,
                contract.PosId,
                contract.PaymentTimestamp,
                fiatAmountHash,
                emailHash
            });

            var useResponse = await _referralClient.ReferralHotelsApi.UseAsync(new ReferralHotelUseRequest
            {
                PartnerId    = contract.PartnerId,
                Amount       = contract.FiatAmount.Value,
                BuyerEmail   = contract.Email,
                CurrencyCode = contract.Currency,
                Location     = contract.ExternalLocationId
            });

            if (useResponse.ErrorCode != ReferralHotelUseErrorCode.None)
            {
                var errorCode = useResponse.ErrorCode.ToString();
                _log.Warning($"Could not use referral for customerId: {contract.CustomerId}", null, new { contract.CustomerId, errorCode });
            }

            _log.Info($"Publishing bonus trigger event with CustomerId: {contract.CustomerId}" +
                      $" and PartnerId: {contract.PartnerId}", new
            {
                contract.CustomerId,
                contract.PartnerId,
                contract.Currency,
                LocationId = contract.ExternalLocationId,
                contract.PosId,
                contract.PaymentTimestamp,
                fiatAmountHash,
                emailHash
            });

            await _bonusCustomerEventPublisher.PublishAsync(new BonusCustomerTriggerEvent
            {
                CustomerId = contract.CustomerId,
                PartnerId  = contract.PartnerId,
                Currency   = contract.Currency,
                Amount     = contract.FiatAmount.Value,
                LocationId = contract.ExternalLocationId
            });

            _log.Info($"Finished processing bonus for customer with id: {contract.CustomerId}",
                      new
            {
                contract.CustomerId,
                contract.PartnerId,
                contract.Currency,
                LocationId = contract.ExternalLocationId,
                contract.PosId,
                contract.PaymentTimestamp,
                fiatAmountHash,
                emailHash
            });

            return(BonusCustomerStatus.OK);
        }
Exemplo n.º 2
0
        public async Task <PaymentsCreateResponse> CreatePaymentRequestAsync(PaymentsCreateRequest contract)
        {
            _log.Info("Creating payment request",
                      new { contract.CustomerId });

            ValidateCreatePaymentRequest(contract);

            LocationInfoResponse locationInfoResponse = null;

            var paymentRequestModel = _mapper.Map <PaymentRequestModel>(contract);

            if (!string.IsNullOrWhiteSpace(contract.ExternalLocationId))
            {
                locationInfoResponse = await _partnerManagementClient.Locations.GetByExternalId2Async(contract.ExternalLocationId);

                if (locationInfoResponse == null)
                {
                    return(new PaymentsCreateResponse {
                        Status = PaymentCreateStatus.LocationNotFound
                    });
                }

                paymentRequestModel.LocationId = locationInfoResponse.Id.ToString();
            }

            var partnerAndLocationStatus = await _partnerAndLocationHelper.ValidatePartnerInfo(contract.PartnerId, locationInfoResponse);

            if (partnerAndLocationStatus != PartnerAndLocationStatus.OK)
            {
                if (partnerAndLocationStatus == PartnerAndLocationStatus.PartnerNotFound)
                {
                    return new PaymentsCreateResponse {
                               Status = PaymentCreateStatus.PartnerNotFound
                    }
                }
                ;
                if (partnerAndLocationStatus == PartnerAndLocationStatus.LocationNotFound)
                {
                    return new PaymentsCreateResponse {
                               Status = PaymentCreateStatus.LocationNotFound
                    }
                }
                ;
            }

            if (contract.PaymentInfo != null)
            {
                var sendMessageResponse =
                    await _messagesService.SendMessageAsync(_mapper.Map <MessagesPostRequest>(contract));

                switch (sendMessageResponse.ErrorCode)
                {
                case MessagesErrorCode.CustomerIsBlocked:
                    return(new PaymentsCreateResponse {
                        Status = PaymentCreateStatus.CustomerIsBlocked
                    });

                case MessagesErrorCode.CustomerNotFound:
                    return(new PaymentsCreateResponse {
                        Status = PaymentCreateStatus.CustomerNotFound
                    });

                case MessagesErrorCode.LocationNotFound:
                    return(new PaymentsCreateResponse {
                        Status = PaymentCreateStatus.LocationNotFound
                    });

                case MessagesErrorCode.PartnerNotFound:
                    return(new PaymentsCreateResponse {
                        Status = PaymentCreateStatus.PartnerNotFound
                    });
                }

                paymentRequestModel.PartnerMessageId = sendMessageResponse.PartnerMessageId;
            }

            var responseModel = await _partnersPaymentsClient.Api.PartnerPaymentAsync(paymentRequestModel);

            var responseContract = new PaymentsCreateResponse()
            {
                PaymentRequestId = responseModel.PaymentRequestId, Status = PaymentCreateStatus.OK
            };

            if (responseModel.Error != PaymentRequestErrorCodes.None)
            {
                _log.Error(null, $"Received error code {responseModel.Error}", responseModel.PaymentRequestId);
                responseContract.Status = _mapper.Map <PaymentCreateStatus>(responseModel.Error);
            }

            //Only save if everything is ok and callback url is provided
            if (responseModel.Error == PaymentRequestErrorCodes.None &&
                !string.IsNullOrEmpty(contract.PaymentProcessedCallbackUrl))
            {
                await _paymentCallbackRepository.InsertAsync(new PaymentProcessedCallbackUrl
                {
                    PaymentRequestId = responseModel.PaymentRequestId,
                    RequestAuthToken = contract.RequestAuthToken,
                    Url = contract.PaymentProcessedCallbackUrl
                });
            }

            return(responseContract);
        }
        public async Task <MessagesPostResponse> SendMessageAsync(MessagesPostRequest contract)
        {
            _log.Info("Send Message Async started",
                      new
            {
                contract.CustomerId,
                LocationId = contract.ExternalLocationId,
                contract.PartnerId,
                contract.SendPushNotification,
                contract.PosId,
                contract.Subject
            });

            //Check customer id
            var customerBlockState =
                await _customerManagementServiceClient.CustomersApi.GetCustomerBlockStateAsync(contract.CustomerId);

            if (customerBlockState.Error == CustomerBlockStatusError.CustomerNotFound)
            {
                _log.Warning("Customer Not Found", null,
                             new { contract.CustomerId, contract.PartnerId, contract.ExternalLocationId });

                return(new MessagesPostResponse
                {
                    PartnerMessageId = null, ErrorCode = MessagesErrorCode.CustomerNotFound
                });
            }
            else if (customerBlockState.Status == CustomerActivityStatus.Blocked)
            {
                _log.Warning("Customer Is Blocked", null,
                             new { contract.CustomerId, contract.PartnerId, contract.ExternalLocationId });

                return(new MessagesPostResponse
                {
                    PartnerMessageId = null, ErrorCode = MessagesErrorCode.CustomerIsBlocked
                });
            }

            //Check location
            LocationInfoResponse locationInfoResponse = null;

            if (!string.IsNullOrWhiteSpace(contract.ExternalLocationId))
            {
                locationInfoResponse =
                    await _partnerManagementClient.Locations.GetByExternalId2Async(contract.ExternalLocationId);

                if (locationInfoResponse == null)
                {
                    _log.Warning("Location Not Found", null,
                                 new { contract.CustomerId, contract.PartnerId, contract.ExternalLocationId });

                    return(new MessagesPostResponse
                    {
                        PartnerMessageId = null, ErrorCode = MessagesErrorCode.LocationNotFound
                    });
                }
            }

            //Check partner and location id
            var partnerAndLocationStatus =
                await _partnerAndLocationHelper.ValidatePartnerInfo(contract.PartnerId, locationInfoResponse);

            if (partnerAndLocationStatus != PartnerAndLocationStatus.OK)
            {
                var(message, errorCode) = partnerAndLocationStatus == PartnerAndLocationStatus.LocationNotFound
                    ? ("Location Not Found", MessagesErrorCode.LocationNotFound)
                    : ("Partner Not Found", MessagesErrorCode.PartnerNotFound);

                _log.Warning(message, null, new { contract.CustomerId, contract.PartnerId, contract.ExternalLocationId });

                return(new MessagesPostResponse {
                    PartnerMessageId = null, ErrorCode = errorCode
                });
            }

            var messageId = await _messagesRepository.InsertAsync(contract);

            _log.Info("Message saved", messageId);

            try
            {
                await _messageContentRepository.SaveContentAsync(messageId.ToString(), contract.Message);
            }
            catch (Exception ex)
            {
                _log.Error(ex, "Failed to save message content", new { messageId, contract.Message });

                await _messagesRepository.DeleteMessageAsync(messageId.ToString());

                throw;
            }

            _log.Info("Message content saved", messageId);

            //Send push notification via Rabbit
            if (contract.SendPushNotification.HasValue && contract.SendPushNotification.Value)
            {
                var evt = new PushNotificationEvent
                {
                    CustomerId         = contract.CustomerId,
                    MessageTemplateId  = _partnerMessageTemplateId,
                    Source             = _componentSourceName,
                    TemplateParameters =
                        new Dictionary <string, string>
                    {
                        { "Subject", contract.Subject },
                        { "PartnerMessageId", messageId.ToString() }
                    }
                };

                await _partnerMessagesPublisher.PublishAsync(evt);

                _log.Info("Partner message published", new { messageId, contract.CustomerId, contract.Subject });
            }

            return(new MessagesPostResponse
            {
                ErrorCode = MessagesErrorCode.OK, PartnerMessageId = messageId.ToString()
            });
        }
Exemplo n.º 4
0
        public async Task <ReferralInformationResponse> GetReferralInformationAsync(ReferralInformationRequest contract)
        {
            ValidateReferralInformationRequestData(contract);

            var response = new ReferralInformationResponse
            {
                Status    = ReferralInformationStatus.OK,
                Referrals = new List <Domain.Models.Referral>()
            };

            LocationInfoResponse locationInfoResponse = null;

            if (!string.IsNullOrWhiteSpace(contract.ExternalLocationId))
            {
                locationInfoResponse = await _partnerManagementClient.Locations.GetByExternalId2Async(contract.ExternalLocationId);

                if (locationInfoResponse == null)
                {
                    response.Status = ReferralInformationStatus.LocationNotFound;
                    return(response);
                }
            }

            var partnerAndLocationStatus = await _partnerAndLocationHelper.ValidatePartnerInfo(contract.PartnerId,
                                                                                               locationInfoResponse);

            if (partnerAndLocationStatus != PartnerAndLocationStatus.OK)
            {
                if (partnerAndLocationStatus == PartnerAndLocationStatus.PartnerNotFound)
                {
                    response.Status = ReferralInformationStatus.PartnerNotFound;
                }
                if (partnerAndLocationStatus == PartnerAndLocationStatus.LocationNotFound)
                {
                    response.Status = ReferralInformationStatus.LocationNotFound;
                }

                return(response);
            }

            var customerProfile = await _customerProfileClient.CustomerProfiles.GetByCustomerIdAsync(contract.CustomerId);

            if (customerProfile.ErrorCode != CustomerProfileErrorCodes.None)
            {
                _log.Warning("Could not find customer profile", null,
                             new { contract.CustomerId, customerProfile.ErrorCode });

                response.Status = ReferralInformationStatus.CustomerNotFound;
                return(response);
            }

            var referralInfo =
                await _referralClient.ReferralHotelsApi.GetByEmailAsync(
                    new GetHotelReferralsByEmailRequestModel
            {
                Email     = customerProfile.Profile.Email,
                PartnerId = contract.PartnerId,
                Location  = locationInfoResponse?.Id.ToString(),
            });

            foreach (var hotelReferral in referralInfo.HotelReferrals)
            {
                var referralContract = await GetProcessedReferral(hotelReferral);

                if (referralContract == null)
                {
                    _log.Warning($"Skipped processing referral with Id {hotelReferral.Id} due to technical problem");
                    continue;
                }

                response.Referrals.Add(referralContract);
            }

            return(response);
        }