微信支付,扫码支付 首先前提是要有码!
二维码
本章掌握基本属性,使用了解即可! qrious.js二维码插件的可用配置参数如下
下面的代码即可生成一张二维码
pay.html
<html>
<head>
<title>二维码入门小demo</title>
<!--1.引入js 2. 创建一个img标签 用来存储显示二维码的图片 3.创建js对象 4.设置js对象的配置项-->
<script src="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:'https://blog.csdn.net/qq_45542380?spm=1011.2124.3001.5343'
//指定二维码图片代表的真正的值,这里我设置了我的blog地址...
})
</script>
</html>
微信扫码跳转页面! 看到这儿点个赞吧~!!
注意
可以自己下载或者私聊...
支付成功之后,微信服务器会 回调这个api接口,而对本机的项目来说,本地的api方法只有自己可以访问,无法直接通过外网访问!
简单来说就是:
Ngrok 官网: https://www.ngrok.cc/user.html
确认/添加 | 开通!
恭喜你有了一个自己的服务器了!
隧道管理:
发现它默认生成了一个你的,域名
有点意思!
Sunny-Ngrok启动工具.bat
http://wsm.free.idcfengye.com
就等于访问你本机的 127.0.0.1:7008 的端口了7007
并启动微服!
http://wsm.free.idcfengye.com/items/10000000616300.html
效果一样!重点不在一个局域网!!!永久服务器好吧!
Native模式
太发达了!
订单数据会存入到数据库中...
且 进入支付页面
预留的回调地址
,并携带支付状态信息
消息队列
因为支付是一个 高并发且非常重要的操作!
为了提高程序的性能和安全,不可能在支付方面做限流!哪有地方不想要钱的!
对于高并发的,订单采用消息队列进行存储… 信息,有序缓慢逐一进行数据更新!
微信24小时,时间递增不停向商家进行响应
支付状态,直到商家接收并执行响应才会停止!
官方文档: https://pay.weixin.qq.com/wiki/doc/api/index.html
这里就自己使用自己的
微信商户了...
微信支付提供了SDK 或直接使用Maven 依赖
微信支付SDK的以下功能:
WXPayUtil.generateNonceStr()
WXPayUtil.generateSignedXml(param, partnerkey)
WXPayUtil.xmlToMap(result)
微信认证,之间通信api 参数:
以XML方式发送(POST)给微信支付接口(URL),微信支付接口也是以XML方式给予响应。
HttpClient
它支持HTTP协议最新的版本和建议
<!-- 微信支付依赖!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.... -->
上图并没有展示,因为通过,Maven父工程集成了! 一般建立在 util包下!
HttpClient.Java
package com.zb.util;
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() {
// 信任所有
@Override
public 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;
}
}
application.yml
server:
port: 7008
spring:
application:
name: pay-server
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
feign:
httpclient:
connection-timeout: 10000
client:
config:
default: # 指定feignclients对应的名称 如果指定的是default 表示全局所有的client 的超时时间设置
connectTimeout: 10000
readTimeout: 10000
loggerLevel: basic
#微信支付信息配置: 这里请填写自己的id....
weixin:
appid: wwwwwwwwwwwwwww #商家微信公众账号或开放平台APP的唯一标识 这里是我随便写的...(实际开发用自己的即可)
partner: 11473623 #商家财付通平台的商户账号
partnerkey: 2ab9071b06b9f739b950ddb41db2690d #商家财付通平台的商户密钥
notifyurl: http://wsm.free.idcfengye.com/weixin/notifyurl #回调地址后,微信服务器根据这个地址给 商户24小时的返回,支付的结果数据!
调用微信api 输入:唯一订单号
订单金额
回调函数
商家参数...
PayService.Java
@Service
public class 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;
//唯一订单号 交易金额单位: 分
public Map<String, String> pay(String out_trade_no, String total_fee) throws Exception {
//设置统一下单: 请求参数;
Map<String, String> param = new HashMap<>();
param.put("appid", appid); //应用ID
param.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"); //终端IP
param.put("notify_url", notifyurl); //回调地址: 是后面需要进行商户暴漏的回调接口!
param.put("trade_type", "NATIVE"); //交易类型,NATIVE
String xml = WXPayUtil.generateSignedXml(param, partnerkey); //将map 转换成 xml 并传入签名
String url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //统一下单的: api!
//获取HttpClient 请求对象!
HttpClient httpClient = new HttpClient(url);
//传入参数,执行请求post
httpClient.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;
}
}
回调函数
PayController.Java
@RestController
@RequestMapping("/weixin")
public class PayController {
@Autowired
private PayService payService;
@GetMapping("/pay")
public Dto pay(String out_trade_no, String total_fee) throws Exception {
//调用:统一下单!
Map<String, String> pay = payService.pay(out_trade_no, total_fee);
return DtoUtil.returnSuccess("获取二维码成功!", 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 "";
}
}
它不是商户服务调用的,是微信支付服务,在用户支付成功后,不停的调用的一个方法!
支付结果通知回调频率调整为15s/15s/30s/3m/10m/20m/30m/30m/30m/60m/3h/3h/3h/6h/6h - 总计 24h4m
MyPayApp.Java
import com.zb.service.PayService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) //父工程中存在,访问数据库依赖防止报错,禁用调!
@EnableEurekaClient
public class MyPayApp {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(MyPayApp.class, args);
PayService bean = context.getBean(PayService.class);
System.out.println("模拟一个假订单1分钱!---------------------------");
bean.pay("5400", "1");
}
}
支付后 回调!
结合文档学习:
PayService.Java
//查询订单: 参数: 要查询的订单号!
public Map<String,String> queryStatus(String out_trade_no)throws Exception{
//设置统一下单: 请求参数;
Map<String, String> param = new HashMap<>();
param.put("appid", appid);
param.put("mch_id", partner);
param.put("out_trade_no", out_trade_no);
param.put("nonce_str", WXPayUtil.generateNonceStr());
String xml = WXPayUtil.generateSignedXml(param, partnerkey);
//统一下单的: api! 获取HttpClient 请求对象!传入参数,执行请求post
String url = "https://api.mch.weixin.qq.com/pay/orderquery";
HttpClient client =new HttpClient(url);
client.setXmlParam(xml);
client.setHttps(true);
client.post();
//获取返回结果! 打印!
String content = client.getContent();
System.out.println(content);
Map<String, String> stringStringMap = WXPayUtil.xmlToMap(content);
Map<String, String> result = new HashMap<>();
result.put("out_trade_no", out_trade_no);
result.put("trade_state_desc",stringStringMap.get("trade_state_desc"));
return result;
}
MyPayApp.Java
import com.zb.service.PayService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) //父工程中存在,访问数据库依赖防止报错,禁用调!
@EnableEurekaClient
public class MyPayApp {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(MyPayApp.class, args);
PayService bean = context.getBean(PayService.class);
System.out.println("模拟一个假订单1分钱!---------------------------");
bean.pay("5400", "1");
System.out.println("查询订单状态-----------------------------------");
bean.queryStatus("5400");
}
}
PayService.Java
//关闭订单:
public Map<String,String> stopStatus(String out_trade_no)throws Exception{
Map<String, String> param = new HashMap<>();
param.put("appid", appid); //应用ID
param.put("mch_id", partner);
param.put("out_trade_no", out_trade_no);
param.put("nonce_str", WXPayUtil.generateNonceStr());
String xml = WXPayUtil.generateSignedXml(param, partnerkey);
String url = "https://api.mch.weixin.qq.com/pay/closeorder";
HttpClient client =new HttpClient(url);
client.setXmlParam(xml);
client.setHttps(true);
client.post();
String content = client.getContent();
System.out.println(content);
Map<String, String> stringStringMap = WXPayUtil.xmlToMap(content);
Map<String, String> result = new HashMap<>();
result.put("out_trade_no", out_trade_no);
result.put("trade_state_desc",stringStringMap.get("trade_state_desc"));
return result;
}
牛皮。。。。。。。
这里设置的金额不要乱输入,这不是支付宝,还提供沙盒测试…这里的钱是,白花花的银子!!
美团会员充值
外卖下单
电影票购买...
微信支付模块!
而 对于这种多种支付的类型,却一般都需要一个 回调函数!如何处理?
解决方案:
RabbitMQ 来实现!
回调方法
对成功的订单进行处理!
更改用户状态会员续费
/ 发起商家接单服务!
交换机/队列
发送请求 , 不同的模块,不同的队列监听!
支付回调
却互不干涉!
如何,指定回调时候,要发送的交换机/队列?
attach
交换机/队列!
交换机/队列信息....
支付模块同一下单方法:
public Map<String, String> pay(String out_trade_no, String total_fee,String exchange,String routingKey,String username) throws Exception {
Map<String, String> param = new HashMap<>();
param.put("appid", appid); //应用ID
param.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"); //终端IP
param.put("notify_url", notifyurl); //回调地址
param.put("trade_type", "NATIVE"); //交易类型
Map<String,String> attach = new HashMap<>(); //附加数据, 用户回调时,区分不同的订单队列
attach.put("exchange",exchange);
attach.put("routingKey",routingKey);
attach.put("username",username);
param.put("attach",JSON.toJSONString(attach)); //String类型... Map转换成JSON字符!
//发送请求....
String xml = WXPayUtil.generateSignedXml(param, partnerkey);
String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
HttpClient httpClient = new HttpClient(url);
httpClient.setXmlParam(xml);
httpClient.setHttps(true);
httpClient.post();
String content = httpClient.getContent();
System.out.println(content);
Map<String, String> stringStringMap = WXPayUtil.xmlToMap(content);
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"));
return result;
}
微服服务器,提供的Post 回调方法:
@PostMapping("/notifyurl")
public String notifyurl(HttpServletRequest request) {
//省略代码....
//获取attach 附加属性,JSON字符...
String str = new String(baos.toByteArray(), "UTF-8");
System.out.println(str);
Map<String, String> stringStringMap = WXPayUtil.xmlToMap(str);
String id = stringStringMap.get("out_trade_no");
String attachStr= stringStringMap.get("attach");
Map<String,String> mqMap = JSON.parseObject(attachStr,Map.class);
String exchange = mqMap.get("exchange");
String routingKey = mqMap.get("routingKey");
String username = mqMap.get("username");
//给指定交换机,队列,发送数据...
payService.updateStatus(id,exchange,routingKey,username);
//省略代码....支付成功回调,表示成功.... 详情代码参考上面!
Map respMap = new HashMap();
respMap.put("return_code", "SUCCESS");
respMap.put("return_msg", "OK");
return WXPayUtil.mapToXml(respMap);
}
接下来,不同的模块在开启对应交换机/队列的监听,并进行对应的处理操作即可!