| | |
| | | package com.yeshi.buwan.controller; |
| | | |
| | | import com.yeshi.buwan.domain.vip.VIPOrderRecord; |
| | | import com.yeshi.buwan.exception.vip.VIPException; |
| | | import com.yeshi.buwan.service.inter.vip.VIPService; |
| | | import com.yeshi.buwan.util.StringUtil; |
| | | import com.yeshi.buwan.util.log.LoggerUtil; |
| | | import com.yeshi.buwan.util.user.VipUtil; |
| | | import com.yeshi.buwan.util.vip.VIPOrderUtil; |
| | | import net.sf.json.JSONObject; |
| | |
| | | import org.springframework.stereotype.Controller; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.yeshi.utils.entity.wx.WXAPPInfo; |
| | | import org.yeshi.utils.wx.WXPayV3Util; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.crypto.Cipher; |
| | |
| | | import java.security.InvalidKeyException; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.util.Base64; |
| | | import java.util.Date; |
| | | |
| | | @Controller |
| | | @RequestMapping("wx") |
| | |
| | | WXAPPInfo wxappInfo = VipUtil.getWXAPP(); |
| | | //验证证书序列号 |
| | | String mchSerialNo = request.getHeader("Wechatpay-Serial"); |
| | | if (!mchSerialNo.equalsIgnoreCase(wxappInfo.getMchSerialNo())) { |
| | | throw new Exception("证书序列号不一致"); |
| | | } |
| | | LoggerUtil.getTestLogger().info("微信支付mchSerialNo:{}",mchSerialNo); |
| | | // if (!mchSerialNo.equalsIgnoreCase(wxappInfo.getMchSerialNo())) { |
| | | // throw new Exception("证书序列号不一致"); |
| | | // } |
| | | |
| | | String timeStamp = request.getHeader("Wechatpay-Timestamp"); |
| | | String nonce = request.getHeader("Wechatpay-Nonce"); |
| | |
| | | if (request.getInputStream() != null) { |
| | | String entity = IOUtils.toString(request.getInputStream(), "UTF-8"); |
| | | data = entity; |
| | | logger.error("微信支付回调entity:" + entity); |
| | | LoggerUtil.getTestLogger().info("微信支付回调entity:{}",entity); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | |
| | | String ciphertext = resource.optString("ciphertext"); |
| | | String r = decryptToString(wxappInfo.getApiV3Key(), resource.optString("associated_data"), resource.optString("nonce"), ciphertext); |
| | | //解密格式如下 {"mchid":"1520950211","appid":"wxa99686bb65a9f466","out_trade_no":"buwan-vip-8","transaction_id":"4200000796202101259681241680","trade_type":"MWEB","trade_state":"SUCCESS","trade_state_desc":"支付成功","bank_type":"OTHERS","attach":"","success_time":"2021-01-25T16:18:33+08:00","payer":{"openid":"oq7R20lxhKF8qSnkszxFJHViyKEY"},"amount":{"total":10,"payer_total":10,"currency":"CNY","payer_currency":"CNY"}} |
| | | LoggerUtil.getTestLogger().info("数据解码:{}",r); |
| | | JSONObject decript = JSONObject.fromObject(r); |
| | | String outTradeNo = decript.optString("out_trade_no"); |
| | | String appId = decript.optString("appid"); |
| | |
| | | String tradeState = decript.optString("trade_state"); |
| | | //支付成功 |
| | | if (tradeState.equalsIgnoreCase("SUCCESS")) { |
| | | boolean isPaySuccess = WXPayV3Util.isPaySuccess(outTradeNo, wxappInfo); |
| | | if (isPaySuccess) { |
| | | BigDecimal money = new BigDecimal(decript.optJSONObject("amount").optInt("total")).divide(new BigDecimal(100), 2, RoundingMode.FLOOR); |
| | | String id = VIPOrderUtil.getIdFromOutOrderNo(outTradeNo); |
| | | try { |
| | | vipService.paySuccess(id, VIPOrderRecord.PAY_WAY_WX, money, new Date()); |
| | | } catch (VIPException e) { |
| | | logger.error("订单支付成功处理出错:{}", id, e); |
| | | } |
| | | } |
| | | String id = VIPOrderUtil.getIdFromOutOrderNo(outTradeNo); |
| | | vipService.checkOrderPayState(id); |
| | | } |
| | | |
| | | break; |
| | | } |
| | | } |
| | |
| | | data.put("message", "处理成功"); |
| | | response.sendError(200, data.toString()); |
| | | } catch (Exception e) { |
| | | logger.error("微信支付回调处理出错:",e); |
| | | e.printStackTrace(); |
| | | JSONObject data = new JSONObject(); |
| | | data.put("code", "FAIL"); |