Skip to main content

JAVA SDK access documentation

info

The Java SDK encapsulates the signature verification, transfer encoding and other tasks in the server-side API, and provides a faster server-side integration solution in the Java development environment. If your server-side development environment is not Java, please refer to API access Documentation Implement related logic by yourself

Import jar package

Add a dependency on paynicorn-java-sdk to your project's pom.xml file

<dependency>
<groupId>com.paynicorn</groupId>
<artifactId>paynicorn-java-sdk</artifactId>
<version>1.0.4</version>
</dependency>

Reference Code

import static spark.Spark.post;
import static spark.Spark.port;
import com.alibaba.fastjson.JSON;
import com.paynicorn.sdk.model.*;
import java.io.IOException;

public class Demo {
private static final String appKey = "PUT_YOUR_APPKEY_HERE";
private static final String merchantSecret = "PUT_YOUR_MERCHANT_SECRET_HERE";
public static void main(String[] args) throws IOException {


//raise a payment request to PAYNICORN
InitPaymentRequest initPaymentRequest = new InitPaymentRequest();
initPaymentRequest.setOrderId("PUT_YOUR_ORDER_ID_HERE");
initPaymentRequest.setCountryCode("NG");
initPaymentRequest.setCurrency("NGN");
initPaymentRequest.setAmount("100");
initPaymentRequest.setCpFrontPage("PUT_YOUR_WEB_REDIRECT_URL_HERE");
initPaymentRequest.setOrderDescription("TEST GOODS NAME");
InitPaymentResponse initPaymentResponse = Paynicorn.initPayment(appKey,merchantSecret, initPaymentRequest);
System.out.println(JSON.toJSONString(initPaymentResponse));


//query a payment status from PAYNICORN
QueryPaymentRequest queryPaymentRequest = new QueryPaymentRequest();
queryPaymentRequest.setOrderId("PUT_YOUR_ORDER_ID_HERE");
queryPaymentRequest.setTxnType(Paynicorn.TxnType.PAYMENT);
QueryPaymentResponse queryPaymentResponse = Paynicorn.queryPayment(appKey,merchantSecret,queryPaymentRequest);
System.out.println(JSON.toJSONString(queryPaymentResponse));


//receive a payment status postback from PAYNICORN
port(8080);
post("/postback", (request, response) -> {
String body = request.body();
PostbackInfo info = Paynicorn.receivePostback(merchantSecret,body);

if (info.isVerified()){
System.out.println(JSON.toJSONString(info));

//you need to response "success_"+TxnId in PostbackInfo to PAYNICORN if you receive the postback successfully
//otherwise, PAYNICORN will continue to send the postback to your server unless you give that response
response.status(200);
return "success_"+info.getTxnId();
}else{
response.status(500);
return "";
}
});
}
}

Setting parameters

Replace PUT_YOUR_APPKEY_HERE with the appKey of the corresponding payment application. The address for obtaining the appKey is: https://console.paynicorn.com/#/app/list

Replace PUT_YOUR_MERCHANT_SECRET_HERE with the merchant's Merchant secret. The address for obtaining the Merchant secret is: https://console.paynicorn.com/#/developer


private static final String appKey = "PUT_YOUR_APPKEY_HERE";
private static final String merchantSecret = "PUT_YOUR_MERCHANT_SECRET_HERE";

initiate payment

Use the InitPaymentRequest object and the Paynicorn.initPayment method to initiate a new payment request to Paynicorn,

Replace PUT_YOUR_ORDER_ID_HERE with your merchant order number,

Replace PUT_YOUR_WEB_REDIRECT_URL_HERE with the jump address of your merchant website or the deeplink of the Android application. After the user pays, Paynicorn will call this address to jump back to the specified page of your merchant website or Android application.

InitPaymentRequest initPaymentRequest = new InitPaymentRequest();
initPaymentRequest.setOrderId("PUT_YOUR_ORDER_ID_HERE"); //Set the merchant order number
initPaymentRequest.setCountryCode("NG"); //Set the country
initPaymentRequest.setCurrency("NGN"); //Set the currency
initPaymentRequest.setAmount("100"); //Set the amount
initPaymentRequest.setCpFrontPage("PUT_YOUR_WEB_REDIRECT_URL_HERE"); //Set the jump address of Paynicorn cashier after payment is completed, generally set the merchant's query page URL
initPaymentRequest.setOrderDescription("TEST GOODS NAME"); //Set the display name of the product on the Paynicorn cashier
InitPaymentResponse initPaymentResponse = Paynicorn.initPayment(appKey,merchantSecret, initPaymentRequest); //Initiate payment

Determine the transaction initiation status according to the return value of the InitPaymentResponse object

public class InitPaymentResponse {
private String code;
private String message;
private String txnId; //Paynicorn order number
private String status; //Payment status
private String webUrl; //Paynicorn cashier URL
}

If InitPaymentResponse is empty, it can be directly judged that the transaction initiation failed.

If InitPaymentResponse is not empty, get the status member status in InitPaymentResponse to judge the transaction status, status=0 payment failed, status=-1 payment processing, status=1 payment successful,

When the status is -1, it indicates that the transaction is being processed, and the user needs to take further action. At this time, it is judged whether the webUrl is empty. If it is empty, no processing is required. If the webUrl has a value, the webUrl needs to be passed to the client for page loading for the user. Complete subsequent payment actions

Query payment status

Use the QueryPaymentRequest object and the Paynicorn.queryPayment method to initiate a query order status request to Paynicorn,

Replace PUT_YOUR_ORDER_ID_HERE with your merchant order number

QueryPaymentRequest queryPaymentRequest = new QueryPaymentRequest();
queryPaymentRequest.setOrderId("PUT_YOUR_ORDER_ID_HERE");
queryPaymentRequest.setTxnType(Paynicorn.TxnType.PAYMENT);
QueryPaymentResponse queryPaymentResponse = Paynicorn.queryPayment(appKey,merchantSecret,queryPaymentRequest);

Judging the transaction query status according to the return value of the QueryPaymentResponse object

public class QueryPaymentResponse {
private String code;
private String message;
private String txnId; //Paynicorn order number
private String status; //Payment status
private String completeTime; //Payment completion time
}

If QueryPaymentResponse is empty, it can be judged that the order query failed,

If QueryPaymentResponse is not empty, get the status member status in QueryPaymentResponse to judge the transaction status, status=0 payment failed, status=-1 payment processing, status=1 payment successful

Receive callback notification

It is used to receive Paynicorn's asynchronous callback notification. When the payment order status changes, Paynicorn will call this address for notification. The callback notification address is set in the Callback address of each payment application:

Postback Address Setting

Confirm whether the signature verification is passed through the isVerified method of the PostbackInfo object. After passing, the transaction status can be updated according to the status status in the PostbackInfo, status=0 payment failed, status=-1 payment processing, status=1 payment success,

After receiving the notification, you need to return a "success_"+info.getTxnId() response to Paynicorn to inform Paynicorn that the notification has been successfully received, otherwise Paynicorn will continue to notify the order status change until the maximum number of notifications is reached

port(8080);
post("/postback", (request, response) -> {
String body = request.body();
PostbackInfo info = Paynicorn.receivePostback(merchantSecret,body);

if (info.isVerified()){
System.out.println(JSON.toJSONString(info));

//you need to response "success_"+TxnId in PostbackInfo to PAYNICORN if you receive the postback successfully
//otherwise, PAYNICORN will continue to send the postback to your server unless you give that response
response.status(200);
return "success_"+info.getTxnId();
}else{
response.status(500);
return "";
}
});