| | |
| | | } |
| | | |
| | | /** |
| | | * 成功通用回复 |
| | | * |
| | | * @return |
| | | */ |
| | | public static ResponseEntity<String> error(String msg,Object data) { |
| | | ResultVo resultVo = new ResultVo(CODE_ERROR, msg,data); |
| | | ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK); |
| | | return responseEntity; |
| | | } |
| | | |
| | | /** |
| | | * 创建ResponseEntity对象 |
| | | * |
| | | * @param records 页数 |
| | |
| | | import com.java110.core.context.PageData; |
| | | import com.java110.dto.app.AppDto; |
| | | import com.java110.utils.constant.CommonConstant; |
| | | import com.java110.utils.constant.ServiceConstant; |
| | | import com.java110.utils.exception.SMOException; |
| | | import com.java110.utils.factory.ApplicationContextFactory; |
| | | import com.java110.utils.util.BeanConvertUtil; |
| | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.http.*; |
| | | import org.springframework.web.client.HttpStatusCodeException; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | |
| | | * @param <T> |
| | | * @return |
| | | */ |
| | | public static <T> T postForApi(String appId, T param, String serviceCode, Class<T> t) { |
| | | public static <T> T postForApi(String appId, T param, String serviceCode, Class<T> t, String userId) { |
| | | |
| | | IPageData pd = PageData.newInstance().builder("-1", "未知", "", "", "", "", "", "", appId); |
| | | IPageData pd = PageData.newInstance().builder(userId, "未知", "", "", "", "", "", "", appId); |
| | | |
| | | List<T> ts = postForApis(pd, param, serviceCode, t); |
| | | |
| | |
| | | } |
| | | |
| | | return ts.get(0); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 查询 |
| | | * |
| | | * @param param 传入对象 |
| | | * @param serviceCode 服务编码 |
| | | * @param t 返回类 |
| | | * @param <T> |
| | | * @return |
| | | */ |
| | | public static <T> T postForApi(String appId, T param, String serviceCode, Class<T> t) { |
| | | |
| | | return postForApi(appId, param, serviceCode, t, "-1"); |
| | | } |
| | | |
| | | |
| | |
| | | if (pd.getHeaders() != null) { |
| | | for (String key : pd.getHeaders().keySet() |
| | | ) { |
| | | if(CommonConstant.USER_ID.equals(key.toLowerCase())){ |
| | | if (CommonConstant.USER_ID.equals(key.toLowerCase())) { |
| | | continue; |
| | | } |
| | | header.add(key, pd.getHeaders().get(key).toString()); |
| | |
| | | header.add(CommonConstant.HTTP_USER_ID.toLowerCase(), StringUtil.isEmpty(pd.getUserId()) ? CommonConstant.ORDER_DEFAULT_USER_ID : pd.getUserId()); |
| | | |
| | | header.add(CommonConstant.USER_ID.toLowerCase(), StringUtil.isEmpty(pd.getUserId()) ? CommonConstant.ORDER_DEFAULT_USER_ID : pd.getUserId()); |
| | | header.add(CommonConstant.HTTP_TRANSACTION_ID.toLowerCase(), StringUtil.isEmpty(pd.getTransactionId())?GenerateCodeFactory.getUUID():pd.getTransactionId()); |
| | | header.add(CommonConstant.HTTP_REQ_TIME.toLowerCase(), StringUtil.isEmpty(pd.getRequestTime())?DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_DEFAULT):pd.getRequestTime()); |
| | | header.add(CommonConstant.HTTP_TRANSACTION_ID.toLowerCase(), StringUtil.isEmpty(pd.getTransactionId()) ? GenerateCodeFactory.getUUID() : pd.getTransactionId()); |
| | | header.add(CommonConstant.HTTP_REQ_TIME.toLowerCase(), StringUtil.isEmpty(pd.getRequestTime()) ? DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_DEFAULT) : pd.getRequestTime()); |
| | | header.add(CommonConstant.HTTP_SIGN.toLowerCase(), ""); |
| | | |
| | | HttpEntity<String> httpEntity = new HttpEntity<String>(param, header); |
| | |
| | | try { |
| | | responseEntity = restTemplate.exchange(url, httpMethod, httpEntity, String.class); |
| | | } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下 |
| | | logger.error("调用中心服务失败",e); |
| | | logger.error("调用中心服务失败", e); |
| | | responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode()); |
| | | } catch (Exception e) { |
| | | logger.error("调用中心服务失败",e); |
| | | logger.error("调用中心服务失败", e); |
| | | responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); |
| | | } finally { |
| | | logger.debug("请求地址为,{} 请求中心服务信息,{},中心服务返回信息,{}", url, httpEntity, responseEntity); |
| | |
| | | return communitySettingDto.getSettingValue(); |
| | | } |
| | | |
| | | /** |
| | | * 查询设置值 |
| | | * |
| | | * @param communityId |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public static String getRemark(String communityId, String key) { |
| | | Jedis redis = null; |
| | | CommunitySettingDto communitySettingDto = null; |
| | | try { |
| | | redis = getJedis(); |
| | | Object object = SerializeUtil.unserialize(redis.get((communityId + "_" + key + "_community_setting").getBytes())); |
| | | if (object == null) {//这里存在并发问题,但是 等于查询了多次 然后多次写缓存,作者认为 这种应该比加全局锁效率高些 |
| | | communitySettingDto = getCommunitySettingFromDb(communityId, key, redis); |
| | | } else { |
| | | communitySettingDto = (CommunitySettingDto) object; |
| | | } |
| | | } finally { |
| | | if (redis != null) { |
| | | redis.close(); |
| | | } |
| | | } |
| | | |
| | | if (communitySettingDto == null) { |
| | | return null; |
| | | } |
| | | |
| | | return communitySettingDto.getRemark(); |
| | | } |
| | | |
| | | public static CommunitySettingDto getCommunitySettingFromDb(String communityId, String key) { |
| | | Jedis redis = null; |
| | | try { |
| New file |
| | |
| | | package com.java110.acct.cmd.payment; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.java110.acct.smo.IQrCodePaymentSMO; |
| | | import com.java110.core.annotation.Java110Cmd; |
| | | import com.java110.core.context.ICmdDataFlowContext; |
| | | import com.java110.core.event.cmd.AbstractServiceCmdListener; |
| | | import com.java110.core.event.cmd.CmdEvent; |
| | | import com.java110.core.factory.CallApiServiceFactory; |
| | | import com.java110.core.factory.GenerateCodeFactory; |
| | | import com.java110.utils.constant.CommonConstant; |
| | | import com.java110.utils.exception.CmdException; |
| | | import com.java110.utils.factory.ApplicationContextFactory; |
| | | import com.java110.utils.util.Assert; |
| | | import com.java110.utils.util.StringUtil; |
| | | import com.java110.vo.ResultVo; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.ResponseEntity; |
| | | |
| | | /** |
| | | * 扫码付 |
| | | */ |
| | | @Java110Cmd(serviceCode = "payment.checkPayFinish") |
| | | public class CheckPayFinishCmd extends AbstractServiceCmdListener { |
| | | |
| | | |
| | | private IQrCodePaymentSMO qrCodePaymentSMOImpl; |
| | | |
| | | @Override |
| | | public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) { |
| | | Assert.hasKeyAndValue(reqJson, "authCode", "未包含授权码"); |
| | | } |
| | | |
| | | @Override |
| | | public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException { |
| | | String orderId = GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_orderId); |
| | | double receivedAmount = Double.parseDouble(reqJson.getString("receivedAmount")); |
| | | String authCode = reqJson.getString("authCode"); |
| | | if (StringUtil.isEmpty(authCode) || authCode.length() < 2) { |
| | | throw new IllegalArgumentException("授权码错误"); |
| | | } |
| | | |
| | | int pre = Integer.parseInt(authCode.substring(0, 2)); |
| | | if (pre > 24 && pre < 31) { // 支付宝 |
| | | qrCodePaymentSMOImpl = ApplicationContextFactory.getBean("qrCodeAliPaymentAdapt", IQrCodePaymentSMO.class); |
| | | } else { |
| | | qrCodePaymentSMOImpl = ApplicationContextFactory.getBean("qrCodeWechatPaymentAdapt", IQrCodePaymentSMO.class); |
| | | } |
| | | |
| | | ResultVo resultVo = null; |
| | | try { |
| | | resultVo = qrCodePaymentSMOImpl.checkPayFinish(reqJson.getString("communityId"), orderId); |
| | | } catch (Exception e) { |
| | | cmdDataFlowContext.setResponseEntity(ResultVo.error(e.getLocalizedMessage())); |
| | | return; |
| | | } |
| | | if (ResultVo.CODE_OK != resultVo.getCode()) { |
| | | cmdDataFlowContext.setResponseEntity(ResultVo.createResponseEntity(resultVo)); |
| | | return; |
| | | } |
| | | String appId = cmdDataFlowContext.getReqHeaders().get(CommonConstant.APP_ID); |
| | | String userId = cmdDataFlowContext.getReqHeaders().get(CommonConstant.USER_ID); |
| | | String paramOut = CallApiServiceFactory.postForApi(appId, reqJson.toJSONString(), "fee.payFee", String.class, userId); |
| | | cmdDataFlowContext.setResponseEntity(new ResponseEntity(paramOut, HttpStatus.OK)); |
| | | } |
| | | |
| | | } |
| | |
| | | package com.java110.acct.cmd.payment; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alipay.api.AlipayApiException; |
| | | import com.alipay.api.AlipayClient; |
| | | import com.alipay.api.DefaultAlipayClient; |
| | | import com.alipay.api.domain.AlipayTradeAppPayModel; |
| | | import com.alipay.api.request.AlipayTradeAppPayRequest; |
| | | import com.alipay.api.response.AlipayTradeAppPayResponse; |
| | | import com.java110.acct.smo.IQrCodePaymentSMO; |
| | | import com.java110.core.annotation.Java110Cmd; |
| | | import com.java110.core.context.ICmdDataFlowContext; |
| | | import com.java110.core.event.cmd.AbstractServiceCmdListener; |
| | | import com.java110.core.event.cmd.CmdEvent; |
| | | import com.java110.core.factory.CallApiServiceFactory; |
| | | import com.java110.core.factory.GenerateCodeFactory; |
| | | import com.java110.utils.constant.CommonConstant; |
| | | import com.java110.utils.exception.CmdException; |
| | | import com.java110.utils.factory.ApplicationContextFactory; |
| | | import com.java110.utils.util.Assert; |
| | | import com.java110.utils.util.StringUtil; |
| | | import com.java110.vo.ResultVo; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.ResponseEntity; |
| | | |
| | | /** |
| | | * 扫码付 |
| | |
| | | public class QrCodePaymentCmd extends AbstractServiceCmdListener { |
| | | |
| | | |
| | | private IQrCodePaymentSMO qrCodePaymentSMOImpl; |
| | | |
| | | @Override |
| | | public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) { |
| | | |
| | | Assert.hasKeyAndValue(reqJson, "authCode", "未包含授权码"); |
| | | } |
| | | |
| | | @Override |
| | | public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException { |
| | | String orderId = GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_orderId); |
| | | double receivedAmount = Double.parseDouble(reqJson.getString("receivedAmount")); |
| | | String authCode = reqJson.getString("authCode"); |
| | | if (StringUtil.isEmpty(authCode) || authCode.length() < 2) { |
| | | throw new IllegalArgumentException("授权码错误"); |
| | | } |
| | | |
| | | int pre = Integer.parseInt(authCode.substring(0, 2)); |
| | | if (pre > 24 && pre < 31) { // 支付宝 |
| | | qrCodePaymentSMOImpl = ApplicationContextFactory.getBean("qrCodeAliPaymentAdapt", IQrCodePaymentSMO.class); |
| | | }else{ |
| | | qrCodePaymentSMOImpl = ApplicationContextFactory.getBean("qrCodeWechatPaymentAdapt", IQrCodePaymentSMO.class); |
| | | } |
| | | |
| | | ResultVo resultVo = null; |
| | | try { |
| | | resultVo = qrCodePaymentSMOImpl.pay(reqJson.getString("communityId"), orderId, receivedAmount, authCode, ""); |
| | | } catch (Exception e) { |
| | | cmdDataFlowContext.setResponseEntity(ResultVo.error(e.getLocalizedMessage())); |
| | | return; |
| | | } |
| | | if (ResultVo.CODE_OK != resultVo.getCode()) { |
| | | cmdDataFlowContext.setResponseEntity(ResultVo.error(resultVo.getMsg(),reqJson)); |
| | | return; |
| | | } |
| | | String appId = cmdDataFlowContext.getReqHeaders().get(CommonConstant.APP_ID); |
| | | String userId = cmdDataFlowContext.getReqHeaders().get(CommonConstant.USER_ID); |
| | | String paramOut = CallApiServiceFactory.postForApi(appId, reqJson.toJSONString(), "fee.payFee", String.class, userId); |
| | | cmdDataFlowContext.setResponseEntity(new ResponseEntity(paramOut, HttpStatus.OK)); |
| | | } |
| | | |
| | | } |
| | |
| | | import com.alipay.api.response.AlipayTradePayResponse; |
| | | import com.alipay.api.response.AlipayTradeQueryResponse; |
| | | import com.java110.acct.smo.IQrCodePaymentSMO; |
| | | import com.java110.core.factory.CommunitySettingFactory; |
| | | import com.java110.utils.cache.MappingCache; |
| | | import com.java110.utils.constant.WechatConstant; |
| | | import com.java110.vo.ResultVo; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | /** |
| | | * 阿里支付 |
| | | */ |
| | | @Service |
| | | public class QrCodeAliPaymentAdapt implements IQrCodePaymentSMO { |
| | | /** |
| | | * APP_ID 应用id |
| | |
| | | /** |
| | | * (沙箱)网关 |
| | | */ |
| | | public final static String GETEWAY_URL = "https://openapi.alipaydev.com/gateway.do"; |
| | | public final static String GETEWAY_URL = "https://openapi.alipay.com/gateway.do"; |
| | | |
| | | /** |
| | | * 格式化 |
| | |
| | | public ResultVo pay(String communityId, String orderNum, double money, String authCode, String feeName) throws Exception { |
| | | String systemName = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.PAY_GOOD_NAME); |
| | | |
| | | AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", |
| | | "app_id", |
| | | "your private_key", "json", "GBK", "alipay_public_key", "RSA2"); |
| | | AlipayClient alipayClient = new DefaultAlipayClient(GETEWAY_URL, |
| | | CommunitySettingFactory.getValue(communityId,"APP_ID"), |
| | | CommunitySettingFactory.getRemark(communityId,"APP_PRIVATE_KEY"), |
| | | "json", "utf-8", CommunitySettingFactory.getRemark(communityId,"ALIPAY_PUBLIC_KEY"), "RSA2"); |
| | | AlipayTradePayRequest request = new AlipayTradePayRequest(); |
| | | JSONObject bizContent = new JSONObject(); |
| | | bizContent.put("out_trade_no", orderNum); |
| | |
| | | AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "app_id", "your private_key", "json", "GBK", "alipay_public_key", "RSA2"); |
| | | AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); |
| | | request.setBizContent("{" + |
| | | " \"out_trade_no\":\"20150320010101001\"," + |
| | | " \"trade_no\":\"2014112611001004680 073956707\"," + |
| | | " \"out_trade_no\":\""+orderNum+"\"," + |
| | | " \"trade_no\":\"\"," + |
| | | " \"query_options\":[" + |
| | | " \"trade_settle_info\"" + |
| | | " ]" + |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | /** |
| | | * 微信支付 |
| | | */ |
| | | @Service |
| | | public class QrCodeWechatPaymentAdapt implements IQrCodePaymentSMO { |
| | | private static Logger logger = LoggerFactory.getLogger(QrCodeWechatPaymentAdapt.class); |
| | | |