com.paypal.core.rest.PayPalRESTException: Error code : 400

六眼飞鱼酱① 提交于 2020-01-07 03:03:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!