示例#1
0
        internal override void MapResponse(Element response)
        {
            base.MapResponse(response);

            string category = response.GetValue <string>("TableCategory") ?? "";

            if (category == null)
            {
                category = LastCategory;
            }
            var fieldValues = new Dictionary <string, string>();

            foreach (Element field in response.GetAll("Field"))
            {
                fieldValues.Add(field.GetValue <string>("Key"), field.GetValue <string>("Value"));
            }

            if (category.EndsWith("SUMMARY", StringComparison.OrdinalIgnoreCase))
            {
                var summary = new SummaryResponse {
                    SummaryType      = MapSummaryType(category),
                    Count            = fieldValues.GetValue <int>("Count"),
                    Amount           = fieldValues.GetAmount("Amount"),
                    TotalAmount      = fieldValues.GetAmount("Total Amount"),
                    AuthorizedAmount = fieldValues.GetAmount("Authorized Amount"),
                    AmountDue        = fieldValues.GetAmount("Balance Due Amount"),
                };

                if (category.Contains("APPROVED"))
                {
                    if (Approved == null)
                    {
                        Approved = new Dictionary <SummaryType, SummaryResponse>();
                    }
                    Approved.Add(summary.SummaryType, summary);
                }
                else if (category.StartsWith("PENDING"))
                {
                    if (Pending == null)
                    {
                        Pending = new Dictionary <SummaryType, SummaryResponse>();
                    }
                    Pending.Add(summary.SummaryType, summary);
                }
                else if (category.StartsWith("DECLINED"))
                {
                    if (Declined == null)
                    {
                        Declined = new Dictionary <SummaryType, SummaryResponse>();
                    }
                    Declined.Add(summary.SummaryType, summary);
                }
            }
            else if (category.EndsWith("RECORD", StringComparison.OrdinalIgnoreCase))
            {
                TransactionSummary trans;
                if (category.Equals(LastCategory, StringComparison.OrdinalIgnoreCase))
                {
                    trans = LastTransactionSummary;
                }
                else
                {
                    trans = new TransactionSummary {
                        TransactionId         = fieldValues.GetValue <string>("TransactionId"),
                        OriginalTransactionId = fieldValues.GetValue <string>("OriginalTransactionId"),
                        TransactionDate       = fieldValues.GetValue <string>("TransactionTime").ToDateTime(),
                        TransactionType       = fieldValues.GetValue <string>("TransactionType"),
                        MaskedCardNumber      = fieldValues.GetValue <string>("MaskedPAN"),
                        CardType              = fieldValues.GetValue <string>("CardType"),
                        CardEntryMethod       = fieldValues.GetValue <string>("CardAcquisition"),
                        AuthCode              = fieldValues.GetValue <string>("ApprovalCode"),
                        IssuerResponseCode    = fieldValues.GetValue <string>("ResponseCode"),
                        IssuerResponseMessage = fieldValues.GetValue <string>("ResponseText"),
                        HostTimeout           = (bool)fieldValues.GetBoolean("HostTimeOut"),
                        // BaseAmount - Not doing this one
                        TaxAmount        = fieldValues.GetAmount("TaxAmount"),
                        GratuityAmount   = fieldValues.GetAmount("TipAmount"),
                        Amount           = fieldValues.GetAmount("RequestAmount"),
                        AuthorizedAmount = fieldValues.GetAmount("Authorized Amount"),
                        AmountDue        = fieldValues.GetAmount("Balance Due Amount")
                    };
                }
                if (!(category.Equals(LastCategory)))
                {
                    if (category.StartsWith("APPROVED"))
                    {
                        var summary = Approved[SummaryType.Approved];
                        summary.Transactions.Add(trans);
                    }
                    else if (category.StartsWith("PENDING"))
                    {
                        var summary = Pending[SummaryType.Pending];
                        summary.Transactions.Add(trans);
                    }
                    else if (category.StartsWith("DECLINED"))
                    {
                        var summary = Declined[SummaryType.Declined];
                        summary.Transactions.Add(trans);
                    }
                }
                LastTransactionSummary = trans;
            }
            LastCategory = category;
        }
