springboot微信支付pc页面生成二维码
微信支付开发文档:https://pay.weixin.qq.com/wiki/doc/api/index.html
咱们这次采用的是Native支付
Native支付使用场景:用户打开"微信扫一扫“,扫描商户的二维码后完成支付
微信提供的api如下,咋们这次只使用统一下单的api。
首先你得准备如下商号配置:
weixin:appid: wx8397f8696b538317 #微信公众账号或开放平台APP的唯一标识partner: 1473426802 #财付通平台的商户账号partnerkey: T6m9iK73b0kn9g5v426MKfHQH7X8rKwb #财付通平台的商户密钥notifyurl: http://www.itcast.cn #回调地址
写入application.yml文件
server:port: 8040#微信支付信息配置: 这里请填写自己的id....
weixin:appid: wx8397f8696b538317 #微信公众账号或开放平台APP的唯一标识partner: 1473426802 #财付通平台的商户账号partnerkey: T6m9iK73b0kn9g5v426MKfHQH7X8rKwb #财付通平台的商户密钥notifyurl: http://www.itcast.cn #回调地址
pom文件如下:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.3</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.21</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>1.7.21</version></dependency><dependency><groupId>jdom</groupId><artifactId>jdom</artifactId><version>1.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.4</version></dependency><!-- 微信支付依赖!xml————map相互转换 --><dependency><groupId>com.github.wxpay</groupId><artifactId>wxpay-sdk</artifactId><version>0.0.3</version></dependency><!-- HttpClient工具包 --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency><!-- 其它需要的依赖配置...父工程 statrt-web.... --><!-- 二维码 --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.2.0</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.2.0</version></dependency>
HttpClient工具类
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;public class HttpClient {private String url;private Map<String, String> param;private int statusCode;private String content;private String xmlParam;private boolean isHttps;public boolean isHttps() {return isHttps;}public void setHttps(boolean isHttps) {this.isHttps = isHttps;}public String getXmlParam() {return xmlParam;}public void setXmlParam(String xmlParam) {this.xmlParam = xmlParam;}public HttpClient(String url, Map<String, String> param) {this.url = url;this.param = param;}public HttpClient(String url) {this.url = url;}public void setParameter(Map<String, String> map) {param = map;}public void addParameter(String key, String value) {if (param == null)param = new HashMap<String, String>();param.put(key, value);}public void post() throws ClientProtocolException, IOException {HttpPost http = new HttpPost(url);setEntity(http);execute(http);}public void put() throws ClientProtocolException, IOException {HttpPut http = new HttpPut(url);setEntity(http);execute(http);}public void get() throws ClientProtocolException, IOException {if (param != null) {StringBuilder url = new StringBuilder(this.url);boolean isFirst = true;for (String key : param.keySet()) {if (isFirst) {url.append("?");}else {url.append("&");}url.append(key).append("=").append(param.get(key));}this.url = url.toString();}HttpGet http = new HttpGet(url);execute(http);}/*** set http post,put param*/private void setEntity(HttpEntityEnclosingRequestBase http) {if (param != null) {List<NameValuePair> nvps = new LinkedList<NameValuePair>();for (String key : param.keySet()) {nvps.add(new BasicNameValuePair(key, param.get(key))); // 参数}http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数}if (xmlParam != null) {http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));}}private void execute(HttpUriRequest http) throws ClientProtocolException,IOException {CloseableHttpClient httpClient = null;try {if (isHttps) {SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {// 信任所有@Overridepublic boolean isTrusted(X509Certificate[] chain,String authType)throws CertificateException {return true;}}).build();SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();} else {httpClient = HttpClients.createDefault();}CloseableHttpResponse response = httpClient.execute(http);try {if (response != null) {if (response.getStatusLine() != null) {statusCode = response.getStatusLine().getStatusCode();}HttpEntity entity = response.getEntity();// 响应内容content = EntityUtils.toString(entity, Consts.UTF_8);}} finally {response.close();}} catch (Exception e) {e.printStackTrace();} finally {httpClient.close();}}public int getStatusCode() {return statusCode;}public String getContent() throws ParseException, IOException {return content;}
}
PayController
import com.github.wxpay.sdk.WXPayUtil;
import com.sws.weixin.services.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;@RestController
@RequestMapping("/weixin")
public class PayController {@Autowiredprivate PayService payService;@GetMapping("/pay")public Object pay(String out_trade_no, String total_fee) throws Exception {//调用:统一下单!Map<String, String> pay = payService.pay("5000", "1");return pay;}//回调函数//必须得是post 的方式,这里查看yml: http://wsm.free.idcfengye.com/weixin/notifyurl /weixin/notifyurl 绑定的回调就是这个方法!@PostMapping("/notifyurl")public String notifyurl(HttpServletRequest request) {System.out.println("支付后回调---------------------------");InputStream is = null;ByteArrayOutputStream baos = null;try {is = request.getInputStream();baos = new ByteArrayOutputStream();byte[] bytes = new byte[1024];int len = 0;while ((len = is.read(bytes)) != -1) {baos.write(bytes, 0, len);}String str = new String(baos.toByteArray(), "UTF-8");System.out.println(str);//响应数据,如果不写, 商户就不会接受微信的回调,微信就会24小时内频繁进行响应...Map respMap = new HashMap();respMap.put("return_code", "SUCCESS");respMap.put("return_msg", "OK");return WXPayUtil.mapToXml(respMap);//处理异常,最终关闭资源!} catch (Exception e) {e.printStackTrace();} finally {try {baos.close();is.close();} catch (IOException e) {e.printStackTrace();}}return "";}
}
接口
public interface PayService {public Map<String, String> pay(String out_trade_no, String total_fee) throws Exception;
}
实现接口
import com.github.wxpay.sdk.WXPayUtil;
import com.sws.weixin.util.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;@Service
public class PayServiceimpl implements PayService{//从yml 中读取对应的... yml配置属性!@Value("${weixin.appid}")private String appid;@Value("${weixin.partner}")private String partner;@Value("${weixin.partnerkey}")private String partnerkey;@Value("${weixin.notifyurl}")private String notifyurl;//唯一订单号 交易金额单位: 分@Overridepublic Map<String, String> pay(String out_trade_no, String total_fee) throws Exception{//设置统一下单: 请求参数;Map<String, String> param = new HashMap<>();param.put("appid", appid); //应用IDparam.put("mch_id", partner); //商户ID号param.put("nonce_str", WXPayUtil.generateNonceStr()); //随机数param.put("body", "My商场"); //订单描述param.put("out_trade_no", out_trade_no); //商户订单号: 这是唯一的,会在微信存储!param.put("total_fee", total_fee); //交易金额:param.put("spbill_create_ip", "127.0.0.1"); //终端IPparam.put("notify_url", notifyurl); //回调地址: 是后面需要进行商户暴漏的回调接口!param.put("trade_type", "NATIVE"); //交易类型,NATIVEString xml = WXPayUtil.generateSignedXml(param, partnerkey); //将map 转换成 xml 并传入签名String url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //统一下单的: api!//获取HttpClient 请求对象!HttpClient httpClient = new HttpClient(url);//传入参数,执行请求posthttpClient.setXmlParam(xml);httpClient.setHttps(true);httpClient.post();//获取返回结果! 打印!String content = httpClient.getContent();System.out.println(content);//将返回xml 转换成Map 方便展示输出!Map<String, String> stringStringMap = WXPayUtil.xmlToMap(content);//输出: 订单号 金额 支付url(将这个绑定至上面的,二维码中扫码就可以,看到信息进行支付了!!)Map<String, String> result = new HashMap<>();result.put("out_trade_no", out_trade_no);result.put("total_fee", total_fee);result.put("code_url", stringStringMap.get("code_url"));//打印!System.out.println(stringStringMap.get("code_url"));return result;}
}
启动项目访问:http://localhost:8040/weixin/pay
code_url中的值为支付地址:weixin://wxpay/bizpayurl?pr=xxxxxxxx
将地址,采用前端js转换成二维码即可。
<html>
<head>
<title>二维码入门小demo</title>
<!--1.引入js 2. 创建一个img标签 用来存储显示二维码的图片 3.创建js对象 4.设置js对象的配置项-->
<script src="https://www.jq22.com/demo/erweima20161214/dist/umd/qrious.js"> </script>
</head>
<body><img id="myqrious" ></body><script>var qrious = new QRious({element:document.getElementById("myqrious"),// 指定的是图片所在的DOM对象size:250, //指定图片的像素大小level:'H', //指定二维码的容错级别(H:可以恢复30%的数据)value:'weixin://wxpay/bizpayurl?pr=xxxxxxxx'//指定二维码图片代表的真正的值})</script>
</html>
springboot微信支付pc页面生成二维码相关推荐
- 微信小程序实现生成二维码功能并下载到本地
微信小程序实现生成二维码功能并下载到本地 背景 实现 备注 背景 有这样一个需求,后台返回了url地址,微信小程序将url地址转成二维码图片,展示在页面上,并且该二维码图片可下载到用户手机相册中 实现 ...
- 转载:在微信小程序中 生成二维码
目录 转载: weapp-qrcode-canvas-2d 仓库地址 测试环境 使用 安装方法1:直接引入 js 文件 安装方法2:npm安装 安装完成后调用 例子1:没有使用叠加图片 例子2:使用叠 ...
- wordpress页面生成二维码
二维码 的生成方便移动端访问,特别当你的主题是自适应主题的时候,有时候我们可以在侧边栏工具上放置一个生成二维码的功能,让我们的主题很炫,很实用,鲜明分享一些常用的jQuery jquery.qrcod ...
- 微信官方提供的生成二维码接口得到的是当前公众号的二维码
转自:http://blog.csdn.net/phil_jing/article/details/53910083 微信官方提供的生成二维码接口得到的是当前公众号的二维码,官方文档 目前有2种类型的 ...
- 微信官方提供的生成二维码接口得到的是当前公众号的二维码。
一定说明,这种方法我还没有测试,如果有疑问欢迎在评论区域讨论. .................... ................... 谢谢. 微信官方提供的生成二维码接口得到的是当前公众号 ...
- vue H5页面跳转微信小程序以及生成二维码跳转小程序
研究了两种H5跳转小程序的方法,同时携带参数 1. 手机浏览器打开H5页面,点击按钮拉起微信小程序 2. H5页面上生成小程序二维码,手机微信扫码跳转目标小程序 为了开发方便,以上两种均借助了微信小程 ...
- 微信小程序之生成二维码
首先先扯一些题外话,本人是java行业的小白,因为是改行做的java,之前的工作就不提了. 之前写过几篇随便,刚看了下,觉得比较low,就都删了,所以也算是进入java行业的第一篇随笔,如果有表述上或 ...
- 实现支付功能并生成二维码
最近做了一个需求,需要根据返回的url生成二维码,实现支付功能.对于第一次接触这个需求的我还是比较期待的,因为又能学到新的知识啦!话不多说,进入正题. 第一步 正常情况下支付订单的话都会有规定的时间, ...
- 扫码打开微信位置信息界面、微信定位(发送位置)生成二维码解决方案
最近遇到几个文字位置无法直接在导航软件查找定位的事情,很郁闷,就在想现在二维码时代了,居然还不能扫码实现导航吗? 于是用各导航软件研究了一下,发现导航软件是可以扫码导航的,但是只能针对他自己的链接有用 ...
最新文章
- oracle 修改用户密码_干货!数据库安全之Oracle数据库安全加固
- 用BenchmarkDotNet给C#程序做性能测试
- python注入_Python如何考虑代码注入安全?
- [云炬创业基础笔记]第二章创业者测试9
- centos7.6arm安装mysql8.0.17_Centos yum安装 MySQL 5.7
- PCB生成光绘文件教程 (Z)
- Spring学习11-Spring管理各种数据源
- 小白零基础学习Java开发入门教程奉上,希望对你有所帮助!
- 01-UIScrollView01-大图片展示
- Vim/Vi实用技巧(第二版)
- 安装JavaFX Scene Builder 到Eclipse
- 树链剖分之点剖分(点分治)讲解
- 婚礼一条龙服务成为新亮点
- Eclipse搭建springBoot进阶篇-SpringBoot+Mybatis
- 小米路由mini刷潘多拉及老毛子固件-详细教程
- 微信小程序下载图片预览和真机调试均无问题,发布到远程后点击下载没有任何反应
- simple craft system
- win10 旗舰版 系统激活出现 在连接到本地注册表时出现错误0x80041002 提示信息 解决方法...
- 0基础如何学习安卓开发
- 解决No converter for [class java.util.ArrayList] with preset Content-Type ‘null‘问题