目录:
(1)预约挂号-订单支付(生成二维码)-接口开发
(2)预约挂号-订单支付(生成二维码)-前端整合
(3)预约挂号-(订单支付处理支付结果)-前端整合
(4)预约挂号-订单支付-(处理支付结果)-接口开发
(1)预约挂号-订单支付(生成二维码)-接口开发
微信扫码支付是商户系统按微信支付协议生成支付二维码,用户再用微信“扫一扫”完成支付的模式。该模式适用于PC网站支付、实体店单品或订单支付、媒体广告支付等场景。
第一步:注册公众号(类型须为:服务号)
请根据营业执照类型选择以下主体注册:个体工商户| 企业/公司| 政府| 媒体| 其他类型。
第二步:认证公众号
公众号认证后才可申请微信支付,认证费:300元/年。
第三步:提交资料申请微信支付
登录公众平台,点击左侧菜单【微信支付】,开始填写资料等待审核,审核时间为1-5个工作日内。
第四步:开户成功,登录商户平台进行验证
资料审核通过后,请登录联系人邮箱查收商户号和密码,并登录商户平台填写财付通备付金打的小额资金数额,完成账户验证。
第五步:在线签署协议
本协议为线上电子协议,签署后方可进行交易及资金结算,签署完立即生效。
微信支付接口调用的整体思路:
按API要求组装参数,以XML方式发送(POST)给微信支付接口(URL),微信支付接口也是以XML方式给予响应。程序根据返回的结果(其中包括支付URL)生成二维码或判断订单状态。
微信支付SDK
添加依赖
<dependency> <groupId>com.github.wxpaygroupId> <artifactId>wxpay-sdkartifactId> <version>0.0.3version> dependency> |
我们主要会用到微信支付SDK的以下功能:
WXPayUtil.generateNonceStr() |
2.MAP转换为XML字符串(自动添加签名)
WXPayUtil.generateSignedXml(param, partnerkey) |
3.XML字符串转换为MAP
WXPayUtil.xmlToMap(result) |
场景:用户扫描商户展示在各种场景的二维码进行支付
使用案例:
线下:家乐福超市、7-11便利店、上品折扣线下店等
线上:大众点评网站、携程网站、唯品会、美丽说网站等
开发模式:
模式一:商户在后台给你生成二维码,用户打开扫一扫
模式二:商户后台系统调用微信支付【统一下单API】生成预付交易,将接口返回的链接生成二维码,用户扫码后输入密码完成支付交易。注意:该模式的预付单有效期为2小时,过期后无法支付。
微信支付:生成xml发送请求
操作模块:service-order
在service_order模块:先引入微信支付的依赖
在配置文件加入:redis的配置,微信的配置:
#Redis的配置
spring.redis.host=192.168.23.128
spring.redis.port=6379
spring.redis.database= 0
spring.redis.timeout=1800000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
#关联的公众号appid
weixin.appid=wx74862e0dfcf69954
#商户号
weixin.partner=1558950191
#商户key
weixin.partnerkey=T6m9iK73b0kn9g5v426MKfHQH7X8rKwb
引入工具类:
工具类读取配置文件,进行赋值ConstantPropertiesUtils :
package com.atguigu.yygh.order.utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ConstantPropertiesUtils implements InitializingBean {
@Value("${weixin.appid}")
private String appid;
@Value("${weixin.partner}")
private String partner;
@Value("${weixin.partnerkey}")
private String partnerkey;
public static String APPID;
public static String PARTNER;
public static String PARTNERKEY;
public static String CERT;
@Override
public void afterPropertiesSet() throws Exception {
APPID = appid;
PARTNER = partner;
PARTNERKEY = partnerkey;
}
}
HttpClient:
package com.atguigu.yygh.order.utils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.SSLContextBuilder;
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.SSLContexts;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* http请求客户端
*
* @author qy
*
*/
public class HttpClient {
private String url;
private Map param;
private int statusCode;
private String content;
private String xmlParam;
private boolean isHttps;
private boolean isCert = false;
//证书密码 微信商户号(mch_id)
private String certPassword;
public boolean isHttps() {
return isHttps;
}
public void setHttps(boolean isHttps) {
this.isHttps = isHttps;
}
public boolean isCert() {
return isCert;
}
public void setCert(boolean cert) {
isCert = cert;
}
public String getXmlParam() {
return xmlParam;
}
public void setXmlParam(String xmlParam) {
this.xmlParam = xmlParam;
}
public HttpClient(String url, Map param) {
this.url = url;
this.param = param;
}
public HttpClient(String url) {
this.url = url;
}
public String getCertPassword() {
return certPassword;
}
public void setCertPassword(String certPassword) {
this.certPassword = certPassword;
}
public void setParameter(Map map) {
param = map;
}
public void addParameter(String key, String value) {
if (param == null)
param = new HashMap();
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 nvps = new LinkedList();
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) {
if(isCert) {
//TODO 需要完善
FileInputStream inputStream = new FileInputStream(new File(ConstantPropertiesUtils.CERT));
KeyStore keystore = KeyStore.getInstance("PKCS12");
char[] partnerId2charArray = certPassword.toCharArray();
keystore.load(inputStream, partnerId2charArray);
SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
SSLConnectionSocketFactory sslsf =
new SSLConnectionSocketFactory(sslContext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
} else {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
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;
}
}
在这个模块下创建service接口:WeixinService
package com.atguigu.yygh.order.service;
import java.util.Map;
public interface WeixinService {
/**
* 根据订单号下单,生成支付链接
*/
Map createNative(Long orderId);
}
创建controller:WeixinController
package com.atguigu.yygh.order.api;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.order.service.WeixinService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/api/order/weixin")
public class WeixinController {
@Autowired
private WeixinService weixinPayService;
/**
* 下单 生成二维码
*/
@GetMapping("/createNative/{orderId}")
public Result createNative(
@ApiParam(name = "orderId", value = "订单id", required = true)
@PathVariable("orderId") Long orderId) {
Map map=weixinPayService.createNative(orderId);
return Result.ok(map);
}
}
实现类:WeixinServiceImpl:
需要想这个表中加一些支付时信息
先创建创建另外一个 PaymentService :
package com.atguigu.yygh.order.service;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.baomidou.mybatisplus.extension.service.IService;
public interface PaymentService extends IService {
// 向支付记录表添加信息
void savePaymentInfo(OrderInfo order, Integer status);
}
实现类:PaymentServiceImpl :
package com.atguigu.yygh.order.service.impl;
import com.atguigu.yygh.enums.PaymentStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.order.mapper.PaymentMapper;
import com.atguigu.yygh.order.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class PaymentServiceImpl extends
ServiceImpl implements PaymentService {
// 向支付记录表添加信息
@Override
public void savePaymentInfo(OrderInfo order, Integer status) {
//根据订单id和支付类型,查询支付记录表是否存在相同订单
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("order_id",order.getId());
wrapper.eq("payment_type",status);
Integer count = baseMapper.selectCount(wrapper);
if(count > 0) {
return;
}
//添加记录
PaymentInfo paymentInfo = new PaymentInfo();
paymentInfo.setCreateTime(new Date());
paymentInfo.setOrderId(order.getId());
paymentInfo.setPaymentType(status);
paymentInfo.setOutTradeNo(order.getOutTradeNo());
paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
String subject = new DateTime(order.getReserveDate()).toString("yyyy-MM-dd")+"|"+order.getHosname()+"|"+order.getDepname()+"|"+order.getTitle();
paymentInfo.setSubject(subject);
paymentInfo.setTotalAmount(order.getAmount());
baseMapper.insert(paymentInfo);
}
}
创建Mapper:PaymentMapper :
package com.atguigu.yygh.order.mapper;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface PaymentMapper extends BaseMapper {
}
实现类:WeixinServiceImpl:
package com.atguigu.yygh.order.service.impl;
import com.atguigu.yygh.enums.PaymentTypeEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderService;
import com.atguigu.yygh.order.service.PaymentService;
import com.atguigu.yygh.order.service.WeixinService;
import com.atguigu.yygh.order.utils.ConstantPropertiesUtils;
import com.atguigu.yygh.order.utils.HttpClient;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class WeixinServiceImpl implements WeixinService {
@Autowired
private OrderService orderService;
@Autowired
private PaymentService paymentService;
@Autowired
private RedisTemplate redisTemplate;
//生成微信支付二维码
@Override
public Map createNative(Long orderId) {
try {
//从redis获取数据
Map payMap = (Map)redisTemplate.opsForValue().get(orderId.toString());
if(payMap != null) {
return payMap;
}
//1 根据orderId获取订单信息
OrderInfo order = orderService.getById(orderId);
//2 向支付记录表添加信息
paymentService.savePaymentInfo(order, PaymentTypeEnum.WEIXIN.getStatus());
//3设置参数,
//把参数转换xml格式,使用商户key进行加密,加密后微信那边才能认识
Map paramMap = new HashMap();
paramMap.put("appid", ConstantPropertiesUtils.APPID);//公众号appid
paramMap.put("mch_id", ConstantPropertiesUtils.PARTNER);//商户号
paramMap.put("nonce_str", WXPayUtil.generateNonceStr());//唯一的随机字符串
String body = order.getReserveDate() + "就诊"+ order.getDepname();
paramMap.put("body", body);//主体
paramMap.put("out_trade_no", order.getOutTradeNo());//订单编号
//paramMap.put("total_fee", order.getAmount().multiply(new BigDecimal("100")).longValue()+"");
paramMap.put("total_fee", "1"); //支付金额为了测试,统一写成这个值
paramMap.put("spbill_create_ip", "127.0.0.1");//ip地址
paramMap.put("notify_url", "http://guli.shop/api/order/weixinPay/weixinNotify");//回调地址
paramMap.put("trade_type", "NATIVE");//支付类型
//4 调用微信生成二维码接口,httpclient调用
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
//设置map参数:用xml的格式:用微信的类进行转换 使用商户key加密
client.setXmlParam(WXPayUtil.generateSignedXml(paramMap,ConstantPropertiesUtils.PARTNERKEY));
client.setHttps(true);
client.post();
//5 返回相关数据
String xml = client.getContent();
//转换map集合
Map resultMap = WXPayUtil.xmlToMap(xml);
System.out.println("resultMap:"+resultMap);
//6 封装返回结果集
Map map = new HashMap<>();
map.put("orderId", orderId);
map.put("totalFee", order.getAmount());//订单金额
map.put("resultCode", resultMap.get("result_code"));//状态码
map.put("codeUrl", resultMap.get("code_url")); //二维码地址
if(resultMap.get("result_code") != null) {
//把支付的状态存放到Redis中设置失效时间
redisTemplate.opsForValue().set(orderId.toString(),map,120, TimeUnit.MINUTES);
}
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
(2)预约挂号-订单支付(生成二维码)-前端整合
在weixin.js中添加:继续添加js接口
1、安装vue-qriously
npm install vue-qriously
2、引入
在/plugins/myPlugin.js文件添加引入
import VueQriously from 'vue-qriously'
Vue.use(VueQriously) |
在show.vue页面:添加访问方法:
payObi,有二维码地址的部分, 二维码需要在页面进行显示,需要通过一个组件进行下载显示,需要引入插件
点击支付:生成二维码
控制台也会返回内容:
当支付之后呢,我们可以每隔一段时间查询一次支付状态,当支付状态查询之后呢,更新订单信息
(3)预约挂号-(订单支付处理支付结果)-前端整合
在weixin.js中继续添加访问接口:
在show.vue里面继续写方法:
(4)预约挂号-订单支付-(处理支付结果)-接口开发
在WinXinController中继续添加::
//查询支付状态
@GetMapping("queryPayStatus/{orderId}")
public Result queryPayStatus(@PathVariable Long orderId) {
//调用微信接口实现支付状态查询
Map resultMap = weixinService.queryPayStatus(orderId);
//判断
if(resultMap == null) {
return Result.fail().message("支付出错");
}
if("SUCCESS".equals(resultMap.get("trade_state"))) { //支付成功
//更新订单状态
String out_trade_no = resultMap.get("out_trade_no");//订单编码
paymentService.paySuccess(out_trade_no,resultMap);//更新订单
return Result.ok().message("支付成功");
}
return Result.ok().message("支付中");
}
WeixinService 接口: queryPayStatus
package com.atguigu.yygh.order.service;
import java.util.Map;
public interface WeixinService {
/**
* 根据订单号下单,生成支付链接
*/
Map createNative(Long orderId);
//调用微信接口实现支付状态查询
Map queryPayStatus(Long orderId);
}
实现类:WeixinServiceImpl:继续添加
//调用微信接口实现支付状态查询
@Override
public Map queryPayStatus(Long orderId) {
try {
//1 根据orderId获取订单信息
OrderInfo orderInfo = orderService.getById(orderId);
//2 封装提交参数
Map paramMap = new HashMap();
paramMap.put("appid", ConstantPropertiesUtils.APPID);//微信支付公众号id
paramMap.put("mch_id", ConstantPropertiesUtils.PARTNER);//商户号
paramMap.put("out_trade_no", orderInfo.getOutTradeNo());//订单编号
paramMap.put("nonce_str", WXPayUtil.generateNonceStr());//随机生成字符串
//3 设置请求内容 调用微信接口查询支付状态
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");//查询状态路径
client.setXmlParam(WXPayUtil.generateSignedXml(paramMap,ConstantPropertiesUtils.PARTNERKEY));//使用商户的key加密
client.setHttps(true);
client.post();//发送请求
//4 得到微信接口返回数据
String xml = client.getContent();
Map resultMap = WXPayUtil.xmlToMap(xml);
System.out.println("支付状态resultMap:"+resultMap);
//5 把接口数据返回
return resultMap;
}catch(Exception e) {
return null;
}
}
PaymentService 接口: paySuccess
package com.atguigu.yygh.order.service;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.Map;
public interface PaymentService extends IService {
// 向支付记录表添加信息
void savePaymentInfo(OrderInfo order, Integer status);
//更新支付状态
void paySuccess(String out_trade_no, Map resultMap);
}
主要修改这个表:中主要的payment_status值
和订单表:中这个字段order_status
调用医院模块那端的接口
实现类:
//支付成功 更新订单状态
@Override
public void paySuccess(String out_trade_no, Map resultMap) {
//1 根据订单编号得到支付记录
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("out_trade_no",out_trade_no);
wrapper.eq("payment_type", PaymentTypeEnum.WEIXIN.getStatus());
PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
//2 更新支付记录信息
paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());//支付状态
paymentInfo.setCallbackTime(new Date());//时间
paymentInfo.setTradeNo(resultMap.get("transaction_id"));//业务编号
paymentInfo.setCallbackContent(resultMap.toString());//内容
baseMapper.updateById(paymentInfo);
//3 根据订单号得到订单信息
//4 更新订单信息
OrderInfo orderInfo = orderService.getById(paymentInfo.getOrderId());
orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
orderService.updateById(orderInfo);
//5 调用医院接口,更新订单支付信息
SignInfoVo signInfoVo = hospitalFeignClient.getSignInfoVo(orderInfo.getHoscode());//签名
Map reqMap = new HashMap<>();
reqMap.put("hoscode",orderInfo.getHoscode());
reqMap.put("hosRecordId",orderInfo.getHosRecordId());
reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
reqMap.put("sign", sign);
JSONObject result = HttpRequestHelper.sendRequest(reqMap, signInfoVo.getApiUrl() + "/order/updatePayStatus");
//JSONObject result = HttpRequestHelper.sendRequest(reqMap, "http://localhost:9998/order/updatePayStatus");
}
测试:点击支付,扫描升成的二维码:
控制台中有内容输出: