java后台⼩程序统⼀下单⽀付、以及⼆次签名(亲测可⽤)
java 后台⼩程序统⼀下单⽀付、以及⼆次签名(仅供参考)
本⼈使⽤的是ssh框架,不过没关系,都是业务代码,不影响,话不多说,直接贴代码
⼀、⾸先去官⽹下载java的sdk,因为这⾥需要⽤到sdk⾥⾯的⼀些⽅法,感兴趣的话可以直接研究sdk中给的代码,这⾥是本⼈参考别⼈然后看了sdk中的代码后⾃⼰写的,也能实现统⼀下单,sdk下载地址
⼆、下载后将demo引⼊到⾃⼰的⼯程⾥⾯,可以选择性的引⼊,也可以全部引⼊,我是全部引⼊进去的
引⼊之后就是处理报错问题啦,就是导⼊各种jar包,jar包的话就不给出了,直接在maven上下载就可以
三、创建Configure.java ,以及RandomStringGenerator.java (后⾯会⽤到)
⼀些常亮配置Configure .java
public class Configure {
// 商户⽀付秘钥
private static String key = "XXXXXXXXXXXXX";
//⼩程序ID
private static String appID = "XXXXXXXXXXXXX";
//商户号
private static String mch_id = "XXXXXXXXXXXXX";
// ⼩程序的secret
private static String secret = "XXXXXXXXXXXXX";
public static String getKey() {
return key;
}
public static void setKey(String key) {
Configure.key = key;
}
public static String getAppID() {
return appID;
}
public static void setAppID(String appID) {
Configure.appID = appID;
}
public static String getMch_id() {
return mch_id;
}
public static void setMch_id(String mch_id) {
}
public static String getSecret() {
return secret;
}
public static void setSecret(String secret) {
Configure.secret = secret;
}
}
随机字符串⽣成的类RandomStringGenerator .java
package com.b.pay.wxpay.plugins;
import java.util.Random;
/**
* 随机字符串⽣成
*
*/
public class RandomStringGenerator {
/**
* 获取⼀定长度的随机字符串
* @param length 指定字符串长度
* @return ⼀定长度的字符串
*/
public static String getRandomStringByLength(int length) {
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = Int(base.length());
sb.append(base.charAt(number));
}
String();
}
}
四、创建发送请求的类HttpRequest.java(缺少的jar包⾃⼰导⼊啦,请求的时候可能会出现请求失败的问题,这时候检查下,是不是
jar包是不是有冲突哦)
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.fig.RequestConfig;
import org.apache.hods.HttpPost;
import org.ity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.l.DomDriver;
import com.thoughtworks.l.XmlFriendlyNameCoder;
import javax.ssl.X509TrustManager;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import CertificateException;
public class HttpRequest {
//连接超时时间,默认10秒
private static final int socketTimeout = 10000;
//传输超时时间,默认30秒
private static final int connectTimeout = 30000;
/**
* post请求
* @throws IOException
* @throws IOException
* @throws ClientProtocolException
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws KeyManagementException
* @throws UnrecoverableKeyException
*/
public static String sendPost(String url, Object xmlObj) throws ClientProtocolException, IOException, UnrecoverableKeyException, KeyManagementExcepti on, KeyStoreException, NoSuchAlgorithmException {
HttpPost httpPost = new HttpPost(url);
//解决XStream对出现双下划线的bug
XStream xStreamForRequestPostData = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
xStreamForRequestPostData.alias("xml", Class());
//将要提交给API的数据对象转换成XML格式数据Post给API
String postDataXML = XML(xmlObj);
//得指明使⽤UTF-8编码,否则到API服务器XML的中⽂不能被成功识别
StringEntity postEntity = new StringEntity(postDataXML, "UTF-8");
httpPost.addHeader("Content-Type", "text/xml");
httpPost.setEntity(postEntity);
//设置请求器的配置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
HttpClient httpClient = ateDefault();
HttpResponse response = ute(httpPost);
HttpEntity entity = Entity();
String result = String(entity, "UTF-8");
return result;
}
public static String sendPost(String url, String signXml) throws ClientProtocolException, IOException, UnrecoverableKeyException, KeyManagementExcept ion, KeyStoreException, NoSuchAlgorithmException {
HttpPost httpPost = new HttpPost(url);
//解决XStream对出现双下划线的bug
XStream xStreamForRequestPostData = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
//得指明使⽤UTF-8编码,否则到API服务器XML的中⽂不能被成功识别
StringEntity postEntity = new StringEntity(signXml, "UTF-8");
httpPost.addHeader("Content-Type", "text/xml");
httpPost.setEntity(postEntity);
//设置请求器的配置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
HttpClient httpClient = ateDefault();
HttpResponse response = ute(httpPost);
HttpEntity entity = Entity();
String result = String(entity, "UTF-8");
return result;
}
}
五、创建接收统⼀下单调⽤接⼝返回的参数的类OrderReturnInfo.java
/**
* 接收统⼀下单调⽤接⼝返回的参数
* @author Administrator
* */
public class OrderReturnInfo {
public class OrderReturnInfo {
private String return_code;
private String return_msg;
private String result_code;
private String appid;
private String mch_id;
private String nonce_str;
private String sign;
private String prepay_id;
private String trade_type;
private String err_code;
private String err_code_des;
public String getReturn_code() {
return return_code;
}
public void setReturn_code(String return_code) { urn_code = return_code;
}
public String getReturn_msg() {
return return_msg;
}
public void setReturn_msg(String return_msg) { urn_msg = return_msg;
}
public String getResult_code() {
return result_code;
}
public void setResult_code(String result_code) { sult_code = result_code;
}
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getMch_id() {
return mch_id;
代码转换}
public void setMch_id(String mch_id) {
}
public String getNonce_str() {
return nonce_str;
}
public void setNonce_str(String nonce_str) {
<_str = nonce_str;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getPrepay_id() {
return prepay_id;
}
public void setPrepay_id(String prepay_id) {
this.prepay_id = prepay_id;
}
public String getTrade_type() {
return trade_type;
}
public void setTrade_type(String trade_type) { ade_type = trade_type;
}
}
public String getErr_code() {
return err_code;
}
public void setErr_code(String err_code) {
<_code = err_code;
}
public String getErr_code_des() {
return err_code_des;
}
public void setErr_code_des(String err_code_des) {
<_code_des = err_code_des;
}
}
六、接下来就是最后的业务处理代码了,我是写在Controller⾥⾯的,你可以把代码写在你的service中,这⾥需要写⼏个常量,注意了
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jason.framework.mvc.spring.BaseController;
import com.jason.framework.util.StringUtil;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
*
* @author jxliu
*
*/
@Controller()
@RequestMapping(value = "")
public class WxpayController extends BaseController{
private static final long serialVersionUID = 1L;
private static final Logger L = Logger(WxpayController.class);
//⽀付回调地址(你⾃⼰的请求地址,可以⾃⼰随意配置啦,写在这⽅便你理解)
private String notify_url = "/mob/pay/wxpay/weixin/callback/wxNotify.do";
//交易类型(这⾥是⼩程序)
private final String trade_type = "JSAPI";
//统⼀下单API接⼝链接(官⽅的接⼝)
private final String url = "h.weixin.qq/pay/unifiedorder";
@RequestMapping(value = "/payment")
public ResponseEntity<String> payment(HttpServletRequest request,String money, String openid,String body){
if (StringUtil.isEmpty(money)) {
return renderData(false, "参数错误:money不能为空!", null);
}
if (StringUtil.isEmpty(openid)) {
return renderData(false, "参数错误:openid不能为空!", null);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论