首页 > 解决方案 > PayPal - 如何在 PayPal 订单请求正文中动态创建订单项?

问题描述

我目前正在尝试将 PayPal 智能按钮集成到我的 asp.net 核心 Web 应用程序中。我参考了以下 API 文档以从我的服务器使用 PayPal API 设置交易:https ://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/#on-服务器。但是,文档对 PayPal 订单请求正文中的行项目进行了硬编码。现在,我有一个名为lineItems的变量,其中包含我要发送到 PayPal 的产品列表。如何在 BuildRequestBody() 方法中动态创建 lineItems?以下是我当前的代码:

更新我仍在尝试解决此问题。非常感谢您的帮助!

Update2还在尝试解决这个问题,非常需要帮助。

Update3有人和我有同样的问题吗?文档非常模糊,我需要帮助。

Update4无进展,仍需帮助!

        public async Task<HttpResponse> createPaypalTransaction()
        {
            List<Product> lineItems = new List<Product>();
            decimal totalPrice = 4.00;

            lineItems.Add(new Product
            {
                ProductId = 1,
                ProductName = "T-Shirt",
                Quantity = 2,
                Price = 2.00M
            });

            lineItems.Add(new SanitizeProduct
            {
                ProductId = 2,
                ProductName = "Shoe",
                Quantity = 2,
                Price = 2.00M
            });

            var request = new OrdersCreateRequest();
            request.Prefer("return=representation");
            request.RequestBody(BuildRequestBody());
            //3. Call PayPal to set up a transaction
            var response = await PayPalClient.client().Execute(request);

            var result = response.Result<PayPalCheckoutSdk.Orders.Order>();
            Console.WriteLine("Status: {0}", result.Status);
            Console.WriteLine("Order Id: {0}", result.Id);
            Console.WriteLine("Intent: {0}", result.Intent);
            Console.WriteLine("Links:");
            foreach (LinkDescription link in result.Links)
            {
                Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
            }
            AmountWithBreakdown amount = result.PurchaseUnits[0].Amount;
            Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
            return response; 
        }

        /*
          Method to generate sample create order body with CAPTURE intent

          @return OrderRequest with created order request
         */
        private static OrderRequest BuildRequestBody(decimal totalPrice, List<SanitizeProduct> lineItems)
        {
            OrderRequest orderRequest = new OrderRequest()
            {
                Intent = "CAPTURE",

                ApplicationContext = new ApplicationContext
                {
                    BrandName = "12345",
                    UserAction = "CONTINUE",
                },
                PurchaseUnits = new List<PurchaseUnitRequest>
        {
          new PurchaseUnitRequest{
            ReferenceId =  "PUHF",
            Description = "Customisable Goods",
            CustomId = "CUST-HighFashions",
            SoftDescriptor = "HighFashions",
            Amount = new AmountWithBreakdown
            {
              CurrencyCode = "SGD",
              Value = "230.00",
              Breakdown = new AmountBreakdown
              {
                // The subtotal for all items (quantity * price)
                ItemTotal = new Money
                {
                  CurrencyCode = "SGD",
                  Value = "230.00"
                },
              }
            },

// How do i dynamically generate the list of items instead of hardcoding it?
             Items = new List<Item>
            {
              new Item
              {
                Name = "T-shirt",
                Description = "Green XL",
                Sku = "sku01",
                UnitAmount = new Money
                {
                  CurrencyCode = "SGD",
                  Value = "90.00"
                },
                Tax = new Money
                {
                  CurrencyCode = "SGD",
                  Value = "10.00"
                },
                Quantity = "1",
                Category = "PHYSICAL_GOODS"
              },
              new Item
              {
                Name = "Shoes",
                Description = "Running, Size 10.5",
                Sku = "sku02",
                UnitAmount = new Money
                {
                  CurrencyCode = "SGD",
                  Value = "45.00"
                },
                Tax = new Money
                {
                  CurrencyCode = "SGD",
                  Value = "5.00"
                },
                Quantity = "2",
                Category = "PHYSICAL_GOODS"
              }
            },
          }
    }
            };
            return orderRequest;
        }

标签: c#asp.netasp.net-corepaypalpayment-gateway

解决方案


推荐阅读