Beispiel #1
0
        private static Payment ParseForPayment(string[] recordParts, MerchantFileType merchantFileType, int lineNumber)
        {
            string locationId         = recordParts[5];
            string acquirerMerchantId = recordParts[25];
            string acquirerIca        = recordParts[26];

            if (merchantFileType == MerchantFileType.MasterCardProvisioning || merchantFileType == MerchantFileType.MasterCardClearing)
            {
                if (string.IsNullOrWhiteSpace(locationId))
                {
                    Log.Error($"LocationID is missing for {merchantFileType.ToString()} detail record at line number {lineNumber}");
                    return(null);
                }
                return(new Payment
                {
                    Id = Guid.NewGuid().ToString(),
                    Processor = PaymentProcessor.MasterCard,
                    IsActive = true,
                    SyncedWithCommerce = false,
                    PaymentMids = new Dictionary <string, string>
                    {
                        { MerchantConstants.MCLocationId, locationId }
                    }
                });
            }
            else if (merchantFileType == MerchantFileType.MasterCardAuth)
            {
                if (string.IsNullOrWhiteSpace(acquirerIca))
                {
                    Log.Error($"Auth AcquirerICA is missing for {merchantFileType.ToString()} detail record at line number {lineNumber}");
                    return(null);
                }
                if (string.IsNullOrWhiteSpace(acquirerMerchantId))
                {
                    Log.Error($"Auth Acquirer MerchantID is missing for {merchantFileType.ToString()} detail record at line number {lineNumber}");
                    return(null);
                }
                return(new Payment
                {
                    Id = Guid.NewGuid().ToString(),
                    Processor = PaymentProcessor.MasterCard,
                    IsActive = true,
                    SyncedWithCommerce = false,
                    PaymentMids = new Dictionary <string, string>
                    {
                        { MerchantConstants.MCAcquiringICA, acquirerIca },
                        { MerchantConstants.MCAcquiringMid, acquirerMerchantId }
                    }
                });
            }

            return(null);
        }
Beispiel #2
0
        private static string ParseForMerchantName(string[] recordParts, MerchantFileType merchantFileType)
        {
            string merchantName   = recordParts[7];
            string mcMerchantName = recordParts[8];

            if ((merchantFileType == MerchantFileType.MasterCardAuth || merchantFileType == MerchantFileType.MasterCardClearing) &&
                (!string.IsNullOrWhiteSpace(mcMerchantName)))
            {
                return(mcMerchantName);
            }
            else
            {
                return(merchantName);
            }
        }
        public async Task <ActionResult> AddMerchants(string id, MerchantFileType merchantFileType, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                if (file.FileName.EndsWith(".xlsx", StringComparison.InvariantCultureIgnoreCase) || file.FileName.EndsWith(".csv", StringComparison.InvariantCultureIgnoreCase))
                {
                    //string fileName = Path.GetFileName(file.FileName);
                    string fileName = string.Concat(Path.GetFileNameWithoutExtension(file.FileName),
                                                    DateTime.UtcNow.ToString("yyyyMMddHHmmssfff"),
                                                    Path.GetExtension(file.FileName));

                    Log.Info("uploading file '{0}' with filename '{1}'", Path.GetFileName(file.FileName), fileName);
                    CloudBlobContainer cloudBlobContainer = CloudBlobHelper.GetBlobContainer(ConfigurationManager.AppSettings["IngestionStorageConnectionString"], ConfigurationManager.AppSettings["IngestionStorageContainerName"]);
                    CloudBlockBlob     cloudBlockBlob     = cloudBlobContainer.GetBlockBlobReference(fileName);
                    cloudBlockBlob.Properties.ContentType = file.ContentType;
                    await cloudBlockBlob.UploadFromStreamAsync(file.InputStream);

                    string result = null;
                    using (var client = GetHttpClient())
                    {
                        HttpResponseMessage response = await client.PostAsJsonAsync(MerchantApi + "import", new { ProviderId = id, FileName = fileName, MerchantFileType = merchantFileType, Author = User.Identity.Name });

                        if (response.IsSuccessStatusCode)
                        {
                            result = await response.Content.ReadAsAsync <string>();
                        }
                    }

                    if (string.IsNullOrWhiteSpace(result))
                    {
                        throw new HttpException((int)HttpStatusCode.InternalServerError, "Scheduling file processor job failed");
                    }

                    return(Content("Scheduled JobId: " + result));
                }
            }

            throw new HttpException((int)HttpStatusCode.BadRequest, "Invalid file");
        }
