/// <summary> /// Generates the XML required for a GenerateRequest call /// </summary> /// <param name="input"></param> /// <returns></returns> private string GenerateRequestXml(RequestInput input) { StringWriter sw = new StringWriter(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineOnAttributes = false; settings.OmitXmlDeclaration = true; using (XmlWriter writer = XmlWriter.Create(sw, settings)) { writer.WriteStartDocument(); writer.WriteStartElement("GenerateRequest"); writer.WriteElementString("PxPayUserId", _PxPayUserId); writer.WriteElementString("PxPayKey", _PxPayKey); PropertyInfo[] properties = input.GetType().GetProperties(); foreach (PropertyInfo prop in properties) { if (prop.CanWrite) { string val = (string)prop.GetValue(input, null); if (val != null || val != string.Empty) { writer.WriteElementString(prop.Name, val); } } } writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); } return sw.ToString(); }
/// <summary> /// Post process payment (used by payment gateways that require redirecting to a third-party URL) /// </summary> /// <param name="postProcessPaymentRequest">Payment info required for an order processing</param> public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest) { var builder = new StringBuilder(); var helper = new PaymentExpressHelper(_PaymentExpressPaymentSettings.PxUrl, _PaymentExpressPaymentSettings.PxUserId, _PaymentExpressPaymentSettings.PxPassword); RequestInput input = new RequestInput(); input.AmountInput = postProcessPaymentRequest.Order.OrderTotal.ToString("F"); input.CurrencyInput = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode; input.MerchantReference = postProcessPaymentRequest.Order.Id.ToString(); input.TxnType = "Purchase"; input.UrlFail = _webHelper.GetStoreLocation(false) + "Plugins/PaymentPaymentExpress/CancelOrder"; input.UrlSuccess = _webHelper.GetStoreLocation(false) + "Plugins/PaymentPaymentExpress/PaymentComplete"; input.TxnId = postProcessPaymentRequest.Order.Id.ToString(); RequestOutput output = helper.GenerateRequest(input); if (output.valid == "1") { // Redirect user to payment page _httpContext.Response.Redirect(output.Url); } else { string errorStr = string.Format("Failed to process Payment Express request. Message {0}", output.URI); _logger.Error(errorStr); throw new Exception("There was an issue connecting with our Payment Provider."); } }
/// <summary> /// /// </summary> /// <param name="input"></param> /// <returns></returns> public RequestOutput GenerateRequest(RequestInput input) { RequestOutput result = new RequestOutput(SubmitXml(GenerateRequestXml(input))); return result; }