2017-02-14 80 views
0

我试图设置PayPal以接受我的网站根据他们上传的照片计算费用的付款。跟着tutorial,但我想通过我在我的其他控制器计算的价格。PayPal API和MVC5:从其他控制器获取价格

我的PayPal控制器:

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

    } 

控制器在我的价格计算方法:

 int Asection; 
     int Bsection; 
     int Csection; 
     int Dsection; 

     if (viewPhotos.GetA1.Any() || viewPhotos.GetA2.Any() || viewPhotos.GetA3.Any() || viewPhotos.GetA4.Any()) 
     { 
      Asection = 1; 
     } 
     else 
     { 
      Asection = 0; 
     } 

     if (viewPhotos.GetB1.Any() || viewPhotos.GetB2.Any() || viewPhotos.GetB3.Any() || viewPhotos.GetB4.Any()) 
     { 
      Bsection = 1; 
     } 
     else 
     { 
      Bsection = 0; 
     } 

     if (viewPhotos.GetC1.Any() || viewPhotos.GetC2.Any() || viewPhotos.GetC3.Any() || viewPhotos.GetC4.Any()) 
     { 
      Csection = 1; 
     } 
     else 
     { 
      Csection = 0; 
     } 

     if (viewPhotos.GetD1.Any() || viewPhotos.GetD2.Any() || viewPhotos.GetD3.Any() || viewPhotos.GetD4.Any()) 
     { 
      Dsection = 1; 
     } 
     else 
     { 
      Dsection = 0; 
     } 

     int TotalSection = Asection + Bsection + Csection + Dsection; 

     viewPhotos.MoneyValue = TotalSection; 

     int RequiredMoney; 
     if (TotalSection == 1) 
     { 
      RequiredMoney = 20; 
     } 
     else if (TotalSection == 2) 
     { 
      RequiredMoney = 25; 
     } 
     else if (TotalSection == 3) 
     { 
      RequiredMoney = 30; 
     } 
     else 
     { 
      RequiredMoney = 36; 
     } 

     viewPhotos.RequiredMoney = RequiredMoney; 

     return View(viewPhotos); 

我的视图,其中的价格显示出用户:

<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") 

到目前为止上述代码使用网站上的默认测试项目价格和详细信息。如果有人能够帮助展示我如何设置PayPal收取的金额到我的计算价格,无需任何运费或税金,我们将不胜感激。提前致谢。

+0

_sample_正在为其“CreatePayment”函数中的“items”创建硬编码数据。你必须连接你的用户选定的项目。换句话说,相应地替换'CreatePayment'中的硬编码项。心连心。 – EdSF

+0

嗨@EdSF,我明白你在说什么,但我不知道如何实现它。你会如此善良地展示或分享相关教程吗?谢谢。 – Eva

+0

请不要像你在做'new Random()'那样内联,因为你可以很容易地得到结果不是随机的情况。你应该总是为'Random'创建一个单一的静态变量,并且只要你需要它就重新使用它。 – Enigmativity

回答

0

搜索和查看日志一段时间后,这里是我如何解决我的问题:

在那里我计算我的价格控制,我用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" 
}); 

按F5,并得到来自贝宝沙箱成功的响应。活泉!