簡體   English   中英

Scala Java Paypal-SDK無法構造有效的交易

[英]scala java paypal-sdk cant construct valid Transaction

我正在努力解決一個奇怪的問題,或者僅僅是我自己的愚蠢。 從邏輯上講,最令我困惑的是我做的與此博文相同: Link 我的代碼如下所示:

def createPayment(products: List[Product], company: Company, user: User):   Either[Payment, String] = {
if (products.nonEmpty) {
  val guid = UUID.randomUUID().toString.replaceAll("-", "")
  val redirectUrls = new RedirectUrls
  redirectUrls.setReturnUrl("https://myurl/#/paysuccess?guid=" + guid)
  redirectUrls.setCancelUrl("https://myurl/#/payfail?guid=" + guid)
  val payer = new Payer
  payer.setPaymentMethod("paypal")
  val itemList = new ItemList
  val items = new util.ArrayList[Item]
  var total = 0.0
  var vat = 0.0
  products.foreach {
    product =>
      logger.info("Product: " + product)
      var price = ""
      if (product.isDeal.get && !product.validto.get.before(new Date())) {
        total += product.oldPrice.get
        vat += product.vat.get
        price = product.oldPrice.get.toString
      } else {
        total += product.price.get
        vat += product.vat.get
        price = product.price.get.toString
      }
      val item = new Item
      item.setName(product.name.get)
      item.setCurrency("EUR")
      item.setPrice(price)
      item.setQuantity("1")
      item.setTax(product.vat.get.toString)
      item.setDescription(product.shortInfo.get)
      logger.info("Item: " + item)
      items.add(item)

  itemList.setItems(items)

  val details = new Details
  details.setShipping("0")
  details.setTax(vat.toString)
  val amount = new Amount
  amount.setCurrency("EUR")
  amount.setTotal(total.toString)
  amount.setDetails(details)
  val transaction = new Transaction
  logger.info("NewTrans: " + transaction)

  transaction.setAmount(amount)
  //transaction.setItemList(itemList)
  transaction.setDescription("Thank you for your order")
  logger.info("FinishedTrans: " + transaction)

  val transactions = new java.util.ArrayList[Transaction]
  logger.info("NewTransList: " + transactions)
  transactions.add(transaction)
  logger.info("FinishedTransList: " + transactions)
  val order = new Payment()
  order.setIntent("sale")
  order.setRedirectUrls(redirectUrls)
  order.setPayer(payer)
  order.setTransactions(transactions)
  order.setNoteToPayer(guid)
  logger.info("Order : " + order)
  try {
    val createdOrder = order.create(accessToken)
    Order.setSale(createdOrder, guid, company, user, products)
    Left(createdOrder)
  } catch {
    case e: PayPalRESTException =>
      logger.error("PayPalRESTException " + e.getMessage)
      Right(e.getMessage)
  }
}
else {
  logger.error("No Products found")
  Right("No Products found")
}
}

現在發生的是:

 transaction = new Transaction 

記錄到:

transactions : []

當我打電話時:

transaction.setAmount(amount) 

它記錄到:

{
"transactions": [],
"amount": {
"currency": "EUR",
"total": "49.99",
"details": {
  "shipping": "0",
  "tax": "7.98"
}

貝寶當然會說這種格式是不正確的。 因為不應該有事務:[]在事務Arraylist中。 在上面的鏈接中,創建的事務對象完全相同,並且它可以正常工作,但對我而言卻沒有。 有誰知道為什么。 在Transaction.java中查看構造函數時,它會指出:

public Transaction() {
    transactions = new ArrayList<Transaction>();
}

使我感到困惑的是,從查看代碼來看,我在日志中得到的結果是正確的。 但是然后代碼不起作用,因為不可能用以下方法構造有效的事務:

val amount = new Amount
amount.setCurrency("EUR")
amount.setTotal(total.toString)
val transaction = new Transaction
transaction.setAmount(amount)
transaction.setDescription("Thank you for your order")
val transactions = new java.util.ArrayList[Transaction]
transactions.add(transaction)
val order = new Payment()
order.setTransactions(transactions)

記錄到:

Order : {"intent": "sale",
         "payer": {"payment_method": "paypal"},
         "transactions": [{"transactions": [],
                           "amount": {"currency": "USD",
                           "total": "12"},
                           "description": "Thank you for your order"
                           }],
         "note_to_payer": "26c4efcad7c44e0ebf2d0fa5344f22a0",
         "redirect_urls": {
              "return_url": "https://mydomain/#/paysuccess?guid\u003d26c4efcad7c44e0ebf2d0fa5344f22a0",
              "cancel_url": "https://mydomain/#/payfail?guid\u003d26c4efcad7c44e0ebf2d0fa5344f22a0"
               }
}

貝寶正確拒絕了此付款對象,聲明:

錯誤-PayPalRESTException響應代碼:400錯誤響應:{“ name”:“ VALIDATION_ERROR”,“ details”:[{“ field”:“ transactions [0] .item_list.items [0] .tax”,“ issue”:“貨幣金額必須為非負數,可以選擇包含正好2個小數位,中間用'。'分隔,可選的千位分隔符',',限制為小數點前7位和有效的ISO貨幣代碼貨幣“}, {“ field”:“交易”,“ issue”:“商品貨幣代碼應與所有存儲桶中的交易貨幣代碼相同”},{“ field”:“ transactions [0] .item_list.items [0]。 currency“,” issue“:”必填字段缺失“},{” field“:” transactions [0] .item_l ist.items [0] .price“,” issue“:”貨幣金額必須為非負數,可以選擇包含精確的2個小數位,以'。'分隔,可選的千位分隔符',',限制為小數點前7位和有效的ISO貨幣代碼的貨幣“}],” message“:”無效的請求-查看詳情“,” informa tion_link“:” https://developer.payp al.com/webapps/developer/docs/api/#VALIDATION_ERROR “,” debug_id“:” 912e594c547b7“}

女巫,我解釋為貝寶告訴我,我的第一筆交易無效。

有人知道發生了什么嗎?

參見上文,lib代碼沒有問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM