微信支付
parent
22a98befb6
commit
137ba5e35b
|
@ -10,6 +10,8 @@ import org.springframework.stereotype.Component;
|
||||||
public class WechatPayConfig {
|
public class WechatPayConfig {
|
||||||
/** 应用ID */
|
/** 应用ID */
|
||||||
private String appId;
|
private String appId;
|
||||||
|
/** 小程序秘钥 */
|
||||||
|
private String appSecret;
|
||||||
/** 商户ID */
|
/** 商户ID */
|
||||||
private String mchId;
|
private String mchId;
|
||||||
/** 秘钥 */
|
/** 秘钥 */
|
||||||
|
@ -18,4 +20,62 @@ public class WechatPayConfig {
|
||||||
private String mchSerialNo;
|
private String mchSerialNo;
|
||||||
/** 证书路径 */
|
/** 证书路径 */
|
||||||
private String privateKeyPath;
|
private String privateKeyPath;
|
||||||
|
/** 支付回调地址 */
|
||||||
|
private String payNoticeUrl;
|
||||||
|
|
||||||
|
public String getAppId() {
|
||||||
|
return appId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAppId(String appId) {
|
||||||
|
this.appId = appId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAppSecret() {
|
||||||
|
return appSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAppSecret(String appSecret) {
|
||||||
|
this.appSecret = appSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMchId() {
|
||||||
|
return mchId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMchId(String mchId) {
|
||||||
|
this.mchId = mchId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getApiV3Key() {
|
||||||
|
return apiV3Key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApiV3Key(String apiV3Key) {
|
||||||
|
this.apiV3Key = apiV3Key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMchSerialNo() {
|
||||||
|
return mchSerialNo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMchSerialNo(String mchSerialNo) {
|
||||||
|
this.mchSerialNo = mchSerialNo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrivateKeyPath() {
|
||||||
|
return privateKeyPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrivateKeyPath(String privateKeyPath) {
|
||||||
|
this.privateKeyPath = privateKeyPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPayNoticeUrl() {
|
||||||
|
return payNoticeUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPayNoticeUrl(String payNoticeUrl) {
|
||||||
|
this.payNoticeUrl = payNoticeUrl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,114 @@
|
||||||
|
package com.jwl.driver.server.controller;
|
||||||
|
|
||||||
|
import com.jwl.driver.server.config.WechatPayConfig;
|
||||||
|
import com.jwl.driver.server.response.BaseResponse;
|
||||||
|
import com.jwl.driver.server.util.WechatPayUtil;
|
||||||
|
import com.jwl.driver.server.vo.AppletPayVo;
|
||||||
|
import com.wechat.pay.java.core.Config;
|
||||||
|
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
||||||
|
import com.wechat.pay.java.core.exception.ServiceException;
|
||||||
|
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
|
||||||
|
import com.wechat.pay.java.service.payments.jsapi.model.*;
|
||||||
|
import com.wechat.pay.java.service.payments.model.Transaction;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信支付小程序 接口
|
||||||
|
*/
|
||||||
|
@Api(tags = "小程序支付")
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/applet/pay")
|
||||||
|
@Slf4j
|
||||||
|
public class AppletPayController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private WechatPayConfig wechatPayConfig;
|
||||||
|
|
||||||
|
//生成预支付订单
|
||||||
|
@ApiOperation("生成预支付订单")
|
||||||
|
@PostMapping("/prepay")
|
||||||
|
public BaseResponse createPrepay(@RequestBody AppletPayVo payDto){
|
||||||
|
// 构建service
|
||||||
|
JsapiServiceExtension service = createService();
|
||||||
|
|
||||||
|
// 请求下单参数
|
||||||
|
PrepayRequest request = new PrepayRequest();
|
||||||
|
Amount amount = new Amount();
|
||||||
|
amount.setTotal(Integer.valueOf((payDto.getMoney()*100)+""));
|
||||||
|
request.setAmount(amount);
|
||||||
|
request.setAppid(wechatPayConfig.getAppId());
|
||||||
|
request.setMchid(wechatPayConfig.getMchId());
|
||||||
|
request.setDescription(payDto.getDescription());
|
||||||
|
request.setNotifyUrl(wechatPayConfig.getPayNoticeUrl());
|
||||||
|
request.setOutTradeNo(payDto.getOutTradeNo());
|
||||||
|
|
||||||
|
Payer payer = new Payer();
|
||||||
|
payer.setOpenid(WechatPayUtil.getOpenId(wechatPayConfig.getAppId(), wechatPayConfig.getAppSecret(), payDto.getCode()));
|
||||||
|
// 调用下单方法,得到应答
|
||||||
|
PrepayWithRequestPaymentResponse response = service.prepayWithRequestPayment(request);
|
||||||
|
// 使用微信扫描 code_url 对应的二维码,即可体验Native支付
|
||||||
|
// log.info(response);
|
||||||
|
return BaseResponse.success(response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 构建service */
|
||||||
|
private JsapiServiceExtension createService() {
|
||||||
|
Config config =
|
||||||
|
new RSAAutoCertificateConfig.Builder()
|
||||||
|
.merchantId(wechatPayConfig.getMchId())
|
||||||
|
.privateKeyFromPath(wechatPayConfig.getPrivateKeyPath())
|
||||||
|
.merchantSerialNumber(wechatPayConfig.getMchSerialNo())
|
||||||
|
.apiV3Key(wechatPayConfig.getApiV3Key())
|
||||||
|
.build();
|
||||||
|
JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();
|
||||||
|
|
||||||
|
return service;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//查询订单接口
|
||||||
|
@ApiOperation("查询支付订单")
|
||||||
|
@PostMapping("/queryOrder")
|
||||||
|
public BaseResponse queryOrder(@RequestBody AppletPayVo payDto){
|
||||||
|
//获取openId
|
||||||
|
QueryOrderByOutTradeNoRequest queryRequest = new QueryOrderByOutTradeNoRequest();
|
||||||
|
queryRequest.setMchid(wechatPayConfig.getMchId());
|
||||||
|
//支付订单id
|
||||||
|
queryRequest.setOutTradeNo(payDto.getOutTradeNo());
|
||||||
|
JsapiServiceExtension service = createService();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Transaction result = service.queryOrderByOutTradeNo(queryRequest);
|
||||||
|
System.out.println(result.getTradeState());
|
||||||
|
return BaseResponse.success(result);
|
||||||
|
} catch (ServiceException e) {
|
||||||
|
// API返回失败, 例如ORDER_NOT_EXISTS
|
||||||
|
System.out.printf("code=[%s], message=[%s]\n", e.getErrorCode(), e.getErrorMessage());
|
||||||
|
System.out.printf("reponse body=[%s]\n", e.getResponseBody());
|
||||||
|
return BaseResponse.fail(e.getErrorMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//关闭订单接口
|
||||||
|
@ApiOperation("关闭支付订单")
|
||||||
|
@PostMapping("/closeOrder")
|
||||||
|
public BaseResponse closeOrder(@RequestBody AppletPayVo payDto){
|
||||||
|
JsapiServiceExtension service = createService();
|
||||||
|
CloseOrderRequest closeRequest = new CloseOrderRequest();
|
||||||
|
closeRequest.setMchid(wechatPayConfig.getMchId());
|
||||||
|
closeRequest.setOutTradeNo(payDto.getOutTradeNo());
|
||||||
|
// 方法没有返回值,意味着成功时API返回204 No Content
|
||||||
|
service.closeOrder(closeRequest);
|
||||||
|
return BaseResponse.success();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,113 @@
|
||||||
|
package com.jwl.driver.server.controller;
|
||||||
|
|
||||||
|
import com.jwl.driver.server.config.WechatPayConfig;
|
||||||
|
import com.jwl.driver.server.response.BaseResponse;
|
||||||
|
import com.jwl.driver.server.vo.H5PayVo;
|
||||||
|
import com.wechat.pay.java.core.Config;
|
||||||
|
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
||||||
|
import com.wechat.pay.java.core.exception.ServiceException;
|
||||||
|
import com.wechat.pay.java.service.payments.h5.H5Service;
|
||||||
|
import com.wechat.pay.java.service.payments.h5.model.*;
|
||||||
|
import com.wechat.pay.java.service.payments.model.Transaction;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信支付小程序 接口
|
||||||
|
*/
|
||||||
|
@Api(tags = "H5支付")
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/H5/pay")
|
||||||
|
@Slf4j
|
||||||
|
public class H5PayController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private WechatPayConfig wechatPayConfig;
|
||||||
|
|
||||||
|
//生成预支付订单
|
||||||
|
@ApiOperation("生成预支付订单")
|
||||||
|
@PostMapping("/prepay")
|
||||||
|
public BaseResponse createPrepay(@RequestBody H5PayVo payVo){
|
||||||
|
// 构建service
|
||||||
|
H5Service service = createService();
|
||||||
|
|
||||||
|
// 请求下单参数
|
||||||
|
PrepayRequest request = new PrepayRequest();
|
||||||
|
Amount amount = new Amount();
|
||||||
|
amount.setTotal(Integer.valueOf((payVo.getMoney()*100)+""));
|
||||||
|
request.setAmount(amount);
|
||||||
|
request.setAppid(wechatPayConfig.getAppId());
|
||||||
|
request.setMchid(wechatPayConfig.getMchId());
|
||||||
|
request.setDescription(payVo.getDescription());
|
||||||
|
request.setNotifyUrl(wechatPayConfig.getPayNoticeUrl());
|
||||||
|
request.setOutTradeNo(payVo.getOutTradeNo());
|
||||||
|
//场景参数
|
||||||
|
SceneInfo sceneInfo = new SceneInfo();
|
||||||
|
sceneInfo.setPayerClientIp(payVo.getClientIp());
|
||||||
|
|
||||||
|
// 调用下单方法,得到应答
|
||||||
|
PrepayResponse response = service.prepay(request);
|
||||||
|
// 使用微信扫描 code_url 对应的二维码,即可体验Native支付
|
||||||
|
// log.info(response);
|
||||||
|
return BaseResponse.success(response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private H5Service createService() {
|
||||||
|
Config config =
|
||||||
|
new RSAAutoCertificateConfig.Builder()
|
||||||
|
.merchantId(wechatPayConfig.getMchId())
|
||||||
|
.privateKeyFromPath(wechatPayConfig.getPrivateKeyPath())
|
||||||
|
.merchantSerialNumber(wechatPayConfig.getMchSerialNo())
|
||||||
|
.apiV3Key(wechatPayConfig.getApiV3Key())
|
||||||
|
.build();
|
||||||
|
H5Service service = new H5Service.Builder().config(config).build();
|
||||||
|
|
||||||
|
return service;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//查询订单接口
|
||||||
|
@ApiOperation("查询支付订单")
|
||||||
|
@PostMapping("/queryOrder")
|
||||||
|
public BaseResponse queryOrder(@RequestBody H5PayVo payVo){
|
||||||
|
//获取openId
|
||||||
|
QueryOrderByOutTradeNoRequest queryRequest = new QueryOrderByOutTradeNoRequest();
|
||||||
|
queryRequest.setMchid(wechatPayConfig.getMchId());
|
||||||
|
//支付订单id
|
||||||
|
queryRequest.setOutTradeNo(payVo.getOutTradeNo());
|
||||||
|
H5Service service = createService();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Transaction result = service.queryOrderByOutTradeNo(queryRequest);
|
||||||
|
System.out.println(result.getTradeState());
|
||||||
|
return BaseResponse.success(result);
|
||||||
|
} catch (ServiceException e) {
|
||||||
|
// API返回失败, 例如ORDER_NOT_EXISTS
|
||||||
|
System.out.printf("code=[%s], message=[%s]\n", e.getErrorCode(), e.getErrorMessage());
|
||||||
|
System.out.printf("reponse body=[%s]\n", e.getResponseBody());
|
||||||
|
return BaseResponse.fail(e.getErrorMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//关闭订单接口
|
||||||
|
@ApiOperation("关闭支付订单")
|
||||||
|
@PostMapping("/closeOrder")
|
||||||
|
public BaseResponse closeOrder(@RequestBody H5PayVo payVo){
|
||||||
|
H5Service service = createService();
|
||||||
|
CloseOrderRequest closeRequest = new CloseOrderRequest();
|
||||||
|
closeRequest.setMchid(wechatPayConfig.getMchId());
|
||||||
|
closeRequest.setOutTradeNo(payVo.getOutTradeNo());
|
||||||
|
// 方法没有返回值,意味着成功时API返回204 No Content
|
||||||
|
service.closeOrder(closeRequest);
|
||||||
|
return BaseResponse.success();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,25 @@
|
||||||
package com.jwl.driver.server.controller;
|
package com.jwl.driver.server.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.jwl.driver.server.config.WechatPayConfig;
|
||||||
|
import com.jwl.driver.server.response.BaseResponse;
|
||||||
|
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
||||||
|
import com.wechat.pay.java.core.notification.NotificationConfig;
|
||||||
|
import com.wechat.pay.java.core.notification.NotificationParser;
|
||||||
|
import com.wechat.pay.java.core.notification.RequestParam;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.springframework.http.HttpRequest;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.servlet.resource.HttpResource;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -13,8 +29,61 @@ import org.springframework.stereotype.Controller;
|
||||||
* @author Automated procedures
|
* @author Automated procedures
|
||||||
* @since 2023-08-10
|
* @since 2023-08-10
|
||||||
*/
|
*/
|
||||||
|
@Api(tags = "支付回调")
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("//payNoticeLog")
|
@RequestMapping("/payNoticeLog")
|
||||||
public class PayNoticeLogController {
|
public class PayNoticeLogController {
|
||||||
|
@Resource
|
||||||
|
private WechatPayConfig wechatPayConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支付回调接口 本接口不能做验证
|
||||||
|
* @param wechatPayCertificateSerialNumber
|
||||||
|
* @param signature
|
||||||
|
* @param timstamp
|
||||||
|
* @param nonce
|
||||||
|
* @param requestBody
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@ApiOperation("支付回调接口")
|
||||||
|
@PostMapping()
|
||||||
|
public JSONObject payNotice(@RequestHeader("Wechatpay-Serial") String wechatPayCertificateSerialNumber,
|
||||||
|
@RequestHeader("Wechatpay-Signature") String signature,
|
||||||
|
@RequestHeader("Wechatpay-Timestamp") String timstamp,
|
||||||
|
@RequestHeader("Wechatpay-Nonce") String nonce,
|
||||||
|
@RequestBody String requestBody){
|
||||||
|
NotificationConfig config = new RSAAutoCertificateConfig.Builder()
|
||||||
|
.merchantId(wechatPayConfig.getMchId())
|
||||||
|
.privateKeyFromPath(wechatPayConfig.getPrivateKeyPath())
|
||||||
|
.merchantSerialNumber(wechatPayConfig.getPrivateKeyPath())
|
||||||
|
.apiV3Key(wechatPayConfig.getApiV3Key())
|
||||||
|
.build();
|
||||||
|
RequestParam requestParam = new RequestParam.Builder()
|
||||||
|
.serialNumber(wechatPayCertificateSerialNumber)
|
||||||
|
.nonce(nonce)
|
||||||
|
.signature(signature)
|
||||||
|
.timestamp(timstamp)
|
||||||
|
// 若未设置signType,默认值为 WECHATPAY2-SHA256-RSA2048
|
||||||
|
|
||||||
|
.body(requestBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 初始化 NotificationParser
|
||||||
|
NotificationParser parser = new NotificationParser(config);
|
||||||
|
|
||||||
|
// 验签并解密报文
|
||||||
|
JSONObject decryptObject = parser.parse(requestParam,JSONObject.class);
|
||||||
|
System.out.println("decryptObject="+decryptObject.toJSONString());
|
||||||
|
|
||||||
|
String trade_state=decryptObject.getString("trade_state");
|
||||||
|
JSONObject jsonResponse = new JSONObject();
|
||||||
|
if(trade_state.equals("SUCCESS")) {
|
||||||
|
//各种业务逻辑
|
||||||
|
}else{
|
||||||
|
//还是各种业务逻辑
|
||||||
|
}
|
||||||
|
jsonResponse.put("code", "SUCCESS");
|
||||||
|
jsonResponse.put("message", "成功");
|
||||||
|
return jsonResponse;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
package com.jwl.driver.server.controller;
|
|
||||||
|
|
||||||
import com.jwl.driver.server.dto.WechatPayDto;
|
|
||||||
import com.jwl.driver.server.response.BaseResponse;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 微信支付 接口
|
|
||||||
*/
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/wechat/pay")
|
|
||||||
public class WechatPayController {
|
|
||||||
|
|
||||||
//生成预支付订单
|
|
||||||
@PostMapping("/prepay")
|
|
||||||
public BaseResponse createPrepay(@RequestBody WechatPayDto payDto){
|
|
||||||
//获取openId
|
|
||||||
|
|
||||||
return BaseResponse.success();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//支付回调接口
|
|
||||||
|
|
||||||
//查询订单接口
|
|
||||||
|
|
||||||
//关闭订单接口
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
|
|
||||||
package com.jwl.driver.server.dto;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class WechatPayDto {
|
|
||||||
|
|
||||||
private Double money;//金额
|
|
||||||
|
|
||||||
private String code;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,266 @@
|
||||||
|
package com.jwl.driver.server.util;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import javax.net.ssl.*;
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.ConnectException;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用http发送方法
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
public class HttpUtils
|
||||||
|
{
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向指定 URL 发送GET方法的请求
|
||||||
|
*
|
||||||
|
* @param url 发送请求的 URL
|
||||||
|
* @return 所代表远程资源的响应结果
|
||||||
|
*/
|
||||||
|
public static String sendGet(String url)
|
||||||
|
{
|
||||||
|
return sendGet(url, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向指定 URL 发送GET方法的请求
|
||||||
|
*
|
||||||
|
* @param url 发送请求的 URL
|
||||||
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||||
|
* @return 所代表远程资源的响应结果
|
||||||
|
*/
|
||||||
|
public static String sendGet(String url, String param)
|
||||||
|
{
|
||||||
|
return sendGet(url, param, "UTF-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向指定 URL 发送GET方法的请求
|
||||||
|
*
|
||||||
|
* @param url 发送请求的 URL
|
||||||
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||||
|
* @param contentType 编码类型
|
||||||
|
* @return 所代表远程资源的响应结果
|
||||||
|
*/
|
||||||
|
public static String sendGet(String url, String param, String contentType)
|
||||||
|
{
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
BufferedReader in = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
String urlNameString = StringUtils.isNotBlank(param) ? url + "?" + param : url;
|
||||||
|
log.info("sendGet - {}", urlNameString);
|
||||||
|
URL realUrl = new URL(urlNameString);
|
||||||
|
URLConnection connection = realUrl.openConnection();
|
||||||
|
connection.setRequestProperty("accept", "*/*");
|
||||||
|
connection.setRequestProperty("connection", "Keep-Alive");
|
||||||
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||||
|
connection.connect();
|
||||||
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
|
||||||
|
String line;
|
||||||
|
while ((line = in.readLine()) != null)
|
||||||
|
{
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
log.info("recv - {}", result);
|
||||||
|
}
|
||||||
|
catch (ConnectException e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
catch (SocketTimeoutException e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (in != null)
|
||||||
|
{
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向指定 URL 发送POST方法的请求
|
||||||
|
*
|
||||||
|
* @param url 发送请求的 URL
|
||||||
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||||
|
* @return 所代表远程资源的响应结果
|
||||||
|
*/
|
||||||
|
public static String sendPost(String url, String param)
|
||||||
|
{
|
||||||
|
PrintWriter out = null;
|
||||||
|
BufferedReader in = null;
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
log.info("sendPost - {}", url);
|
||||||
|
URL realUrl = new URL(url);
|
||||||
|
URLConnection conn = realUrl.openConnection();
|
||||||
|
conn.setRequestProperty("accept", "*/*");
|
||||||
|
conn.setRequestProperty("connection", "Keep-Alive");
|
||||||
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||||
|
conn.setRequestProperty("Accept-Charset", "utf-8");
|
||||||
|
conn.setRequestProperty("contentType", "utf-8");
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.setDoInput(true);
|
||||||
|
out = new PrintWriter(conn.getOutputStream());
|
||||||
|
out.print(param);
|
||||||
|
out.flush();
|
||||||
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
||||||
|
String line;
|
||||||
|
while ((line = in.readLine()) != null)
|
||||||
|
{
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
log.info("recv - {}", result);
|
||||||
|
}
|
||||||
|
catch (ConnectException e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
catch (SocketTimeoutException e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (out != null)
|
||||||
|
{
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
if (in != null)
|
||||||
|
{
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String sendSSLPost(String url, String param)
|
||||||
|
{
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
String urlNameString = url + "?" + param;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
log.info("sendSSLPost - {}", urlNameString);
|
||||||
|
SSLContext sc = SSLContext.getInstance("SSL");
|
||||||
|
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
|
||||||
|
URL console = new URL(urlNameString);
|
||||||
|
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
|
||||||
|
conn.setRequestProperty("accept", "*/*");
|
||||||
|
conn.setRequestProperty("connection", "Keep-Alive");
|
||||||
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||||
|
conn.setRequestProperty("Accept-Charset", "utf-8");
|
||||||
|
conn.setRequestProperty("contentType", "utf-8");
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.setDoInput(true);
|
||||||
|
|
||||||
|
conn.setSSLSocketFactory(sc.getSocketFactory());
|
||||||
|
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
|
||||||
|
conn.connect();
|
||||||
|
InputStream is = conn.getInputStream();
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
||||||
|
String ret = "";
|
||||||
|
while ((ret = br.readLine()) != null)
|
||||||
|
{
|
||||||
|
if (ret != null && !"".equals(ret.trim()))
|
||||||
|
{
|
||||||
|
result.append(new String(ret.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.info("recv - {}", result);
|
||||||
|
conn.disconnect();
|
||||||
|
br.close();
|
||||||
|
}
|
||||||
|
catch (ConnectException e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
catch (SocketTimeoutException e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
log.error("调用HttpsUtil.sendSSLPost Exception, url=" + url + ",param=" + param, e);
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class TrustAnyTrustManager implements X509TrustManager
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void checkClientTrusted(X509Certificate[] chain, String authType)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkServerTrusted(X509Certificate[] chain, String authType)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public X509Certificate[] getAcceptedIssuers()
|
||||||
|
{
|
||||||
|
return new X509Certificate[] {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class TrustAnyHostnameVerifier implements HostnameVerifier
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean verify(String hostname, SSLSession session)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,28 @@
|
||||||
package com.jwl.driver.server.util;
|
package com.jwl.driver.server.util;
|
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信支付工具
|
* 微信支付工具
|
||||||
*/
|
*/
|
||||||
public class WechatPayUtil {
|
public class WechatPayUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取微信openID
|
||||||
|
* @param appId
|
||||||
|
* @param appSecret
|
||||||
|
* @param jsCode
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getOpenId(String appId, String appSecret, String jsCode){
|
||||||
|
String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appId
|
||||||
|
+"&secret="+appSecret+"&js_code="+jsCode+"&grant_type=authorization_code";
|
||||||
|
String result = HttpUtils.sendGet(url);
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||||
|
if(!jsonObject.isEmpty() && (jsonObject.get("openid")!=null)){
|
||||||
|
return jsonObject.get("openid").toString();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.jwl.driver.server.vo;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("微信支付")
|
||||||
|
public class AppletPayVo {
|
||||||
|
|
||||||
|
@ApiModelProperty("支付金额")
|
||||||
|
private Double money;//金额
|
||||||
|
|
||||||
|
@ApiModelProperty("小程序端 获取的code")
|
||||||
|
private String code;//小程序端 获取的code
|
||||||
|
|
||||||
|
@ApiModelProperty("用户id")
|
||||||
|
private String userId;
|
||||||
|
|
||||||
|
@ApiModelProperty("支付描述")
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
@ApiModelProperty("商户系统的订单号")
|
||||||
|
private String outTradeNo;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.jwl.driver.server.vo;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@ApiModel("H5支付")
|
||||||
|
@Data
|
||||||
|
public class H5PayVo {
|
||||||
|
@ApiModelProperty("支付金额")
|
||||||
|
private Double money;//金额
|
||||||
|
|
||||||
|
@ApiModelProperty("用户id")
|
||||||
|
private String userId;
|
||||||
|
|
||||||
|
@ApiModelProperty("支付描述")
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
@ApiModelProperty("商户系统的订单号")
|
||||||
|
private String outTradeNo;
|
||||||
|
|
||||||
|
@ApiModelProperty("用户的客户端IP")
|
||||||
|
private String clientIp;
|
||||||
|
}
|
|
@ -44,6 +44,7 @@ driver:
|
||||||
- /driver-api/tdSysUser/code
|
- /driver-api/tdSysUser/code
|
||||||
- /tdQuestion/duima/list
|
- /tdQuestion/duima/list
|
||||||
- /tdQuestion/duima/update
|
- /tdQuestion/duima/update
|
||||||
|
- /payNoticeLog
|
||||||
|
|
||||||
# 需要权限校验url集合
|
# 需要权限校验url集合
|
||||||
needAuthEndPoints:
|
needAuthEndPoints:
|
||||||
|
@ -60,9 +61,12 @@ message:
|
||||||
|
|
||||||
|
|
||||||
wechatpay:
|
wechatpay:
|
||||||
appId: 'wx756a7425037609fb'
|
appId: wx756a7425037609fb
|
||||||
mchId: '1650477646'
|
appSecret: 3e8053032b16c574e38d554ddd438cfd
|
||||||
apiV3Key: 'JingWuLianJiaKao20120813ZhouHong'
|
mchId: 1650477646
|
||||||
mchSerialNo: '52974C99DFCC518EA2E5AD20C3753E38B924868D'
|
apiV3Key: JingWuLianJiaKao20120813ZhouHong
|
||||||
privateKeyPath: 'classpath*:/wechatPay/**Mapper.xml'
|
mchSerialNo: 52974C99DFCC518EA2E5AD20C3753E38B924868D
|
||||||
|
privateKeyPath: classpath*:/wechatPay/apiclient_key.pem
|
||||||
|
payNoticeUrl: https://jwl.ahduima.com/driver-api/payNoticeLog
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue