public ActionResult ErrorHandler(string Error)
        {
            int code = 0;

            Int32.TryParse(Error, out code);
            if (code != 0)
            {
                Error = ZarinpalHelper.StatusToMessage(code).Message;
            }
            ViewBag.Err = string.Concat("خطا : ", Error);
            return(View("~/Plugins/Payments.Zarinpal/Views/ErrorHandler.cshtml"));
        }
        public ActionResult ResultHandler(string Status, string Authority, string OGUID)
        {
            if (!(_paymentPluginManager.LoadPluginBySystemName("Payments.Zarinpal") is ZarinPalPaymentProcessor processor) || !_paymentPluginManager.IsPluginActive(processor))
            {
                throw new NopException("ZarinPal module cannot be loaded");
            }

            Guid orderNumberGuid = Guid.Empty;

            try
            {
                orderNumberGuid = new Guid(OGUID);
            }
            catch { }

            Order order = _orderService.GetOrderByGuid(orderNumberGuid);
            var   total = Convert.ToInt32(Math.Round(order.OrderTotal, 2));

            if (_ZarinPalPaymentSettings.RialToToman)
            {
                total = total / 10;
            }

            if (string.IsNullOrEmpty(Status) == false && string.IsNullOrEmpty(Authority) == false)
            {
                long _refId = 0;
                System.Net.ServicePointManager.Expect100Continue = false;
                int _status          = -1;
                var storeScope       = _storeContext.ActiveStoreScopeConfiguration;
                var ZarinPalSettings = _settingService.LoadSetting <ZarinpalPaymentSettings>(storeScope);

                if (_ZarinPalPaymentSettings.UseSandbox)
                {
                    using (ServiceReferenceZarinpalSandBox.PaymentGatewayImplementationServicePortTypeClient ZpalSr = new ServiceReferenceZarinpalSandBox.PaymentGatewayImplementationServicePortTypeClient())
                    {
                        var res = ZpalSr.PaymentVerificationAsync(
                            ZarinPalSettings.MerchantID,
                            Authority,
                            total).Result; //test
                        _status = res.Body.Status;
                        _refId  = res.Body.RefID;
                    }
                }
                else
                {
                    using (ServiceReferenceZarinpal.PaymentGatewayImplementationServicePortTypeClient ZpalSr = new ServiceReferenceZarinpal.PaymentGatewayImplementationServicePortTypeClient())
                    {
                        var res = ZpalSr.PaymentVerificationAsync(
                            ZarinPalSettings.MerchantID,
                            Authority,
                            total).Result; //test
                        _status = res.Body.Status;
                        _refId  = res.Body.RefID;
                    }
                }

                var result = ZarinpalHelper.StatusToMessage(_status);

                order.OrderNotes.Add(new OrderNote()
                {
                    Note = string.Concat(
                        "پرداخت ",
                        (result.IsOk ? "" : "نا"), "موفق", " - ",
                        "پیغام درگاه : ", result.Message,
                        result.IsOk ? string.Concat(" - ", "کد پی گیری : ", _refId.ToString()) : ""
                        ),
                    DisplayToCustomer = true,
                    CreatedOnUtc      = DateTime.UtcNow
                });

                _orderService.UpdateOrder(order);

                if (result.IsOk && _orderProcessingService.CanMarkOrderAsPaid(order))
                {
                    order.AuthorizationTransactionId = _refId.ToString();
                    _orderService.UpdateOrder(order);
                    _orderProcessingService.MarkOrderAsPaid(order);
                    return(RedirectToRoute("CheckoutCompleted", new { orderId = order.Id }));
                }
            }
            return(RedirectToRoute("orderdetails", new { orderId = order.Id }));
        }
