public async Task<PaymentContext> CreatePaymentAsync(Payment Payment) { using (var client = CreateClient(Settings.UseSandbox, _token)) { try { _token = _token ?? await GetAccessTokenAsync(client, Settings); var response = await client.PostAsJsonAsync("v1/payments/payment", Payment); if (response.IsSuccessStatusCode) { var createdPayment = await response.Content.ReadAsAsync<Payment>(); return new PaymentContext() { UseSandbox = Settings.UseSandbox, Payment = createdPayment, ValidUntil = _clock.UtcNow.AddSeconds(_token.ExpiresIn), Token = _token }; } else { var errorMsg = await response.Content.ReadAsStringAsync(); Logger.Error("Payment creation failed. ({0}) {1}\r\n{2}", response.StatusCode, response.ReasonPhrase, errorMsg); throw new OrchardException(T("Payment creation failed.")); } } catch(Exception exp) { throw new OrchardException(T("Payment creation failed."), exp); } } }
private HttpClient CreateClient(bool UseSandbox, AccessToken Token = null) { var client = new HttpClient(); client.BaseAddress = new Uri(UseSandbox ? PaypalApiService.SandboxEndpoint : PaypalApiService.LiveEndpoint); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (Token != null) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(Token.TokenType, Token.Token); } return client; }
public async Task<string> CreateWebProfile(WebProfile Profile) { using (var client = CreateClient(Settings.UseSandbox, _token)) { try { _token = _token ?? await GetAccessTokenAsync(client, Settings); var response = await client.PostAsJsonAsync("v1/payment-experience/web-profiles", Profile); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsAsync<CreateProfileResponse>(); return result.Id; } else { throw new OrchardException(T("Web profile creation failed.")); } } catch (Exception exp) { throw new OrchardException(T("Web profile creation failed."), exp); } } }