java 对接银盛支付代码
时间: 2023-09-14 08:14:59 浏览: 174
以下是Java代码示例,用于对接银盛支付:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class YinshengPay {
// 银盛支付API地址
private static final String API_URL = "https://api.ysepay.com/gateway/api/directPay";
// 商户号和API密钥
private static final String MERCHANT_ID = "your_merchant_id";
private static final String API_KEY = "your_api_key";
public static void main(String[] args) throws Exception {
// 构造请求参数
Map<String, String> params = new HashMap<String, String>();
params.put("version", "1.0");
params.put("merchantId", MERCHANT_ID);
params.put("orderNo", "your_order_no");
params.put("orderAmount", "100.00");
params.put("orderCurrency", "CNY");
params.put("orderDatetime", "20210813120000");
params.put("productName", "your_product_name");
params.put("productDesc", "your_product_desc");
params.put("payType", "1");
params.put("bankCode", "ICBC");
params.put("cardType", "1");
params.put("cardNo", "your_card_no");
params.put("cardExpire", "202212");
params.put("cardCvv", "123");
params.put("notifyUrl", "your_notify_url");
params.put("signType", "MD5");
// 生成签名
String sign = generateSign(params, API_KEY);
params.put("signMsg", sign);
// 发送请求
String response = sendRequest(API_URL, params);
// 解析响应结果
System.out.println(response);
}
/**
* 生成签名
*/
private static String generateSign(Map<String, String> params, String apiKey) throws Exception {
StringBuilder sb = new StringBuilder();
sb.append(apiKey);
Iterator<String> it = params.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String value = params.get(key);
sb.append(key).append("=").append(value).append("&");
}
sb.deleteCharAt(sb.length() - 1);
String sign = MD5(sb.toString()).toUpperCase();
return sign;
}
/**
* 发送请求
*/
private static String sendRequest(String url, Map<String, String> params) throws Exception {
HttpURLConnection conn = null;
OutputStreamWriter osw = null;
BufferedReader br = null;
try {
URL u = new URL(url);
conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
osw.write(getRequestData(params));
osw.flush();
br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
} finally {
if (osw != null) {
osw.close();
}
if (br != null) {
br.close();
}
if (conn != null) {
conn.disconnect();
}
}
}
/**
* 构造请求数据
*/
private static String getRequestData(Map<String, String> params) throws Exception {
StringBuilder sb = new StringBuilder();
Iterator<String> it = params.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String value = params.get(key);
sb.append(key).append("=").append(URLEncoder.encode(value, "UTF-8")).append("&");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
/**
* 计算MD5摘要
*/
private static String MD5(String s) throws Exception {
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(s.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte b : array) {
sb.append(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
}
}
```
上面的代码中,需要替换掉示例中的商户号、API密钥、订单号、金额、商品名称、商品描述、银行卡信息等参数。发送请求和解析响应的代码也需要根据具体的业务进行修改。
阅读全文