最详细的⽀付JSAPI完整流程_java版本
要实现⽀付要满⾜⼏个条件:
⼀、有⼀个服务号,菜单栏⾥⾯有⽀付菜单的
java下载过程⼆、需要开通商户号,不是,必须和做关联操作
常⽤的⼏个参数 下⾯例⼦参数值已经经过处理
public final class WxConfig {
public final static String appId ="wx98dc29047cexxxx";// appid
public final static String mchId ="1566911111";// 商户ID
public final static String key ="xxxxxxx4TI4srY33SwkyXYojBzK6Fsp";// 商户KEY 跟⽀付约定的密钥
public final static String payUrl ="h.weixin.qq/pay/unifiedorder";// 统⼀下单地址
public final static String tradeType ="JSAPI";// ⽀付⽅式
getch 的功能public final static String secret="xxx9b86bdb6637a4ccfaf6e7e2609";//商户APPSECRET
}
以下是⽀付流程,后⾯会根据流程上响应的代码
⼯具包下载,其中很多类可以使⽤
stripe手续费1、获取⽤户code
var code =getUrlParam('code')// 截取路径中的code,如果没有就去授权,如果已经获取到了就直接传code给后台获取openId
var local = window.location.href
if(code ==null|| code ===''){
window.location.href ='open.weixin.qq/connect/oauth2/authorize?appid=wx98dc29047xxxxx&redirect_uri='+encodeURIComponent(local)+'& response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
}
中间有⼏个参数
会通过回调的⽅式跳回原来的页⾯,code就是⽤户的会话ID了,某个时段有效。
redirect_uri/?code=CODE&state=STATE
2、获取openId
拿到code以后为了安全性提交到后台,后台我这⾥是⽤的java springboot框架
String code = ("code").toString();
String res =get("api.weixin.qq/sns/oauth2/access_token?appid="+ WxConfig.appId +"&secret="+ WxConfig.secret +"&code="+ code +"&gran t_type=authorization_code");
JSONObject json1 = JSONObject.parseObject(res);
String openId = String("openid");
System.out.println(openId+"------------------------------");
这⾥使⽤的 WxConfig 就是⽂章开头的配置⽂件类
public String get(String url)throws Exception {
// 创建HttpClient实例
CloseableHttpClient client = ate().build();
// 根据URL创建HttpGet实例
HttpGet get =new HttpGet(url);
// 执⾏get请求,得到返回体
HttpResponse response = ute(get);
// 判断是否正常返回
StatusLine().getStatusCode()== HttpStatus.SC_OK){
// 解析数据
String data = Entity(), Charsets.UTF_8);
System.out.println(data);
return data;
}
return"";
}
<dependency>
<groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
javase语法<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
3、发起订单
拿到openId以后就可以发起订单了
@RequestMapping("/payH5")
@ResponseBody
public Object payH5(HttpServletRequest request, HttpServletResponse response,@RequestBody Map<String, Object> json) throws Exception {
try{
16个诱导公式String orderNo = UUID.randomUUID().toString().replaceAll("-","");
//记得⾃⼰把订单id保存下来 orderNo
String code = ("code").toString();
String res =get("api.weixin.qq/sns/oauth2/access_token?appid="+ WxConfig.appId +"&secret="
+ WxConfig.secret +"&code="+ code +"&grant_type=authorization_code");
JSONObject json1 = JSONObject.parseObject(res);
String openId = String("openid");//获取的⽤户唯⼀标识
System.out.println(openId+"------------------------------");
Map<String, String> paraMap =new HashMap<>();
paraMap.put("appid", WxConfig.appId);//appid
paraMap.put("body", ("body").toString());//商品说明
paraMap.put("mch_id", hId);//商家id
paraMap.put("nonce_str", ateNonceStr());
paraMap.put("openid", openId);
paraMap.put("out_trade_no", orderNo);
paraMap.put("spbill_create_ip","120.79.1.1");
paraMap.put("total_fee", String.valueOf((int)(Double.Spjg())*100)));
paraMap.put("trade_type", adeType);
paraMap.put("notify_url","//api/wxnotify");
String apiKey = WxConfig.key;
System.out.println(WXPayUtil.mapToXml(paraMap));
String sign = ateSignature(paraMap, apiKey);
paraMap.put("sign", sign);
String xml = WXPayUtil.mapToXml(paraMap);
System.out.println(xml);
// 统⼀下单 h.weixin.qq/pay/unifiedorder
String unifiedorder_url ="h.weixin.qq/pay/unifiedorder";
String xmlStr = WXPayReport.httpRequest(unifiedorder_url, xml,2000,5000);
// 以下内容是返回前端页⾯的json数据
System.out.println(xmlStr);
hbase开发手册String prepay_id ="";// 预⽀付id
if(xmlStr.indexOf("SUCCESS")!=-1){
Map<String, String> map = lToMap(xmlStr);
prepay_id =(String) ("prepay_id");
}
Map<String, String> payMap =new HashMap<String, String>();
payMap.put("appId", WxConfig.appId);
payMap.put("timeStamp", CurrentTimestamp()+"");
payMap.put("nonceStr", ateNonceStr());
payMap.put("signType","MD5");
payMap.put("package","prepay_id="+ prepay_id);
String paySign = ateSignature(payMap, WxConfig.key);
payMap.put("paySign", paySign);
payMap.put("orderNo", orderNo);
payMap.put("success","0");
return payMap;
}catch(Exception e){
e.printStackTrace();
Map<String, String> payMap =new HashMap<String, String>();
payMap.put("success","1");
return payMap;
}
// return new SuccessResponseData();
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论