To implement a card swipe payment (MicroPay), follow these steps:
- Build Request Parameters: Use the
MicroPayModel.builder() to set parameters such as service (use ServiceEnum.MICRO_PAY.toString()), mch_id, out_trade_no, total_fee, and auth_code. - Sign the Request: Call
.createSign(key, SignType.MD5) on the model. Note that UnionPay barcode front-end interfaces currently only support MD5 encryption. - Execute Request: Use
UnionPayApi.execution(serverUrl, params) to send the request. - Parse Response: Convert the returned XML string to a Map using
WxPayKit.xmlToMap(xmlResult). - Verify Signature: Use
WxPayKit.verifyNotify(result, key, SignType.MD5) to ensure the response is authentic. - Check Status: Verify the payment success by checking if
status and result_code are both equal to "0".
// 1. Build and sign request parameters
Map<String, String> params = MicroPayModel.builder()
.service(ServiceEnum.MICRO_PAY.toString())
.mch_id(unionPayBean.getMachId())
.out_trade_no(WxPayKit.generateStr())
.body("IJPay 云闪付测试")
.attach("聚合支付 SDK")
.total_fee(totalFee)
.mch_create_ip(ip)
.auth_code(authCode)
.nonce_str(WxPayKit.generateStr())
.build()
.createSign(unionPayBean.getKey(), SignType.MD5);
// 2. Execute the request
String xmlResult = UnionPayApi.execution(unionPayBean.getServerUrl(), params);
// 3. Convert XML to Map
Map<String, String> result = WxPayKit.xmlToMap(xmlResult);
// 4. Verify signature
if (!WxPayKit.verifyNotify(result, unionPayBean.getKey(), SignType.MD5)) {
throw new Exception("Signature verification failed");
}
// 5. Check payment status
String returnCode = result.get("status");
String resultCode = result.get("result_code");
if ("0".equals(returnCode) && "0".equals(resultCode)) {
// Success
}