public static async Task <Merchant> GetMerchantForSearch(this Merchant merchant, MerchantSearchType searchType) { if (searchType == MerchantSearchType.SearchUsingParametersSupplied) { return(merchant); } if (searchType == MerchantSearchType.SearchUsingWildCardInName) { return(merchant.GetMerchantForSearchUsingWildCardInName()); } if (searchType == MerchantSearchType.SearchWithoutStreetAddress) { return(merchant.GetMerchantForSearchWithoutStreetAddress()); } if (searchType == MerchantSearchType.SearchUsingBingAddress) { return(await merchant.GetMerchantForSearchUsingBingAddress()); } return(null); }
private async Task <SearchInfo> GetPaymentDetailsByAttributeAsync(Merchant merchant, bool isNational = true) { List <Payment> payments = null; var errMsg = string.Empty; var merchantSearchTypes = isNational ? MerchantSearchTypesForNational : MerchantSearchTypesForNonNational; MerchantSearchType finalMerchantSearchType = MerchantSearchType.SearchUsingParametersSupplied; foreach (var merchantSearchType in merchantSearchTypes) { finalMerchantSearchType = merchantSearchType; var merchantSearch = await merchant.GetMerchantForSearch(merchantSearchType); if (merchantSearch == null) { continue; } if (merchantSearchType == MerchantSearchType.SearchUsingBingAddress || merchantSearchType == MerchantSearchType.SearchWithoutStreetAddress) { if (string.Equals(merchantSearch.Location.Address, merchant.Location.Address, StringComparison.CurrentCultureIgnoreCase)) { continue; } } var request = merchantSearch.GetSearchMerchantByAttributeRequest(VisaConstants.CommunityCodeGroupLevel); var policy = retryPolicy.Get(); var response = await policy.ExecuteAsync( async() => { //if the city or zip is empty should we still call and let Visa API return the correct error var client = VisaRtmClientManager.Instance.GetVisaRtmClient(); var response1 = await client.SearchMerchantDetailsByAttributeAsync(request); return(response1); } ); //var singleMerchantReturnedForEmptyAddress = false; if (response?.Merchants != null && response.Merchants.Any()) { //var merchantAddressNullOrEmpty = string.IsNullOrEmpty(merchantSearch.Address); //singleMerchantReturnedForEmptyAddress = merchantAddressNullOrEmpty && response.Merchants.Length == 1; //if (singleMerchantReturnedForEmptyAddress || !merchantAddressNullOrEmpty) { payments = new List <Payment>(); foreach (var visaMerchant in response.Merchants) { //TODO: should we check visaMerchant.IsDeleted //If VISA return multiple merchants do we have to do the matching on our end - this is only problem if our request contains only merchant name and zip code //like STARBUCKS, 98052. In this case VISA will return multiple locations. However we are assuming we will send VISA complete address. In this case VISA should //not return multiple locations. If it returns multiple MIDS then we can assume they are for the same location. Need to be verified if (visaMerchant.VisaMerchantId > 0 && visaMerchant.VisaStoreId > 0) { var paymentMids = new Dictionary <string, string> { { MerchantConstants.VisaMid, visaMerchant.VisaMerchantId.ToString(CultureInfo.InvariantCulture) }, { MerchantConstants.VisaSid, visaMerchant.VisaStoreId.ToString(CultureInfo.InvariantCulture) } }; var visaPaymentInfo = new Payment { Id = Guid.NewGuid().ToString(), Processor = PaymentProcessor.Visa, PaymentMids = paymentMids, IsActive = true, LastUpdate = DateTime.UtcNow }; payments.Add(visaPaymentInfo); } } //if not national chain and only a single merchant then update merchant mid name and merchant sid name if (!isNational && response.Merchants.Length == 1) { if (merchant.ExtendedAttributes == null) { merchant.ExtendedAttributes = new Dictionary <string, string>(); } var visaMerchant = response.Merchants[0]; merchant.ExtendedAttributes[MerchantConstants.VisaMidName] = visaMerchant.VisaMerchantName; merchant.ExtendedAttributes[MerchantConstants.VisaSidName] = visaMerchant.VisaStoreName; //await OnboardMerchantsAsync(visaMerchant); } } } if (payments != null && payments.Any()) { if (string.IsNullOrEmpty(merchantSearch.Location.Address)) { finalMerchantSearchType = MerchantSearchType.SearchWithoutStreetAddress; } break; } if (response.HasError()) { var errorInfoDetail = response.GetErrorInfoDetail(); errMsg = errMsg + Environment.NewLine + $"SearchType:{merchantSearchType} VisaErrorInfo:{errorInfoDetail}"; } //else if (!singleMerchantReturnedForEmptyAddress) //{ // errMsg = errMsg + Environment.NewLine + $"SearchType:{merchantSearchType}. Multiple merchants returned for empty address"; //} else { errMsg = errMsg + Environment.NewLine + $"SearchType:{merchantSearchType}. No MID information returned by Visa"; } } if (payments == null || !payments.Any()) { var merchantInfo = merchant.GetMerchantInfo(); errMsg = errMsg + "MerchantInfo:" + merchantInfo; throw new Exception(errMsg); } var searchInfo = new SearchInfo { Payments = payments, SearchType = finalMerchantSearchType }; return(searchInfo); }