簡體   English   中英

使用PayPal REST API ASP.NET MVC一次支付多個項目

[英]Pay Multiple items at once using PayPal REST API ASP.NET MVC

嗨,我知道這是一個菜鳥問題,但我找不到與此相關的任何文檔,我想在同一筆交易中支付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指導和演示僅適用於一項

謝謝

我的猜測(沒有看到完整的答復)是,您的總數未加上詳細信息/項目列表。 驗證將所有項目值加起來,必須等於小計,小計+稅等(細節)必須等於總計。

總和(item.price * item.count)==小計

細節總和==總計

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM