com.paypal.core.rest.PayPalRESTException:错误代码:400

时间:2015-12-28 12:48:04

标签: scala paypal-sandbox

使用scala实现paypal-rest-api调用。 我有java中的工作示例。

但是在scala中实现相同的时候,从paypal sandbox api获取“未知错误”。

这是代码。

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)
 }   
 }

}

在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 个答案:

答案 0 :(得分:0)

错过了添加

    creditCard.setBillingAddress(billingAddress)

在将其添加到fundingInstruments之前。现在修好了。