代付代码示例
# 代码示例
- 代码示例仅供参考
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.Map;
import java.util.TreeMap;
public class TopPayDemo {
private static final String MCH_NO = "PHOT000001"; // 商户号
private static final String PLAT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2JoMfFqLsSJjAiCahEnlP3aRj8yCT+WHzR+VvPBTw9S1i7iYWb+MY09CG/HYuHF4+IxshXDJygmndxKf/esuwPybS8mAd//yubHpmZsmBqg1FffT8VH1APa6ZRWASUp4U01ZrbCCp35QA8FuWrJGMJxGx4xk7KUtV2yujxC8noQIDAQAB"; // 平台公钥
private static final String MCH_PRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAJU8gKFKD0luIYx7X8+**************************/9FTGN0vA+9ZPC2cwHtNxI2kXf3Vp"; // 商户私钥
private static final String cashUrl = "https://ph-openapi.toppayment.com/ph/disbursement/cash";
private static final String cashNotify = "http://host:port/notify";
public static void main(String[] args) throws Exception {
cash();
}
private static void cash() throws Exception {
Map<String, String> requestParams = new TreeMap<>();
requestParams.put("mchNo", MCH_NO);
requestParams.put("orderNum", "186888188666"); // 商户订单号
requestParams.put("amount", "125"); // 订单金额
requestParams.put("feeType", "0"); // 手续费类型(0-帐内扣除,1-帐外扣除)
requestParams.put("timestamp", Long.valueOf(System.currentTimeMillis()));
requestParams.put("bankCard", "2021071209403321313122"); // 客户银行卡号
requestParams.put("bankCode", "014"); // 印尼银行编码:参考附录I 代付
requestParams.put("accountName", "test cash name"); // 客户名称
requestParams.put("description", "test cash"); // 描述
requestParams.put("downNotifyUrl", cashNotify); // 回调地址
StringBuilder stringBuilder = new StringBuilder();
for (String key : requestParams.keySet()) {
stringBuilder.append(requestParams.get(key)); // 拼接参数
}
String keyStr = stringBuilder.toString(); // 得到待加密的字符串
System.out.println("keyStr:" + keyStr);
String signedStr = TopPayRequestUtil.privateEncrypt(keyStr, TopPayRequestUtil.getPrivateKey(MCH_PRIVATE_KEY)); // 私钥加密
requestParams.put("sign", signedStr);
String postJson = new Gson().toJson(requestParams);
System.out.println("Post Json Params:" + postJson);
String responseJson = TopPayRequestUtil.doPost(cashUrl, postJson); // 发送 post json请求
System.out.println("Response Msg:" + responseJson);
boolean pass = TopPayRequestUtil.verifySign(new Gson().fromJson(responseJson, JsonObject.class), PLAT_PUBLIC_KEY); // 签名验证
if (pass) {
// ... 签名验证通过,执行正常的业务逻辑
} else {
// ... 签名验证错误
}
}
}