微信支付回调接口Demo

微信支付回调接口Demo
废话不都说,直接上代码,某些微信支付的参数,可以参考上一篇文章 《微信支付2.0 更换参数即可使用》https://blog.csdn.net/qq_41568648/article/details/122344820

pom.xml文件

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<log4j.version>1.2.17</log4j.version>
<spring.version>4.2.3.RELEASE</spring.version>
<jdk.version>1.8</jdk.version>

junit junit 3.8.1 test log4j log4j ${log4j.version} com.alibaba fastjson 1.2.30
	<dependency>
		 <groupId>com.github.wechatpay-apiv3</groupId>
		<artifactId>wechatpay-apache-httpclient</artifactId>
		<version>0.2.2</version>
   </dependency>
   
   <!-- spring核心包 -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>${spring.version}</version>
	</dependency>	
控制层:
/**
 * 微信支付成功后.异步请求该接口
 * @param request
 * @return
 * @throws IOException
 */  
@RequestMapping(value="/weChatNotifyUrl",method=RequestMethod.POST)
@ResponseBody
public JSONObject weChatNotifyUrl(HttpServletRequest request,HttpServletResponse response) throws IOException {  
	log.info("weChatNotifyUrl method start...");
	JSONObject status = new JSONObject();
	try {
		BufferedReader reader = request.getReader();
        StringBuffer sb = new StringBuffer();
		String text = null;
        while((text = reader.readLine()) !=null ){
        	log.info("==================weChatNotifyUrl method send info:" + text);
        	sb.append(text);
        }
        JSONObject parseWechatNotifyStr = paymentService.parseWechatNotifyStr(sb.toString());
        String outTradeNo = parseWechatNotifyStr.getString("out_trade_no");
        OrderRecordInfo orderRecordInfo = OrderReplayService.selectOrderRecordInfoByXcfOrderNo(outTradeNo);
	    String userId = orderRecordInfo.getUserId();
	     
	    // 查看是否在微信公众号购买了年卡
	    JSONObject weChatInfo = orderReplayApiController.buyCardByWeChatTouch(userId);
        
        status = paymentService.weChatNotify(parseWechatNotifyStr, weChatInfo);
        String result = status.getString("code");
        
	} catch (Exception e) {
		log.info("weChatNotifyUrl method error" + e.getMessage());
	}
	
	log.info("weChatNotifyUrl method end" + status);
	return status;
}

实现层:

/**
* 对微信返回的信息进行解码
*/
public JSONObject parseWechatNotifyStr(String wechatNotifyStr) {
JSONObject resourceObj = new JSONObject();
try {
JSONObject resultInfo = JSONObject.parseObject(wechatNotifyStr);
JSONObject resourceInfo = resultInfo.getJSONObject(“resource”);
logger.info(“parseWechatNotifyStr resource:”+wechatNotifyStr);

		String associated_data = resourceInfo.getString("associated_data");
		byte[] associatedData = associated_data.getBytes();
		String nonce = resourceInfo.getString("nonce");
		byte[] nonCe = nonce.getBytes();
		String ciphertext = resourceInfo.getString("ciphertext");;
		String key = OpenPropertyUtil.getConfig("we_chat_apiv3key", "");
		byte[] bytes = key.getBytes();
		AesUtil aesUtil = new AesUtil(bytes);
		
		String decryptToString = aesUtil.decryptToString(associatedData, nonCe, ciphertext);
		resourceObj = JSONObject.parseObject(decryptToString);
	} catch (GeneralSecurityException e) {
		logger.error("parseWechatNotifyStr method error..." + e.getMessage());
	} catch (IOException e) {
		logger.error("parseWechatNotifyStr method error..." + e.getMessage());
	}
	logger.info("parseWechatNotifyStr decryptToString:"+resourceObj);
	return  resourceObj;
}

public JSONObject weChatNotify(JSONObject resourceObj, JSONObject weChatInfo) {
logger.info(“weChatNotify service start:”);
JSONObject result = new JSONObject();
result.put(“code”, “FAIL”);
result.put(“message”, “失败”);

	String tradeState = resourceObj.getString("trade_state");
	
	
	String mchId = OpenPropertyUtil.getConfig("we_chat_merchant_id", "");
	String appId = OpenPropertyUtil.getConfig("we_chat_appid", "");
	
	String mchid = resourceObj.getString("mchid");
	String appid = resourceObj.getString("appid");
	
	String outTradeNo = resourceObj.getString("out_trade_no");
	OrderRecordInfo orderRecordInfo = orderRecordInfoMapper.selectOrderRecordInfoByXcfOrderNo(outTradeNo);
	
	if(null != orderRecordInfo && "SUCCESS".equalsIgnoreCase(tradeState) && mchId.equalsIgnoreCase(mchid) && appId.equalsIgnoreCase(appid)) {
		logger.info("weChatNotify Start processing order information:"+outTradeNo); 
		JSONObject amount = resourceObj.getJSONObject("amount");
		String total = amount.getString("total");
		
		Integer totalWeCaht = Integer.valueOf(total);
		
		Integer orderAmount = orderRecordInfo.getOrderAmount().multiply(new BigDecimal(100)).intValue();
		
		logger.info("totalWeCaht.equals(orderAmount)  = "+(totalWeCaht.equals(orderAmount)));
		
		if(totalWeCaht.equals(orderAmount)) {
			// 根据订单类型,处理相应的业务生效逻辑
	    	boolean handleOrderBusiness = handleOrderBusinessByOrderInfo(orderRecordInfo, weChatInfo);
			if(handleOrderBusiness) {
				orderRecordInfo.setOrderCompletionTime(new Date());
		    	orderRecordInfo.setRealPayAmount(orderRecordInfo.getOrderAmount());
		    	orderRecordInfo.setOrderStatus(20);
		    	orderRecordInfoMapper.updateOrderRecordInfo(orderRecordInfo);
		    	
		    	result.put("code",  "SUCCESS");
		    	result.put("message",  "成功");
		    	return result;
			}
		}
	}
	logger.info("weChatNotify service end:"+result);
	return result ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值