Skip to content

Commit

Permalink
Adapt the library to API 002.70 of connect2pay
Browse files Browse the repository at this point in the history
* Create new containers and move fields in there
* Adapt and create new tests
* Update dependencies versions
* Bump version to 2.0.1 as backward compatibility is not preserved
* Add orderId to rebill call that was added in API 002.62
* Add payment mean info stubs for WeChat and AliPay
  • Loading branch information
holajsh committed Feb 26, 2021
1 parent 0ba8a8e commit 784872c
Show file tree
Hide file tree
Showing 30 changed files with 1,803 additions and 1,217 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.project
.settings
.idea
/connect2pay-client.iml
183 changes: 111 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,123 @@ Connect2Pay Payment Page Java Client
This is the Java implementation of the Connect2pay Payment Page API.

The main class to use is ```Connect2payClient``` that implements all the API calls.
It requires request object as entries and returns response objects.
It requires request object as entries and returns response objects.

Example of a payment creation with user redirection:

```java
// Process a payment of €39.99
PaymentRequest request = new PaymentRequest();
request.setOrderId("1234ABCD").setCurrency("EUR").setAmount(3999);
request.setShippingType(ShippingType.VIRTUAL);
request.setPaymentMode(PaymentMode.SINGLE);
// User will be redirected here when clicking on "Go back to merchant" button
request.setCtrlRedirectURL("http://my.website/somewhere");
// The payment page will do a callback on this URL after the payment is processed
request.setCtrlCallbackURL("http://my.website/payment-callback");

// Validate the request
try {
request.validate();
} catch (BadRequestException e) {
logger.error("Ooops, an error occurred validating the payment request: " + e.getMessage());
// Handle the error...
}

// Instantiate the client and send the prepare request
// Second argument is the originator ID, third one is the associated API key
Connect2payClient c2p = new Connect2payClient("https://provided.url", "123456", "GreatP4ssw0rd");
PaymentResponse response = null;
try {
response = c2p.preparePayment(request);
} catch (Exception e) {
logger.error("Ooops, an error occurred preparing the payment: " + e.getMessage());
// Handle the error...
}

if (response != null && ResultCode.SUCCESS.equals(response.getCode()) {
// Get the URL to redirect the user to
String redirectURL = response.getCustomerRedirectURL();
if (redirectURL != null) {
// Redirect the user towards this URL, this will display the payment page
}
} else {
// Handle the failure
}
import com.payxpert.connect2pay.client.containers.Account;
import com.payxpert.connect2pay.client.containers.Order;
import com.payxpert.connect2pay.client.containers.Shipping;
import com.payxpert.connect2pay.client.containers.Shopper;
import com.payxpert.connect2pay.client.requests.PaymentRequest;
import com.payxpert.connect2pay.constants.PaymentMode;
import com.payxpert.connect2pay.constants.sca.ShippingType;
import com.payxpert.connect2pay.constants.sca.ShopperAccountAge;
import com.payxpert.connect2pay.constants.sca.ShopperAccountLastChange;

class TestPayment {
public void processPayment() {
// Process a payment of €39.99
PaymentRequest request = new PaymentRequest();
request.setCurrency("EUR").setAmount(3999);
request.setPaymentMode(PaymentMode.SINGLE);
// User will be redirected here when clicking on "Go back to merchant" button
request.setCtrlRedirectURL("http://my.website/somewhere");
// The payment page will do a callback on this URL after the payment is processed
request.setCtrlCallbackURL("http://my.website/payment-callback");

Order order = new Order();
order.setId("1234ABCD").setShippingType(ShippingType.DIGITAL_GOODS);
Shopper shopper = new Shopper();
shopper.setEmail("[email protected]").setFirstName("John").setLastName("Doe")
.setHomePhonePrefix("47").setHomePhone("123456789").setAddress1("Main Street 41")
.setZipcode("123456").setCity("London").setCountryCode("GB");
Account account = new Account();
account.setAge(ShopperAccountAge.BETWEEN_30_60_DAYS).setSuspicious(false)
.setLastChange(ShopperAccountLastChange.BETWEEN_30_60_DAYS);
shopper.setAccount(account);
Shipping shipping = new Shipping();
shipping.setName("Jane Doe").setAddress1("Other Street, 54").setCity("London")
.setZipcode("654321").setCountryCode("GB");

request.setOrder(order);
request.setShopper(shopper);
request.setShipping(shipping);

// Validate the request
try {
request.validate();
} catch (BadRequestException e) {
logger.error("Ooops, an error occurred validating the payment request: " + e.getMessage());
// Handle the error...
}

// Instantiate the client and send the prepare request
// Second argument is the originator ID, third one is the associated API key
Connect2payClient c2p = new Connect2payClient("https://provided.url", "123456", "GreatP4ssw0rd");
PaymentResponse response = null;
try {
response = c2p.preparePayment(request);
} catch (Exception e) {
logger.error("Ooops, an error occurred preparing the payment: " + e.getMessage());
// Handle the error...
}

if (response != null && ResultCode.SUCCESS.equals(response.getCode())) {
// Get the URL to redirect the user to
String redirectURL = response.getCustomerRedirectURL();
if (redirectURL != null) {
// Redirect the user towards this URL, this will display the payment page
}
} else {
// Handle the failure
}
}
}
```

