Exemplo n.º 1
0
        static async Task Main(string[] args)
        {
            Console.Write("Enter clienId:");
            var clientId = Console.ReadLine();

            Console.Write("Enter SecretKey:");
            var secret = Console.ReadLine();


            var model = new PayPalCreateOrderModel
            {
                intent         = "SALE",
                purchase_units = new List <PurchaseUnit>
                {
                    new PurchaseUnit
                    {
                        reference_id = Guid.NewGuid().ToString(),
                        description  = "Test paypal",
                        amount       = new Amount
                        {
                            currency = "USD",
                            total    = $"10",
                            details  = new Details
                            {
                                subtotal = $"10",
                                shipping = "0",
                                tax      = $"0"
                            }
                        },
                        items = new List <Item>
                        {
                            new Item
                            {
                                currency = "USD",
                                name     = $"Test Item",
                                price    = $"10",
                                tax      = "0",
                                quantity = "1"
                            }
                        },
                    }
                },
                redirect_urls = new RedirectUrls
                {
                    cancel_url = "",
                    return_url = ""
                }
            };


            var paypal = new PayPal(clientId, secret, "Sandbox");
            await paypal.CreatePayment(model);

            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }
Exemplo n.º 2
0
        public async Task <PayPalCreateOrderResponse> CreatePayment(PayPalCreateOrderModel orderModel)
        {
            try
            {
                ServicePointManager.ServerCertificateValidationCallback +=
                    (sender, cert, chain, sslPolicyErrors) => true;
                var request = new HttpRequestMessage(HttpMethod.Post, _GetCheckoutEndpoint());

                request.Headers.Add("Accept", "application/json");

                var getToken = await _GetToken();

                if (getToken == null)
                {
                    return(null);
                }

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


                //pass in data specifying the encoding type and media type passed in
                request.Content = new StringContent(JsonSerializer.Serialize(orderModel), Encoding.UTF8, "application/json");

                var client   = new HttpClient();
                var response = await client.SendAsync(request);

                if (response.IsSuccessStatusCode)
                {
                    await using var responseStream = await response.Content.ReadAsStreamAsync();

                    var res = await JsonSerializer.DeserializeAsync <PayPalCreateOrderResponse>(responseStream);

                    Console.WriteLine(JsonSerializer.Serialize(res));

                    return(res);
                }

                return(null);
            }
            catch (Exception)
            {
                return(null);
            }
        }