本文为大家分享了java版本之JSAPI支付+发送模板消息的相关资料,供大家参考,具体内容如下
1.工具类
工具类见:微信支付JAVA版本之Native付款
2.公众账号设置
3.代码实现
openId:openId为用户与该公众账号之间代表用户的唯一标示
以下类中涉及到生成token,关闭订单接口调用,获取配置文件信息,和工具类,在其他文章中有具体代码实现
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;
@RequestMapping(value="/weiXinSend",method=RequestMethod.GET)
@ResponseBody
public Object weiXinSend(HttpServletRequest request,HttpServletResponse response,String orderNo,String openId){
System.out.println("==========================微信发送消息开始========================"+getWeiXinAccessTokenService.accessToken());
try{
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File(
CustomizedPropertyPlaceholderConfigurer.getContextProperty("wx.cert").toString()));
try {
keyStore.load(instream, "见邮件".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详见获取token的文章
HttpPost httppost = new HttpPost(
"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getWeiXinAccessTokenService.accessToken());
PayHist ph = null;
List
4.weixin_jspay项目中代码实现
控制层代码
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开始=================");
System.out.println("packagevalue==============="+packagevalue);
System.out.println("paySign====================="+paySign);
request.setAttribute("packagevalue", "prepay_id=" + packagevalue);
request.setAttribute("paySign", paySign);
System.out.println("===================微信jsPay页面跳转开始=================");
return "weixin";
}
}
jsp代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
微信支付MD5.js:md5.js 和微信支付开发文档:下载地址
本文已被整理到了《Javascript微信开发技巧汇总》,欢迎大家学习阅读。
为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。