示例#2
0
        public UpaSAFResponse(JsonDoc root)
        {
            Approved = new Dictionary <SummaryType, SummaryResponse>();
            Pending  = new Dictionary <SummaryType, SummaryResponse>();
            Declined = new Dictionary <SummaryType, SummaryResponse>();

            var firstDataNode = root.Get("data");

            if (firstDataNode == null)
            {
                throw new MessageException(INVALID_RESPONSE_FORMAT);
            }

            var cmdResult = firstDataNode.Get("cmdResult");

            if (cmdResult == null)
            {
                throw new MessageException(INVALID_RESPONSE_FORMAT);
            }

            Status = cmdResult.GetValue <string>("result");
            if (string.IsNullOrEmpty(Status))
            {
                var errorCode = cmdResult.GetValue <string>("errorCode");
                var errorMsg  = cmdResult.GetValue <string>("errorMessage");
                DeviceResponseText = $"Error: {errorCode} - {errorMsg}";
            }
            else
            {
                // If the Status is not "Success", there is either nothing to process, or something else went wrong.
                // Skip the processing of the rest of the message, as we'll likely hit null reference exceptions
                if (Status == "Success")
                {
                    var secondDataNode = firstDataNode.Get("data");
                    if (secondDataNode == null)
                    {
                        throw new MessageException(INVALID_RESPONSE_FORMAT);
                    }
                    Multiplemessage = secondDataNode.GetValue <string>("multipleMessage");
                    var safDetailsList = secondDataNode.GetEnumerator("SafDetails");

                    if (safDetailsList != null)
                    {
                        foreach (var detail in safDetailsList)
                        {
                            if (TotalAmount == null)
                            {
                                TotalAmount = 0.00m;
                            }

                            TotalAmount += detail.GetValue <decimal>("SafTotal");
                            TotalCount  += detail.GetValue <int>("SafCount");

                            SummaryResponse summaryResponse = new SummaryResponse()
                            {
                                TotalAmount  = detail.GetValue <decimal>("SafTotal"),
                                Count        = detail.GetValue <int>("SafCount"),
                                SummaryType  = MapSummaryType(detail.GetValue <string>("SafType")),
                                Transactions = new List <TransactionSummary>()
                            };

                            if (detail.Has("SafRecords"))
                            {
                                foreach (var record in detail.GetEnumerator("SafRecords"))
                                {
                                    TransactionSummary transactionSummary = new TransactionSummary()
                                    {
                                        TransactionType   = record.GetValue <string>("transactionType"),
                                        TerminalRefNumber = record.GetValue <string>("transId"), // The sample XML says tranNo?
                                        ReferenceNumber   = record.GetValue <string>("referenceNumber"),
                                        GratuityAmount    = record.GetValue <decimal>("tipAmount"),
                                        TaxAmount         = record.GetValue <decimal>("taxAmount"),
                                        Amount            = record.GetValue <decimal>("baseAmount"),
                                        AuthorizedAmount  = record.GetValue <decimal>("authorizedAmount"),
                                        //AmountDue = record.GetValue<decimal>("totalAmount"),
                                        CardType         = record.GetValue <string>("cardType"),
                                        MaskedCardNumber = record.GetValue <string>("maskedPan"),
                                        TransactionDate  = record.GetValue <DateTime>("transactionTime"),
                                        AuthCode         = record.GetValue <string>("approvalCode"),
                                        HostTimeout      = record.GetValue <bool>("hostTimeOut"),
                                        CardEntryMethod  = record.GetValue <string>("cardAcquisition"),
                                        Status           = record.GetValue <string>("responseCode"),
                                        //record.GetValue<decimal>("requestAmount")
                                    };
                                    summaryResponse.Transactions.Add(transactionSummary);
                                }
                            }

                            if (summaryResponse.SummaryType == SummaryType.Approved)
                            {
                                if (Approved == null)
                                {
                                    Approved = new Dictionary <SummaryType, SummaryResponse>();
                                }
                                Approved.Add(summaryResponse.SummaryType, summaryResponse);
                            }
                            else if (summaryResponse.SummaryType == SummaryType.Pending)
                            {
                                if (Pending == null)
                                {
                                    Pending = new Dictionary <SummaryType, SummaryResponse>();
                                }
                                Pending.Add(summaryResponse.SummaryType, summaryResponse);
                            }
                            else if (summaryResponse.SummaryType == SummaryType.Declined)
                            {
                                if (Declined == null)
                                {
                                    Declined = new Dictionary <SummaryType, SummaryResponse>();
                                }
                                Declined.Add(summaryResponse.SummaryType, summaryResponse);
                            }
                        }
                    }
                }
                else   // the only other option is "Failed"
                {
                    var errorCode = cmdResult.GetValue <string>("errorCode");
                    var errorMsg  = cmdResult.GetValue <string>("errorMessage");
                    DeviceResponseText = $"Error: {errorCode} - {errorMsg}";
                }
            }
        }