public SipBaseResponse(byte[] buffer, params string[] messageIds) { _response = string.Empty; foreach (byte b in buffer) { _response += (char)b; } var messages = _response.Split('\r'); foreach (var message in messages) { if (string.IsNullOrEmpty(message)) { continue; } var root = ElementTree.Parse(message).Get("SIP"); Command = root.GetValue <string>("Response"); if (Command != null && !messageIds.ToList().Contains(Command)) { throw new MessageException("Excpected {0} but recieved {1}".FormatWith(string.Join(", ", messageIds), Command)); } Version = root.GetValue <string>("Version"); EcrId = root.GetValue <string>("ECRId"); SipId = root.GetValue <string>("SIPId"); Status = root.GetValue <string>("MultipleMessage"); DeviceResponseCode = NormalizeResponse(root.GetValue <string>("Result")); DeviceResponseText = root.GetValue <string>("ResultText"); if (DeviceResponseCode.Equals("00", StringComparison.OrdinalIgnoreCase)) { MapResponse(root); } } FinalizeResponse(); }
public byte[] Send(IDeviceMessage message) { OnMessageSent?.Invoke(message.ToString()); try { return(Task.Run(async() => { GatewayResponse serviceResponse = await StageTransactionAsync(message); if (serviceResponse.StatusCode == HttpStatusCode.OK) { var root = ElementTree.Parse(serviceResponse.RawResponse).Get("CreateTransactionResponse"); var errors = root.GetAll("Message"); if (errors.Length > 0) { var sb = new StringBuilder(); foreach (var error in root.GetAll("Message")) { sb.AppendLine(error.GetValue <string>("Information")); } throw new MessageException(sb.ToString()); } string transportKey = root.GetValue <string>("TransportKey"); string validationKey = root.GetValue <string>("ValidationKey"); return await InitializeTransactionAsync(transportKey); } else { throw new MessageException(serviceResponse.StatusCode.ToString()); } }).Result); } catch (Exception exc) { throw new MessageException("Failed to send message. Check inner exception for more details.", exc); } }
private bool MessageReceived(byte[] buffer) { if (message_queue == null) { return(false); } message_queue.AddRange(buffer); var msg = ElementTree.Parse(buffer).Get("SIP"); var multiMessage = msg.GetValue <int>("MultipleMessage"); var response = msg.GetValue <string>("Response"); var text = msg.GetValue <string>("ResponseText"); if (multiMessage != 0) { message_queue.Add((byte)'\r'); // Delimiting character return(false); } else { return(true); } }
private Transaction MapResponse <T>(T builder, string rawResponse) where T : TransactionBuilder <Transaction> { string rootName = "{0}Response".FormatWith(MapTransactionType(builder)); var root = ElementTree.Parse(rawResponse).Get(rootName); string status = root.GetValue <string>("status"); string responseCode = NormalizeResponse(root.GetValue <string>("responseCode")); string responseMessage = root.GetValue <string>("responseMessage"); if (!status.Equals("PASS")) { throw new GatewayException( "Unexpected Gateway Response: {0} - {1}".FormatWith(responseCode, responseMessage), responseCode, responseMessage ); } Transaction trans = new Transaction { ResponseCode = responseCode, ResponseMessage = responseMessage, AuthorizationCode = root.GetValue <string>("authCode"), // hostResponseCode // hostReferenceNumber, // taskID, TransactionId = root.GetValue <string>("transactionID"), Timestamp = root.GetValue <string>("transactionTimestamp"), // transactionAmount AuthorizedAmount = root.GetValue <decimal>("processedAmount"), // totalAmount // tip // salesTax // orderNumber // externalReferenceID AvsResponseCode = root.GetValue <string>("addressVerificationCode"), CvnResponseCode = root.GetValue <string>("cvvVerificationCode"), // cardHolderVerificationCode CardType = root.GetValue <string>("cardType"), CardLast4 = root.GetValue <string>("maskedCardNumber"), // token // expirationDate // accountUpdaterResponseCode CommercialIndicator = root.GetValue <string>("commercialCard"), // cavvResponseCode // ucafCollectionIndicator // paymentAccountReference // panReferenceIdentifier BalanceAmount = root.GetValue <decimal>("balanceAmount"), // fcsID // transactionIntegrityClassification // aci // cardTransactionIdentifier // discountAmount // discountType // discountValue // firstName // lastName // prescriptionAmount // visionAmount // dentalAmount // clinicAmount // customerReceipt // merchantReceipt // consolidatedCustomerReceipt // consolidatedMerchantReceipt // pan // panExpirationDate // tokenAssuranceLevel // maskedPAN // tokenAccRangeStatus // splitTenderID // additionalAmountAndAccountType }; return(trans); }