【问题标题】:Pay Multiple items at once using PayPal REST API ASP.NET MVC使用 PayPal REST API ASP.NET MVC 一次支付多个项目
【发布时间】:2014-05-30 14:15:53
【问题描述】:

您好,我知道这是一个菜鸟问题,但我找不到任何文件,我想在同一笔交易中支付 1 件或多件商品,但我收到此错误:

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

我对项目列表部分进行了硬编码,但我不明白 Amount.Total 和我所有项目价格 * 数量之间的差异

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

如果我删除项目列表,它可以工作,但数量中只描述了 1 个项目 我能做些什么?你有这方面的指南吗? Paypal 指南和演示仅适用于一项

谢谢

【问题讨论】:

标签: c# asp.net-mvc paypal shopping-cart


【解决方案1】:

我没有看到完整回复的猜测是,您的总数与详细信息/项目列表不相加。验证将所有项目值相加,必须等于小计,小计 + 税等(详细信息)必须等于总计。

sum (item.price * item.count) == sub total

详细信息的总和 == 总计

【讨论】:

    猜你喜欢
    • 2013-08-03
    • 2019-05-15
    • 1970-01-01
    • 2015-08-01
    • 2017-02-16
    • 2015-11-23
    • 2014-04-05
    • 2016-11-01
    • 2013-11-17
    相关资源
    最近更新 更多