跳到主要内容

JAVA SDK 接入文档

信息

Java SDK 将服务端API中的签名验证,传输编码等工作进行了封装,提供了Java开发环境下的更快捷的服务端集成方案,如果您的服务端开发环境不是Java,请参考 API接入文档 自行实现相关逻辑

引入jar包

在项目的 pom.xml 文件中添加对 paynicorn-java-sdk 的依赖

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

参考代码

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

设置参数

将 PUT_YOUR_APPKEY_HERE 替换成对应支付应用的 appKey,appKey 的获取地址为:https://console.paynicorn.com/#/app/list

PUT_YOUR_MERCHANT_SECRET_HERE 替换成商户的 Merchant secret,Merchant secret 的获取地址为:https://console.paynicorn.com/#/developer


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

发起支付

使用 InitPaymentRequest 对象和 Paynicorn.initPayment 方法向 Paynicorn 发起一笔新的支付请求,

将 PUT_YOUR_ORDER_ID_HERE 替换成你的商户订单号,

PUT_YOUR_WEB_REDIRECT_URL_HERE 替换成你的商户网站跳转地址或者Android应用的deeplink,用户支付完成后,Paynicorn会调用该地址跳转回到你的商户网站或者Android应用的指定页面

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"); //设置完成支付后,Paynicorn 收银台的跳转地址,一般设置商户的查询页面URL
initPaymentRequest.setOrderDescription("TEST GOODS NAME"); //设置商品在Paynicorn收银台上的显示名称
InitPaymentResponse initPaymentResponse = Paynicorn.initPayment(appKey,merchantSecret, initPaymentRequest); //发起支付

根据 InitPaymentResponse 对象返回值判断交易发起状态

public class InitPaymentResponse {
private String code;
private String message;
private String txnId; //Paynicorn 订单号
private String status; //支付状态
private String webUrl; //Paynicorn 收银台 URL
}

如果 InitPaymentResponse 为空,可以直接判断为交易发起失败,

如果 InitPaymentResponse 不为空,获取 InitPaymentResponse 中的 status 成员状态,判断交易状态,status=0支付失败,status=-1支付处理中,status=1支付成功,

status为-1时,标识交易正在处理中,需要用户做进一步动作,此时判断webUrl是否为空,为空无需处理,如果 webUrl 有值,需要将该 webUrl 传给客户端进行页面加载,供用户完成后续支付动作

查询支付状态

使用 QueryPaymentRequest 对象和 Paynicorn.queryPayment 方法向 Paynicorn 发起查询订单状态请求,

将 PUT_YOUR_ORDER_ID_HERE 替换成你的商户订单号

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

根据 QueryPaymentResponse 对象返回值判断交易查询状态

public class QueryPaymentResponse {
private String code;
private String message;
private String txnId; //Paynicorn 订单号
private String status; //支付状态
private String completeTime; //支付完成时间
}

如果 QueryPaymentResponse 为空,可以判断订单查询失败,

如果 QueryPaymentResponse 不为空,获取 QueryPaymentResponse 中的 status 成员状态,判断交易状态,status=0支付失败,status=-1支付处理中,status=1支付成功

接收回调通知

用于接收 Paynicorn 的异步回调通知,当支付订单状态有变化时,Paynicorn 会调用该地址进行通知,回调通知地址在每个支付应用的 Callback address 中进行设置:

Postback Address Setting

通过 PostbackInfo 对象的 isVerified 方法确认签名验证是否通过,通过后可根据 PostbackInfostatus 状态更新交易状态,status=0支付失败,status=-1支付处理中,status=1支付成功,

收到通知后,需返回 "success_"+info.getTxnId() 响应给到 Paynicorn 告知通知已成功接收,否则 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 "";
}
});