2014-04-16 47 views
0

嗨,我知道這是一個noob問題,但我無法找到任何文檔,我想在同一交易中支付一個或多個項目,但是我得到了這個錯誤:使用PayPal REST API一次支付多個項目ASP.NET MVC

Exception in HttpConnection Execute: Invalid HTTP response The remote server returned an error: (400) Bad Request. 

IM硬編碼的項目列表的一部分,但我不understan beetween 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項目我能做什麼?你有這個指導嗎?貝寶的GUID和演示是一個項目只

感謝

+3

http://stackoverflow.com/q/3308898 –

+0

HTTPS://developer.paypal .com/docs/classic/use-cases/uc_webcheckout-multiple-items/ –

+0

謝謝但我不想在視圖中執行此操作,而PayPal鏈接是針對經典api的,即時通訊使用Rest api並且它沒有示例代碼只是商家和其他信息的費用 –

回答

1

我的猜測,這是沒有看到完整的迴應是,你總不與細節/項目名單加起來。驗證合計所有項目值,必須等於小計,小計+稅等(細節)必須等於總計。

總和(item.price * item.count)==子總

的細節總和==總