Example of the payment callback handling:

```java
// Instantiate the client and handle the callback
Connect2payClient c2p = new Connect2payClient("https://provided.url", "123456", "GreatP4ssw0rd");
PaymentStatusResponse response = null;
try {
// Request is either the body of the received request as a string or an InputStream pointing to the received body
response = c2p.handleCallbackStatus(request);
} catch (Exception e) {
logger.error("Ooops, an error occurred handling the callback: " + e.getMessage());
// Handle the error...
}

if (response != null && ResultCode.SUCCESS.equals(response.getCode()) {
// Check the payment status: 000 means success
if ("000".equals(response.getErrorCode()) {
// Handle the payment success case
// ...
// Access the payment mean information
CreditCardPaymentMeanInfo pmInfo = response.getCCPaymentMeanInfo();
logger.info("Successful payment done by " + pmInfo.getCardHolderName() + "with card " + pmInfo.getCardNumber());
} else {
// Handle the payment failure case
}

if (handledSuccessfully) {
out.write(CallbackStatusResponse.getDefaultSuccessResponse().toJson()) // out is the output stream
} else {
out.write(CallbackStatusResponse.getDefaultFailureResponse().toJson()) // out is the output stream
}
} else {
// Handle the failure
out.write(CallbackStatusResponse.getDefaultFailureResponse().toJson()) // out is the output stream
}
import java.io.OutputStream;

class TestPayment {
private OutputStream out;

public void handleCallback(String request) {
// Instantiate the client and handle the callback
Connect2payClient c2p = new Connect2payClient("https://provided.url", "123456", "GreatP4ssw0rd");
PaymentStatusResponse response = null;
try {
// Request is either the body of the received request as a string or an InputStream pointing to the received body
response = c2p.handleCallbackStatus(request);
} catch (Exception e) {
logger.error("Ooops, an error occurred handling the callback: " + e.getMessage());
// Handle the error...
}

if (response != null && ResultCode.SUCCESS.equals(response.getCode())) {
// Check the payment status: 000 means success
if ("000".equals(response.getErrorCode())) {
// Handle the payment success case
// ...
// Access the payment mean information
CreditCardPaymentMeanInfo pmInfo = response.getCCPaymentMeanInfo();
logger.info("Successful payment done by " + pmInfo.getCardHolderName() + "with card " + pmInfo.getCardNumber());
} else {
// Handle the payment failure case
}

if (handledSuccessfully) {
this.out.write(CallbackStatusResponse.getDefaultSuccessResponse().toJson()); // out is the output stream
} else {
this.out.write(CallbackStatusResponse.getDefaultFailureResponse().toJson()); // out is the output stream
}
} else {
// Handle the failure
this.out.write(CallbackStatusResponse.getDefaultFailureResponse().toJson()); // out is the output stream
}
}
}
```
18 changes: 13 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<name>Connect2pay Java Client</name>
<groupId>com.payxpert</groupId>
<artifactId>connect2pay-client</artifactId>
<version>1.0.20</version>
<version>2.0.1</version>
<packaging>jar</packaging>
<description>This is the Java implementation of the PayXpert Connect2pay Payment Page API.</description>
<url>https://www.payxpert.com/</url>
Expand Down Expand Up @@ -108,6 +108,8 @@
<exclude>com/payxpert/connect2pay/utils/Utils.java</exclude>
</sourceFileExcludes>
<excludePackageNames>com.payxpert.connect2pay.utils.json</excludePackageNames>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
<source>8</source>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -178,7 +180,13 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -197,7 +205,7 @@
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.12</version>
<version>1.15</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
Expand All @@ -212,12 +220,12 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.3</version>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>net.sf.oval</groupId>
Expand Down
Loading

0 comments on commit 784872c

Please sign in to comment.