/// <summary> /// Voids a payment /// </summary> /// <param name="voidPaymentRequest">Request</param> /// <returns>Result</returns> public VoidPaymentResult Void(VoidPaymentRequest voidPaymentRequest) { var result = new VoidPaymentResult(); var orderGuid = voidPaymentRequest.Order.OrderGuid; if (orderGuid == Guid.NewGuid()) { result.AddError("Order Unique identifier code does not exist!"); return(result); } var transx = _sagePayServerTransactionService.GetSagePayServerTransactionByVendorTxCode(orderGuid.ToString()); if (transx == null) { result.AddError(String.Format("SagePay Server vendor transaction code {0} does not exist.", orderGuid.ToString())); return(result); } var webClient = new WebClient(); var data = new NVPCodec(); data.Add("VPSProtocol", SagePayHelper.GetProtocol()); data.Add("TxType", "VOID"); data.Add("Vendor", _sagePayServerPaymentSettings.VendorName); var voidGuid = Guid.NewGuid(); data.Add("VendorTxCode", voidGuid.ToString()); data.Add("VPSTxId", transx.VPSTxId); data.Add("SecurityKey", transx.SecurityKey); data.Add("TxAuthNo", transx.TxAuthNo); var postURL = SagePayHelper.GetSageSystemUrl(_sagePayServerPaymentSettings.ConnectTo, "void"); string strResponse = string.Empty; try { Byte[] responseData = webClient.UploadValues(postURL, data); strResponse = Encoding.ASCII.GetString(responseData); } catch (WebException ex) { result.AddError(String.Format( @"Your server was unable to release this transaction with Sage Pay. Check that you do not have a firewall restricting the POST and that your server can correctly resolve the address {0}. <br/> The Status Number is: {1}<br/> The Description given is: {2}", postURL, ex.Status, ex.Message)); return(result); } if (string.IsNullOrWhiteSpace(strResponse)) { result.AddError(String.Format( @"Your server was unable to register this transaction with Sage Pay. Check that you do not have a firewall restricting the POST and that your server can correctly resolve the address {0}.", postURL)); return(result); } var strStatus = SagePayHelper.FindField("Status", strResponse); var strStatusDetail = SagePayHelper.FindField("StatusDetail", strResponse); switch (strStatus) { case "OK": result.NewPaymentStatus = PaymentStatus.Voided; break; case "MALFORMED": result.AddError(string.Format("Error ({0}: {1}) <br/> {2}", strStatus, strStatusDetail, data.Encode())); return(result); case "INVALID": result.AddError(string.Format("Error ({0}: {1}) <br/> {2}", strStatus, strStatusDetail, data.Encode())); return(result); default: result.AddError(string.Format("Error ({0}: {1})", strStatus, strStatusDetail)); return(result); } return(result); }
/// <summary> /// Voids a payment /// </summary> /// <param name="voidPaymentRequest">Request</param> /// <returns>Result</returns> public VoidPaymentResult Void(VoidPaymentRequest voidPaymentRequest) { var result = new VoidPaymentResult(); var orderGuid = voidPaymentRequest.Order.OrderGuid; if (orderGuid == Guid.NewGuid()) { result.AddError("Order Unique identifier code does not exist!"); return result; } var transx = _sagePayServerTransactionService.GetSagePayServerTransactionByVendorTxCode(orderGuid.ToString()); if (transx == null) { result.AddError(String.Format("SagePay Server vendor transaction code {0} does not exist.", orderGuid.ToString())); return result; } var webClient = new WebClient(); var data = new NVPCodec(); data.Add("VPSProtocol", SagePayHelper.GetProtocol()); data.Add("TxType", "VOID"); data.Add("Vendor", _sagePayServerPaymentSettings.VendorName); var voidGuid = Guid.NewGuid(); data.Add("VendorTxCode", voidGuid.ToString()); data.Add("VPSTxId", transx.VPSTxId); data.Add("SecurityKey", transx.SecurityKey); data.Add("TxAuthNo", transx.TxAuthNo); var postURL = SagePayHelper.GetSageSystemUrl(_sagePayServerPaymentSettings.ConnectTo, "void"); string strResponse = string.Empty; try { Byte[] responseData = webClient.UploadValues(postURL, data); strResponse = Encoding.ASCII.GetString(responseData); } catch (WebException ex) { result.AddError(String.Format( @"Your server was unable to release this transaction with Sage Pay. Check that you do not have a firewall restricting the POST and that your server can correctly resolve the address {0}. <br/> The Status Number is: {1}<br/> The Description given is: {2}", postURL, ex.Status, ex.Message)); return result; } if (string.IsNullOrWhiteSpace(strResponse)) { result.AddError(String.Format( @"Your server was unable to register this transaction with Sage Pay. Check that you do not have a firewall restricting the POST and that your server can correctly resolve the address {0}.", postURL)); return result; } var strStatus = SagePayHelper.FindField("Status", strResponse); var strStatusDetail = SagePayHelper.FindField("StatusDetail", strResponse); switch (strStatus) { case "OK": result.NewPaymentStatus = PaymentStatus.Voided; break; case "MALFORMED": result.AddError(string.Format("Error ({0}: {1}) <br/> {2}", strStatus, strStatusDetail, data.Encode())); return result; case "INVALID": result.AddError(string.Format("Error ({0}: {1}) <br/> {2}", strStatus, strStatusDetail, data.Encode())); return result; default: result.AddError(string.Format("Error ({0}: {1})", strStatus, strStatusDetail)); return result; } return result; }