|  |  | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson2.JSON; | 
 |  |  | import com.alibaba.fastjson2.JSONObject; | 
 |  |  | import com.dy.common.multiDataSource.DataSourceContext; | 
 |  |  | import com.dy.common.util.AES; | 
 |  |  | import com.dy.common.webUtil.BaseResponse; | 
 |  |  | import com.dy.common.webUtil.BaseResponseUtils; | 
 |  |  | import com.dy.common.webUtil.ResultCodeMsg; | 
 |  |  | import com.dy.pipIrrGlobal.cert.WxCertUtil; | 
 |  |  | import com.dy.pipIrrGlobal.pojoSe.*; | 
 |  |  | import com.dy.pipIrrGlobal.voSe.VoClient; | 
 |  |  | import com.dy.pipIrrWechat.result.WechatResultCode; | 
 |  |  | 
 |  |  | import com.dy.pipIrrWechat.virtualCard.dto.DtoVirtualCard; | 
 |  |  | import com.dy.pipIrrWechat.virtualCard.enums.LastOperateENUM; | 
 |  |  | import com.dy.pipIrrWechat.virtualCard.enums.RefundItemStateENUM; | 
 |  |  | import com.dy.pipIrrWechat.wechatpay.dto.Code2Session; | 
 |  |  | import com.dy.pipIrrWechat.wechatpay.dto.DtoOrder; | 
 |  |  | import com.dy.pipIrrWechat.wechatpay.dto.NotifyResource; | 
 |  |  | import com.dy.pipIrrWechat.wechatpay.dto.OrderNotify; | 
 |  |  | import com.dy.pipIrrWechat.wechatpay.dto.*; | 
 |  |  | import com.dy.pipIrrWechat.wechatpay.enums.RefundStatusENUM; | 
 |  |  | import io.swagger.v3.oas.annotations.Operation; | 
 |  |  | import io.swagger.v3.oas.annotations.media.Content; | 
 |  |  | 
 |  |  | import jakarta.validation.Valid; | 
 |  |  | import lombok.RequiredArgsConstructor; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.core.io.ResourceLoader; | 
 |  |  | import org.springframework.http.HttpHeaders; | 
 |  |  | import org.springframework.http.MediaType; | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  | import org.springframework.validation.BindingResult; | 
 |  |  | import org.springframework.web.bind.annotation.*; | 
 |  |  |  | 
 |  |  | import javax.crypto.NoSuchPaddingException; | 
 |  |  | import java.io.BufferedReader; | 
 |  |  | import java.io.IOException; | 
 |  |  | import java.security.GeneralSecurityException; | 
 |  |  | 
 |  |  | import java.security.SignatureException; | 
 |  |  | import java.security.spec.InvalidKeySpecException; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.Objects; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @author ZhuBaoMin | 
 |  |  | 
 |  |  | @RequestMapping(path="payment") | 
 |  |  | @RequiredArgsConstructor | 
 |  |  | public class PaymentCtrl { | 
 |  |  |     private final ResourceLoader resourceLoader; | 
 |  |  |     private final PaymentSv paymentSv; | 
 |  |  |     private final RestTemplateUtil restTemplateUtil; | 
 |  |  |     private final PayHelper payHelper; | 
 |  |  |     private final VirtualCardSv virtualCardSv; | 
 |  |  |     private final String privateCertFileName = com.dy.pipIrrWechat.wechatpay.PayInfo.privateCertFileName; | 
 |  |  |     private final String appid = com.dy.pipIrrWechat.wechatpay.PayInfo.appid; | 
 |  |  |     private final String secret = com.dy.pipIrrWechat.wechatpay.PayInfo.secret; | 
 |  |  |     private final String mchid = com.dy.pipIrrWechat.wechatpay.PayInfo.mchid; | 
 |  |  | 
 |  |  |         String nonceStr = payHelper.generateRandomString(); | 
 |  |  |         Long timestamp = System.currentTimeMillis() / 1000; | 
 |  |  |  | 
 |  |  |         String header = schema + " " + payHelper.getToken(method, httpUrl, "", nonceStr, timestamp, privateCertFileName); | 
 |  |  |         byte[] certFileBs = WxCertUtil.getKey_pemBytes(resourceLoader) ; | 
 |  |  |         String header = schema + " " + payHelper.getToken(method, httpUrl, "", nonceStr, timestamp, certFileBs); | 
 |  |  |  | 
 |  |  |         Map<String, String> headers = new HashMap<>(); | 
 |  |  |         headers.put("Authorization", header); | 
 |  |  | 
 |  |  |         // 构造验签名串 | 
 |  |  |         String signatureStr = payHelper.responseSign(wechatpayTimestamp, wechatpayNonce, job_body.toJSONString()); | 
 |  |  |         // 验证签名 | 
 |  |  |         Boolean valid = payHelper.responseSignVerify(wechatpaySerial, signatureStr, wechatpaySignature); | 
 |  |  |         Boolean valid = payHelper.responseSignVerify(wechatpaySerial, signatureStr, wechatpaySignature, certFileBs); | 
 |  |  |  | 
 |  |  |         return BaseResponseUtils.buildSuccess(); | 
 |  |  |     } | 
 |  |  | 
 |  |  |      */ | 
 |  |  |     @PostMapping(path = "placeOrder") | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     public BaseResponse<Boolean> placeOrder(@RequestBody @Valid DtoOrder order, BindingResult bindingResult) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, IOException, SignatureException, InvalidKeyException { | 
 |  |  |     public BaseResponse<Boolean> placeOrder(@RequestBody @Valid DtoOrder order, BindingResult bindingResult) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, SignatureException, InvalidKeyException, Exception { | 
 |  |  |         if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |             return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  |         } | 
 |  |  | 
 |  |  |         virtualCard.setClientId(clientId); | 
 |  |  |         virtualCard.setOrderNumber(orderNumber); | 
 |  |  |         virtualCard.setRechargeAmount(rechargeAmount); | 
 |  |  |         BaseResponse result = virtualCardSv.insertVCRecharge(virtualCard); | 
 |  |  |         if(!result.getCode().equals("0001")) { | 
 |  |  |  | 
 |  |  |         // -1:虚拟卡不存在,0:添加充值记录失败 | 
 |  |  |         Long rechargeId = virtualCardSv.insertVCRecharge(virtualCard); | 
 |  |  |         if(rechargeId.equals(-1)) { | 
 |  |  |             return BaseResponseUtils.buildFail(WechatResultCode.NO_ACCOUNT.getMessage()); | 
 |  |  |         } | 
 |  |  |         if(rechargeId.equals(0)) { | 
 |  |  |             return BaseResponseUtils.buildFail(WechatResultCode.RECHARGE_FAIL.getMessage()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 生成充值操作记录,充值操作人为农户 | 
 |  |  |         SeVcOperate seVcOperate = new SeVcOperate(); | 
 |  |  |         seVcOperate.setVcId(virtualId); | 
 |  |  |         seVcOperate.setClientId(clientId); | 
 |  |  |         seVcOperate.setOperateType(LastOperateENUM.RECHARGE.getCode()); | 
 |  |  |         seVcOperate.setRechargeId(rechargeId); | 
 |  |  |         seVcOperate.setOperator(clientId); | 
 |  |  |         seVcOperate.setOperateTime(new Date()); | 
 |  |  |         Long vcOperateId = virtualCardSv.insertVcOperate(seVcOperate); | 
 |  |  |         if(vcOperateId == null) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(WechatResultCode.RECHARGE_ADD_FAIL.getMessage()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  | 
 |  |  |         job_body.put("mchid", mchid); | 
 |  |  |         job_body.put("description", description); | 
 |  |  |         job_body.put("out_trade_no", orderNumber); | 
 |  |  |         job_body.put("attach", DataSourceContext.get()); | 
 |  |  |         job_body.put("notify_url", notifyUrl); | 
 |  |  |  | 
 |  |  |         //订单金额 | 
 |  |  | 
 |  |  |         String httpUrl = "/v3/pay/transactions/jsapi"; | 
 |  |  |  | 
 |  |  |         String body = job_body.toJSONString(); | 
 |  |  |         String header = schema + " " + payHelper.getToken(method, httpUrl, body, nonceStr, timestamp, privateCertFileName); | 
 |  |  |         byte[] certFileBs = WxCertUtil.getKey_pemBytes(resourceLoader) ; | 
 |  |  |         String header = schema + " " + payHelper.getToken(method, httpUrl, body, nonceStr, timestamp, certFileBs); | 
 |  |  |  | 
 |  |  |         Map<String, String> headers = new HashMap<>(); | 
 |  |  |         headers.put("Authorization", header); | 
 |  |  | 
 |  |  |         String nonceStr = payHelper.generateRandomString(); | 
 |  |  |         String pkg = "prepay_id=" + prepayId; | 
 |  |  |         String message = payHelper.buildMessage_signAgain(appid, timeStamp, nonceStr, pkg); | 
 |  |  |         String paySign = payHelper.sign(message.getBytes("utf-8"), privateCertFileName); | 
 |  |  |         byte[] certFileBs = WxCertUtil.getKey_pemBytes(resourceLoader) ; | 
 |  |  |         String paySign = payHelper.sign(message.getBytes("utf-8"), certFileBs); | 
 |  |  |  | 
 |  |  |         JSONObject job_result = new JSONObject(); | 
 |  |  |         job_result.put("timeStamp", timeStamp); | 
 |  |  | 
 |  |  |     }) | 
 |  |  |     @PostMapping(path = "orderNotify", consumes = MediaType.APPLICATION_JSON_VALUE) | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     public JSONObject orderNotify(@RequestHeader HttpHeaders headers, HttpServletRequest request, HttpServletResponse response) throws IOException, GeneralSecurityException { | 
 |  |  |     public JSONObject orderNotify(@RequestHeader HttpHeaders headers, HttpServletRequest request, HttpServletResponse response) throws IOException, GeneralSecurityException, Exception { | 
 |  |  |         JSONObject result = new JSONObject(); | 
 |  |  |  | 
 |  |  |         /** | 
 |  |  | 
 |  |  |  | 
 |  |  |         // 构造验签名串 | 
 |  |  |         String signatureStr = payHelper.responseSign(wechatpayTimestamp, wechatpayNonce, bodyStr); | 
 |  |  |         byte[] certFileBs = WxCertUtil.getKey_pemBytes(resourceLoader) ; | 
 |  |  |         // 验证签名 | 
 |  |  |         Boolean valid = payHelper.responseSignVerify(wechatpaySerial, signatureStr, wechatpaySignature); | 
 |  |  |         Boolean valid = payHelper.responseSignVerify(wechatpaySerial, signatureStr, wechatpaySignature, certFileBs); | 
 |  |  |         if(!valid) { | 
 |  |  |             response.setStatus(500); | 
 |  |  |             result.put("code", "FAIL"); | 
 |  |  | 
 |  |  |             JSONObject job_resource = JSONObject.parseObject(resource); | 
 |  |  |  | 
 |  |  |             // 解密后取出:商户订单号、微信支付订单号、交易状态、支付完成时间 | 
 |  |  |             String attach = job_resource.getString("attach"); | 
 |  |  |             DataSourceContext.set(attach); | 
 |  |  |             String out_trade_no = job_resource.getString("out_trade_no"); | 
 |  |  |             String transaction_id = job_resource.getString("transaction_id"); | 
 |  |  |             String trade_state = job_resource.getString("trade_state"); | 
 |  |  | 
 |  |  |         result.put("message", "成功"); | 
 |  |  |         return  result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 添加微信支付信息 | 
 |  |  |      * @param po | 
 |  |  |      * @param bindingResult | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping(path = "add_wechatpay", consumes = MediaType.APPLICATION_JSON_VALUE) | 
 |  |  |     public BaseResponse<Boolean> addWechatpay(@RequestBody @Valid Wechatpay po, BindingResult bindingResult) throws Exception { | 
 |  |  |         if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |             return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         if(paymentSv.getWechatpayByAppId(po.getAppId()) != null) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("该微信支付信息已经存在"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         SeWechatpay seWechatpay = new SeWechatpay(); | 
 |  |  |         seWechatpay.setAppId(AES.encrypt(po.getAppId())); | 
 |  |  |         seWechatpay.setAppSecret(AES.encrypt(po.getAppSecret())); | 
 |  |  |         seWechatpay.setMchId(AES.encrypt(po.getMchId())); | 
 |  |  |         seWechatpay.setMchKey(AES.encrypt(po.getMchKey())); | 
 |  |  |         seWechatpay.setSerialNo((AES.encrypt(po.getSerialNo()))); | 
 |  |  |         seWechatpay.setNotifyUrl(AES.encrypt(po.getNotifyUrl())); | 
 |  |  |         seWechatpay.setRemarks(po.getRemarks()); | 
 |  |  |  | 
 |  |  |         Long rec = Optional.ofNullable(paymentSv.addWechatpay(seWechatpay)).orElse(0L); | 
 |  |  |         if(rec == 0) { | 
 |  |  |             return BaseResponseUtils.buildFail("添加微信支付信息失败"); | 
 |  |  |         } | 
 |  |  |         return BaseResponseUtils.buildSuccess(true) ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 初始化微信支付信息 | 
 |  |  |      * @param appId | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping(path = "init_wechatpay") | 
 |  |  |     public BaseResponse<Boolean> initWechatpay(@RequestParam("appId") String appId) throws Exception { | 
 |  |  |         if(appId == null || appId.trim().length() == 0) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("小程序唯一标识不能为空"); | 
 |  |  |         } | 
 |  |  |         appId = AES.encrypt(appId); | 
 |  |  |  | 
 |  |  |         SeWechatpay seWechatpay = paymentSv.getWechatpayByAppId(appId); | 
 |  |  |         if(seWechatpay != null) { | 
 |  |  |             PayInfo.appid = AES.decrypt(seWechatpay.getAppId()); | 
 |  |  |             PayInfo.secret = AES.decrypt(seWechatpay.getAppSecret()); | 
 |  |  |             PayInfo.mchid = AES.decrypt(seWechatpay.getMchId()); | 
 |  |  |             PayInfo.key = AES.decrypt(seWechatpay.getMchKey()); | 
 |  |  |             PayInfo.serial_no = AES.decrypt(seWechatpay.getSerialNo()); | 
 |  |  |             PayInfo.notifyUrl = AES.decrypt(seWechatpay.getNotifyUrl()); | 
 |  |  |             return BaseResponseUtils.buildSuccess(true) ; | 
 |  |  |         }else { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("小程序唯一标识错误或其信息不存在"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  | } |