From d0b51c6d6f2b7a530c0557b6279cc6dfced223cf Mon Sep 17 00:00:00 2001 From: zhubaomin <470473919@qq.com> Date: 星期日, 27 四月 2025 19:01:04 +0800 Subject: [PATCH] 各轮灌组开阀时间提前10分钟,第一组除外,改为配置文件设置 --- pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/wechatpay/PaymentCtrl.java | 566 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 566 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/wechatpay/PaymentCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/wechatpay/PaymentCtrl.java new file mode 100644 index 0000000..7c77eb6 --- /dev/null +++ b/pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/wechatpay/PaymentCtrl.java @@ -0,0 +1,566 @@ +package com.dy.pipIrrWechat.wechatpay; + +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.util.AesUtil; +import com.dy.pipIrrWechat.util.PayHelper; +import com.dy.pipIrrWechat.util.RestTemplateUtil; +import com.dy.pipIrrWechat.virtualCard.VirtualCardSv; +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.*; +import com.dy.pipIrrWechat.wechatpay.enums.RefundStatusENUM; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +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 java.io.BufferedReader; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.SignatureException; +import java.security.spec.InvalidKeySpecException; +import java.text.SimpleDateFormat; +import java.util.*; + +/** + * @author ZhuBaoMin + * @date 2024-07-16 15:05 + * @LastEditTime 2024-07-16 15:05 + * @Description + */ + +@Slf4j +@Tag(name = "寰俊鏀粯绠$悊", description = "寰俊鏀粯鍚勭鎿嶄綔") +@RestController +@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 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; + //private final String schema = com.dy.pipIrrWechat.wechatpay.PayInfo.schema; + //private final String signType = com.dy.pipIrrWechat.wechatpay.PayInfo.signType; + //private final String description = com.dy.pipIrrWechat.wechatpay.PayInfo.description; + //private final String loginUrl = com.dy.pipIrrWechat.wechatpay.PayInfo.loginUrl; + //private final String notifyUrl = com.dy.pipIrrWechat.wechatpay.PayInfo.notifyUrl; + //private final String grantType = com.dy.pipIrrWechat.wechatpay.PayInfo.grantType; + + // 骞冲彴璇佷功鍏挜 + private final Map CERTIFICATE_MAP = new HashMap(); + + /** + * 鐧诲綍鍑瘉鏍¢獙锛屽啘鎴风粦瀹氳处鍙烽�昏緫鍖呭惈鐧诲綍鍑瘉鏍¢獙锛屾鎺ュ彛浣滃簾 + * @param code2Session 鐧诲綍鍑瘉鏍¢獙浼犲叆瀵硅薄 + * @param bindingResult + * @return + * @throws Exception + */ + @Operation(summary = "鐧诲綍鍑瘉鏍¢獙", description = "鐧诲綍鍑瘉鏍¢獙") + @ApiResponses(value = { + @ApiResponse( + responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, + description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�", + content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, + schema = @Schema(implementation = Boolean.class))} + ) + }) + @PostMapping(path = "getSessionId") + @Transactional(rollbackFor = Exception.class) + public BaseResponse<Boolean> getSessionId(@RequestBody @Valid Code2Session code2Session, BindingResult bindingResult) throws Exception { + if(bindingResult != null && bindingResult.hasErrors()){ + return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); + } + + String phoneNumber = code2Session.getPhoneNumber(); + String jsCode = code2Session.getJs_code(); + + Map<String, Object> queryParams = new HashMap<>(); + queryParams.put("appid", PayInfo.appid); + queryParams.put("secret", PayInfo.secret); + queryParams.put("js_code", jsCode); + queryParams.put("grant_type", com.dy.pipIrrWechat.wechatpay.PayInfo.grantType); + Map<String, String> headerParams = new HashMap<>(); + JSONObject job = restTemplateUtil.get(com.dy.pipIrrWechat.wechatpay.PayInfo.loginUrl, queryParams, headerParams); + + if(job.getLong("errcode") != null && job.getLong("errcode") >= -1) { + return BaseResponseUtils.buildFail("鐧诲綍鍑瘉鏍¢獙澶辫触"); + } + + String openid = job.getString("openid"); + String sessionKey = job.getString("session_key"); + + Long clientId = paymentSv.getClientIdByPhone(phoneNumber); + String SessionId = ""; + if(clientId != null) { + // 娣诲姞寰俊鐢ㄦ埛璐︽埛璁板綍 + SeOpenId seOpenId = new SeOpenId(); + seOpenId.setClientId(clientId); + seOpenId.setOpenId(openid); + seOpenId.setSessionKey(sessionKey); + seOpenId.setCreateTime(new Date()); + Long rec = paymentSv.addOpenId(seOpenId); + if(rec != null) { + SessionId = String.valueOf(rec); + } + return BaseResponseUtils.buildSuccess(SessionId); + + } else { + return BaseResponseUtils.buildError(WechatResultCode.PHONE_NUMBER_IS_ERROR.getMessage()); + } + } + + /** + * 涓嬭浇寰俊鏀粯骞冲彴璇佷功 娴嬭瘯瀹屽簾闄� + * @return + * @throws Exception + */ + @Operation(summary = "涓嬭浇骞冲彴璇佷功", description = "涓嬭浇骞冲彴璇佷功") + @ApiResponses(value = { + @ApiResponse( + responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, + description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�", + content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, + schema = @Schema(implementation = Boolean.class))} + ) + }) + @GetMapping(path = "certificates") + @Transactional(rollbackFor = Exception.class) + public BaseResponse<Boolean> certificates() throws Exception { + String method = "GET"; + String httpUrl = "/v3/certificates"; + String nonceStr = payHelper.generateRandomString(); + Long timestamp = System.currentTimeMillis() / 1000; + + byte[] certFileBs = WxCertUtil.getKey_pemBytes(resourceLoader) ; + String header = com.dy.pipIrrWechat.wechatpay.PayInfo.schema + " " + payHelper.getToken(method, httpUrl, "", nonceStr, timestamp, certFileBs); + + Map<String, String> headers = new HashMap<>(); + headers.put("Authorization", header); + headers.put("Accept", "application/json"); + JSONObject job_result = restTemplateUtil.getHeaders(com.dy.pipIrrWechat.wechatpay.PayInfo.certificates,null, headers); + JSONObject job_headers = job_result.getJSONObject("headers"); + String wechatpayNonce = job_headers.getJSONArray("Wechatpay-Nonce").getString(0); + String wechatpaySerial = job_headers.getJSONArray("Wechatpay-Serial").getString(0); + String wechatpaySignature = job_headers.getJSONArray("Wechatpay-Signature").getString(0); + String wechatpaySignatureType = job_headers.getJSONArray("Wechatpay-Signature-Type").getString(0); + String wechatpayTimestamp = job_headers.getJSONArray("Wechatpay-Timestamp").getString(0); + + JSONObject job_body = job_result.getJSONObject("body"); + + // 鏋勯�犻獙绛惧悕涓� + String signatureStr = payHelper.responseSign(wechatpayTimestamp, wechatpayNonce, job_body.toJSONString()); + // 楠岃瘉绛惧悕 + Boolean valid = payHelper.responseSignVerify(wechatpaySerial, signatureStr, wechatpaySignature, certFileBs); + + return BaseResponseUtils.buildSuccess(); + } + + /** + * JSAPI涓嬪崟 + * @param order 涓嬪崟璇锋眰瀵硅薄锛屽寘鍚渶瑕佷紶鍏ョ殑鍙傛暟 + * @param bindingResult + * @return 棰勬敮浠樹氦鏄撲細璇濇爣璇嗭紙鏈夋晥鏈�2灏忔椂锛� + */ + @PostMapping(path = "placeOrder") + @Transactional(rollbackFor = Exception.class) + 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()); + } + + // 鎺ユ敹鍙傛暟锛氱櫥褰曟�両D銆佽櫄鎷熷崱ID銆佸厖鍊奸噾棰�(鍗曚綅鍏�) + Long sessionId = order.getSessionId(); + Long virtualId = order.getVcId(); + //Integer rechargeAmount = (int)(order.getRechargeAmount()*100); + + String prepayId = ""; + + SeOpenId po = paymentSv.selectOne(sessionId); + if(po == null) { + return BaseResponseUtils.buildErrorMsg(WechatResultCode.SESSION_ID_ERROR.getMessage()); + } + String openid = po.getOpenId(); + + SeVirtualCard seVirtualCard = virtualCardSv.selectVirtuCardById(virtualId); + if(seVirtualCard == null) { + return BaseResponseUtils.buildErrorMsg(WechatResultCode.VIRTUAL_CARD_NOT_EXIST.getMessage()); + } + Long clientId = seVirtualCard.getClientId(); + + VoClient voClient = paymentSv.getOneClient(clientId); + if(voClient == null) { + return BaseResponseUtils.buildErrorMsg(WechatResultCode.VIRTUAL_CARD_CLIENT_NOT_EXIST.getMessage()); + } + String clientNum = voClient.getClientNum(); + + // 鐢熸垚璁㈠崟鍙峰苟娣诲姞鍏呭�艰褰� + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS"); + String orderNumber = clientNum + dateFormat.format(new Date()); + + // 鐢熸垚铏氭嫙鍗″厖鍊艰褰曪紙閮ㄥ垎瀛楁锛� + DtoVirtualCard virtualCard = new DtoVirtualCard(); + virtualCard.setVirtualId(virtualId); + virtualCard.setClientId(clientId); + virtualCard.setOrderNumber(orderNumber); + //virtualCard.setRechargeAmount(rechargeAmount); + virtualCard.setRechargeAmount(order.getRechargeAmount()); + + // -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()); + } + + JSONObject job_body = new JSONObject(); + job_body.put("appid", PayInfo.appid); + job_body.put("mchid", PayInfo.mchid); + job_body.put("description", com.dy.pipIrrWechat.wechatpay.PayInfo.description); + job_body.put("out_trade_no", orderNumber); + job_body.put("attach", DataSourceContext.get()); + job_body.put("notify_url", com.dy.pipIrrWechat.wechatpay.PayInfo.notifyUrl); + + //璁㈠崟閲戦 + JSONObject job_amount = new JSONObject(); + job_amount.put("total", (int)(order.getRechargeAmount()*100)); + job_amount.put("currency", "CNY"); + job_body.put("amount", job_amount); + + //鏀粯鑰� + JSONObject job_payer = new JSONObject(); + job_payer.put("openid", openid); + job_body.put("payer", job_payer); + + // 鑾峰彇闅忔満涓插拰鏃堕棿鎴� + String nonceStr = payHelper.generateRandomString(); + Long timestamp = System.currentTimeMillis() / 1000; + + String method = "POST"; + String httpUrl = "/v3/pay/transactions/jsapi"; + + String body = job_body.toJSONString(); + byte[] certFileBs = WxCertUtil.getKey_pemBytes(resourceLoader) ; + String header = com.dy.pipIrrWechat.wechatpay.PayInfo.schema + " " + payHelper.getToken(method, httpUrl, body, nonceStr, timestamp, certFileBs); + + Map<String, String> headers = new HashMap<>(); + headers.put("Authorization", header); + headers.put("Accept", "application/json"); + headers.put("Content-Type", "application/json"); + + // 鏆傛椂娉ㄩ噴鎺夛紝璁よ瘉閫氳繃鍚庡啀鏀惧紑 + JSONObject job_result = restTemplateUtil.post(com.dy.pipIrrWechat.wechatpay.PayInfo.orderUrl, body, headers); + if(job_result == null) { + return BaseResponseUtils.buildFail(WechatResultCode.RECHARGE_ADD_FAIL.getMessage()); + } + + return BaseResponseUtils.buildSuccess(job_result) ; + } + + /** + * 鍐嶆绛惧悕 + * @param prepayId 棰勬敮浠樹氦鏄撲細璇濇爣璇� + * @return 灏忕▼搴忚皟璧锋敮浠樺弬鏁� + * @throws Exception + */ + @Operation(summary = "鍐嶆绛惧悕", description = "鍐嶆绛惧悕") + @ApiResponses(value = { + @ApiResponse( + responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, + description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�", + content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, + schema = @Schema(implementation = Boolean.class))} + ) + }) + @GetMapping(path = "/signAgain") + @Transactional(rollbackFor = Exception.class) + public BaseResponse<JSONObject> signAgain(@RequestParam("prepayId") String prepayId) throws Exception { + + // 鑾峰彇闅忔満涓插拰鏃堕棿鎴筹紝鏀惧湪姝ゅ浠ヤ繚璇� + String appid = com.dy.pipIrrWechat.wechatpay.PayInfo.appid; + String timeStamp = String.valueOf(System.currentTimeMillis() / 1000); + String nonceStr = payHelper.generateRandomString(); + String pkg = "prepay_id=" + prepayId; + String message = payHelper.buildMessage_signAgain(appid, timeStamp, nonceStr, pkg); + 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); + job_result.put("nonceStr", nonceStr); + job_result.put("package", pkg); + job_result.put("signType", com.dy.pipIrrWechat.wechatpay.PayInfo.signType); + job_result.put("paySign", paySign); + + return BaseResponseUtils.buildSuccess(job_result) ; + } + + /** + * 鏀粯閫氱煡/閫�娆剧粨鏋滈�氱煡 + * @param headers + * @param request + * @param response + * @return + * @throws IOException + * @throws GeneralSecurityException + */ + @Operation(summary = "鏀粯閫氱煡", description = "鏀粯閫氱煡") + @ApiResponses(value = { + @ApiResponse( + responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, + description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�", + content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, + schema = @Schema(implementation = Boolean.class))} + ) + }) + @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, Exception { + JSONObject result = new JSONObject(); + + /** + * 1.楠岀澶勭悊 + * 浠巋eader涓彇鍑�4涓瓙鍙傛暟 + * 楠屾椂闂村樊锛岃秴杩�5鍒嗛挓鐨勪笉澶勭悊 + * 楠岃瘉绛惧悕 + * 楠岃瘉涔﹀簭鍒楀彿锛屽繀椤讳笌鏌愪竴涓瘉涔︾殑搴忓垪鍙蜂竴鑷� + */ + String wechatpayNonce = String.valueOf(headers.get("Wechatpay-Nonce").get(0)); + String wechatpaySerial = String.valueOf(headers.get("Wechatpay-Serial").get(0)); + String wechatpaySignature = String.valueOf(headers.get("Wechatpay-Signature").get(0)); + String wechatpayTimestamp = String.valueOf(headers.get("Wechatpay-Timestamp").get(0)); + + // 鑾峰彇body鍐呭 + BufferedReader reader = request.getReader(); + StringBuilder stringBuilder = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + stringBuilder.append(line); + } + String bodyStr = stringBuilder.toString(); + + // body杞璞� + OrderNotify orderNotify = JSON.parseObject(bodyStr, OrderNotify.class); + + // 楠屾椂闂存埑锛屾椂闂村樊澶т簬5鍒嗛挓鐨勬嫆缁� + Long timeDiff = (System.currentTimeMillis() / 1000 - Long.parseLong(wechatpayTimestamp))/60; + if(timeDiff > 5) { + response.setStatus(500); + result.put("code", "FAIL"); + result.put("message", "澶辫触"); + return result; + } + + // 鏋勯�犻獙绛惧悕涓� + String signatureStr = payHelper.responseSign(wechatpayTimestamp, wechatpayNonce, bodyStr); + byte[] certFileBs = WxCertUtil.getKey_pemBytes(resourceLoader) ; + // 楠岃瘉绛惧悕 + Boolean valid = payHelper.responseSignVerify(wechatpaySerial, signatureStr, wechatpaySignature, certFileBs); + if(!valid) { + response.setStatus(500); + result.put("code", "FAIL"); + result.put("message", "澶辫触"); + return result; + } + + // 搴忓垪鍙烽獙璇佽鏀惧湪楠岀鍚庯紝鍥犱负楠岀鏃跺彲鑳戒細涓嬭浇鏂扮殑璇佷功 + boolean SerialIsValid = false; + for (String key : payHelper.CERTIFICATE_MAP.keySet()) { + if(key.equals(wechatpaySerial)) { + SerialIsValid = true; + } + } + if(!SerialIsValid) { + response.setStatus(500); + result.put("code", "FAIL"); + result.put("message", "澶辫触"); + return result; + } + + // 瑙e瘑澶勭悊 + String eventType = orderNotify.getEvent_type(); + + if(eventType != null && eventType.equals("TRANSACTION.SUCCESS")) { + // 鏀粯鎴愬姛鍥炶皟 + /** + * 鏀粯鎴愬姛鐨勫洖璋� + * 鍙栧嚭閫氱煡鏁版嵁瀵硅薄锛岀户鑰屽彇鍑鸿В瀵嗘墍闇�鐨刟ssociatedData鍜宯once锛屼互鍙婂瘑鏂嘽iphertext + * 瑙e瘑ciphertext寰楀埌 + */ + NotifyResource notifyResource = orderNotify.getResource(); + String associatedData = notifyResource.getAssociated_data(); + String nonce = notifyResource.getNonce(); + String ciphertext = notifyResource.getCiphertext(); + + String resource = AesUtil.decryptToString(com.dy.pipIrrWechat.wechatpay.PayInfo.key.getBytes("utf-8"), associatedData.getBytes("utf-8"), nonce.getBytes("utf-8"), ciphertext); + JSONObject job_resource = JSONObject.parseObject(resource); + + // 瑙e瘑鍚庡彇鍑猴細鍟嗘埛璁㈠崟鍙枫�佸井淇℃敮浠樿鍗曞彿銆佷氦鏄撶姸鎬併�佹敮浠樺畬鎴愭椂闂� + 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"); + Date success_time = job_resource.getDate("success_time"); + + // 濡傛灉褰撳墠璁㈠崟鐘舵�佷负鏈敮浠樼姸鎬侊紝鍒欐洿鏂拌櫄鎷熷崱琛ㄥ強鍏呭�艰〃鍝嶅簲瀛楁 + SeVcRecharge seVcRecharge = virtualCardSv.getVCRechargeByorderNumber(out_trade_no); + if(seVcRecharge != null && seVcRecharge.getOrderState() == 1) { + BaseResponse result_ = virtualCardSv.updateVCRecharge(out_trade_no, success_time); + if(!result_.getCode().equals("0001")) { + response.setStatus(500); + result.put("code", "FAIL"); + result.put("message", "澶辫触"); + return result; + } + } + } else if(eventType != null && eventType.equals("REFUND.SUCCESS")) { + // 閫�娆炬垚鍔熷悗鍥炶皟 + /** + * 閫�娆炬垚鍔熺殑鍥炶皟 + * 鍙栧嚭閫氱煡鏁版嵁瀵硅薄锛岀户鑰屽彇鍑鸿В瀵嗘墍闇�鐨刟ssociatedData鍜宯once锛屼互鍙婂瘑鏂嘽iphertext + * 瑙e瘑ciphertext寰楀埌 + */ + NotifyResource notifyResource = orderNotify.getResource(); + String associatedData = notifyResource.getAssociated_data(); + String nonce = notifyResource.getNonce(); + String ciphertext = notifyResource.getCiphertext(); + + String resource = AesUtil.decryptToString(PayInfo.key.getBytes("utf-8"), associatedData.getBytes("utf-8"), nonce.getBytes("utf-8"), ciphertext); + JSONObject job_resource = JSONObject.parseObject(resource); + + // 瑙e瘑鍚庡彇鍑猴細鍟嗘埛璁㈠崟鍛樸�佸井淇℃敮浠樿鍗曞彿銆佷氦鏄撶姸鎬併�佹敮浠樺畬鎴愭椂闂� + String out_trade_no = job_resource.getString("out_trade_no"); + String transaction_id = job_resource.getString("transaction_id"); + String out_refund_no = job_resource.getString("out_refund_no"); + String refund_status = job_resource.getString("refund_status"); + Date success_time = job_resource.getDate("success_time"); + if(!refund_status.equals("SUCCESS")) { + response.setStatus(500); + result.put("code", "FAIL"); + result.put("message", "澶辫触"); + return result; + } + + // 鏇存柊铏氭嫙鍗¤〃鍙婂厖鍊艰〃鍝嶅簲瀛楁 + SeVcRefundItem seVcRefundItem = new SeVcRefundItem(); + seVcRefundItem.setRefundTime(success_time); + seVcRefundItem.setRefundStatus(RefundItemStateENUM.REFUNDED.getCode()); + Integer rec = virtualCardSv.updateRefundItem(seVcRefundItem); + if(rec == null && rec <= 0) { + response.setStatus(500); + result.put("code", "FAIL"); + result.put("message", "澶辫触"); + return result; + } + + // 鏍规嵁閫�娆惧崟鍙峰弽鏌ラ��娆綢D锛屾牴鎹��娆綢D鑾峰彇閫�娆剧姸鎬佹槸鏈��娆剧殑璁板綍鏁伴噺锛屽鏋滄槸0鍒欒鏄庡叏閮ㄩ��娆惧畬鎴愶紝鏇存柊閫�娆捐〃鐘舵�佷负宸查��娆撅紝灏嗛��娆惧悗閲戦鏇存柊鍒拌櫄鎷熷崱琛� + /** + * 鏍规嵁閫�娆鹃�氱煡鎺ュ彛杩斿洖鐨勯��娆惧崟鍙峰弽鏌ラ��娆綢D锛屾煡璇㈣閫�娆綢D涓嬫湭閫�娆捐褰曟暟閲� + * 濡傛灉缁撴灉涓�0锛屽垯璇ラ��娆惧凡缁忓畬鎴� + * 1. 鏇存柊閫�娆捐〃鐘舵�佷负宸查��娆� + * 2. 灏嗛��娆惧悗浣欓鏇存柊鍒拌櫄鎷熷崱琛ㄤ腑 + */ + Integer noRefundedCount = virtualCardSv.getNoRefundedCount(out_refund_no); + if(noRefundedCount != null && noRefundedCount == 0) { + // 鑾峰彇閫�娆惧璞″苟淇敼閫�娆剧姸鎬� + Long refundId = virtualCardSv.getRefundIdByRefundNumber(out_refund_no); + SeVcRefund seVcRefund = virtualCardSv.selectRefundByRefundId(refundId); + seVcRefund.setRefundStatus(RefundStatusENUM.REFUNDED.getCode()); + virtualCardSv.updateRefund(seVcRefund); + + // 鑾峰彇铏氭嫙鍗″璞″苟淇敼浣欓銆佹渶鍚庢搷浣溿�佹渶鍚庢搷浣滄椂闂� + Long vcId = seVcRefund.getVcId(); + Double afterRefund = seVcRefund.getAfterRefund(); + SeVirtualCard seVirtualCard = virtualCardSv.selectVirtuCardById(vcId); + seVirtualCard.setMoney(afterRefund); + seVirtualCard.setLastOperate(LastOperateENUM.REFUND.getCode()); + seVirtualCard.setLastOperateTime(new Date()); + virtualCardSv.updateVirtualCard(seVirtualCard); + } + } + + // 閫氱煡搴旂瓟 + response.setStatus(200); + result.put("code", "SUCCESS"); + 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) ; + } +} \ No newline at end of file -- Gitblit v1.8.0