package com.sky.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.properties.WeChatProperties;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
/**
* 微信支付工具类
*/
@Component
public class WeChatPayUtil {
//微信支付下单接口地址
public static final String JSAPI = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
//申请退款接口地址
public static final String REFUNDS = "https://api.mch.weixin.qq.com/v3/refund/domestic/refunds";
@Autowired
private WeChatProperties weChatProperties;
/**
* 获取调用微信接口的客户端工具对象
*
* @return
*/
private CloseableHttpClient getClient() {
PrivateKey merchantPrivateKey = null;
try {
//merchantPrivateKey商户API私钥,如何加载商户API私钥请看常见问题
merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(new File(weChatProperties.getPrivateKeyFilePath())));
//加载平台证书文件
X509Certificate x509Certificate = PemUtil.loadCertificate(new FileInputStream(new File(weChatProperties.getWeChatPayCertFilePath())));
//wechatPayCertificates微信支付平台证书列表。你也可以使用后面章节提到的“定时更新平台证书功能”,而不需要关心平台证书的来龙去脉
List<X509Certificate> wechatPayCertificates = Arrays.asList(x509Certificate);
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(weChatProperties.getMchid(), weChatProperties.getMchSerialNo(), merchantPrivateKey)
.withWechatPay(wechatPayCertificates);
// 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签
CloseableHttpClient httpClient = builder.build();
return httpClient;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* 发送post方式请求
*
* @param url
* @param body
* @return
*/
private String post(String url, String body) throws Exception {
CloseableHttpClient httpClient = getClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.toString());
httpPost.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
httpPost.addHeader("Wechatpay-Serial", weChatProperties.getMchSerialNo());
httpPost.setEntity(new StringEntity(body, "UTF-8"));
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
String bodyAsString = EntityUtils.toString(response.getEntity());
return bodyAsString;
} finally {
httpClient.close();
response.close();
}
}
/**
* 发送get方式请求
*
* @param url
* @return
*/
private String get(String url) throws Exception {
CloseableHttpClient httpClient = getClient();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.toString());
httpGet.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
httpGet.addHeader("Wechatpay-Serial", weChatProperties.getMchSerialNo());
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
String bodyAsString = EntityUtils.toString(response.getEntity());
return bodyAsString;
} finally {
httpClient.close();
response.close();
}
}
/**
* jsapi下单
*
* @param orderNum 商户订单号
* @param total 总金额
* @param description 商品描述
* @param openid 微信用户的openid
* @return
*/
private String jsapi(String orderNum, BigDecimal total, String description, String openid) throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("appid", weChatProperties.getAppid());
jsonObject.put("mchid", weChatProperties.getMchid());
jsonObject.put("description", description);
jsonObject.put("out_trade_no", orderNum);
jsonObject.put("notify_url", weChatProperties.getNotifyUrl());
JSONObject amount = new JSONObject();
amount.put("total", total.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue());
amount.put("currency", "CNY");
jsonObject.put("amount", amount);
JSONObject payer = new JSONObject();
payer.put("openid", openid);
jsonObject.put("payer", payer);
String body = jsonObject.toJSONString();
return post(JSAPI, body);
}
/**
* 小程序支付
*
* @param orderNum 商户订单号
* @param total 金额,单位 元
* @param description 商品描述
* @param openid 微信用户的openid
* @return
*/
public JSONObject pay(String orderNum, BigDecimal total, String description, String openid) throws Exception {
//统一下单,生成预支付交易单
String bodyAsString = jsapi(orderNum, total, description, openid);
//解析返回结果
JSONObject jsonObject = JSON.parseObject(bodyAsString);
System.out.println(jsonObject);
String prepayId = jsonObject.getString("prepay_id");
if (prepayId != null) {
String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
String nonceStr = RandomStringUtils.randomNumeric(32);
ArrayList<Object> list = new ArrayList<>();
list.add(weChatProperties.getAppid());
list.add(timeStamp);
list.add(nonceStr);
list.add("prepay_id=" + prepayId);
//二次签名,调起支付需要重新签名
StringBuilder stringBuilder = new StringBuilder();
for (Object o : list) {
stringBuilder.append(o).append("\n");
}
String signMessage = stringBuilder.toString();
byte[] message = signMessage.getBytes();
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(PemUtil.loadPrivateKey(new FileInputStream(new File(weChatProperties.getPrivateKeyFilePath()))));
signature.update(message);
String packageSign = Base64.getEncoder().encodeToString(signature.sign());
//构造数据给微信小程序,用于调起微信支付
JSONObject jo = new JSONObject();
jo.put("timeStamp", timeStamp);
jo.put("nonceStr", nonc
没有合适的资源?快使用搜索试试~ 我知道了~
sky-take-out阶段性工程代码(day04-套餐之分页查询、新增套餐、修改套餐、启用禁用套餐)
共325个文件
class:135个
java:107个
xml:16个
需积分: 0 0 下载量 5 浏览量
2023-11-05
22:33:27
上传
评论
收藏 408KB ZIP 举报
温馨提示
sky-take-out阶段性工程代码(day04-套餐之分页查询、新增套餐、修改套餐、启用禁用套餐)。但是发现前端代码貌似有问题,所以前后端联调还是会有问题。
资源推荐
资源详情
资源评论
收起资源包目录
sky-take-out阶段性工程代码(day04-套餐之分页查询、新增套餐、修改套餐、启用禁用套餐) (325个子文件)
0957efd8c91291a6dd8e719fd3317e9e6fcc46 701B
09e122416c0ddce1968f33ff34191218223584 77B
19390fd05edd15741c4f132d4c25b2a2fef88f 952B
19e6ee6103d1923726c42e94a3020ed6ca4a7e 615B
1ca80bbe0a8b82741b0d0ebb39d9897c86cea3 2KB
1eea5aa5cab9b9a6fd984d49b22ac0cfab6531 766B
3039c0907e0d3e23f3dac55aa289f818c50a4c 46B
3187362742b9abd5418c263f33ce5b05d58c14 189B
3515c20ac6256d146dd70786e2997255d638e4 221B
3664dbdd6afe513a6869eee49f79411834d089 225B
38e5ef1ff2ed74438aebc3f798cc44d0bea45f 445B
42b1644b6e22918e103a7663dbb753619e78ae 313B
4848f8c2198701e1983664c727d8e4fc450915 68B
5530ea166d31a0fdc84a9e0b4db5cf2f38e652 2KB
666a33be2bf31bcefe80f90c533ab6e27c75b7 182B
71eeaaa3be0ed72781174208ec9e027e70a723 218B
76cb9bc5469535e159473ae3df5b3c9632203d 121B
7ec55fee7d92161abe66410ea8c37dd9061dec 858B
7f50cff56f4841de1ab65b011f291e08591e91 966B
800a196fd115466b2306388d12e9fd1d2da764 76B
8395ba3393fb558ec4c4a079ac73ba1788e0d9 163B
849e84fda27b79f5bdb89fe0da418af43dbb64 45B
8eb2dcd415d440a6f8b0eb111d5e4712887169 500B
90eb957ae21899d064f2927695c45b62d30a5e 79B
9de29bb2d1d6434b8b29ae775ad8c2e48c5391 15B
a5bf5f6b4345524d09114947f634f62d96048e 72B
ad0007470d9e2e2ba7935f0e025efbbef57a07 75B
ad4970466d0dfe11d418e222a98190e08e247f 426B
b623a7006ad419b1aa6070f90daf1e08e92d84 81B
Orders.class 14KB
WeChatPayUtil.class 9KB
OrdersDTO.class 8KB
AddressBook.class 8KB
Employee.class 7KB
SetmealVO.class 7KB
Setmeal.class 7KB
Dish.class 7KB
DishVO.class 7KB
SetmealServiceImpl.class 6KB
ShoppingCart.class 6KB
HttpClientUtil.class 6KB
DishServiceImpl.class 6KB
Category.class 6KB
OrderDetail.class 6KB
OrdersSubmitDTO.class 6KB
EmployeeController.class 6KB
WeChatProperties.class 5KB
WebMvcConfiguration.class 5KB
Orders$OrdersBuilder.class 5KB
AliOssUtil.class 5KB
User.class 5KB
SetmealDTO.class 5KB
DishDTO.class 5KB
OrderReportVO.class 5KB
OrdersPageQueryDTO.class 5KB
EmployeeServiceImpl.class 5KB
SetmealDish.class 4KB
DishController.class 4KB
CategoryController.class 4KB
CategoryServiceImpl.class 4KB
BusinessDataVO.class 4KB
OrderOverViewVO.class 4KB
SetmealController.class 4KB
JwtProperties.class 4KB
OrderPaymentVO.class 4KB
EmployeeDTO.class 4KB
Result.class 4KB
EmployeeLoginVO.class 4KB
OrderSubmitVO.class 3KB
AddressBook$AddressBookBuilder.class 3KB
AutoFillAspect.class 3KB
DishFlavor.class 3KB
DishItemVO.class 3KB
SetmealVO$SetmealVOBuilder.class 3KB
Employee$EmployeeBuilder.class 3KB
DishVO$DishVOBuilder.class 3KB
AliOssProperties.class 3KB
Setmeal$SetmealBuilder.class 3KB
SetmealPageQueryDTO.class 3KB
DishPageQueryDTO.class 3KB
Dish$DishBuilder.class 3KB
ShoppingCart$ShoppingCartBuilder.class 3KB
CategoryDTO.class 3KB
UserReportVO.class 3KB
UserLoginVO.class 3KB
CommonController.class 3KB
OrderDetail$OrderDetailBuilder.class 3KB
Category$CategoryBuilder.class 3KB
ShopController.class 3KB
ShopController.class 3KB
OrderVO.class 3KB
CategoryPageQueryDTO.class 3KB
OrderStatisticsVO.class 3KB
JacksonObjectMapper.class 3KB
JwtTokenAdminInterceptor.class 2KB
PasswordEditDTO.class 2KB
ShoppingCartDTO.class 2KB
DataOverViewQueryDTO.class 2KB
SetmealOverViewVO.class 2KB
DishOverViewVO.class 2KB
共 325 条
- 1
- 2
- 3
- 4
资源评论
鸡鸭扣
- 粉丝: 3204
- 资源: 28
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功