Exemplo n.º 1
0
        /// <summary>
        /// Add CSS element
        /// </summary>
        /// <param name="html">HTML helper</param>
        /// <param name="location">A location of the script element</param>
        /// <param name="src">Script path (minified version)</param>
        /// <param name="debugSrc">Script path (full debug version). If empty, then minified version will be used</param>
        /// <param name="excludeFromBundle">A value indicating whether to exclude this script from bundling</param>
        public static void AddCssFileParts(this IHtmlHelper html, ResourceLocation location,
                                           string src, string debugSrc = "", bool excludeFromBundle = false)
        {
            IPageHeadBuilder pageHeadBuilder = ServiceProviderFactory.ServiceProvider.GetService <IPageHeadBuilder>();

            pageHeadBuilder.AddCssFileParts(location, src, debugSrc, excludeFromBundle);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Add CSS element
 /// </summary>
 /// <param name="html">HTML helper</param>
 /// <param name="location">A location of the script element</param>
 /// <param name="part">CSS part</param>
 /// <param name="pageHeadBuilder"></param>
 public static void AddCssFileParts(this HtmlHelper html, ResourceLocation location, string part,
     IPageHeadBuilder pageHeadBuilder = null)
 {
     if (pageHeadBuilder == null)
         pageHeadBuilder = EngineContext.Current.Resolve<IPageHeadBuilder>();
     pageHeadBuilder.AddCssFileParts(location, part);
 }
        /// <summary>
        /// Refunds a payment
        /// </summary>
        /// <param name="refundPaymentRequest">Request</param>
        /// <returns>Result</returns>
        public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
        {
            var result = new RefundPaymentResult();

            //primary currency
            var currency = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId);

            if (currency == null)
            {
                throw new NopException("Currency could not be loaded");
            }

            //settings
            var g2apayPaymentSettings = _settingService.LoadSetting <G2APayPaymentSettings>(refundPaymentRequest.Order.StoreId);

            //hash
            var stringToHash = $"{refundPaymentRequest.Order.CaptureTransactionId}{refundPaymentRequest.Order.OrderGuid}{refundPaymentRequest.Order.OrderTotal.ToString("0.00", CultureInfo.InvariantCulture)}{refundPaymentRequest.AmountToRefund.ToString("0.00", CultureInfo.InvariantCulture)}{g2apayPaymentSettings.SecretKey}";

            //post parameters
            var parameters = QueryHelpers.ParseQuery(string.Empty);

            parameters.Add("action", "refund");
            parameters.Add("order_id", refundPaymentRequest.Order.OrderGuid.ToString());
            parameters.Add("amount", refundPaymentRequest.AmountToRefund.ToString("0.00", CultureInfo.InvariantCulture));
            parameters.Add("currency", currency.CurrencyCode);
            parameters.Add("hash", GetSHA256Hash(stringToHash));

            var postData = Encoding.Default.GetBytes(parameters.ToString());

            //post
            var request = (HttpWebRequest)WebRequest.Create($"{GetG2APayRestUrl(g2apayPaymentSettings)}/rest/transactions/{refundPaymentRequest.Order.CaptureTransactionId}");

            request.Method        = "PUT";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = postData.Length;
            request.Headers.Add(HttpRequestHeader.Authorization, GetAuthHeader(g2apayPaymentSettings));

            try
            {
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(postData, 0, postData.Length);
                }
                var httpResponse = (HttpWebResponse)request.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var response = JsonConvert.DeserializeObject <G2APayPaymentResponse>(streamReader.ReadToEnd());
                    if (!response.Status.Equals("ok", StringComparison.InvariantCultureIgnoreCase))
                    {
                        throw new NopException($"G2A Pay refund error: transaction status is {response.Status}");
                    }

                    //leaving payment status, we will change it later, upon receiving IPN
                    result.NewPaymentStatus = refundPaymentRequest.Order.PaymentStatus;
                    result.AddError(_localizationService.GetResource("Plugins.Payments.G2APay.Refund"));

                    //change error notification to warning one
                    _pageHeadBuilder.AddCssFileParts(ResourceLocation.Head, @"~/Plugins/Payments.G2APay/Content/styles.css", string.Empty);
                }
            }
            catch (WebException ex)
            {
                var error = "G2A Pay refund error. ";
                using (var streamReader = new StreamReader(ex.Response.GetResponseStream()))
                {
                    error += streamReader.ReadToEnd();
                }
                _logger.Error(error, ex);
                result.AddError(string.Format("{1}. {0}", error, ex.Message));
            }

            return(result);
        }