private static JObject ToDto(CreatePaymentParameter parameter) { if (parameter == null) { throw new ArgumentNullException(nameof(parameter)); } var jObj = new JObject(); var redirectUrls = new JObject(); var transactions = new JArray(); var intent = string.Empty; switch (parameter.Intent) { case IntentPayments.sale: intent = "sale"; break; case IntentPayments.authorize: intent = "authorize"; break; case IntentPayments.order: intent = "order"; break; } if (parameter.Transactions != null) { foreach (var transaction in parameter.Transactions) { transactions.Add(ToDto(transaction)); } } redirectUrls.Add("return_url", parameter.ReturnUrl); redirectUrls.Add("cancel_url", parameter.CancelUrl); jObj.Add("intent", intent); if (parameter.Payer != null) { jObj.Add("payer", ToDto(parameter.Payer)); } jObj.Add("transactions", transactions); jObj.Add("redirect_urls", redirectUrls); return(jObj); }
public async Task <CreatePaymentResponse> CreatePayment(CreatePaymentParameter parameter) { if (parameter == null) { throw new ArgumentNullException(nameof(parameter)); } if (string.IsNullOrWhiteSpace(parameter.AccessToken)) { throw new ArgumentNullException(nameof(parameter.AccessToken)); } var jobj = ToDto(parameter); var client = _httpClientFactory.GetHttpClient(); var request = new HttpRequestMessage { Method = HttpMethod.Post, Content = new StringContent(jobj.ToString(), Encoding.UTF8, "application/json"), RequestUri = new Uri($"{BaseConstants.RESTSandboxEndpoint}v1/payments/payment") }; request.Headers.Add("Authorization", $"Bearer {parameter.AccessToken}"); var response = await client.SendAsync(request).ConfigureAwait(false); var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var jObj = JObject.Parse(content); try { response.EnsureSuccessStatusCode(); } catch (Exception) { return(new CreatePaymentResponse { IsValid = false, ErrorResponse = ToErrorResponseModel(jObj) }); } return(ToCreatePaymentModel(jObj)); }