Exemple #1
0
        public async Task <IActionResult> Checkout()
        {
            //SandboxEnvironment(clientId, clientSerect)
            var environment = new SandboxEnvironment("AQpgvR2MiWimK_hj88CQvkSyNg5xE_097z5rawOtDRvidGAGGxrdJdgKhb34l7sQfKTPeA1Z7O-f1Lkl", "ENitUdOKY6bjZ9w3tnqaETbcJeqYLrFot0lBhUgZlccaAEdkr1gFeTWGzh0zG_npFJ1MUGZbTzZLvJWe");
            var client      = new PayPalHttpClient(environment);

            //Đọc thông tin đơn hàng từ Session
            var itemList = new ItemList()
            {
                Items = new List <Item>()
            };

            var tongTien = Cart.Sum(p => p.ThanhTien);

            foreach (var item in Cart)
            {
                itemList.Items.Add(new Item()
                {
                    Name     = item.HangHoa.TenHh,
                    Currency = "USD",
                    Price    = item.HangHoa.GiaBan.ToString(),
                    Quantity = item.SoLuong.ToString(),
                    Sku      = "sku",
                    Tax      = "0"
                });
            }
            //itemList.Items.Add(new Item()
            //{
            //    Name = "Coca",
            //    Currency = "USD",
            //    Price = "5",
            //    Quantity = "1",
            //    Sku = "sku",
            //    Tax = "0"
            //});
            //itemList.Items.Add(new Item()
            //{
            //    Name = "Pepsi",
            //    Currency = "USD",
            //    Price = "2",
            //    Quantity = "3",
            //    Sku = "sku",
            //    Tax = "0"
            //});

            var payment = new Payment()
            {
                Intent       = "sale",
                Transactions = new List <Transaction>()
                {
                    new Transaction()
                    {
                        Amount = new Amount()
                        {
                            Total    = tongTien.ToString(),
                            Currency = "USD",
                            Details  = new AmountDetails
                            {
                                Tax      = "0",
                                Shipping = "0",
                                Subtotal = tongTien.ToString()
                            }
                        },
                        ItemList      = itemList,
                        Description   = "Don hang 001",
                        InvoiceNumber = DateTime.Now.Ticks.ToString()
                    }
                },
                RedirectUrls = new RedirectUrls()
                {
                    CancelUrl = "http://localhost:50289/Paypal/Fail",
                    ReturnUrl = "http://localhost:50289/Paypal/Success"
                },
                Payer = new Payer()
                {
                    PaymentMethod = "paypal"
                }
            };

            PaymentCreateRequest request = new PaymentCreateRequest();

            request.RequestBody(payment);

            try
            {
                HttpResponse response = await client.Execute(request);

                var     statusCode = response.StatusCode;
                Payment result     = response.Result <Payment>();

                var    links             = result.Links.GetEnumerator();
                string paypalRedirectUrl = null;
                while (links.MoveNext())
                {
                    LinkDescriptionObject lnk = links.Current;
                    if (lnk.Rel.ToLower().Trim().Equals("approval_url"))
                    {
                        //saving the payapalredirect URL to which user will be redirected for payment
                        paypalRedirectUrl = lnk.Href;
                    }
                }

                return(Redirect(paypalRedirectUrl));
            }
            catch (HttpException httpException)
            {
                var statusCode = httpException.StatusCode;
                var debugId    = httpException.Headers.GetValues("PayPal-Debug-Id").FirstOrDefault();

                return(RedirectToAction("Fail"));
            }
        }