예제 #1
0
        private async Task <PayPalAccessToken> GetPayPalAccessTokenAsync(HttpClient http)
        {
            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 responce = await http.SendAsync(request);

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

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

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

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken1.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 responce = await http.SendAsync(request);

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

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

            return(PayPalPaymentCreated);
        }
예제 #3
0
        private async Task <PayPalPaymentExecutedResponse> executedPaymentAsync(HttpClient http, PayPalAccessToken accessToken1, string paymentId, string payerId)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"/v1/payments/payment/{paymentId}/execute");

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken1.access_token);
            var payment = JObject.FromObject(new
            {
                payer_id = payerId
            });

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

            HttpResponseMessage responce = await http.SendAsync(request);

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

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

            return(executedPayment);
        }