使用swing写的一个小工具
jdk1.8,使用的jar包
主类ApplymentMicroSubmit.java
其他相关的java文件去下载吧,项目资源链接压缩包:
https://github.com/daiguobang/weixin/raw/master/WeixinPay.rar
package com.bang.test;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import com.alibaba.fastjson.JSONObject;
/**
* 小微商户进件接口调用Demo
*
* @author nihao
*
*/
public class ApplymentMicroSubmit extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static String mch_id;
private static String apiKey;
private static String apiv3Key;
private static String keyStoreUrl;
private static String fileUrl;
private static String requestUrl = "https://api.mch.weixin.qq.com/applyment/micro/submit";
private static String queryUrl = "https://api.mch.weixin.qq.com/applyment/micro/getstate";
private static String certSnUrl = "https://api.mch.weixin.qq.com/risk/getcertficates";
private static String photoUpdateUrl = "https://api.mch.weixin.qq.com/secapi/mch/uploadmedia";
// 资料路径
private JPanel contentPane;
private JTextField MchIdFiled = new JTextField();//商户号
private JTextField ApiKeyFiled = new JTextField();//
private JTextField ApiV3KeyFiled = new JTextField() ;//
private JTextField KeyStoreUrlFiled = new JTextField();//
private JTextField FileUrlFiled = new JTextField();//
//进件申请编号
private JTextField ApplymentIdFiled = new JTextField();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ApplymentMicroSubmit frame = new ApplymentMicroSubmit();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ApplymentMicroSubmit() {
setTitle("小微商户进件");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 350);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel labe_l = new JLabel("服务商商户号:");
labe_l.setForeground(Color.RED);
labe_l.setBounds(50,30,120, 15);
contentPane.add(labe_l);
JLabel labe_2 = new JLabel("证书路径:");
labe_2.setForeground(Color.RED);
labe_2.setBounds(50,60,120, 15);
contentPane.add(labe_2);
JLabel labe_3 = new JLabel("api秘钥:");
labe_3.setForeground(Color.RED);
labe_3.setBounds(50,90,120, 15);
contentPane.add(labe_3);
JLabel labe_4 = new JLabel("apiv3秘钥:");
labe_4.setForeground(Color.RED);
labe_4.setBounds(50,120,120, 15);
contentPane.add(labe_4);
JLabel labe_5 = new JLabel("资料路径:");
labe_5.setForeground(Color.RED);
labe_5.setBounds(50,150,120, 15);
contentPane.add(labe_5);
MchIdFiled = new JTextField();
MchIdFiled.setForeground(Color.DARK_GRAY);
MchIdFiled.setBounds(150, 30, 300, 21);
contentPane.add(MchIdFiled);
MchIdFiled.setText("");
KeyStoreUrlFiled = new JTextField();
KeyStoreUrlFiled.setForeground(Color.DARK_GRAY);
KeyStoreUrlFiled.setBounds(150, 60, 300, 21);
contentPane.add(KeyStoreUrlFiled);
KeyStoreUrlFiled.setText("E:\\xxxxx\\apiclient_cert.p12");;
ApiKeyFiled = new JTextField();
ApiKeyFiled.setForeground(Color.DARK_GRAY);
ApiKeyFiled.setBounds(150, 90, 300, 21);
contentPane.add(ApiKeyFiled);
ApiKeyFiled.setText("xxxxxx");
ApiV3KeyFiled = new JTextField();
ApiV3KeyFiled.setForeground(Color.DARK_GRAY);
ApiV3KeyFiled.setBounds(150, 120, 300, 21);
contentPane.add(ApiV3KeyFiled);
ApiV3KeyFiled.setText("xxxxxxx");
FileUrlFiled = new JTextField();
FileUrlFiled.setForeground(Color.DARK_GRAY);
FileUrlFiled.setBounds(150, 150, 300, 21);
contentPane.add(FileUrlFiled);
FileUrlFiled.setText("E:\\pic\\");
JButton register = new JButton("开始进件");
register.setForeground(Color.RED);
register.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
mch_id = MchIdFiled.getText().trim();
apiKey = ApiKeyFiled.getText().trim();
apiv3Key = ApiV3KeyFiled.getText().trim();
keyStoreUrl = KeyStoreUrlFiled.getText().trim();
fileUrl = FileUrlFiled.getText().trim();
if(StringUtil.isEmpty(mch_id)||
StringUtil.isEmpty(apiKey)||
StringUtil.isEmpty(apiv3Key)||
StringUtil.isEmpty(keyStoreUrl)||
StringUtil.isEmpty(fileUrl)){
JOptionPane.showMessageDialog(contentPane, "参数不能为空,都需要填写!", "标题", JOptionPane.WARNING_MESSAGE);
}else{
try{
String result = submit();
System.out.println(result);
if(result.startsWith("错误")){
JOptionPane.showMessageDialog(contentPane, result, "标题", JOptionPane.INFORMATION_MESSAGE);
}else{
Map<String, String> returnMap = new HashMap<>();
returnMap = TransformXmlMap.xml2map(result, "xml");
String applyment_id = returnMap.get("applyment_id");
ApplymentIdFiled.setText(applyment_id);
JOptionPane.showMessageDialog(contentPane, returnMap, "标题", JOptionPane.INFORMATION_MESSAGE);
}
}catch(Exception e){
JOptionPane.showMessageDialog(contentPane, "异常:"+e.getMessage(), "标题", JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
register.setBounds(220, 180, 93, 23);
contentPane.add(register);
JLabel label = new JLabel("进件申请编号:");
label.setForeground(Color.RED);
label.setBounds(50, 210,100, 15);
contentPane.add(label);
ApplymentIdFiled = new JTextField();
ApplymentIdFiled.setForeground(Color.DARK_GRAY);
ApplymentIdFiled.setBounds(150,210, 300, 21);
contentPane.add(ApplymentIdFiled);
JButton query = new JButton("查询进件结果");
query.setForeground(Color.RED);
query.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(ApplymentIdFiled.getText().trim().equals("")){
JOptionPane.showMessageDialog(contentPane, "进件申请编号不能为空!", "标题", JOptionPane.WARNING_MESSAGE);
}else{
try{
String result = query(ApplymentIdFiled.getText().trim());
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable tText = new StringSelection(result);
clip.setContents(tText, null);
JOptionPane.showMessageDialog(contentPane, result, "标题", JOptionPane.INFORMATION_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(contentPane, "异常:"+e.getMessage(), "标题", JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
query.setBounds(220, 240, 120, 23);
contentPane.add(query);
}
// submit();
//query("2000002129861278");
public static String submit() throws Exception {
// 获取平证书序列号等相关信息
Map<String, String> certSn = SubmitUtil.fetchCertSn(certSnUrl, mch_id, apiKey);
String certificates = certSn.get("certificates");
JSONObject certificate = (JSONObject) JSONObject.parse(certificates);
JSONObject certSnJson = certificate.getJSONArray("data").getJSONObject(0);
String cert_sn = certSnJson.getString("serial_no");
JSONObject encrypt_certificate = certSnJson.getJSONObject("encrypt_certificate");
String nonce = encrypt_certificate.getString("nonce");
String ciphertext = encrypt_certificate.getString("ciphertext");
String associated_data = encrypt_certificate.getString("associated_data");
String publicCertSnKey = RSAEncryptDemo.decryptCertSN(associated_data, nonce, ciphertext, apiv3Key);
List<String> infoList = new ArrayList<String>();
// 上传照片
Map<String, String> photoMap = new HashMap<String, String>();
File file = new File(fileUrl);
String[] photoNames = file.list();
for (int i = 0; i < photoNames.length; i++) {
String photoName = photoNames[i];
String name = (photoName.split("\\."))[0];
String suffix = (photoName.split("\\."))[1];
if (("jpeg,jpg,bmp,png,JPEG,JPG,BMP,PNG").indexOf(suffix) != -1) {
File photoFile = new File(fileUrl + photoName);
if (photoFile.length() > 2 * 1024 * 1024) {
return "错误:"+photoName+"图片大于2M!";
}
String photoid = SubmitUtil.upload(photoFile, mch_id, keyStoreUrl, photoUpdateUrl, apiKey);
photoMap.put(name, photoid);
} else if (("txt").indexOf(suffix) != -1) {
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileUrl + photoName)),"UTF-8"));
String str;
// 按行读取字符串
while ((str = bf.readLine()) != null) {
infoList.add(str.trim());
}
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
SortedMap<String, String> paramMap = new TreeMap<String, String>();
String nonce_str = SubmitUtil.genNonceStr();
String id_card_name = infoList.get(0);//
String id_card_number = infoList.get(1);//
String id_card_valid_time = infoList.get(2);
String account_name = infoList.get(3);
String account_bank = infoList.get(4);
String bank_address_code = infoList.get(5);
String account_number = infoList.get(6);
String store_name = infoList.get(7);
String store_address_code = infoList.get(8);
String store_street = infoList.get(9);
// 门店门口照片
String store_entrance_pic = photoMap.get("门头照");
// 店内环境照片
String indoor_pic = photoMap.get("内景图");
// 身份证人像面照片
String id_card_copy = photoMap.get("身份证正面");
// 身份证国徽面照片
String id_card_national = photoMap.get("身份证背面");
String merchant_shortname = infoList.get(10);
String service_phone = infoList.get(11);
String product_desc = infoList.get(12);
String rate = infoList.get(13);
String contact = infoList.get(14);
String contact_phone = infoList.get(15);
String business_code = RandomUtils.getRandomNumbersAndLetters(32);
System.out.println("business_code:" + business_code);
paramMap.put("version", "3.0");
paramMap.put("cert_sn", cert_sn);
paramMap.put("mch_id", mch_id);
paramMap.put("nonce_str", nonce_str);
paramMap.put("sign_type", SubmitUtil.SIGN_TYPE);
paramMap.put("business_code", business_code);
paramMap.put("id_card_copy", id_card_copy);
paramMap.put("id_card_national", id_card_national);
paramMap.put("id_card_name", RSAEncryptDemo.rsaEncrypt(id_card_name, publicCertSnKey));
paramMap.put("id_card_number", RSAEncryptDemo.rsaEncrypt(id_card_number, publicCertSnKey));
paramMap.put("id_card_valid_time", id_card_valid_time);
paramMap.put("account_name", RSAEncryptDemo.rsaEncrypt(account_name, publicCertSnKey));
paramMap.put("account_bank", account_bank);
paramMap.put("bank_address_code", bank_address_code);
paramMap.put("account_number", RSAEncryptDemo.rsaEncrypt(account_number, publicCertSnKey));
paramMap.put("store_name", store_name);
paramMap.put("store_address_code", store_address_code);
paramMap.put("store_street", store_street);
paramMap.put("store_entrance_pic", store_entrance_pic);
paramMap.put("indoor_pic", indoor_pic);
paramMap.put("merchant_shortname", merchant_shortname);
paramMap.put("service_phone", service_phone);
paramMap.put("product_desc", product_desc);
paramMap.put("rate", rate);
paramMap.put("contact", RSAEncryptDemo.rsaEncrypt(contact, publicCertSnKey));
paramMap.put("contact_phone", RSAEncryptDemo.rsaEncrypt(contact_phone, publicCertSnKey));
String sign = SubmitUtil.sha256Sign(paramMap, apiKey);
paramMap.put("sign", sign);
String result = WXPayHttpUtil.WXHttpPostXMLWithCert(requestUrl, paramMap, keyStoreUrl);
return result;
}
public static String query(String applyment_id) throws Exception {
String resultStr = "";
SortedMap<String, String> paramMap = new TreeMap<String, String>();
String nonce_str = SubmitUtil.genNonceStr();
paramMap.put("version", "1.0");
paramMap.put("mch_id", mch_id);
paramMap.put("nonce_str", nonce_str);
paramMap.put("sign_type", SubmitUtil.SIGN_TYPE);
paramMap.put("applyment_id", applyment_id);
String sign = SubmitUtil.sha256Sign(paramMap, apiKey);
paramMap.put("sign", sign);
String result = WXPayHttpUtil.WXHttpPostXMLWithCert(queryUrl, paramMap, keyStoreUrl);
System.out.println(result);
Map<String, String> returnMap = new HashMap<>();
returnMap = TransformXmlMap.xml2map(result, "xml");
if (returnMap.get("return_code").equals("SUCCESS")) {
if (returnMap.get("result_code").equals("SUCCESS")) {
String applyment_state = returnMap.get("applyment_state");
resultStr ="结果:" + returnMap.get("applyment_state_desc");
if ("TO_BE_SIGNED".equals(applyment_state) || "FINISH".equals(applyment_state)) {
resultStr = "商户号:" + returnMap.get("sub_mch_id") + ",签约链接" + returnMap.get("sign_url");
}
} else {
resultStr = "失败:" + returnMap.get("err_code_des");
}
} else {
resultStr = "失败:" + returnMap.get("return_msg");
}
return resultStr;
}
}
版权声明:本文为bang2tang2原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。