Esempio n. 1
0
        private async Task <PayPalPaymentCreatedResponse> CreatePaypalPaymentAsync(HttpClient http, PayPalAccessToken accessToken, double total, string currency, ItemList itemList)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "v1/payments/payment");

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

            //foreach (var item in itemList.Items)
            //{
            //    Debug.WriteLine(item.Name + " " + item.Quantity);
            //}

            var payment = new Payment()
            {
                Intent       = "sale",
                Transactions = new List <Transaction>()
                {
                    new Transaction()
                    {
                        Amount = new Amount()
                        {
                            Total    = total.ToString(),
                            Currency = currency
                        },
                        ItemList = itemList
                    }
                },
                RedirectUrls = new RedirectUrls()
                {
                    CancelUrl = configuration["PayPal:cancelUrl"],
                    ReturnUrl = configuration["PayPal:returnUrl"]
                },
                Payer = new Payer()
                {
                    PaymentMethod = "paypal"
                }
            };

            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);
        }
Esempio n. 2
0
 public async Task <string> getRedirectURLtoPayPal(double total, string currency, ItemList itemList)
 {
     try
     {
         return(Task.Run(async() =>
         {
             HttpClient http = GetPaypalHttpClient();
             PayPalAccessToken accessToken = await GetPayPalAccessTokenAsync(http);
             PayPalPaymentCreatedResponse createdPayment = await CreatePaypalPaymentAsync(http, accessToken, total, currency, itemList);
             return createdPayment.links.First(x => x.rel == "approval_url").href;
         }).Result);
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Fail to login PayPal step 1 (get redirect): " + ex.Message);
         return(null);
     }
 }