package com.example.xiaoshiweixinback.service.weixinpay; import com.alibaba.fastjson.JSONObject; import com.example.xiaoshiweixinback.business.config.XDns; import com.example.xiaoshiweixinback.business.exception.BusinessException; import com.example.xiaoshiweixinback.business.exception.ExceptionEnum; import com.example.xiaoshiweixinback.business.utils.BatchNoUtil; import com.example.xiaoshiweixinback.business.utils.CacheUtil; import com.example.xiaoshiweixinback.business.utils.FormatUtil; import com.example.xiaoshiweixinback.business.utils.LoginUtils; import com.example.xiaoshiweixinback.domain.Order; import com.example.xiaoshiweixinback.entity.vo.PersonnelVO; import com.example.xiaoshiweixinback.entity.weixinPay.GetAuthorizationVO; import com.example.xiaoshiweixinback.entity.weixinPay.GetPayTicketVO; import com.example.xiaoshiweixinback.entity.weixinPay.JsApiDTO; import com.example.xiaoshiweixinback.entity.weixinPay.WeiXinPayDTO; import com.example.xiaoshiweixinback.service.OrderService; import com.google.gson.Gson; import okhttp3.*; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.util.Objects; import java.util.UUID; import java.util.concurrent.TimeUnit; @Service public class WeixinPayService { /** * 商户号 */ @Autowired private AuthorizationService authorizationService; @Autowired private CacheUtil cacheUtil; @Autowired private OrderService orderService; public static String merchantId = "1673179188"; /** * appId */ public static String appId = "wxaefb842bd0b93ff0"; public static String signType = "RSA"; /** * 下订单接口 * * @return * @throws Exception */ @Transactional(rollbackFor = Exception.class) public GetPayTicketVO getPayTickets(WeiXinPayDTO weiXinPayDTO) throws Exception { GetPayTicketVO getPayTicketVO = new GetPayTicketVO(); String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi"; String tradeNo = BatchNoUtil.getOrderNo(); PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken()); Order order = orderService.addOrder(weiXinPayDTO, tradeNo, personnelVO); if(order.getTruePrice()<=0){ String orderTradeNo =order.getTradeNo(); orderService.payBack(orderTradeNo); return null; } JsApiDTO jsApiDTO = this.loadJsApiDTO(order, tradeNo); OkHttpClient okHttpClient = new OkHttpClient(); String param = new Gson().toJson(jsApiDTO); GetAuthorizationVO authorizationVO = authorizationService.getAuthorization("POST", "/v3/pay/transactions/jsapi", param); BeanUtils.copyProperties(authorizationVO, getPayTicketVO); getPayTicketVO.setAppId(appId); getPayTicketVO.setSignType(signType); String authorization = authorizationVO.getAuthorization(); RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param); Request request = new Request.Builder() .addHeader("Authorization", authorization) .addHeader("Accept", "*/*") .url(url) .post(requestBody) .build(); // 发送请求获取响应 try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { // 打印服务端返回结果 String a = Objects.requireNonNull(response.body()).string(); JSONObject jsonObject = JSONObject.parseObject(a); getPayTicketVO.setPrepayId(jsonObject.get("prepay_id").toString()); GetAuthorizationVO authorizationVO1 = authorizationService.getFrontAuthorization(getPayTicketVO.getPrepayId()); getPayTicketVO.setTimestamp(authorizationVO1.getTimestamp()); getPayTicketVO.setNonceStr(authorizationVO1.getNonceStr()); getPayTicketVO.setSignature(authorizationVO1.getSignature()); return getPayTicketVO; } } catch (IOException e) { e.printStackTrace(); } return null; } private JsApiDTO loadJsApiDTO(Order order, String tradeNo) { JsApiDTO jsApiDTO = new JsApiDTO(); jsApiDTO.setAppid(appId); jsApiDTO.setDescription("会员"); jsApiDTO.setMchid(merchantId); jsApiDTO.setOut_trade_no(tradeNo); JsApiDTO.Amount amount = new JsApiDTO.Amount(); amount.setCurrency("CNY"); Integer price =0; if(order.getTruePrice()!=null){ Double truePrice =order.getTruePrice()*100; price =truePrice.intValue(); } amount.setTotal(price); jsApiDTO.setAmount(amount); JsApiDTO.Payer payer = new JsApiDTO.Payer(); jsApiDTO.setNotify_url("https://xsip.cn/xiaoshi-weixinback/weixinpay/success"); PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken()); payer.setOpenid(personnelVO.getOpenId()); jsApiDTO.setPayer(payer); return jsApiDTO; } }