微信支付回调接口Demo

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>
控制层:

实现层:

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 ;
}
经验分享 程序员 微信小程序 职场和发展