Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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# 贝宝API&;MVC5:从另一个控制器获取价格_C#_Asp.net Mvc_Paypal - Fatal编程技术网

C# 贝宝API&;MVC5:从另一个控制器获取价格

C# 贝宝API&;MVC5:从另一个控制器获取价格,c#,asp.net-mvc,paypal,C#,Asp.net Mvc,Paypal,我正在尝试设置PayPal以接受来自我的网站的付款,该网站根据上传的照片数量计算费用。遵循a,但我想传递我在另一个控制器中计算的价格 我的贝宝控制器: public ActionResult PaymentWithPaypal() { APIContext apiContext = PayPalConfig.GetAPIContext(); try { string payerId = Request.Params

我正在尝试设置PayPal以接受来自我的网站的付款,该网站根据上传的照片数量计算费用。遵循a,但我想传递我在另一个控制器中计算的价格

我的贝宝控制器:

public ActionResult PaymentWithPaypal()
    {
        APIContext apiContext = PayPalConfig.GetAPIContext();

        try
        {
            string payerId = Request.Params["PayerID"];

            if (string.IsNullOrEmpty(payerId))
            {
                string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Paypal/PaymentWithPayPal?";
                var guid = Convert.ToString((new Random()).Next(100000));                    
                var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid);
                var links = createdPayment.links.GetEnumerator();
                string paypalRedirectUrl = null;
                while (links.MoveNext())
                {
                    Links lnk = links.Current;
                    if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                    {                            
                        paypalRedirectUrl = lnk.href;
                    }
                }

                Session.Add(guid, createdPayment.id);
                return Redirect(paypalRedirectUrl);
            }
            else
            {               
                var guid = Request.Params["guid"];
                var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);
                if (executedPayment.state.ToLower() != "approved")
                {
                    return View("FailureView");
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Log("Error" + ex.Message);
            return View("FailureView");
        }
        return View("SuccessView");
    }

    private PayPal.Api.Payment payment;

    private PayPal.Api.Payment ExecutePayment(APIContext apiContext, string payerId, string paymentId)
    {
        var paymentExecution = new PaymentExecution() { payer_id = payerId };
        this.payment = new PayPal.Api.Payment() { id = paymentId };
        return this.payment.Execute(apiContext, paymentExecution);
    }

    private PayPal.Api.Payment CreatePayment(APIContext apiContext, string redirectUrl)
    {
        var itemList = new ItemList() { items = new List<Item>() };
        itemList.items.Add(new Item()
        {
            name = "Participation Fee",
            currency = "USD",
            price = "5",
            quantity = "1",
            sku = "sku"
        });

        var payer = new Payer() { payment_method = "paypal" };
        var redirUrls = new RedirectUrls()
        {
            cancel_url = redirectUrl,
            return_url = redirectUrl
        };            
        var details = new Details()
        {
            tax = "1",
            shipping = "1",
            subtotal = "5"
        };            
        var amount = new Amount()
        {
            currency = "USD",
            total = "7", 
            details = details
        };

        var transactionList = new List<Transaction>();
        transactionList.Add(new Transaction()
        {
            description = "Transaction description.",
            invoice_number = "your invoice number",
            amount = amount,
            item_list = itemList
        });

        this.payment = new PayPal.Api.Payment()
        {
            intent = "sale",
            payer = payer,
            transactions = transactionList,
            redirect_urls = redirUrls
        };
        return this.payment.Create(apiContext);

    }
向用户显示价格的我的视图:

<p>You will need to pay participation fees USD @Model.RequiredMoney.</p>
<h3>Total: USD @Model.RequiredMoney</h3>
@Html.ActionLink("Make Payment with PayPal", "PaymentWithPaypal", "Paypal")
您需要支付USD@Model.RequiredMoney参与费

总计:USD@Model.RequiredMoney @ActionLink(“使用贝宝支付”、“使用贝宝支付”、“贝宝支付”)

到目前为止,上述代码适用于现场的默认测试项目价格和细节。如果有人能帮助我说明如何将PayPal收取的金额设置为我的计算价格,而不收取任何运费或税费,我将不胜感激。提前感谢。

在搜索和查看日志一段时间后,以下是我解决问题的方法:

在我计算价格的控制器上,我使用TempData存储价格:

TempData["ParticipationFee"] = RequiredMoney;
然后在PaypalController的
CreatePayment
功能下

var itemList = new ItemList() { items = new List<Item>() };

string FeeAmount = TempData["ParticipationFee"].ToString();

itemList.items.Add(new Item()
{
    name = "Participation Fee",
    currency = "USD",
    price = FeeAmount,
    quantity = "1",
    sku = "sku"
 });
var itemList=new itemList(){items=new List()};
string feeamunit=TempData[“ParticipationFee”].ToString();
itemList.items.Add(新项()
{
name=“参与费”,
货币=“美元”,
价格=金额,
quantity=“1”,
sku=“sku”
});

点击F5并从Paypal Sandbox获得成功响应。呜

示例正在其
CreatePayment
函数中为“项目”创建硬编码数据。您必须将用户选择的项目连接到它。换句话说,相应地替换
CreatePayment
中的硬编码项目。嗨,嗨,EdSF,我明白你的意思,但我不知道如何做到。您是否愿意演示或分享相关的教程?谢谢。请不要像现在这样内联
new Random()
,因为很容易出现结果不是随机的情况。您应该始终为
Random
创建一个静态变量,并在任何需要的地方重复使用。感谢@Enigmativity的评论。我对这整件事还很陌生,我的项目之所以仍在运行完全是因为我遵循的教程,所以我不知道如果不是Random()可以替换什么。但我也有点担心您提到的内容,因此我添加了当前userID,后跟Random()作为guid,以减少重复的可能性。我希望这能降低出错的风险。@Eva-不,不会的。您需要将
new Random()
移出到字段级变量,即
private static Random rnd=new Random()。然后,只要在使用
new Random()
时使用
rnd
var itemList = new ItemList() { items = new List<Item>() };

string FeeAmount = TempData["ParticipationFee"].ToString();

itemList.items.Add(new Item()
{
    name = "Participation Fee",
    currency = "USD",
    price = FeeAmount,
    quantity = "1",
    sku = "sku"
 });