Exemple #1
0
        public async Task <bool> HidePaymentMethodAsync(IList <ShoppingCartItem> cart)
        {
            bool hide = false;
            var  zarinPalPaymentSettings = await _settingService.LoadSettingAsync <ZarinpalPaymentSettings>(await _storeContext.GetActiveStoreScopeConfigurationAsync());

            hide = string.IsNullOrWhiteSpace(_zarinPalPaymentSettings.MerchantID);
            if (_zarinPalPaymentSettings.BlockOverseas)
            {
                hide = hide || ZarinpalHelper.isOverseaseIp(_httpContextAccessor.HttpContext.Connection.RemoteIpAddress);
            }
            return(hide);
        }
        public static string ProduceRedirectUrl(string StoreLocation, int?status, bool UseSandbox, string Authority, string ZarinGate)
        {
            string urlProduced = (status.HasValue && status.Value == 100 ?
                                  string.Concat($"https://{(UseSandbox ? "sandbox" : "www")}.zarinpal.com/pg/StartPay/",
                                                Authority, ZarinGate)
                               : string.Concat(StoreLocation, "Plugins/PaymentZarinpal/ErrorHandler", "?Error=",
                                               ZarinpalHelper.StatusToMessage(status).Message)
                                  );
            var uri = new Uri(urlProduced);

            return(uri.AbsoluteUri);
        }
Exemple #3
0
        public async Task PostProcessPaymentAsync(PostProcessPaymentRequest postProcessPaymentRequest)
        {
            Order    order    = postProcessPaymentRequest.Order;
            Customer customer = await _customerService.GetCustomerByIdAsync(postProcessPaymentRequest.Order.CustomerId);

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

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

            string phoneOfUser    = String.Empty;
            var    billingAddress = await _addressService.GetAddressByIdAsync(order.BillingAddressId);

            var shippingAddress = await _addressService.GetAddressByIdAsync(order.ShippingAddressId ?? 0);

            if (_customerSettings.PhoneEnabled)// Default Phone number of the Customer
            {
                phoneOfUser = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.PhoneAttribute);
            }
            if (string.IsNullOrEmpty(phoneOfUser))//Phone number of the BillingAddress
            {
                phoneOfUser = billingAddress.PhoneNumber;
            }
            if (string.IsNullOrEmpty(phoneOfUser))//Phone number of the ShippingAddress
            {
                phoneOfUser = string.IsNullOrEmpty(shippingAddress?.PhoneNumber) ? phoneOfUser : $"{phoneOfUser} - {shippingAddress.PhoneNumber}";
            }

            var name = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.FirstNameAttribute);

            var family = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.LastNameAttribute);

            string fullName      = $"{name ?? ""} {family ?? ""}".Trim();
            string urlToRedirect = "";
            string zarinGate     = _zarinPalPaymentSettings.UseZarinGate ? _zarinPalPaymentSettings.ZarinGateType.ToString() : null;
            //string description = $"{_storeService.GetStoreByIdAsync(order.StoreId).Name}{(string.IsNullOrEmpty(NameFamily) ? "" : $" - {NameFamily}")} - {customer.Email}{(string.IsNullOrEmpty(phoneOfUser) ? "" : $" - {phoneOfUser}")}";
            string description  = "asdsadsadsad";
            string callbackURL  = string.Concat(_webHelper.GetStoreLocation(), "Plugins/PaymentZarinpal/ResultHandler", "?OGUId=" + postProcessPaymentRequest.Order.OrderGuid);
            string storeAddress = _webHelper.GetStoreLocation();

            if (_zarinPalPaymentSettings.Method == EnumMethod.SOAP)
            {
                if (_zarinPalPaymentSettings.UseSandbox)
                {
                    using (ServiceReferenceZarinpalSandBox.PaymentGatewayImplementationServicePortTypeClient zpalSr = new ServiceReferenceZarinpalSandBox.PaymentGatewayImplementationServicePortTypeClient())
                    {
                        ServiceReferenceZarinpalSandBox.PaymentRequestResponse resp = zpalSr.PaymentRequestAsync(
                            _zarinPalPaymentSettings.MerchantID,
                            total,
                            description,
                            customer.Email,
                            phoneOfUser,
                            callbackURL
                            ).Result;

                        urlToRedirect = ZarinpalHelper.ProduceRedirectUrl(storeAddress,
                                                                          resp.Body.Status,
                                                                          _zarinPalPaymentSettings.UseSandbox,
                                                                          resp.Body.Authority,
                                                                          zarinGate);
                    }
                }
                else
                {
                    using (ServiceReferenceZarinpal.PaymentGatewayImplementationServicePortTypeClient zpalSr = new ServiceReferenceZarinpal.PaymentGatewayImplementationServicePortTypeClient())
                    {
                        var resp = zpalSr.PaymentRequestAsync
                                   (
                            _zarinPalPaymentSettings.MerchantID,
                            total,
                            description,
                            customer.Email,
                            phoneOfUser,
                            callbackURL
                                   ).Result;


                        urlToRedirect = ZarinpalHelper.ProduceRedirectUrl
                                        (
                            storeAddress,
                            resp.Body.Status,
                            _zarinPalPaymentSettings.UseSandbox,
                            resp.Body.Authority,
                            zarinGate
                                        );
                    }
                }
            }
            else if (_zarinPalPaymentSettings.Method == EnumMethod.REST)
            {
                var url = $"https://{(_zarinPalPaymentSettings.UseSandbox ? "sandbox" : "www")}.zarinpal.com/pg/rest/WebGate/PaymentRequest.json";

                var values = new Dictionary <string, string>
                {
                    { "MerchantID", _zarinPalPaymentSettings.MerchantID }, //Change This To work, some thing like this : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
                    { "Amount", total.ToString() },                        //Toman
                    { "CallbackURL", callbackURL },
                    { "Mobile", phoneOfUser },
                    { "Email", customer.Email },
                    { "Description", description }
                };

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

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

                var restRequestModel = JsonConvert.DeserializeObject <RestRequestModel>(responseString);

                urlToRedirect = ZarinpalHelper.ProduceRedirectUrl(storeAddress,
                                                                  restRequestModel?.Status,
                                                                  _zarinPalPaymentSettings.UseSandbox,
                                                                  restRequestModel.Authority,
                                                                  zarinGate);
            }

            var uri = new Uri(urlToRedirect);

            _httpContextAccessor.HttpContext.Response.Redirect(uri.AbsoluteUri);
        }