develop_guide
参考范例:
创建订单流程
- java
private HashNutClient hashnutClient;
private HashNutService hashNutService;
private final String chainCode="polygon-erc20";
private final String coinCode="usdt";
private final String merchantAddress="0xe1fd94f8874d698567e03f671a8c62e4e2e4be90";
private final String accessKeyId="01HRP45GKQX6X8HVVSNRAYNWDY";
private final String secretKey ="4ppKsAP3RPR8i5vKVqaXAMKW57GYvFiW";
private final String receiptAddress="0x202921baf49319c522f8885368ae96415b9ca414";
@Before
public void before(){
hashnutClient = new HashNutClientImpl(secretKey, true);
hashNutService = new HashNutServiceImpl(hashnutClient);
}
@Test
public void createOrder() throws HashNutException {
final String merchantOrderId = UUID.randomUUID().toString();
BigDecimal amount=new BigDecimal("1.13");
CreateOrderResponse response = hashNutService.createOrder(new CreateOrderRequest.Builder()
.withAccessKeyId(accessKeyId)
.withMerchantOrderId(merchantOrderId)
.withChainCode(chainCode)
.withCoinCode(coinCode)
.withAmount(amount)
.withReceiptAddress(receiptAddress)
.build());
HashNutOrder order=response.getData();
ObjectMapper objectMapper=new ObjectMapper();
ObjectNode objectNode=objectMapper.createObjectNode();
objectNode.put("payOrderId",order.getPayOrderId());
objectNode.put("merchantOrderId",order.getMerchantOrderId());
objectNode.put("accessSign",order.getAccessSign());
objectNode.put("receiptAddress",order.getReceiptAddress());
System.out.println(objectNode.toPrettyString());
}
查询订单
- java
@Test
public void queryOrder() throws HashNutException {
final String payOrderId="01HRKFC3H3BVZZAEEB1KM06SG0";
final String mchOrderNo="3df2cd1d-5c2d-45a2-9ea7-fa47d6fec362";
final String accessSign="F85EAB044144878CCA4C2C874A0E72042435D3D199AB44387D4F2559F40D7B88";
QueryOrderResponse response=hashNutService.queryOrder(new QueryOrderRequest.Builder()
.withPayOrderId(payOrderId)
.withMerchantOrderId(mchOrderNo)
.withAccessSign(accessSign)
.build());
HashNutOrder order=response.getData();
System.out.println("get order payOrderId: " + order.getPayOrderId());
System.out.println("get order merchantOrderId: " + order.getMerchantOrderId());
System.out.println("get order accessSign: " + order.getAccessSign());
System.out.println("get order amount: " + order.getAmount());
System.out.println("get order state: " + OrderState.toString(order.getState()));
}
处理回调通知流程
- java
// NOTE: read body as a String not json
private String readBodyFromRequest(HttpServletRequest request){
try{
InputStream inputStream = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder requestBody = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
requestBody.append(line);
}
return requestBody.toString();
}catch (Exception e){
return null;
}
}
@PostMapping(value="/hashNutWebhook")
public void hashNutWebhook(HttpServletRequest request,
HttpServletResponse response) throws Exception {
log.info("hashnut notify process start");
// verify sign, NOTE: must read body as a String not json
String body=readBodyFromRequest(request);
if(!verifyWebhook(request,body)){
log.info("hashnut notify process failed");
outResult(response,"failed");
return;
}
// query pay order detail info and process pay order
JsonNode jsonNode=objectMapper.readTree(body);
String payOrderId=jsonNode.get("payOrderId").asText();
String merchantOrderId=jsonNode.get("merchantOrderId").asText();
String accessSign=jsonNode.get("accessSign").asText();
QueryOrderResponse hashnutResponse=hashNutService.queryOrder(new QueryOrderRequest.Builder()
.withPayOrderId(payOrderId)
.withMerchantOrderId(merchantOrderId)
.withAccessSign(accessSign)
.build());
HashNutOrder hashNutOrder=hashnutResponse.getData();
if(hashNutOrder.getState() >= OrderState.SUCCESS){
log.info("hashnut pay order success");
goodsOrderService.updateOrderState(merchantOrderId, Constant.GOODS_ORDER_STATUS_SUCCESS);
}
if(hashNutOrder.getState() < OrderState.INIT){
log.info("hashnut pay order failed state {}",hashNutOrder.getState());
goodsOrderService.updateOrderState(merchantOrderId, Constant.GOODS_ORDER_STATUS_FAIL);
}
log.info("hashnut notify process success");
outResult(response,"success");
}