import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.Map; public class order {
private static String create_order(String ip, String merchantId, String key, String payWay, String amount)
throws NoSuchAlgorithmException, InvalidKeyException, IOException {
// url参数
Map<String, Object> url_map = new TreeMap<String, Object>();
url_map.put("merchantId", merchantId);
url_map.put("signatureMethod", "HmacSHA256");
url_map.put("signatureVersion", "1");
url_map.put("timestamp", System.currentTimeMillis()); // url参数拼接
StringBuilder url_data = new StringBuilder();
for (Map.Entry<String, Object> url :url_map.entrySet()) {
url_data.append(url.getKey()).append("=").append(url.getValue()).append("&");
}
url_data = new StringBuilder(url_data.substring(0, url_data.length() - 1));
// System.out.println(url_data); // body参数
Map<String, Object> body_map = new TreeMap<String, Object>();
body_map.put("jUserId", "666666");
body_map.put("jUserIp", "110.110.110.110");
body_map.put("jOrderId", Long.toString(System.currentTimeMillis()));
body_map.put("orderType", "1");
body_map.put("payWay", payWay);
body_map.put("amount", amount);
body_map.put("currency", "CNY");
body_map.put("jUserName", "奥里给");
body_map.put("notifyUrl", "https://test.com"); // 全部参数
Map<String, Object> all_map = new TreeMap<String, Object>();
all_map.putAll(url_map);
all_map.putAll(body_map); // 参数拼接
StringBuilder all_data = new StringBuilder();
for (Map.Entry<String, Object> url :all_map.entrySet()) {
all_data.append(url.getKey()).append("=").append(url.getValue()).append("&");
} all_data = new StringBuilder(all_data.substring(0, all_data.length() - 1));
// System.out.println("data:" + all_data); // HmacSHA256加密
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] array = sha256_HMAC.doFinal(all_data.toString().getBytes(StandardCharsets.UTF_8));
StringBuilder sec = new StringBuilder();
for (byte item : array) {
sec.append(Integer.toHexString((item & 0xFF) | 0x100), 1, 3);
}
String signature_data = sec.toString().toUpperCase();
System.out.println("signature:" + signature_data); String URL = ip + "?" + url_data + "&signature=" + signature_data;
System.out.println("url:" + URL);
HttpPost httppost = new HttpPost(URL); // 请求body参数拼接
StringBuilder list_data = new StringBuilder();
for (Map.Entry<String, Object> m :body_map.entrySet()) {
list_data.append(m.getKey()).append("=").append(m.getValue()).append("&");
}
list_data = new StringBuilder(list_data.substring(0, list_data.length() - 1));
System.out.println("data:" + list_data); // 创建订单-发送请求
StringEntity entity = new StringEntity(list_data.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/x-www-form-urlencoded");
httppost.setEntity(entity);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httppost);
String strResult = EntityUtils.toString(response.getEntity());
System.out.println("result:" + strResult + "\t");
httppost.releaseConnection(); return strResult;
} public static void main(String[] args) throws Exception {
create_order("https://test.com", "merchantId",
"key", "pay", "666");
}
}

随机推荐

  1. 常用SQL

    1. CEILING 向上取整2. FLOOR 向下取整3. FORMATmysql> SELECT FORMAT(12332.123456, 4); -> '12,332.1235'my ...

  2. 通过smtp协议简单实现邮件发送

    使用到的类: ①SmtpClient--发送邮件的类(using System.Net.Mail;) ②MailMessage--初始化邮件的类 ③ NetworkCredential--身份验证的类 ...

  3. 怎么学好python?

    文章摘自:http://www.jb51.net/article/16100.htm 1)学好python的第一步,就是马上到www.python.org网站上下载一个python版本.我建议初学者, ...

  4. vm虚拟机挂载usb

    首先得保证能在VM里看到usb设备,如图

  5. 用于展现图表的50种JavaScript库

    在很多项目中都会有在前端展现数据图表的需求,而在开发过程中,开发者往往会使用一些JavaScript库,从而更有效地达到想要的目标.最近,TechSlide上的一篇文章总结了50种用于展现图表的Jav ...

  6. ORM框架SQLAlchemy与权限管理系统的数据库设计

    SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用对象关系映射进行数据库操作,即:将对象转换成SQL,然后使用数据API执行SQL并获取执行结果. 执行流 ...

  7. Python_文本操作

    #向文本文件中写入内容 s='Hello world\n文本文件的读取方法\n文本文件的写入方法\n' f=open('sample1.txt','a+') #打开文件 f.write(s) #写入文 ...

  8. react native头部标题样式修改

    navigationOptions: ({navigation}) => ({ headerTitle:'评估记录', headerBackTitle:null, headerLeft:null ...

  9. [MySQL]InnoDB引擎的行锁和表锁

    1.行锁和表锁 在mysql 的 InnoDB引擎支持行锁,与Oracle不同,mysql的行锁是通过索引加载的,即是行锁是加在索引响应的行上的,要是对应的SQL语句没有走索引,则会全表扫描, 行锁则 ...

  10. vue中computed和watch的用法

    computed用来监控自己定义的变量,该变量不在data里面声明,直接在computed里面定义,然后就可以在页面上进行双向数据绑定展示出结果或者用作其他处理: computed比较适合对多个变量或 ...