public IActionResult AccessTokenCallback()
        {
            //handle access token callback
            try
            {
                if (string.IsNullOrEmpty(_squarePaymentSettings.ApplicationId) || string.IsNullOrEmpty(_squarePaymentSettings.ApplicationSecret))
                {
                    throw new NopException("Plugin is not configured");
                }

                //check whether there are errors in the request
                if (Request.Query.TryGetValue("error", out StringValues error) |
                    Request.Query.TryGetValue("error_description", out StringValues errorDescription))
                {
                    throw new NopException($"{error} - {errorDescription}");
                }

                //validate verification string
                if (!Request.Query.TryGetValue("state", out StringValues verificationString) || !verificationString.Equals(_squarePaymentSettings.AccessTokenVerificationString))
                {
                    throw new NopException("The verification string did not pass the validation");
                }

                //check whether there is an authorization code in the request
                if (!Request.Query.TryGetValue("code", out StringValues authorizationCode))
                {
                    throw new NopException("No service response");
                }

                //exchange the authorization code for an access token
                var accessToken = _squarePaymentManager.ObtainAccessToken(new ObtainAccessTokenRequest
                {
                    ApplicationId     = _squarePaymentSettings.ApplicationId,
                    ApplicationSecret = _squarePaymentSettings.ApplicationSecret,
                    AuthorizationCode = authorizationCode,
                    RedirectUrl       = Url.RouteUrl(SquarePaymentDefaults.AccessTokenRoute)
                });
                if (string.IsNullOrEmpty(accessToken))
                {
                    throw new NopException("No service response");
                }

                //if access token successfully received, save it for the further usage
                _squarePaymentSettings.AccessToken = accessToken;
                _settingService.SaveSetting(_squarePaymentSettings);

                _notificationService.SuccessNotification(_localizationService.GetResource("Plugins.Payments.Square.ObtainAccessToken.Success"));
            }
            catch (Exception exception)
            {
                //display errors
                _notificationService.ErrorNotification(_localizationService.GetResource("Plugins.Payments.Square.ObtainAccessToken.Error"));
                if (!string.IsNullOrEmpty(exception.Message))
                {
                    _notificationService.ErrorNotification(exception.Message);
                }
            }

            return(RedirectToAction("Configure", "PaymentSquare", new { area = AreaNames.Admin }));
        }
Esempio n. 2
0
        public IActionResult AccessTokenCallback()
        {
            //load settings for a current store
            var storeId  = _storeContext.ActiveStoreScopeConfiguration;
            var settings = _settingService.LoadSetting <SquarePaymentSettings>(storeId);

            //handle access token callback
            try
            {
                if (string.IsNullOrEmpty(settings.ApplicationId) || string.IsNullOrEmpty(settings.ApplicationSecret))
                {
                    throw new NopException("Plugin is not configured");
                }

                //check whether there are errors in the request
                if (Request.Query.TryGetValue("error", out var error) | Request.Query.TryGetValue("error_description", out var errorDescription))
                {
                    throw new NopException($"{error} - {errorDescription}");
                }

                //validate verification string
                if (!Request.Query.TryGetValue("state", out var verificationString) || !verificationString.Equals(settings.AccessTokenVerificationString))
                {
                    throw new NopException("The verification string did not pass the validation");
                }

                //check whether there is an authorization code in the request
                if (!Request.Query.TryGetValue("code", out var authorizationCode))
                {
                    throw new NopException("No service response");
                }

                //exchange the authorization code for an access token
                var(accessToken, refreshToken) = _squarePaymentManager.ObtainAccessToken(authorizationCode, storeId);
                if (string.IsNullOrEmpty(accessToken) || string.IsNullOrEmpty(refreshToken))
                {
                    throw new NopException("No service response");
                }

                //if access token successfully received, save it for the further usage
                settings.AccessToken  = accessToken;
                settings.RefreshToken = refreshToken;

                _settingService.SaveSetting(settings, x => x.AccessToken, storeId, false);
                _settingService.SaveSetting(settings, x => x.RefreshToken, storeId, false);

                _settingService.ClearCache();

                _notificationService.SuccessNotification(_localizationService.GetResource("Plugins.Payments.Square.ObtainAccessToken.Success"));
            }
            catch (Exception exception)
            {
                //display errors
                _notificationService.ErrorNotification(_localizationService.GetResource("Plugins.Payments.Square.ObtainAccessToken.Error"));
                if (!string.IsNullOrEmpty(exception.Message))
                {
                    _notificationService.ErrorNotification(exception.Message);
                }
            }

            return(RedirectToAction("Configure", "PaymentSquare", new { area = AreaNames.Admin }));
        }
Esempio n. 3
0
        public ActionResult AccessTokenCallback()
        {
            //handle access token callback
            try
            {
                if (string.IsNullOrEmpty(_squarePaymentSettings.ApplicationId) || string.IsNullOrEmpty(_squarePaymentSettings.ApplicationSecret))
                {
                    throw new NopException("Plugin is not configured");
                }

                //check whether there are errors in the request
                var error            = this.Request.QueryString["error"];
                var errorDescription = this.Request.QueryString["error_description"];
                if (!string.IsNullOrEmpty(error) || !string.IsNullOrEmpty(errorDescription))
                {
                    throw new NopException($"{error} - {errorDescription}");
                }

                //validate verification string
                var verificationString = this.Request.QueryString["state"];
                if (string.IsNullOrEmpty(verificationString) || !verificationString.Equals(_squarePaymentSettings.AccessTokenVerificationString))
                {
                    throw new NopException("The verification string did not pass the validation");
                }

                //check whether there is an authorization code in the request
                var authorizationCode = this.Request.QueryString["code"];
                if (string.IsNullOrEmpty(authorizationCode))
                {
                    throw new NopException("No service response");
                }

                //exchange the authorization code for an access token
                var accessToken = _squarePaymentManager.ObtainAccessToken(new ObtainAccessTokenRequest
                {
                    ApplicationId     = _squarePaymentSettings.ApplicationId,
                    ApplicationSecret = _squarePaymentSettings.ApplicationSecret,
                    AuthorizationCode = authorizationCode,
                    RedirectUrl       = this.Url.RouteUrl(SquarePaymentDefaults.AccessTokenRoute)
                });
                if (string.IsNullOrEmpty(accessToken))
                {
                    throw new NopException("No service response");
                }

                //if access token successfully received, save it for the further usage
                _squarePaymentSettings.AccessToken = accessToken;
                _settingService.SaveSetting(_squarePaymentSettings);

                SuccessNotification(_localizationService.GetResource("Plugins.Payments.Square.ObtainAccessToken.Success"));
            }
            catch (Exception exception)
            {
                //display errors
                ErrorNotification(_localizationService.GetResource("Plugins.Payments.Square.ObtainAccessToken.Error"));
                if (!string.IsNullOrEmpty(exception.Message))
                {
                    ErrorNotification(exception.Message);
                }
            }

            //we cannot redirect to the Configure action since it is only for child requests, so redirect to the Payment.ConfigureMethod
            return(RedirectToAction("ConfigureMethod", "Payment", new { systemName = SquarePaymentDefaults.SystemName, area = "admin" }));
        }