From 004391089df2fda26b9c514d76bac1c329cad750 Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期三, 23 十月 2024 16:47:22 +0800 Subject: [PATCH] 2024-10-23 朱宝民 可同时支持多个微信小程序 --- pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java | 234 ++++++++++++++++++++++++++++++---------------------------- 1 files changed, 120 insertions(+), 114 deletions(-) diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java index 4aad549..49b6ef7 100644 --- a/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java +++ b/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java @@ -1,14 +1,13 @@ package com.dy.pipIrrSell.wechatpay; +import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.dy.common.aop.SsoAop; import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.BaseResponseUtils; import com.dy.common.webUtil.ResultCodeMsg; -import com.dy.pipIrrGlobal.pojoSe.SeVcRefund; -import com.dy.pipIrrGlobal.pojoSe.SeVcRefundItem; -import com.dy.pipIrrGlobal.pojoSe.SeVirtualCard; -import com.dy.pipIrrGlobal.pojoSe.SeWebchatLogonState; +import com.dy.pipIrrGlobal.cert.WxCertUtil; +import com.dy.pipIrrGlobal.pojoSe.*; import com.dy.pipIrrGlobal.voSe.VoClient; import com.dy.pipIrrSell.client.ClientSv; import com.dy.pipIrrSell.result.SellResultCode; @@ -20,19 +19,19 @@ import com.dy.pipIrrSell.virtualCard.enums.LastOperateENUM; import com.dy.pipIrrSell.virtualCard.enums.RefundItemStateENUM; import com.dy.pipIrrSell.wallet.enums.RefundStatusENUM; -import com.dy.pipIrrSell.wechatpay.dto.Code2Session; -import com.dy.pipIrrSell.wechatpay.dto.DtoOrder; -import com.dy.pipIrrSell.wechatpay.dto.OrderNotify; +import com.dy.pipIrrSell.wechatpay.dto.*; 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; @@ -40,6 +39,7 @@ 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.InvalidKeyException; @@ -47,10 +47,7 @@ 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 @@ -65,14 +62,17 @@ @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 ClientSv clientSv; - private final String privateCertFileName = PayInfo.privateCertFileName; + //private final String privateCertFileName = PayInfo.privateCertFileName; private final String appid = PayInfo.appid; + private final String secret = PayInfo.secret; private final String mchid = PayInfo.mchid; private final String schema = PayInfo.schema; private final String signType = PayInfo.signType; @@ -85,7 +85,7 @@ private final Map CERTIFICATE_MAP = new HashMap(); /** - * 鐧诲綍鍑瘉鏍¢獙 + * 鐧诲綍鍑瘉鏍¢獙锛屽啘鎴风粦瀹氳处鍙烽�昏緫鍖呭惈鐧诲綍鍑瘉鏍¢獙锛屾鎺ュ彛浣滃簾 * @param code2Session 鐧诲綍鍑瘉鏍¢獙浼犲叆瀵硅薄 * @param bindingResult * @return @@ -108,10 +108,13 @@ 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", appid); - queryParams.put("secret", code2Session.getSecret()); - queryParams.put("js_code", code2Session.getJs_code()); + queryParams.put("secret", secret); + queryParams.put("js_code", jsCode); queryParams.put("grant_type", grantType); Map<String, String> headerParams = new HashMap<>(); JSONObject job = restTemplateUtil.get(loginUrl, queryParams, headerParams); @@ -123,35 +126,24 @@ String openid = job.getString("openid"); String sessionKey = job.getString("session_key"); - // 妫�楠岀櫥褰曟�� - //JSONObject checkSessionKey = payHelper.checkSessionKey(appid, secret, openid, sessionKey); - //if(checkSessionKey != null) { - // Integer errcode = checkSessionKey.getInteger("errcode"); - // String errmsg = checkSessionKey.getString("errmsg"); - //} + Long clientId = clientSv.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 = clientSv.addOpenId(seOpenId); + if(rec != null) { + SessionId = String.valueOf(rec); + } + return BaseResponseUtils.buildSuccess(SessionId); - // 閲嶇疆鐧诲綍鎬� - //JSONObject resetUserSessionKey = payHelper.resetUserSessionKey(appid, secret, openid, sessionKey); - //if(resetUserSessionKey != null) { - // Integer errcode = checkSessionKey.getInteger("errcode"); - // String errmsg = checkSessionKey.getString("errmsg"); - // String openid_New = checkSessionKey.getString("openid"); - // String sessionKey_New = checkSessionKey.getString("session_key"); - //} - - // 娣诲姞鐧诲綍鎬佽褰� - SeWebchatLogonState po = new SeWebchatLogonState(); - po.setOpenId(openid); - po.setSessionKey(sessionKey); - Date createTime = new Date(); - po.setCreateTime(createTime); - Long id = paymentSv.insert(po); - if(id == null || id <= 0) { - return BaseResponseUtils.buildFail("鐧诲綍鎬佽褰曟坊鍔犲け璐�"); + } else { + return BaseResponseUtils.buildError(SellResultCode.PHONE_NUMBER_IS_ERROR.getMessage()); } - String SessionId = String.valueOf(id); - - return BaseResponseUtils.buildSuccess(SessionId) ; } /** @@ -177,7 +169,8 @@ String nonceStr = payHelper.generateRandomString(); Long timestamp = System.currentTimeMillis() / 1000; - String header = schema + " " + payHelper.getToken(method, httpUrl, "", nonceStr, timestamp, privateCertFileName); + byte[] keyPemBs = WxCertUtil.getKey_pemBytes(resourceLoader) ; + String header = schema + " " + payHelper.getToken(method, httpUrl, "", nonceStr, timestamp, keyPemBs); Map<String, String> headers = new HashMap<>(); headers.put("Authorization", header); @@ -195,7 +188,7 @@ // 鏋勯�犻獙绛惧悕涓� String signatureStr = payHelper.responseSign(wechatpayTimestamp, wechatpayNonce, job_body.toJSONString()); // 楠岃瘉绛惧悕 - Boolean valid = payHelper.responseSignVerify(wechatpaySerial, signatureStr, wechatpaySignature); + Boolean valid = payHelper.responseSignVerify(wechatpaySerial, signatureStr, wechatpaySignature, keyPemBs); return BaseResponseUtils.buildSuccess(); } @@ -204,7 +197,7 @@ * JSAPI涓嬪崟 * @param order 涓嬪崟璇锋眰瀵硅薄锛屽寘鍚渶瑕佷紶鍏ョ殑鍙傛暟 * @param bindingResult - * @return + * @return 棰勬敮浠樹氦鏄撲細璇濇爣璇嗭紙鏈夋晥鏈�2灏忔椂锛� */ @Operation(summary = "JSAPI涓嬪崟", description = "JSAPI涓嬪崟") @ApiResponses(value = { @@ -218,17 +211,18 @@ @PostMapping(path = "placeOrder") @Transactional(rollbackFor = Exception.class) @SsoAop() - 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 NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, IOException, SignatureException, InvalidKeyException, Exception { if(bindingResult != null && bindingResult.hasErrors()){ return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); } - // 鎺ユ敹鍙傛暟锛氱櫥褰曟�両D銆佸啘鎴稩D銆佽櫄鎷熷崱ID銆佸厖鍊奸噾棰� - String sessionId = order.getSessionId(); - Long virtualId = order.getVirtualId(); + // 鎺ユ敹鍙傛暟锛氱櫥褰曟�両D銆佽櫄鎷熷崱ID銆佸厖鍊奸噾棰濓紙鍒嗭級 + Long sessionId = order.getSessionId(); + Long virtualId = order.getVcId(); Integer rechargeAmount = order.getRechargeAmount(); String prepayId = ""; - SeWebchatLogonState po = paymentSv.selectOne(Long.parseLong(sessionId)); + + SeOpenId po = paymentSv.selectOne(sessionId); String openid = po.getOpenId(); SeVirtualCard seVirtualCard = virtualCardSv.selectVirtuCardById(virtualId); @@ -261,7 +255,7 @@ //璁㈠崟閲戦 JSONObject job_amount = new JSONObject(); - job_amount.put("total", 1); + job_amount.put("total", rechargeAmount); job_amount.put("currency", "CNY"); job_body.put("amount", job_amount); @@ -278,7 +272,7 @@ String httpUrl = "/v3/pay/transactions/jsapi"; String body = job_body.toJSONString(); - String header = schema + " " + payHelper.getToken(method, httpUrl, body, nonceStr, timestamp, privateCertFileName); + String header = schema + " " + payHelper.getToken(method, httpUrl, body, nonceStr, timestamp, WxCertUtil.getKey_pemBytes(resourceLoader)); Map<String, String> headers = new HashMap<>(); headers.put("Authorization", header); @@ -286,19 +280,56 @@ headers.put("Content-Type", "application/json"); // 鏆傛椂娉ㄩ噴鎺夛紝璁よ瘉閫氳繃鍚庡啀鏀惧紑 - //JSONObject job_result = restTemplateUtil.post(PayInfo.orderUrl, body, headers); - //if(job_result != null) { - // System.out.println(job_result.toString()); - // prepayId = job_result.getString("prepay_id"); - //} + JSONObject job_result = restTemplateUtil.post(PayInfo.orderUrl, body, headers); + if(job_result == null) { + return BaseResponseUtils.buildFail(SellResultCode.RECHARGE_ADD_FAIL.getMessage()); + } - return BaseResponseUtils.buildSuccess(prepayId) ; + 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) + @SsoAop() + public BaseResponse<JSONObject> signAgain(@RequestParam("prepayId") String prepayId) throws Exception { + + // 鑾峰彇闅忔満涓插拰鏃堕棿鎴筹紝鏀惧湪姝ゅ浠ヤ繚璇� + String appid = 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); + String paySign = payHelper.sign(message.getBytes("utf-8"), WxCertUtil.getKey_pemBytes(resourceLoader)); + + JSONObject job_result = new JSONObject(); + job_result.put("timeStamp", timeStamp); + job_result.put("nonceStr", nonceStr); + job_result.put("package", pkg); + job_result.put("signType", signType); + job_result.put("paySign", paySign); + + return BaseResponseUtils.buildSuccess(job_result) ; } /** * 鏀粯閫氱煡/閫�娆剧粨鏋滈�氱煡 * @param headers - * @param orderNotify + * @param request * @param response * @return * @throws IOException @@ -316,12 +347,12 @@ @PostMapping(path = "orderNotify", consumes = MediaType.APPLICATION_JSON_VALUE) @Transactional(rollbackFor = Exception.class) @SsoAop() - public JSONObject orderNotify(@RequestHeader HttpHeaders headers, @RequestBody OrderNotify orderNotify, HttpServletResponse response) throws IOException, GeneralSecurityException { + public JSONObject orderNotify(@RequestHeader HttpHeaders headers, HttpServletRequest request, HttpServletResponse response) throws IOException, GeneralSecurityException, Exception { JSONObject result = new JSONObject(); /** * 1.楠岀澶勭悊 - * 浠巋eader涓彇鍑�4涓瓙鍙傛暟锛屽悓鏃跺彇鍑篵ody + * 浠巋eader涓彇鍑�4涓瓙鍙傛暟 * 楠屾椂闂村樊锛岃秴杩�5鍒嗛挓鐨勪笉澶勭悊 * 楠岃瘉绛惧悕 * 楠岃瘉涔﹀簭鍒楀彿锛屽繀椤讳笌鏌愪竴涓瘉涔︾殑搴忓垪鍙蜂竴鑷� @@ -330,7 +361,18 @@ 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)); - String bodyStr = JSONObject.toJSONString(orderNotify); + + // 鑾峰彇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; @@ -343,8 +385,9 @@ // 鏋勯�犻獙绛惧悕涓� String signatureStr = payHelper.responseSign(wechatpayTimestamp, wechatpayNonce, bodyStr); + byte[] keyPemBs = WxCertUtil.getKey_pemBytes(resourceLoader) ; // 楠岃瘉绛惧悕 - Boolean valid = payHelper.responseSignVerify(wechatpaySerial, signatureStr, wechatpaySignature); + Boolean valid = payHelper.responseSignVerify(wechatpaySerial, signatureStr, wechatpaySignature, keyPemBs); if(!valid) { response.setStatus(500); result.put("code", "FAIL"); @@ -366,10 +409,7 @@ return result; } - /** - * 瑙e瘑澶勭悊 - * 1 - */ + // 瑙e瘑澶勭悊 String eventType = orderNotify.getEvent_type(); if(eventType != null && eventType.equals("TRANSACTION.SUCCESS")) { @@ -379,7 +419,7 @@ * 鍙栧嚭閫氱煡鏁版嵁瀵硅薄锛岀户鑰屽彇鍑鸿В瀵嗘墍闇�鐨刟ssociatedData鍜宯once锛屼互鍙婂瘑鏂嘽iphertext * 瑙e瘑ciphertext寰楀埌 */ - OrderNotify.NotifyResource notifyResource = orderNotify.getResource(); + NotifyResource notifyResource = orderNotify.getResource(); String associatedData = notifyResource.getAssociated_data(); String nonce = notifyResource.getNonce(); String ciphertext = notifyResource.getCiphertext(); @@ -387,19 +427,22 @@ 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瘑鍚庡彇鍑猴細鍟嗘埛璁㈠崟鍛樸�佸井淇℃敮浠樿鍗曞彿銆佷氦鏄撶姸鎬併�佹敮浠樺畬鎴愭椂闂� + // 瑙e瘑鍚庡彇鍑猴細鍟嗘埛璁㈠崟鍙枫�佸井淇℃敮浠樿鍗曞彿銆佷氦鏄撶姸鎬併�佹敮浠樺畬鎴愭椂闂� 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"); - // 鏇存柊铏氭嫙鍗¤〃鍙婂厖鍊艰〃鍝嶅簲瀛楁 - 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; + // 濡傛灉褰撳墠璁㈠崟鐘舵�佷负鏈敮浠樼姸鎬侊紝鍒欐洿鏂拌櫄鎷熷崱琛ㄥ強鍏呭�艰〃鍝嶅簲瀛楁 + 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")) { // 閫�娆炬垚鍔熷悗鍥炶皟 @@ -408,7 +451,7 @@ * 鍙栧嚭閫氱煡鏁版嵁瀵硅薄锛岀户鑰屽彇鍑鸿В瀵嗘墍闇�鐨刟ssociatedData鍜宯once锛屼互鍙婂瘑鏂嘽iphertext * 瑙e瘑ciphertext寰楀埌 */ - OrderNotify.NotifyResource notifyResource = orderNotify.getResource(); + NotifyResource notifyResource = orderNotify.getResource(); String associatedData = notifyResource.getAssociated_data(); String nonce = notifyResource.getNonce(); String ciphertext = notifyResource.getCiphertext(); @@ -474,41 +517,4 @@ return 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) - @SsoAop() - public BaseResponse<JSONObject> signAgain(@RequestParam("prepayId") String prepayId) throws Exception { - - // 鑾峰彇闅忔満涓插拰鏃堕棿鎴筹紝鏀惧湪姝ゅ浠ヤ繚璇� - String appid = 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); - String paySign = payHelper.sign(message.getBytes("utf-8"), privateCertFileName); - - JSONObject job_result = new JSONObject(); - job_result.put("timestamp", timestamp); - job_result.put("nonceStr", nonceStr); - job_result.put("package", pkg); - job_result.put("signType", signType); - job_result.put("paySign", paySign); - - return BaseResponseUtils.buildSuccess(job_result) ; - } } -- Gitblit v1.8.0