代码示例
# 代码示例
- 代码示例仅供参考
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
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 payUrl = "https://ph-openapi.toppayment.com/ph/pay/prePay";
private static final String cashUrl = "https://ph-openapi.toppayment.com/ph/disbursement/cash";
private static final String payNotify = "your notify url";
private static final String cashNotify = "your notify url";
public static void main(String[] args) throws Exception {
// 代收
pay();
}
private static void pay() throws Exception {
Map<String, String> requestParams = new TreeMap<>();
requestParams.put("mchNo", MCH_NO);
requestParams.put("timestamp", Long.valueOf(System.currentTimeMillis()));
requestParams.put("method", StringUtils.isNotBlank(method) ? method : ""); // 支付渠道
requestParams.put("orderNum", "T" + System.currentTimeMillis()); // 商户订单号
requestParams.put("amount", 20000); // 订单金额
requestParams.put("customerName", "Lilia");// 客户名称
requestParams.put("customerPhone", "082122965511"); // 手机号
requestParams.put("customerEmail", "[email protected]");// 客户邮箱
requestParams.put("downNotifyUrl", payNotify);// 回调地址
requestParams.put("redirectUrl", "http://123.com");// 返回跳转地址
LocalDateTime now = LocalDateTime.now();
requestParams.put("expiryPeriod", Long.valueOf(30L)); // 过期时间 单位(分)
requestParams.put("productDetail", "Test Pay");// 订单详情
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(payUrl, postJson); // 发送 post json请求
System.out.println("Response Msg:" + responseJson);
}
}