Exemple #1
0
 public async Task <string> getRedirectURLToPayPal(double total, string currency)
 {
     try
     {
         return(Task.Run(async() =>
         {
             HttpClient http = GetPaypalHttpClient();
             PayPalAccessToken accessToken = await GetPayPalAccessTokenAsync(http);
             PayPalPaymentCreatedResponse createdPayment = await CreatePaypalPaymentAsync(http, accessToken, total, currency);
             return createdPayment.links.First(x => x.rel == "approval_url").href;
         }).Result);
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex, "Failed to login to PayPal");
         return(null);
     }
 }
Exemple #2
0
        private async Task <PayPalPaymentCreatedResponse> CreatePaypalPaymentAsync(HttpClient http, 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 http.SendAsync(request);

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

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

            return(payPalPaymentCreated);
        }