微信支付java版本之JSAPI支付+發(fā)送模板消息
本文為大家分享了java版本之JSAPI支付+發(fā)送模板消息的相關(guān)資料,供大家參考,具體內(nèi)容如下
1.工具類
工具類見(jiàn):微信支付JAVA版本之Native付款
2.公眾賬號(hào)設(shè)置


3.代碼實(shí)現(xiàn)
openId:openId為用戶與該公眾賬號(hào)之間代表用戶的唯一標(biāo)示
以下類中涉及到生成token,關(guān)閉訂單接口調(diào)用,獲取配置文件信息,和工具類,在其他文章中有具體代碼實(shí)現(xiàn)
package com.zhrd.bussinss.platform.controller.rest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.zhrd.bussinss.platform.bo.JsPay;
import com.zhrd.bussinss.platform.bo.PayHist;
import com.zhrd.bussinss.platform.constants.PayHistoryPayStatus;
import com.zhrd.bussinss.platform.constants.PayHistoryPayType;
import com.zhrd.bussinss.platform.service.GetWeiXinAccessTokenService;
import com.zhrd.bussinss.platform.service.WeiXinPayService;
import com.zhrd.bussinss.platform.utils.CloseWeiXinOrderUtils;
import com.zhrd.bussinss.platform.utils.CustomizedPropertyPlaceholderConfigurer;
import com.zhrd.bussinss.platform.weixinPayUtils.ClientCustomSSL;
@RestController
@RequestMapping("/rest/weiXinSendMessage")
public class WeiXinSendMessageRESTFULController {
@Autowired
WeiXinPayService weiXinPayService;
@Autowired
GetWeiXinAccessTokenService getWeiXinAccessTokenService;
private static long standardTime = 1662652800000L;
/**
* 微信發(fā)送消息
* @param request
* @param response
* @return
*/
@RequestMapping(value="/weiXinSend",method=RequestMethod.GET)
@ResponseBody
public Object weiXinSend(HttpServletRequest request,HttpServletResponse response,String orderNo,String openId){
System.out.println("==========================微信發(fā)送消息開(kāi)始========================"+getWeiXinAccessTokenService.accessToken());
try{
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File(
CustomizedPropertyPlaceholderConfigurer.getContextProperty("wx.cert").toString()));
try {
keyStore.load(instream, "見(jiàn)郵件".toCharArray());
}finally {
instream.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,
"10061401".toCharArray()).build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf).build();
// HttpGet httpget = new
// HttpGet("https://api.mch.weixin.qq.com/secapi/pay/refund"); //獲取token詳見(jiàn)獲取token的文章
HttpPost httppost = new HttpPost(
"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getWeiXinAccessTokenService.accessToken());
PayHist ph = null;
List<Map<String,Object>> td = weiXinPayService.getTrade(orderNo);
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String nonceStr = sdf.format(dt).toString();
Date now = new Date();
String tradePayNo = orderNo+String.format("%10d",standardTime - now.getTime()).substring(0, 10);
System.out.println("訂單標(biāo)號(hào)orderNo======="+orderNo);
System.out.println("10位隨機(jī)數(shù)======="+String.format("%10d",standardTime - now.getTime()).substring(0, 10));
String price = Math.round(Float.valueOf(td.get(0).get("price").toString())*100)+"";
Long timeExpireStrOld = dt.getTime(); //獲取配置文件信息 詳見(jiàn)獲取配置文件信息的文章
Long timeNew = Long.parseLong(CustomizedPropertyPlaceholderConfigurer.getContextProperty("weixin.send2finish.overtime").toString());
Long timeExpireNew = timeExpireStrOld+timeNew;
Date dtTimeExpire = new Date(timeExpireNew);
SimpleDateFormat dtSdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timeExpire = dtSdf.format(dtTimeExpire).toString();
System.out.println("nonceStr=="+nonceStr);
System.out.println("orderNo=="+orderNo);
System.out.println("price=="+price);
System.out.println("timeStart=="+nonceStr);
System.out.println("timeExpire=="+timeExpire);
JSONObject resultJsPay = (JSONObject) getJsPay(nonceStr, "訂單", tradePayNo, price, nonceStr,timeExpire,openId);
resultJsPay.getString("prepayId");
System.out.println("================222prepay_id222========================="+resultJsPay.getString("prepayId").toString());
List<Map<String,Map<String,String>>> data = new ArrayList<Map<String,Map<String,String>>>();
Map<String,Map<String,String>> firstparam = new HashMap<String, Map<String,String>>();
Map<String,String> valueParam = new HashMap<String, String>();
valueParam.put("value", td.get(0).get("buyerName")+"(先生/女士)的訂單");
valueParam.put("color", "#173177");
Map<String,String> valueParam1 = new HashMap<String, String>();
valueParam1.put("value", orderNo);
valueParam1.put("color", "#173177");
Map<String,String> valueParam2 = new HashMap<String, String>();
valueParam2.put("value", "智慧社區(qū)愛(ài)生鮮訂單");
valueParam2.put("color", "#173177");
Map<String,String> valueParam3 = new HashMap<String, String>();
valueParam3.put("value", td.get(0).get("price")+"元");
valueParam3.put("color", "#173177");
Map<String,String> valueParam4 = new HashMap<String, String>();
valueParam4.put("value", "等待支付");
valueParam4.put("color", "#173177");
Map<String,String> valueParam5 = new HashMap<String, String>();
valueParam5.put("value", "點(diǎn)擊支付");
valueParam5.put("color", "#173177");
firstparam.put("first", valueParam);
firstparam.put("keyword1", valueParam1);
firstparam.put("keyword2", valueParam2);
firstparam.put("keyword3", valueParam3);
firstparam.put("keyword4", valueParam4);
firstparam.put("remark", valueParam5);
data.add(firstparam);
JsPay jp = new JsPay();
jp.setTouser(openId);
jp.setTemplate_id("MieXd4-4uqRFMdjnvqMjH0egFYpm16r5DPDT6P9gPgg");
jp.setTopcolor("#173177");
jp.setUrl("http://公眾賬號(hào)設(shè)置的鏈接/weixin_jspay/weixinJsPay/init.action?packageValue="+resultJsPay.getString("prepayId").toString());
jp.setData(data);
JSONObject jsonObject = JSONObject.fromObject(jp);
String str = new String(jsonObject.toString().getBytes("utf-8"), "iso8859-1");
// String xml = ClientCustomSSL.RefundNativePackage(weiXinRefundService.getTradePayNo(orderNo),nonceStr,totalFee,refundFee,nonceStr);
try {
System.out.println("data=========="+str.toString());
StringEntity se = new StringEntity(str.toString().replace("[", "").replace("]", "").toString());
httppost.setEntity(se);
System.out.println("executing request" + httppost.getRequestLine());
CloseableHttpResponse responseEntry = httpclient.execute(httppost);
try {
HttpEntity entity = responseEntry.getEntity();
InputStream in = entity.getContent();
byte b[] = new byte[1024];
int len = 0;
int temp=0; //所有讀取的內(nèi)容都使用temp接收
while((temp=in.read())!=-1){ //當(dāng)沒(méi)有讀取完時(shí),繼續(xù)讀取
b[len]=(byte)temp;
len++;
}
in.close();
System.out.println(new String(b,0,len));
if (entity != null) {
System.out.println("Response content length: "
+ entity.getContentLength());
System.out.println("==="+responseEntry.getEntity().toString());
JSONObject result = JSONObject.fromObject(new String(b,0,len));
if(result.getString("errmsg").equals("ok")){
ph = new PayHist();
ph.setTradePayUrl("");
ph.setPayTradeNo(orderNo);
ph.setTradePayNo(tradePayNo);
ph.setPayStatus(PayHistoryPayStatus.WECHAT_PAY_STATUS_WAIT);
ph.setPayType(PayHistoryPayType.WECHAT_JS_PAY);
List<PayHist> payHistList = weiXinPayService.getPayHist(orderNo,"wechat","");
if(payHistList == null || payHistList.size() == 0){
weiXinPayService.addPayHist(ph);
}else{ //關(guān)閉訂單詳見(jiàn)取消訂單的文章
JSONObject strStatus = (JSONObject) CloseWeiXinOrderUtils.closeWeiXinOrder(payHistList.get(0).getTradePayNo());
if(strStatus.getString("status").equals("success")){
System.out.println(ph.getTradePayNo());
weiXinPayService.updatePayHist(ph);
}
}
}
return result;
}
EntityUtils.consume(entity);
}
finally {
responseEntry.close();
}
}
finally {
httpclient.close();
}
return null;
}catch(Exception e){
e.printStackTrace();
JSONObject result = new JSONObject();
result.put("status","error");
result.put("msg",e.getMessage());
return result;
}
}
public static Object getJsPay(String nonceStr,String orderDescribe,String orderNo,String price,String timeStart,String timeExpire,String openId) {
try{
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File(
CustomizedPropertyPlaceholderConfigurer.getContextProperty("wx.cert").toString()));
try {
keyStore.load(instream, "10061401".toCharArray());
}finally {
instream.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,
"見(jiàn)郵件".toCharArray()).build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf).build();
// HttpGet httpget = new
// HttpGet("https://api.mch.weixin.qq.com/secapi/pay/refund");
HttpPost httppost = new HttpPost(
"https://api.mch.weixin.qq.com/pay/unifiedorder");
String xml = ClientCustomSSL.CreateJsApiPackage(nonceStr,orderDescribe,orderNo,price,timeStart,timeExpire,openId);
try {
StringEntity se = new StringEntity(xml);
httppost.setEntity(se);
System.out.println("executing request" + httppost.getRequestLine());
CloseableHttpResponse responseEntry = httpclient.execute(httppost);
try {
HttpEntity entity = responseEntry.getEntity();
System.out.println("----------------------------------------");
System.out.println(responseEntry.getStatusLine());
if (entity != null) {
System.out.println("Response content length: "
+ entity.getContentLength());
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(entity.getContent());
Element rootElt = document.getRootElement();
Document documentXml =DocumentHelper.parseText(xml);
Element rootEltXml = documentXml.getRootElement();
System.out.println("根節(jié)點(diǎn):" + rootElt.getName());
System.out.println("==="+rootElt.elementText("result_code"));
System.out.println("==="+rootElt.elementText("return_msg"));
String resultCode = rootElt.elementText("result_code");
JSONObject result = new JSONObject();
if(resultCode.equals("SUCCESS")){
System.out.println("=================prepay_id===================="+ rootElt.elementText("prepay_id"));
result.put("prepayId", rootElt.elementText("prepay_id"));
result.put("sign",rootEltXml.elementText("sign"));
result.put("status","success");
result.put("msg","success");
}else{
result.put("status","false");
result.put("msg",rootElt.elementText("err_code_des"));
}
return result;
}
EntityUtils.consume(entity);
}
finally {
responseEntry.close();
}
}
finally {
httpclient.close();
}
return null;
}catch(Exception e){
e.printStackTrace();
JSONObject result = new JSONObject();
result.put("status","error");
result.put("msg",e.getMessage());
return result;
}
}
}
4.weixin_jspay項(xiàng)目中代碼實(shí)現(xiàn)
控制層代碼
package com.weixin.jspay.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/weixinJsPay")
public class WeiXinJsPayController {
@RequestMapping(value = "/init", method = RequestMethod.GET)
public String init( HttpServletRequest request,HttpServletResponse response,String packageValue,String paySign) {
System.out.println("===================微信jsPay開(kāi)始=================");
System.out.println("packageValue==============="+packageValue);
System.out.println("paySign====================="+paySign);
request.setAttribute("packageValue", "prepay_id=" + packageValue);
request.setAttribute("paySign", paySign);
System.out.println("===================微信jsPay頁(yè)面跳轉(zhuǎn)開(kāi)始=================");
return "weixin";
}
}
jsp代碼
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="/weixin_jspay/js/md5.js"></script>
<script language="javascript">
window.onload=function(){
var packageValue = '${packageValue}';
var paySign = '${paySign}';
alert("packageValue======"+packageValue);
alert("paySign======"+paySign);
var signString ="appId=見(jiàn)公共賬號(hào)&nonceStr=隨機(jī)字符串&package="+packageValue+"&paySign="+paySign+"&signType=MD5&timeStamp=時(shí)間";
//alert(signString);
var md5SignValue= ("" + CryptoJS.MD5(signString)).toUpperCase();
//alert(md5SignValue);
WeixinJSBridge.invoke('getBrandWCPayRequest',{
"appId" : "<span style="font-family: Arial, Helvetica, sans-serif;">見(jiàn)公共賬號(hào)</span>",
"timeStamp" : "1395712654",
"nonceStr" : "123456",
"package" : packageValue,
"signType" : "MD5",
"paySign" : md5SignValue
},function(res){
alert(res.err_msg);
WeixinJSBridge.log(res.err_msg);
if(res.err_msg == "get_brand_wcpay_request:ok"){
alert("微信支付成功");
}else if(res.err_msg == "get_brand_wcpay_request:cancel"){
alert("用戶取消支付");
}else{
alert("支付失敗");
}
})
}
</script>
</head>
<body>
<button type="button" onclick="callpay('${packageValue}','${paySign}')" >微信jsPay</button>
</body>
</html>
微信支付MD5.js:md5.js 和微信支付開(kāi)發(fā)文檔:下載地址
本文已被整理到了《JavaScript微信開(kāi)發(fā)技巧匯總》,歡迎大家學(xué)習(xí)閱讀。
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開(kāi)發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
必須掌握的十個(gè)Lambda表達(dá)式簡(jiǎn)化代碼提高生產(chǎn)力
這篇文章主要為大家介紹了必須掌握的十個(gè)Lambda表達(dá)式來(lái)簡(jiǎn)化代碼提高生產(chǎn)力,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Spring Boot如何使用httpcomponents實(shí)現(xiàn)http請(qǐng)求
這篇文章主要介紹了Spring Boot使用httpcomponents實(shí)現(xiàn)http請(qǐng)求的示例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
SpringBoot整合Redis將對(duì)象寫入redis的實(shí)現(xiàn)
本文主要介紹了SpringBoot整合Redis將對(duì)象寫入redis的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Java實(shí)現(xiàn)復(fù)原IP地址的方法
這篇文章主要介紹了Java實(shí)現(xiàn)復(fù)原IP地址的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
spring-security關(guān)于hasRole的坑及解決
這篇文章主要介紹了spring-security關(guān)于hasRole的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
maven無(wú)法依賴spring-cloud-stater-zipkin的解決方案
這篇文章主要介紹了maven無(wú)法依賴spring-cloud-stater-zipkin如何解決,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
SpringBoot3整合Druid監(jiān)控功能的項(xiàng)目實(shí)踐
Druid連接池作為一款強(qiáng)大的數(shù)據(jù)庫(kù)連接池,提供了豐富的監(jiān)控和管理功能,成為很多Java項(xiàng)目的首選,本文主要介紹了SpringBoot3整合Druid監(jiān)控功能的項(xiàng)目實(shí)踐,感興趣的可以了解一下2024-01-01
SpringMVC中的@RequestMapping注解解析
這篇文章主要介紹了SpringMVC中的@RequestMapping注解解析,SpringMVC使用@RequestMapping注解為控制器指定可以處理哪些?URL?請(qǐng)求,在控制器的類定義及方法定義處都可標(biāo)注@RequestMapping,需要的朋友可以參考下2023-12-12
java實(shí)現(xiàn)銀行家算法(Swing界面)
這篇文章主要為大家詳細(xì)介紹了銀行家算法的java代碼實(shí)現(xiàn),Swing寫的界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12

