微信下单

master
litao 2 years ago
parent 3035c389fc
commit a940305f90
  1. 35
      src/main/java/org/springblade/modules/weixin/controller/AppEbLoginController.java
  2. 53
      src/main/java/org/springblade/modules/weixin/utils/WxPayUtils.java

@ -1,36 +1,39 @@
package org.springblade.modules.weixin.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.auth.endpoint.BladeTokenEndPoint;
import org.springblade.modules.weixin.config.WechatPayValidator;
import org.springblade.modules.weixin.entity.WeChatPhone;
import org.springblade.modules.weixin.entity.WeChatPhoneInfo;
import org.springblade.modules.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.service.IWeChatUserService;
import org.springblade.modules.weixin.utils.WeChatUtil;
import org.springblade.modules.weixin.utils.WxPayUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
@RestController
@Api(tags = "登录-小程序")
@AllArgsConstructor
@RequestMapping("/app")
@Slf4j
public class AppEbLoginController {
private final IWeChatUserService weChatUserService;
@ -94,21 +97,13 @@ public class AppEbLoginController {
return R.data(phoneInfo.getPurePhoneNumber());
}
/**
* 微信下单
* @param openId
*/
@PostMapping("/createOrder")
public R createOrder(String openId) {
RestTemplate restTemplate = new RestTemplate();
String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
String content = JSON.toJSONString(WxPayUtils.buildWxJsApiV3PayJson(openId, 0));
HttpEntity<String> httpEntity = new HttpEntity<>(content, headers);
// 发送post请求,并输出结果
String s = restTemplate.postForObject(url, httpEntity, String.class);
return R.data(s);
// return R.data(WxPayUtils.createOrderJSApiV3(openId));
public R createOrder(@RequestParam String openId,@RequestParam String outTradeNo) {
return R.data(WxPayUtils.getPayParam(openId,outTradeNo));
}
}

@ -34,10 +34,10 @@ public class WxPayUtils {
//微信下单回调地址,支付成功后会回调这个地址。
//对应文档 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_5.shtml
public static String callbackUri = "http://www.baidu.com";
public static String callbackUri = "";
//APIv3密钥 在微信支付商户平台-账户中心-API安全里设置,如果设置了不知道,就只有修改
public static String apiV3Key = "2q5w8e3a6s9d1eeeeee8p5M6N9a09";
public static String apiV3Key = "YLJJyilvjiaju3613369520230822YLJ";
// 第三方用户唯一凭证密钥,即appsecret 微信公众平台-基本配置-开发者密码(AppSecret) 公众号基本配置里查询
public static String appSecret = "859df8b167e74223e9237dee1b344524";
@ -47,11 +47,36 @@ public class WxPayUtils {
// 商户私钥 获取对应的签名需要 在微信支付商户平台-账户中心-API安全里设置 养宠物那边复制的
// 对应文档 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/open/pay/chapter2_1.shtml
private static String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC3qbz0hDWoilpKR6vYnyi4q+CCjmQUjZVIO9R9QE3OfU4qVIaUYavTL/QReO9QfMZcU3LN8JrfKBRedleBfObXXVTP9e1FZh078LEYOiI9Kdzipf3NvSzjaJqkweMe7gOaGWj60zUBQyIVN9+kQVIcqA/2oNgq3Pz88fRFaOgV91okPTj4yfE5mV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AMQKBgQDu6g0BS6Qe/byHtyHu" +
"pS2KDtmtIB4Bcy5zksV1v/pmt/FIKAv1ar93Iyao5rAYlFr/KmxU1M7kSg2hiINF" +
"ZsS+d44DwYQweVRR74PQQrxj0IP33/bCy53GxF4nWDTDHdA16tbq3OVdAlz506Ex" +
"40paFuwTNMxaInp7W6tacZcg6wKBgQDcRRJXYkFCxDBWvx8j7bc59f5ZiNwDamxq" +
"ELk7SpfSE4oB5LaA1bQzNZ+F9kPVgtiRtUjTNJYNbIeHjDzRQ9wxsO3ojcooa7V6" +
"JQGJQfGDiftiJEM3pw8xO14ocakDS87cel+sA5MhguBnQl65bFrTE7jXmGRSME4g" +
"MWNCC/Wq2QKBgQCW8P4IbH6jKdG7uKLSgD8WMy2paFostlWJpRflvcV4sgvZd5ue" +
"kLOolGevXBezX79h6K6KpeyUZIjaHkI5TaVNZIEvtEpMycrQ/U68+/zD3xARQclH" +
"5pam5pdJqjujbimGdvkTZz3tljmhiOgVebEW29aIh9n3tELHvvGycYN2wwKBgGkA" +
"QCsJuoRiU4y2lNiitApcOoBh3kd+junMyQ/7kf3htJMMfPdTaBBVSpMoftOkCSsE" +
"WQ/dbH4/syepcUBsceX6Kp8GIE0+cwcj5uSQW9nRh9UT5jr8VyCn5U2wSNo6ZAT0" +
"DPFJzvRVmDWBDPilL3KZXy7fBUusnUF5aZCNdPGZAoGBAO5So8Qjq0zZPD2wRmEk" +
"JTknPKY9wTpdz7fXPMrIeeOyvGnMfdpphUc069JdZlnscy7hJsNoR8y4vD6RVqJc" +
"wyPwPv2KWLGIs+bePwtiiVwGZPjQsUrrMuX4YXpI6WwRwAVpwA+rZuh+c8KXjYVe" +
"G7twjd+H3FFCyQp5UCPhfxla";
//商户证书序列号
// 对应文档 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/open/pay/chapter2_1.shtml
private static String mchSerialNo = "23BC1D2F580D5eeeeeeF63D95837CC5";
private static String mchSerialNo = "492C7FD9F594909FE284B0788B7EC68C4EF9ABB8";
private static CloseableHttpClient httpClient;
@ -97,12 +122,12 @@ public class WxPayUtils {
* 官方文档路径
* https://pay.weixin.qq.com/wiki/doc/apiv3_partner/open/pay/chapter2_1.shtml
*/
public static String createOrderJSApiV3(String openId) {
public static String createOrderJSApiV3(String openId, String outTradeNo) {
payLoading();
//请求URL
HttpPost httpPost = new HttpPost(JSApiPayUrl);
// 请求body参数
StringEntity entity = new StringEntity(JSONObject.toJSONString(buildWxJsApiV3PayJson(openId, 0)), "utf-8");
StringEntity entity = new StringEntity(JSONObject.toJSONString(buildWxJsApiV3PayJson(openId, outTradeNo, 0)), "utf-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
@ -171,8 +196,8 @@ public class WxPayUtils {
* @param openId openId
* @return 支付参数
*/
public static Object getPayParam(String openId) {
JSONObject jsonObject = JSON.parseObject(createOrderJSApiV3(openId));
public static Object getPayParam(String openId, String outTradeNo) {
JSONObject jsonObject = JSON.parseObject(createOrderJSApiV3(openId, outTradeNo));
return getTokenWeiXin(jsonObject.getString("prepay_id"));
}
@ -186,8 +211,7 @@ public class WxPayUtils {
* @param openId 用户ID onAky51Fojn3NoLrnKwcY
* @return JSONObject
*/
public static JSONObject buildWxJsApiV3PayJson(String description, String amount, String openId) {
public static JSONObject buildWxJsApiV3PayJson(String description, String amount, String openId, String outTradeNo) {
//订单金额json
JSONObject amountJson = new JSONObject();
amountJson.put("total", Integer.valueOf(amount));
@ -205,7 +229,8 @@ public class WxPayUtils {
json.put("mchid", mchId);
json.put("description", description);
// 商户订单号 自己生成
json.put("out_trade_no", generateNonceStr());
// json.put("out_trade_no", generateNonceStr());
json.put("out_trade_no", outTradeNo);
json.put("notify_url", callbackUri);
json.put("amount", amountJson);
json.put("payer", payerJson);
@ -217,8 +242,8 @@ public class WxPayUtils {
*
* @return JSONObject
*/
public static JSONObject buildWxJsApiV3PayJson(String openId, Integer goodsId) {
return buildWxJsApiV3PayJson("测试商品", "1", openId);
public static JSONObject buildWxJsApiV3PayJson(String openId, String outTradeNo, Integer goodsId) {
return buildWxJsApiV3PayJson("测试商品", "1", openId, outTradeNo);
}
/**
@ -227,7 +252,7 @@ public class WxPayUtils {
* @return JSONObject
*/
public static JSONObject buildWxJsApiV3PayJson() {
return buildWxJsApiV3PayJson("测试商品", "1", "123123123124");
return buildWxJsApiV3PayJson("测试商品", "1", "123123123124", "");
}
/**

Loading…
Cancel
Save