public void SetSettings(PaypalSettings Settings) {
            var settingsPart = Services.WorkContext.CurrentSite.As<OShopPaypalSettingsPart>();

            settingsPart.UseSandbox = Settings.UseSandbox;
            settingsPart.ClientId = Settings.ClientId;
            settingsPart.ClientSecret = Convert.ToBase64String(_encryptionService.Encode(Encoding.UTF8.GetBytes(Settings.ClientSecret)));
        }
 public async Task<bool> ValidateCredentialsAsync(PaypalSettings Settings) {
     using (var client = CreateClient(Settings.UseSandbox)) {
         try {
             return await GetAccessTokenAsync(client, Settings) != null;
         }
         catch {
             return false;
         }
     }
 }
        public PaypalSettings GetSettings() {
            var settingsPart = Services.WorkContext.CurrentSite.As<OShopPaypalSettingsPart>();

            var settings = new PaypalSettings() {
                UseSandbox = settingsPart.UseSandbox,
                ClientId = settingsPart.ClientId,
            };

            if (!string.IsNullOrEmpty(settingsPart.ClientSecret)) {
                settings.ClientSecret = Encoding.UTF8.GetString(_encryptionService.Decode(Convert.FromBase64String(settingsPart.ClientSecret)));
            }

            return settings;
        }
        public ActionResult SettingsSave(PaypalSettings model) {
            if (!Services.Authorizer.Authorize(OShopPermissions.ManageShopSettings, T("Not allowed to manage Shop Settings")))
                return new HttpUnauthorizedResult();

            if (TryUpdateModel(model)) {
                _settingsService.SetSettings(model);
                Services.Notifier.Information(T("PayPal Settings saved successfully."));
            }
            else {
                Services.Notifier.Error(T("Could not save PayPal Settings."));
            }

            return View(model);
        }
        public async Task<ActionResult> SettingsValidate(PaypalSettings model) {
            if (!Services.Authorizer.Authorize(OShopPermissions.ManageShopSettings, T("Not allowed to manage Shop Settings")))
                return new HttpUnauthorizedResult();

            if (TryUpdateModel(model)) {
                if (await _apiService.ValidateCredentialsAsync(model)) {
                    Services.Notifier.Information(T("Valid credentials."));
                }
                else {
                    Services.Notifier.Warning(T("Invalid credentials."));
                }
            }
            else {
                Services.Notifier.Error(T("Could not validate credentials."));
            }

            return View(model);
        }
        private async Task<AccessToken> GetAccessTokenAsync(HttpClient Client, PaypalSettings Settings) {
            Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(
                ASCIIEncoding.ASCII.GetBytes(Settings.ClientId + ":" + Settings.ClientSecret)
            ));

            var response = await Client.PostAsync("v1/oauth2/token", new FormUrlEncodedContent(new KeyValuePair<string, string>[] {
                new KeyValuePair<string, string>("grant_type", "client_credentials")
            }));

            if (response.IsSuccessStatusCode) {
                var token = await response.Content.ReadAsAsync<AccessToken>();

                if (token != null) {
                    // Set authorization header for further requests
                    Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.TokenType, token.Token);
                }

                return token;
            }
            else {
                throw new OrchardException(T("Unable to obtain Access Token from PayPal API."));
            }
        }