codeExample
# codeExample
- theCodeSamplesAreForReferenceOnly
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"; // merchantNo
private static final String PLAT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2JoMfFqLsSJjAiCahEnlP3aRj8yCT+WHzR+VvPBTw9S1i7iYWb+MY09CG/HYuHF4+IxshXDJygmndxKf/esuwPybS8mAd//yubHpmZsmBqg1FffT8VH1APa6ZRWASUp4U01ZrbCCp35QA8FuWrJGMJxGx4xk7KUtV2yujxC8noQIDAQAB"; // platformPublicKey
private static final String MCH_PRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAJU8gKFKD0luIYx7X8+**************************/9FTGN0vA+9ZPC2cwHtNxI2kXf3Vp"; // merchantPrivateKey
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()); // merchantOrderNumber
requestParams.put("amount", 20000); // order amount
requestParams.put("customerName", "Lilia");// customerName
requestParams.put("customerPhone", "082122965511"); // customerPhone
requestParams.put("customerEmail", "[email protected]");// customerEmail
requestParams.put("downNotifyUrl", payNotify);// callbackAddress
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 http json request
System.out.println("Response Msg:" + responseJson);
}
}