Ejemplo n.º 1
0
        public async Task <string> GetRedirectUrlToPaypal(double total, string currency)
        {
            try
            {
                HttpClient http = GetPaypalHttpClient();

                PaypalAccessToken accessToken = await GetPaypalAccessTokenAsync(http);

                PaypalPaymentCreateResponce paypalPaymentCreateResponce = await CreatePaypalPaymentAsync(http, accessToken, total, currency);

                return(paypalPaymentCreateResponce.links.First(x => x.rel == "approval_url").href);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex, "Failed to login to PalPal");
                return(null);
            }
        }
Ejemplo n.º 2
0
        public async Task <PaypalPaymentCreateResponce> CreatePaypalPaymentAsync(HttpClient httpclient, PaypalAccessToken accessToken, double total, string currency)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/v1/payments/payment/");//--/v1/checkout/orders/ /v2/checkout/orders/


            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();

            PaypalPaymentCreateResponce payPalPaymentExecutedResponse = JsonConvert.DeserializeObject <PaypalPaymentCreateResponce>(content);

            return(payPalPaymentExecutedResponse);
        }