Example #3
0
        public async Task <ActionResult> ResultHandler(string status, string authority, string oGUID)
        {
            if (!(await _paymentPluginManager.LoadPluginBySystemNameAsync("Payments.Zarinpal") is ZarinPalPaymentProcessor processor) || !_paymentPluginManager.IsPluginActive(processor))
            {
                throw new NopException("ZarinPal module cannot be loaded");
            }

            var orderNumberGuid = Guid.Empty;

            orderNumberGuid = new Guid(oGUID);


            var order = await _orderService.GetOrderByGuidAsync(orderNumberGuid);

            var total = Convert.ToInt32(Math.Round(order.OrderTotal, 2));

            if (_zarinPalPaymentSettings.RialToToman)
            {
                total = total / 10;
            }


            if (string.IsNullOrEmpty(status) == false && string.IsNullOrEmpty(authority) == false)
            {
                var refId = "0";
                System.Net.ServicePointManager.Expect100Continue = false;
                var statusCode = -1;
                var storeScope = await _storeContext.GetActiveStoreScopeConfigurationAsync();

                var zarinPalSettings = await _settingService.LoadSettingAsync <ZarinpalPaymentSettings>(storeScope);

                if (_zarinPalPaymentSettings.Method == EnumMethod.SOAP)
                {
                    if (_zarinPalPaymentSettings.UseSandbox)
                    {
                        using (PaymentGatewayImplementationServicePortTypeClient zpalSr = new PaymentGatewayImplementationServicePortTypeClient())
                        {
                            var res = zpalSr.PaymentVerificationAsync
                                      (
                                zarinPalSettings.MerchantID,
                                authority,
                                total
                                      ).Result; //test

                            statusCode = res.Body.Status;
                            refId      = res.Body.RefID.ToString();
                        }
                    }
                    else
                    {
                        using (PaymentGatewayImplementationServicePortTypeClient zpalSr = new ServiceReferenceZarinpal.PaymentGatewayImplementationServicePortTypeClient())
                        {
                            var res = zpalSr.PaymentVerificationAsync(
                                zarinPalSettings.MerchantID,
                                authority,
                                total).Result;
                            statusCode = res.Body.Status;
                            refId      = res.Body.RefID.ToString();
                        }
                    }
                }
                else if (_zarinPalPaymentSettings.Method == EnumMethod.REST)
                {
                    var url    = $"https://{(_zarinPalPaymentSettings.UseSandbox ? "sandbox" : "www")}.zarinpal.com/pg/rest/WebGate/PaymentVerification.json";
                    var values = new Dictionary <string, string>
                    {
                        { "MerchantID", zarinPalSettings.MerchantID },
                        { "Authority", authority },
                        { "Amount", total.ToString() }     //Toman
                    };

                    var paymenResponsetJsonValue = JsonConvert.SerializeObject(values);
                    var content = new StringContent(paymenResponsetJsonValue, Encoding.UTF8, "application/json");

                    var response       = ZarinPalPaymentProcessor.ClientZarinPal.PostAsync(url, content).Result;
                    var responseString = response.Content.ReadAsStringAsync().Result;

                    var restVerifyModel =
                        JsonConvert.DeserializeObject <RestVerifyModel>(responseString);
                    statusCode = restVerifyModel.Status;
                    refId      = restVerifyModel.RefID;
                }

                var result = ZarinpalHelper.StatusToMessage(statusCode);

                var orderNote = new OrderNote()
                {
                    OrderId = order.Id,
                    Note    = string.Concat(
                        "پرداخت ",
                        (result.IsOk ? "" : "نا"), "موفق", " - ",
                        "پیغام درگاه : ", result.Message,
                        result.IsOk ? string.Concat(" - ", "کد پی گیری : ", refId) : ""
                        ),
                    DisplayToCustomer = true,
                    CreatedOnUtc      = DateTime.UtcNow
                };

                await _orderService.InsertOrderNoteAsync(orderNote);

                if (result.IsOk && _orderProcessingService.CanMarkOrderAsPaid(order))
                {
                    order.AuthorizationTransactionId = refId;
                    await _orderService.UpdateOrderAsync(order);

                    await _orderProcessingService.MarkOrderAsPaidAsync(order);

                    return(RedirectToRoute("CheckoutCompleted", new { orderId = order.Id }));
                }
            }

            return(RedirectToRoute("orderdetails", new { orderId = order.Id }));
        }