WebClientResponse ProcessWebClient(dynamic outrequest)
        {
            WebClientResponse wcresponse = new WebClientResponse();
            List <string>     errors     = new List <string>();
            WebClient         client     = WePayPaymentHelper.WePayWebClient(_wepayPaymentSettings);
            string            uriString  = GetWePayUrl() + outrequest.actionUrl;
            var data = JsonConvert.SerializeObject(outrequest);

            try
            {
                client.UploadString(new Uri(uriString), "POST", data);

                var chkoutreq = new CheckoutRequest {
                    checkout_id = Convert.ToInt64(outrequest.checkout_id)
                };
                data      = JsonConvert.SerializeObject(chkoutreq);
                uriString = GetWePayUrl() + chkoutreq.actionUrl;
                client    = WePayPaymentHelper.WePayWebClient(_wepayPaymentSettings);
                var     clientresp = client.UploadString(new Uri(uriString), "POST", data);
                dynamic response   = JsonConvert.DeserializeObject(clientresp);
                wcresponse.state = Convert.ToString(response.state);
            }
            catch (WebException we)
            {
                if (we.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse httpErrorResponse = (HttpWebResponse)we.Response as HttpWebResponse;
                    StreamReader    reader            = new StreamReader(httpErrorResponse.GetResponseStream(), Encoding.UTF8);
                    string          responseBody      = reader.ReadToEnd();
                    var             errResp           = JsonConvert.DeserializeObject <ErrorResponse>(responseBody);
                    errors.Add(string.Format("Error: {0};" + Environment.NewLine + " Error Message: {1};" + Environment.NewLine + " Error Description: {2}", errResp.error, errResp.error_description, we.Message));
                }
                else
                {
                    errors.Add(string.Format("Error: {0}", we.Message));
                }
            }
            catch (Exception exc)
            {
                errors.Add(string.Format("Error: {0}", exc.Message));
            }
            wcresponse.Errors = errors;
            return(wcresponse);
        }
        /// <summary>
        /// Voids a payment
        /// </summary>
        /// <param name="voidPaymentRequest">Request</param>
        /// <returns>Result</returns>
        public VoidPaymentResult Void(VoidPaymentRequest voidPaymentRequest)
        {
            var result = new VoidPaymentResult();
            CheckoutCancelRequest outrequest = new CheckoutCancelRequest
            {
                checkout_id   = voidPaymentRequest.Order.AuthorizationTransactionId,
                cancel_reason = "Cancel payment for Order #" + voidPaymentRequest.Order.OrderGuid
            };
            WebClientResponse wcresponse = ProcessWebClient(outrequest);

            foreach (string error in wcresponse.Errors)
            {
                result.AddError(error);
            }
            if (result.Errors.Count <= 0 && wcresponse.state == "cancelled")
            {
                result.NewPaymentStatus = PaymentStatus.Voided;
            }
            return(result);
        }
        /// <summary>
        /// Captures payment
        /// </summary>
        /// <param name="capturePaymentRequest">Capture payment request</param>
        /// <returns>Capture payment result</returns>
        public CapturePaymentResult Capture(CapturePaymentRequest capturePaymentRequest)
        {
            var result = new CapturePaymentResult();
            CheckoutCaptureRequest outrequest = new CheckoutCaptureRequest
            {
                checkout_id = capturePaymentRequest.Order.AuthorizationTransactionId
            };

            WebClientResponse wcresponse = ProcessWebClient(outrequest);

            foreach (string error in wcresponse.Errors)
            {
                result.AddError(error);
            }
            if (result.Errors.Count <= 0 && wcresponse.state == "captured")
            {
                result.NewPaymentStatus = PaymentStatus.Paid;
            }
            return(result);
        }
        /// <summary>
        /// Refunds a payment
        /// </summary>
        /// <param name="refundPaymentRequest">Request</param>
        /// <returns>Result</returns>
        public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
        {
            var     result = new RefundPaymentResult();
            dynamic outrequest;

            if (refundPaymentRequest.IsPartialRefund)
            {
                outrequest        = new CheckoutPartialRefundRequest();
                outrequest.amount = refundPaymentRequest.AmountToRefund;
            }
            else
            {
                outrequest = new CheckoutRefundRequest();
            }

            outrequest.checkout_id   = refundPaymentRequest.Order.AuthorizationTransactionId;
            outrequest.refund_reason = "Refund for Order #" + refundPaymentRequest.Order.OrderGuid;

            WebClientResponse wcresponse = ProcessWebClient(outrequest);

            foreach (string error in wcresponse.Errors)
            {
                result.AddError(error);
            }
            if (result.Errors.Count <= 0 && wcresponse.state == "refunded")
            {
                if (refundPaymentRequest.IsPartialRefund)
                {
                    result.NewPaymentStatus = PaymentStatus.PartiallyRefunded;
                }
                else
                {
                    result.NewPaymentStatus = PaymentStatus.Refunded;
                }
            }

            return(result);
        }