scala java paypal-sdk 无法构造有效交易
scala java paypal-sdk cant construct valid Transaction
我正在为一个奇怪的问题而苦苦挣扎,或者也许只是我自己的愚蠢。最让我困惑的是,从逻辑上讲,我在 post: 中做了同样的事情。我的代码如下所示:
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"
}
Paypal当然说这种格式不对。因为事务 Arraylist 中不应该有事务 : []。在上面的 link 中,交易对象的创建完全相同并且它可以正常工作,对我来说它没有。有谁知道为什么。查看 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"
}
}
此付款对象被 paypal 正确拒绝,指出:
ERROR - PayPalRESTException Response code: 400 Error response: {"name":"VALIDATION_ERROR","details":[{"field":"transactions[0].item_list.items[0].tax","issue": "Currency amount must be non-negative number, may optionally contain exactly 2 decimal places separated by '.', optional thousands separator ',', limited to 7 d igits before the decimal point and currency which is a valid ISO Currency Code"},{"field":"transactions","issue":"Item currency codes should be same as the tran saction currency code in all buckets"},{"field":"transactions[0].item_list.items[0].currency","issue":"Required field missing"},{"field":"transactions[0].item_l ist.items[0].price","issue":"Currency amount must be non-negative number, may optionally contain exactly 2 decimal places separated by '.', optional thousands s eparator ',', limited to 7 digits before the decimal point and currency which is a valid ISO Currency Code"}],"message":"Invalid request - see details","informa tion_link":"https://developer.paypal.com/webapps/developer/docs/api/#VALIDATION_ERROR","debug_id":"912e594c547b7"}
我理解为 paypal 告诉我我的第一笔交易无效。
有人知道发生了什么事吗?
见上文lib代码有问题
我正在为一个奇怪的问题而苦苦挣扎,或者也许只是我自己的愚蠢。最让我困惑的是,从逻辑上讲,我在 post:
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"
}
Paypal当然说这种格式不对。因为事务 Arraylist 中不应该有事务 : []。在上面的 link 中,交易对象的创建完全相同并且它可以正常工作,对我来说它没有。有谁知道为什么。查看 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"
}
}
此付款对象被 paypal 正确拒绝,指出:
ERROR - PayPalRESTException Response code: 400 Error response: {"name":"VALIDATION_ERROR","details":[{"field":"transactions[0].item_list.items[0].tax","issue": "Currency amount must be non-negative number, may optionally contain exactly 2 decimal places separated by '.', optional thousands separator ',', limited to 7 d igits before the decimal point and currency which is a valid ISO Currency Code"},{"field":"transactions","issue":"Item currency codes should be same as the tran saction currency code in all buckets"},{"field":"transactions[0].item_list.items[0].currency","issue":"Required field missing"},{"field":"transactions[0].item_l ist.items[0].price","issue":"Currency amount must be non-negative number, may optionally contain exactly 2 decimal places separated by '.', optional thousands s eparator ',', limited to 7 digits before the decimal point and currency which is a valid ISO Currency Code"}],"message":"Invalid request - see details","informa tion_link":"https://developer.paypal.com/webapps/developer/docs/api/#VALIDATION_ERROR","debug_id":"912e594c547b7"}
我理解为 paypal 告诉我我的第一笔交易无效。
有人知道发生了什么事吗?
见上文lib代码有问题