99 lines
4.2 KiB
Java
99 lines
4.2 KiB
Java
import java.io.InputStream;
|
||
import java.io.OutputStream;
|
||
import java.net.HttpURLConnection;
|
||
import java.net.URL;
|
||
import java.nio.charset.StandardCharsets;
|
||
import java.util.LinkedHashMap;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* 代付 Demo。编译:javac -encoding UTF-8 *.java
|
||
* 运行:java Demo balance|submit|query <OrderNo>
|
||
*/
|
||
public class Demo {
|
||
static final String BASE_URL = env("BASE_URL", "https://example.com").replaceAll("/$", "");
|
||
static final String ACCESS_KEY = env("ACCESS_KEY", "your_access_key");
|
||
static final String SECRET_KEY = env("SECRET_KEY", "your_secret_key");
|
||
static final String PAY_CHANNEL_ID = env("PAY_CHANNEL_ID", "821");
|
||
|
||
public static void main(String[] args) throws Exception {
|
||
if (args.length < 1) {
|
||
System.out.println("usage: java Demo balance|submit|query <OrderNo>");
|
||
return;
|
||
}
|
||
String cmd = args[0];
|
||
if ("balance".equals(cmd)) {
|
||
Map<String, Object> body = new LinkedHashMap<>();
|
||
body.put("Timestamp", System.currentTimeMillis() / 1000);
|
||
body.put("AccessKey", ACCESS_KEY);
|
||
System.out.println(post("/api/apiv1/open/withdrawal/querybalance", body));
|
||
} else if ("submit".equals(cmd)) {
|
||
String orderNo = "DEMO" + new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date()) + String.format("%04d", new java.util.Random().nextInt(10000));
|
||
Map<String, Object> body = new LinkedHashMap<>();
|
||
body.put("Timestamp", System.currentTimeMillis() / 1000);
|
||
body.put("AccessKey", ACCESS_KEY);
|
||
body.put("PayChannelId", PAY_CHANNEL_ID);
|
||
body.put("OrderNo", orderNo);
|
||
body.put("Amount", "100.01");
|
||
body.put("CallbackUrl", "https://xxxxx.com/xxxx/callback");
|
||
body.put("Ext", "-");
|
||
body.put("Payee", "张三");
|
||
body.put("PayeeNo", "6235753100001261596");
|
||
body.put("PayeeAddress", "招商银行");
|
||
System.out.println(post("/api/apiv1/open/withdrawal/submit", body));
|
||
} else if ("query".equals(cmd)) {
|
||
if (args.length < 2) {
|
||
System.out.println("usage: java Demo query <OrderNo>");
|
||
return;
|
||
}
|
||
Map<String, Object> body = new LinkedHashMap<>();
|
||
body.put("Timestamp", System.currentTimeMillis() / 1000);
|
||
body.put("AccessKey", ACCESS_KEY);
|
||
body.put("OrderNo", args[1]);
|
||
System.out.println(post("/api/apiv1/open/withdrawal/queryorder", body));
|
||
} else {
|
||
System.out.println("unknown command: " + cmd);
|
||
}
|
||
}
|
||
|
||
static String post(String path, Map<String, Object> body) throws Exception {
|
||
body.put("Sign", SignUtil.buildSign(body, SECRET_KEY));
|
||
String json = toJson(body);
|
||
HttpURLConnection conn = (HttpURLConnection) new URL(BASE_URL + path).openConnection();
|
||
conn.setRequestMethod("POST");
|
||
conn.setConnectTimeout(15000);
|
||
conn.setReadTimeout(30000);
|
||
conn.setDoOutput(true);
|
||
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
||
try (OutputStream os = conn.getOutputStream()) {
|
||
os.write(json.getBytes(StandardCharsets.UTF_8));
|
||
}
|
||
try (InputStream in = conn.getResponseCode() >= 400 ? conn.getErrorStream() : conn.getInputStream()) {
|
||
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
|
||
}
|
||
}
|
||
|
||
static String env(String k, String def) {
|
||
String v = System.getenv(k);
|
||
return v == null || v.isEmpty() ? def : v;
|
||
}
|
||
|
||
/** 极简 JSON(仅本 Demo 字段类型) */
|
||
static String toJson(Map<String, Object> map) {
|
||
StringBuilder sb = new StringBuilder("{");
|
||
boolean first = true;
|
||
for (Map.Entry<String, Object> e : map.entrySet()) {
|
||
if (!first) sb.append(',');
|
||
first = false;
|
||
sb.append('"').append(e.getKey()).append('"').append(':');
|
||
Object v = e.getValue();
|
||
if (v instanceof Number) {
|
||
sb.append(v);
|
||
} else {
|
||
sb.append('"').append(String.valueOf(v).replace("\\", "\\\\").replace("\"", "\\\"")).append('"');
|
||
}
|
||
}
|
||
return sb.append('}').toString();
|
||
}
|
||
}
|