I am working on integrating DO_DIRECT_PAYMENT
on a java application. I have the following maven dependencies in the project pom.xml file.
<!-- PayPal SDK Dependencies -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.paypal.sdk</groupId>
<artifactId>paypal-base</artifactId>
<version>1.1.0</version>
</dependency>
I am trying to create a simple integration test that works successfully.
Below is the test client class:
public class PaypalServiceTest {
@Test
public void shouldDoASuccessfullDirectPayment() throws PayPalException {
//execute
PaypalService paypalService = new PaypalService();
//verify
paypalService.doDirectPayment();
}
}
Below is the SUT(class under test):
public class PaypalService {
public void doDirectPayment() throws PayPalException {
NVPEncoder encoder = new NVPEncoder();
encoder.add("VERSION","72.0");
encoder.add("METHOD", "DoDirectPayment");
encoder.add("PAYMENTACTION", "SALE");
encoder.add("AMT", "20.0"); // value: 20.00
encoder.add("CREDITCARDTYPE",creditCardType); // value: type of credit card(Amex| MasterCard etc...)
encoder.add("ACCT", creditCardNumber); // value: test credit card no
encoder.add("CVV2", cvv2Number); // value: test cvv2, 4 digit for amex, three digit for master card
encoder.add("EXPDATE", "11/2023");
encoder.add("FIRSTNAME", "Test1");
encoder.add("LASTNAME", "Test2");
encoder.add("ADDRESS1", "Test Address);
encoder.add("CITY","Test City);
encoder.add("STATE","USA");
encoder.add("ZIP",""02110");
encoder.add( "COUNTRYCODE", "USA");
encoder.add( "CURRENCYCODE", "USD");
encoder.add( "IPADDRESS" , ipAddress); //my IP address
encoder.add( "EMAIL", email);// value: empty
String request = encoder.encode();
APIProfile profile = ProfileFactory.createSignatureAPIProfile();
profile.setAPIUsername(API_USERNAME); // value: useraneme
profile.setAPIPassword(API_PASSWORD); // value: password
profile.setSignature(API_SIGNATURE); // value: signature
profile.setEnvironment(API_ENVIRONMENT); // value: Sandbox
NVPCallerServices caller = new NVPCallerServices();
caller.setAPIProfile(profile);
String response = caller.call(request);
}
}
When I run the test, I am having the error below:
com.paypal.sdk.exceptions.FatalException: Unable to complete HTTPS transaction
at com.paypal.sdk.core.nvp.NVPAPICaller.call(NVPAPICaller.java:372)
at com.paypal.sdk.services.NVPCallerServices.call(NVPCallerServices.java:56)
at io.dexi.paypal.PaypalService.doDirectPayment(PaypalService.java:73)
at io.dexi.paypal.PaypalServiceTest.shouldDoASuccessfullDirectPayment(PaypalServiceTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: com.paypal.sdk.exceptions.FatalException: HTTP Error code 400 received, transaction not submitted
at com.paypal.sdk.core.nvp.NVPAPICaller.call(NVPAPICaller.java:355)
... 25 more
I will appreciate any help or guide in solving this issue.
Regards, Rando.
CodePudding user response:
I lost a couple of hours with this issue. As I thought, the problem was a mistake in the request parameters that I sent to Paypal.
I fixed this by modifying the format of EXPDATE like below:
// encoder.add("EXPDATE", "11/2023");
encoder.add("EXPDATE", "112023");