2017-02-14 79 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,並得到來自貝寶沙箱成功的響應。活泉!