Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用贝宝REST API ASP.NET MVC一次性支付多个项目_C#_Asp.net Mvc_Paypal_Shopping Cart - Fatal编程技术网

C# 使用贝宝REST API ASP.NET MVC一次性支付多个项目

C# 使用贝宝REST API ASP.NET MVC一次性支付多个项目,c#,asp.net-mvc,paypal,shopping-cart,C#,Asp.net Mvc,Paypal,Shopping Cart,您好,我知道这是一个noob问题,但我找不到有关此问题的任何文档,我想在同一笔交易中支付1个或多个项目,但我遇到以下错误: Exception in HttpConnection Execute: Invalid HTTP response The remote server returned an error: (400) Bad Request. 我硬编码的项目清单的一部分,但我不明白之间的差异金额。总额和我的所有项目的价格价格*数量 public ActionResult CreateP

您好,我知道这是一个noob问题,但我找不到有关此问题的任何文档,我想在同一笔交易中支付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);
        }
    }
public ActionResult CreatePayment(字符串描述,十进制价格,十进制税=0,十进制装运=0)
{
var viewData=新的PayPalViewData();
var guid=guid.NewGuid().ToString();
var paymentInit=新付款
{
intent=“授权”,
付款人=新付款人
{
付款方式=“贝宝”
},
事务=新列表
{
新交易
{
项目列表=新项目列表{
项目=新列表{
新项目{
name=“第1项”,
货币=“美元”,
price=“20”,
数量=“2”
},
新项目{
name=“第2项”,
货币=“美元”,
price=“40”,
数量=“1”
},
新项目{
name=“第3项”,
货币=“美元”,
price=“40”,
数量=“1”
}
}
},
金额=新金额
{
货币=“欧元”,
总计=(价格+税费+运费)。ToString(),
详细信息=新的详细信息
{
小计=price.ToString(),
tax=tax.ToString(),
shipping=shipping.ToString()
}
},
描述=描述
},
},
重定向\u URL=新的重定向URL
{
return_url=Utilities.toabsolutionUrl(HttpContext,String.Format(“~/paypal/confirm?id={0}”,guid)),
cancel\u url=Utilities.toabsolutionUrl(HttpContext,String.Format(“~/paypal/canceled?id={0}”,guid)),
},
};
viewData.JsonRequest=JObject.Parse(paymentInit.ConvertToJson()).ToString(Formatting.Indented);
尝试
{
var accessToken=新的OAuthTokenCredential(ConfigManager.Instance.GetProperties()[“ClientID”],ConfigManager.Instance.GetProperties()[“ClientSecret”]).GetAccessToken();
var apiContext=新的apiContext(accessToken);
var createdPayment=paymentInit.Create(apiContext);
var approvalUrl=createdPayment.links.ToArray().FirstOrDefault(f=>f.rel.Contains(“批准url”);
如果(approvalUrl!=null)
{
添加(guid,createdPayment.id);
返回重定向(approvalUrl.href);
}
viewData.JsonResponse=JObject.Parse(createdPayment.ConvertToJson()).ToString(Formatting.Indented);
返回视图(“错误”,viewData);
}
捕获(PayPalException例外)
{
viewData.ErrorMessage=例如消息;
返回视图(“错误”,viewData);
}
}
如果我删除项目列表,它可以工作,但只有金额中描述的1个项目 我能做什么?你有这方面的指南吗?Paypal GUI和演示仅适用于一个项目


谢谢

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

总和(项目价格*项目计数)=小计


sum of details==total

谢谢,但我不想在视图中这样做,paypal链接是用于经典api的,我使用Rest api,它没有示例代码,只是商家和其他信息的费用。我想你必须自己编写一些代码。你解决过这个问题吗?遇到类似的问题