예제 #1
0
        private async Task <PayPalPaymentExecuteResponse> ExecutePayPalPaymentAsync(HttpClient httpClient,
                                                                                    PayPalAccessToken accessToken,
                                                                                    string paymentID,
                                                                                    string payerID)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"v1/payments/payment/{paymentID}/execute");

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.access_token);

            var payment = JObject.FromObject(new { payer_id = payerID });

            request.Content = new StringContent(JsonConvert.SerializeObject(payment), Encoding.UTF8, "application/json");

            HttpResponseMessage response = await httpClient.SendAsync(request);

            string content = await response.Content.ReadAsStringAsync();

            PayPalPaymentExecuteResponse executedPayment = JsonConvert.DeserializeObject <PayPalPaymentExecuteResponse>(content);

            return(executedPayment);
        }
예제 #2
0
        public async Task <PayPalPaymentCreatedResponse> CreatePayPalPaymentAsync(HttpClient httpClient,
                                                                                  PayPalAccessToken accessToken,
                                                                                  double total,
                                                                                  string currency)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/v1/payments/payment");

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.access_token);

            var payment = JObject.FromObject(new
            {
                intent        = "sale",
                redirect_urls = new
                {
                    return_url = configuration["PayPal:returnUrl"],
                    cancel_url = configuration["PayPal:cancelUrl"]
                },
                payer        = new { payment_method = "PayPal" },
                transactions = JArray.FromObject(new[]
                {
                    new {
                        amount = new {
                            total    = total,
                            currency = currency
                        }
                    }
                })
            });

            request.Content = new StringContent(JsonConvert.SerializeObject(payment), Encoding.UTF8, "application/json");

            HttpResponseMessage response = await httpClient.SendAsync(request);

            string content = await response.Content.ReadAsStringAsync();

            PayPalPaymentCreatedResponse payPalPaymentCreated = JsonConvert.DeserializeObject <PayPalPaymentCreatedResponse>(content);

            return(payPalPaymentCreated);
        }
예제 #3
0
        private async Task <PayPalAccessToken> GetPayPalAccessTokenAsync(HttpClient httpClient)
        {
            byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes($"{configuration["PayPal:clientID"]}:{configuration["PayPal:secret"]}");

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/v1/oauth2/token");

            request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(bytes));

            var form = new Dictionary <string, string> {
                ["grant_type"] = "client_credentials"
            };

            request.Content = new FormUrlEncodedContent(form);

            HttpResponseMessage response = await httpClient.SendAsync(request);

            string content = await response.Content.ReadAsStringAsync();

            PayPalAccessToken accessToken = JsonConvert.DeserializeObject <PayPalAccessToken>(content);

            return(accessToken);
        }