问题
Am implementing the paypal-rest-api call using scala. Am having the working example in java.
But getting "Unknown error" from paypal sandbox api, when implementing the same in scala.
Here is the code.
class DirectPayment {
def pay(): Unit = {
val payment = new Payment
val accessToken = AccessTokenGenerator.getAccessToken()
val apiContext = new APIContext(accessToken)
try {
val amount = new Amount
amount.setCurrency("USD")
amount.setTotal("12")
val transaction = new Transaction
transaction.setDescription("creating a direct payment with credit card");
transaction.setAmount(amount);
val transactions = new java.util.ArrayList[Transaction]
transactions.add(transaction)
val creditCard = new CreditCard
creditCard.setType("visa")
creditCard.setNumber("xxxxxxxxxxxxxxxxx")
creditCard.setExpireMonth(12)
creditCard.setExpireYear(2020)
creditCard.setCvv2("874")
creditCard.setFirstName("xxxx")
creditCard.setLastName("xxxxxxx")
val billingAddress = new Address
billingAddress.setLine1("111 First Street")
billingAddress.setCity("Saratoga")
billingAddress.setState("CA")
billingAddress.setPostalCode("95070")
billingAddress.setCountryCode("US")
val fundingInstrument = new FundingInstrument
fundingInstrument.setCreditCard(creditCard)
val fundingInstrumentList = new ArrayList[FundingInstrument]()
fundingInstrumentList.add(fundingInstrument)
val payer = new Payer
payer.setFundingInstruments(fundingInstrumentList)
payer.setPaymentMethod("credit_card")
payment.setIntent("sale")
payment.setPayer(payer)
payment.setTransactions(transactions)
val sdkConfig = new HashMap[String, String]()
sdkConfig.put("mode", "sandbox")
val requestId = UUID.randomUUID().toString();
val apiContext = new APIContext(accessToken, requestId);
apiContext.setConfigurationMap(sdkConfig)
println("apiContext" + apiContext)
val createdPayment = payment.create(apiContext)
//exception occurs here
println(Payment.getLastResponse())
println(String.format("created payment with id [%s] and status=[%s]", createdPayment.getId(), createdPayment.getState()))
} catch {
case e: PayPalRESTException => {
println("EXCEPTION IN DO FINAL PAYMENT METHOD")
val msg = e.getMessage
println(e)
println(msg)
}
}
}
having this in buil.sbt
libraryDependencies += "com.paypal.sdk" % "paypal-core" % "1.5.2"
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.4" % "test"
libraryDependencies += "com.paypal.sdk" % "rest-api-sdk" % "0.7.0"
回答1:
missed to add
creditCard.setBillingAddress(billingAddress)
before adding it to the fundingInstruments. Got fixed now.
来源:https://stackoverflow.com/questions/34493831/com-paypal-core-rest-paypalrestexception-error-code-400