static private PlugNPayApi.PNP CreatePaymentRequest(int orderNumber, decimal orderTotal, string currencyCode, Address billingAddress) { // instantiate new request object var request = new PlugNPayApi.PNP(); var details = request.Request.TransactionRequest; var billing = details.BillDetails; var order = details.Order; // request details details.TransactionID = orderNumber.ToString(); details.IPaddress = CommonLogic.CustomerIpAddress(); // billing information billing.CardName = billingAddress.CardName; billing.CardAddress1 = billingAddress.Address1; billing.CardAddress2 = billingAddress.Address2; billing.CardCity = billingAddress.City; billing.CardState = billingAddress.State; billing.CardZip = billingAddress.Zip; billing.CardCountry = AppLogic.GetCountryTwoLetterISOCode(billingAddress.Country); billing.Email = billingAddress.EMail; // order information order.CardAmount = Localization.CurrencyStringForGatewayWithoutExchangeRate(orderTotal); order.Currency = currencyCode; return(request); }
public override String VoidOrder(int orderNumber) { string result = string.Empty; // create payment request var request = new PlugNPayApi.PNP(); // request details var details = request.Request.TransactionRequest; details.Mode = PlugNPayApi.RequestMode.Void; details.TxnType = PlugNPayApi.TxnType.Auth; using (SqlConnection dbconn = DB.dbConn()) { dbconn.Open(); using (IDataReader rs = DB.GetRS("select AuthorizationPNREF, OrderTotal from orders with (NOLOCK) where OrderNumber = " + orderNumber.ToString(), dbconn)) { rs.Read(); details.TransactionID = DB.RSField(rs, "AuthorizationPNREF"); details.Order.CardAmount = Localization.CurrencyStringForGatewayWithoutExchangeRate(DB.RSFieldDecimal(rs, "OrderTotal")); details.Order.Currency = Localization.StoreCurrency(); } } // transmit request to PlugNPay and receive response PlugNPayApi.PNP response; string transactionCommandOut = string.Empty; try { response = TransmitRequest(request, out transactionCommandOut); } catch (Exception e) { return(e.Message); } // examine the response var responseDetails = response.Response.TransactionResponse; if (responseDetails.FinalStatus == PlugNPayApi.ResponseFinalStatus.Success) { result = AppLogic.ro_OK; } else { result = responseDetails.MErrMsg; if (result == null || result.Length == 0) { result = "Unspecified Error"; } } return(result); }
public override String CaptureOrder(Order o) { string result = string.Empty; // create payment request var request = new PlugNPayApi.PNP(); // request details var details = request.Request.TransactionRequest; details.Mode = PlugNPayApi.RequestMode.Mark; details.TransactionID = o.OrderNumber.ToString(); details.Order.CardAmount = Localization.CurrencyStringForGatewayWithoutExchangeRate(o.Total(true)); details.Order.Currency = Localization.StoreCurrency(); // transmit request to PlugNPay and receive response PlugNPayApi.PNP response; string transactionCommandOut = string.Empty; try { response = TransmitRequest(request, out transactionCommandOut); } catch (Exception e) { return(e.Message); } // examine the response var responseDetails = response.Response.TransactionResponse; if (responseDetails.FinalStatus == PlugNPayApi.ResponseFinalStatus.Pending || responseDetails.FinalStatus == PlugNPayApi.ResponseFinalStatus.Success) { result = AppLogic.ro_OK; } else { result = responseDetails.MErrMsg; if (result == null || result.Length == 0) { result = "Unspecified Error"; } } return(result); }
static private PlugNPayApi.PNP TransmitRequest(PlugNPayApi.PNP request, out string req) { string plugNPayServer = "https://pay1.plugnpay.com/payment/xml.cgi"; PlugNPayApi.PNP response; // serialize Request XmlSerializer serRequest = new XmlSerializer(typeof(PlugNPayApi.PNP)); StringWriter swRequest = new StringWriter(); serRequest.Serialize(swRequest, request); req = swRequest.ToString(); // Send request to PlugNPay HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(plugNPayServer); webRequest.Method = "POST"; // Transmit the request to PlugNPay byte[] data = System.Text.Encoding.ASCII.GetBytes(req); webRequest.ContentLength = data.Length; Stream requestStream; try { requestStream = webRequest.GetRequestStream(); } catch (WebException e) // could not connect to PlugNPay endpoint { throw new Exception("Tried to reach PlugNPay Server (" + plugNPayServer + "): " + e.Message); } requestStream.Write(data, 0, data.Length); requestStream.Close(); // get the response from PlugNPay WebResponse webResponse = null; string resp; try { webResponse = webRequest.GetResponse(); } catch (WebException e) // could not receive a response from PlugNPay endpoint { throw new Exception("No response from PlugNPay Server (" + plugNPayServer + "): " + e.Message); } using (StreamReader sr = new StreamReader(webResponse.GetResponseStream())) { resp = sr.ReadToEnd(); sr.Close(); } webResponse.Close(); // check for non-Xml response (indicates a problem) if (!resp.StartsWith("<")) { throw new Exception("PlugNPay server returned this message: " + resp.Replace('\n', ' ')); } // deserialize the xml response into a Response object XmlSerializer serResponse = new XmlSerializer(typeof(PlugNPayApi.PNP)); StringReader srResponse = new StringReader(resp); try { response = (PlugNPayApi.PNP)serResponse.Deserialize(srResponse); } catch (InvalidOperationException e) // invalid xml, or no reply received from PlugNPay { throw new Exception("Could not parse response from PlugNPay server: " + e.Message + " Response received: " + resp.Replace('\n', ' ')); } srResponse.Close(); return(response); }