使用PayPal REST API ASP.NET MVC一次支付多个项目

本文关键字:一次 项目 API REST PayPal ASP NET MVC 使用 | 更新日期: 2023-09-27 17:58:41

嗨,我知道这是一个棘手的问题,但我找不到任何文档,我想在同一笔交易中支付1个或多个项目,但我收到了这个错误:

Exception in HttpConnection Execute: Invalid HTTP response The remote server returned an error: (400) Bad Request.

我很难对商品清单部分进行编码,但我不理解数量之间的差异。我所有商品的价格*数量

public ActionResult CreatePayment(string description, decimal price, decimal tax = 0, decimal shipping = 0)
    {
        var viewData = new PayPalViewData();
        var guid = Guid.NewGuid().ToString();
        var paymentInit = new Payment
        {
            intent = "authorize",
            payer = new Payer
            {
                payment_method = "paypal"
            },
            transactions = new List<Transaction>
            {
                new Transaction 
                {
                    item_list = new ItemList{
                        items = new List<Item>{
                                new Item{
                                name = "item 1",
                                currency = "USD",
                                price = "20",
                                quantity = "2"
                                },
                                new Item{
                                name = "item 2",
                                currency = "USD",
                                price = "40",
                                quantity = "1"
                                },
                                new Item{
                                name = "item 3",
                                currency = "USD",
                                price = "40",
                                quantity = "1"
                                }
                        }
                    },
                    amount = new Amount
                    {
                        currency = "EUR",
                        total = (price + tax + shipping).ToString(),
                        details = new Details
                        {
                            subtotal = price.ToString(),
                            tax = tax.ToString(),
                            shipping = shipping.ToString()
                        }
                    },
                    description = description
                },
            },
            redirect_urls = new RedirectUrls
            {
                return_url = Utilities.ToAbsoluteUrl(HttpContext, String.Format("~/paypal/confirmed?id={0}", guid)),
                cancel_url = Utilities.ToAbsoluteUrl(HttpContext, String.Format("~/paypal/canceled?id={0}", guid)),
            },
        };
        viewData.JsonRequest = JObject.Parse(paymentInit.ConvertToJson()).ToString(Formatting.Indented);
        try
        {
            var accessToken = new OAuthTokenCredential(ConfigManager.Instance.GetProperties()["ClientID"], ConfigManager.Instance.GetProperties()["ClientSecret"]).GetAccessToken();
            var apiContext = new APIContext(accessToken);
            var createdPayment = paymentInit.Create(apiContext);
            var approvalUrl = createdPayment.links.ToArray().FirstOrDefault(f => f.rel.Contains("approval_url"));
            if (approvalUrl != null)
            {
                Session.Add(guid, createdPayment.id);
                return Redirect(approvalUrl.href);
            }
            viewData.JsonResponse = JObject.Parse(createdPayment.ConvertToJson()).ToString(Formatting.Indented);
            return View("Error", viewData);
        }
        catch (PayPalException ex)
        {
            viewData.ErrorMessage = ex.Message;
            return View("Error", viewData);
        }
    }

如果我删除项目列表,它有效,但金额中只描述了一个项目我能做什么?你有这方面的指南吗?Paypal指南和演示仅适用于一项

感谢

使用PayPal REST API ASP.NET MVC一次支付多个项目

我的猜测是,在没有看到完整响应的情况下,您的总数与详细信息/项目列表不一致。验证将所有项目值相加,小计必须相等,小计+税款等(详细信息)必须相等。

sum(item.price*item.count)==小计

详细信息之和=总