Beispiel #4
0
        public static Merchant Parse(string record, char delimiter, MerchantFileType merchantFileType, int lineNumber)
        {
            Merchant merchant = null;

            if (string.IsNullOrWhiteSpace(record))
            {
                Log.Error($"Invalid {merchantFileType.ToString()} detail record. Empty record at line numnber {lineNumber}");
                return(null);
            }

            string[] recordParts = record.Split(new char[] { delimiter });
            if (recordParts.Length != 39)
            {
                Log.Error($"Invalid {merchantFileType.ToString()} detail record. Number of fields did not equate to 39 at line numnber {lineNumber}");
                return(null);
            }

            string actionCode = recordParts[2];

            if (merchantFileType == MerchantFileType.MasterCardProvisioning && actionCode != "A")
            {
                Log.Error($"Invalid {merchantFileType.ToString()} detail record at line number {lineNumber}.Detail record has an action code other than Add. This cannot be handled");
                return(null);
            }

            string mcId     = recordParts[33];
            string mcSiteId = recordParts[3];

            if (merchantFileType == MerchantFileType.MasterCardAuth || merchantFileType == MerchantFileType.MasterCardClearing)
            {
                if (actionCode != MerchantMatchedActionCode && actionCode != MerchantNonMatchedActionCode &&
                    actionCode != ValidatedMatchActionCode)
                {
                    Log.Error($"Invalid {merchantFileType.ToString()} detail record at line number {lineNumber}. Detail record has an action code other than Match/NonMatch/ValidatedMatch. This cannot be handled");
                    return(null);
                }
                if (actionCode == MerchantNonMatchedActionCode)
                {
                    Log.Warn($"No match found for MID in {merchantFileType.ToString()} at line number {lineNumber}");
                    return(null);
                }
                if (!string.IsNullOrEmpty(mcId) && mcId.Length != MCIDLENGTH)
                {
                    Log.Error($"Invalid {merchantFileType.ToString()} detail record at line number {lineNumber}. Invalid length for MasterCard Microsoft Unique Identifier");
                    return(null);
                }
                if (string.IsNullOrEmpty(mcId) && string.IsNullOrEmpty(mcSiteId))
                {
                    Log.Error($"Invalid {merchantFileType.ToString()} detail record at line number {lineNumber}. Both MCID and MCSiteId is missing");
                    return(null);
                }
            }

            Payment payment = ParseForPayment(recordParts, merchantFileType, lineNumber);

            if (payment != null)
            {
                merchant = new Merchant
                {
                    IsActive    = true,
                    PhoneNumber = recordParts[20],
                    Location    = new Location
                    {
                        Address = !string.IsNullOrWhiteSpace(recordParts[10]) ? recordParts[10] : recordParts[11],
                        City    = !string.IsNullOrWhiteSpace(recordParts[12]) ? recordParts[12] : recordParts[13],
                        State   = !string.IsNullOrWhiteSpace(recordParts[14]) ? recordParts[14] : recordParts[15],
                        Zip     = !string.IsNullOrWhiteSpace(recordParts[16]) ? recordParts[16] : recordParts[17]
                    },
                    Payments = new List <Payment> {
                        payment
                    },
                    ExtendedAttributes = new Dictionary <string, string>
                    {
                        { MerchantConstants.MCSiteId, mcSiteId },
                        { MCBeginDate, recordParts[29] },
                        { MCFileDate, recordParts[38] }
                    }
                };
                merchant.Name = ParseForMerchantName(recordParts, merchantFileType);
                if (!string.IsNullOrWhiteSpace(mcId))
                {
                    merchant.ExtendedAttributes.Add(MerchantConstants.MCID, mcId);
                }
            }

            return(merchant);
        }
        private Tuple <string, IList <Merchant> > ParseMasterCardFile(Stream fileStream, MerchantFileType merchantFileType)
        {
            bool   validFile            = true;
            string provisioningFileDate = null;

            List <Merchant> lstMerchants = new List <Merchant>();

            using (StreamReader streamReader = new StreamReader(fileStream))
            {
                int    lineNumber  = 0;
                string currentLine = streamReader.ReadLine();
                while (!string.IsNullOrWhiteSpace(currentLine) && validFile)
                {
                    string recordType = currentLine.Substring(0, RecordLengthConstants.RecordType);

                    switch (recordType)
                    {
                    case HeaderRecord.RecordType:
                        Log.Info("Parsing the header record");
                        provisioningFileDate = HeaderRecord.Parse(currentLine, MerchantConstants.MasterCardRecordDelimiter);
                        validFile            = !string.IsNullOrWhiteSpace(provisioningFileDate);
                        lineNumber++;
                        break;

                    case DetailRecord.ProvisioningRecordType:
                        Log.Info($"Parsing the {merchantFileType.ToString()} merchant detail record");
                        Merchant merchant = DetailRecord.Parse(currentLine, MerchantConstants.MasterCardRecordDelimiter, merchantFileType, lineNumber);
                        if (merchant != null)
                        {
                            lstMerchants.Add(merchant);
                            Log.Info($"Parsed the {merchantFileType.ToString()} in line number {lineNumber}");
                        }
                        lineNumber++;
                        break;

                    case DetailRecord.ResponseFileRecordType:
                        Log.Info($"Parsing the {merchantFileType.ToString()} merchant detail record");
                        merchant = DetailRecord.Parse(currentLine, MerchantConstants.MasterCardRecordDelimiter, merchantFileType, lineNumber);
                        if (merchant != null)
                        {
                            Merchant existingMerchant = null;

                            //Check if we have already seen this merchant in the current auth/clearing file based on MCID or MCSiteId
                            //If so, then update the payment info for the existing merchant. If not, add the
                            //new merchant to the list.
                            if (merchant.ExtendedAttributes != null && merchant.ExtendedAttributes.ContainsKey(MerchantConstants.MCID))
                            {
                                existingMerchant = lstMerchants.FirstOrDefault(m => (m.ExtendedAttributes != null && m.ExtendedAttributes.ContainsKey(MerchantConstants.MCID) &&
                                                                                     m.ExtendedAttributes[MerchantConstants.MCID] == merchant.ExtendedAttributes[MerchantConstants.MCID]));
                            }
                            else if (merchant.ExtendedAttributes != null && merchant.ExtendedAttributes.ContainsKey(MerchantConstants.MCSiteId))
                            {
                                existingMerchant = lstMerchants.FirstOrDefault(m => (m.ExtendedAttributes != null && m.ExtendedAttributes.ContainsKey(MerchantConstants.MCSiteId) &&
                                                                                     m.ExtendedAttributes[MerchantConstants.MCSiteId] == merchant.ExtendedAttributes[MerchantConstants.MCSiteId]));
                            }

                            if (existingMerchant != null)
                            {
                                existingMerchant.Payments.Add(merchant.Payments[0]);
                            }
                            else
                            {
                                lstMerchants.Add(merchant);
                            }
                            Log.Info($"Parsed the {merchantFileType.ToString()} in line number {lineNumber}");
                        }
                        lineNumber++;
                        break;

                    case TrailerRecord.RecordType:
                        Log.Info("Parsing the trailer record");
                        int detailRecordCount = TrailerRecord.Parse(currentLine, MerchantConstants.MasterCardRecordDelimiter);
                        //linenumber -2 = discarding the header record to get the total detailed records read
                        validFile = ((lineNumber - 1) == detailRecordCount);
                        break;

                    default:
                        Log.Error($"Unknown record type {recordType} in line number {lineNumber}");
                        lineNumber++;
                        break;
                    }

                    currentLine = streamReader.ReadLine();
                }
            }

            return(validFile ? new Tuple <string, IList <Merchant> >(provisioningFileDate, lstMerchants) : null);
        }