| @@ -158,7 +158,7 @@ public class AlipayController extends BaseController { | |||
| } | |||
| if (!StringUtils.isBlank(wxMall.getAlipayMemberTemplateId())) { | |||
| return new ResultData(Result.ERROR,"已经创建过模板,不能重复创建"); | |||
| return new ResultData(Result.ERROR,"已经创建过模板,不能重复创建. 重新生成,则C端的授权地址需要重新生成"); | |||
| } | |||
| try { | |||
| @@ -38,6 +38,18 @@ public class AliPayController extends BaseController { | |||
| @Autowired | |||
| private AliPayCUserService aliPayCUserService; | |||
| private void writeResponse(HttpServletResponse response,String msg) { | |||
| PrintWriter out; | |||
| try { | |||
| out = response.getWriter(); | |||
| out.print(msg); | |||
| out.close(); | |||
| } catch (IOException e) { | |||
| logger.error(" alipayCallback writeResponse error.",e); | |||
| } | |||
| } | |||
| /** | |||
| * 支付宝客户端回调接口 | |||
| * 1. 商圈授权给isv后的回调。 isv账号登陆开放平台(https://openhome.alipay.com),选择平台应用,菜单“商家授权应用-发起授权”,复制链接或二维码给商圈支付宝账号扫。 | |||
| @@ -55,28 +67,32 @@ public class AliPayController extends BaseController { | |||
| if (null == outString) { | |||
| outString = ""; | |||
| } | |||
| PrintWriter out = response.getWriter(); | |||
| //商家授权给ISV后的回调 | |||
| if ("alipay_app_auth".equals(source)) { | |||
| String tenantId = request.getParameter("state"); | |||
| if (StringUtils.isBlank(tenantId)) { | |||
| out.print("Param Error!tenantId is null."); | |||
| writeResponse(response,"Param Error!tenantId is null."); | |||
| return; | |||
| }else { | |||
| WxMall wxMall = mallService.getByTenantId(tenantId); | |||
| if (null == wxMall ) { | |||
| out.print("Auth Failed!wxMall not fond."); | |||
| writeResponse(response,"Auth Failed!wxMall not fond."); | |||
| return; | |||
| }else { | |||
| String appAuthCode = request.getParameter("app_auth_code"); | |||
| if (StringUtils.isBlank(appAuthCode)) { | |||
| out.print("Auth Failed!app_auth_code is null."); | |||
| writeResponse(response,"Auth Failed!app_auth_code is null."); | |||
| return; | |||
| }else { | |||
| String token = alipayUtil.getAppAuthToken(appAuthCode); | |||
| if (StringUtils.isBlank(token)) { | |||
| out.print("Auth Failed!token is empity , Retry."); | |||
| writeResponse(response,"Auth Failed!token is empity , Retry."); | |||
| return; | |||
| }else { | |||
| wxMall.setAlipayAppAuthToken(token); | |||
| mallService.update(wxMall); | |||
| out.print("Auth success!."); | |||
| writeResponse(response,"Auth success!."); | |||
| return; | |||
| } | |||
| } | |||
| } | |||
| @@ -90,22 +106,27 @@ public class AliPayController extends BaseController { | |||
| String tenantId = args[1]; | |||
| WxMall wxMall = mallService.getByTenantId(tenantId); | |||
| if (null == wxMall ) { | |||
| out.print("Auth Failed!wxMall not fond."); | |||
| writeResponse(response,"Auth Failed!wxMall not fond."); | |||
| return; | |||
| } | |||
| if (null == wxMall.getAlipayAppAuthToken() || "".equals(wxMall.getAlipayAppAuthToken()) ) { | |||
| out.print("Auth Failed!wxMall alipayAppToken is null."); | |||
| writeResponse(response,"Auth Failed!wxMall alipayAppToken is null."); | |||
| return; | |||
| }else { | |||
| UserAuthData authData = alipayUtil.queryUserAuthData(wxMall.getAlipayAppAuthToken(), authCode); | |||
| if (null == authData ) { | |||
| out.print("Auth Failed!queryUserAuthData return null."); | |||
| writeResponse(response,"Auth Failed!queryUserAuthData return null."); | |||
| return; | |||
| }else { | |||
| Map<EnumMemberCardConfig,Object> map = alipayUtil.queryUserFormData(wxMall.getAlipayAppAuthToken(), authData.getAccessToken(), templateId, requestId); | |||
| if (null == map) { | |||
| out.print("Auth Failed!queryUserFormData return null."); | |||
| writeResponse(response,"Auth Failed!queryUserFormData return null."); | |||
| return; | |||
| }else { | |||
| String phone = (String) map.get(EnumMemberCardConfig.OPEN_FORM_FIELD_MOBILE); | |||
| if (StringUtils.isBlank(phone)) { | |||
| out.print("Auth Failed! Phone is Empity."); | |||
| writeResponse(response,"Auth Failed! Phone is Empity."); | |||
| return; | |||
| }else { | |||
| //查询该userId是否已经开卡,已经开卡不再开卡 | |||
| AliPayCUser query = new AliPayCUser(); | |||
| @@ -126,20 +147,18 @@ public class AliPayController extends BaseController { | |||
| //跳转到商圈积分授权页面 | |||
| String pointsUrl = alipayUtil.getH5SmartDistrictMallVipPointsUrl(tenantId); | |||
| if (null != alicuser.getCardOpen() && alicuser.getCardOpen() > 0) { | |||
| out.print("Auth success!."); | |||
| out.close(); | |||
| response.sendRedirect(pointsUrl); | |||
| return; | |||
| }else { | |||
| boolean opencard = alipayUtil.openCard(wxMall.getAlipayAppAuthToken(), authData.getAccessToken(), templateId, authData.getUserId()); | |||
| if (opencard) { | |||
| alicuser.setCardOpen(1); | |||
| aliPayCUserService.saveOrUpdate(alicuser); | |||
| out.print("Auth success!."); | |||
| out.close(); | |||
| //跳转到商圈积分授权页面 | |||
| response.sendRedirect(pointsUrl); | |||
| }else { | |||
| out.print("Auth Failed!"); | |||
| writeResponse(response,"Auth Failed!"); | |||
| return; | |||
| } | |||
| } | |||
| } | |||
| @@ -147,12 +166,9 @@ public class AliPayController extends BaseController { | |||
| } | |||
| }//商圈节分授权 | |||
| }else if ("mall_vip_points".equals(scope)) { | |||
| out.print("Auth success!."); | |||
| writeResponse(response,"Auth success!."); | |||
| return; | |||
| } | |||
| try { | |||
| out.close(); | |||
| }catch(Exception e) { | |||
| } | |||
| } | |||
| /** | |||
| @@ -230,12 +230,18 @@ public class AliPayUtil { | |||
| public UserAuthData queryUserAuthData(String appAuthToken,String authCode) { | |||
| try { | |||
| AlipayOpenApiGenericResponse response = AliPayApi.queryUserAuthData(getConfig(), appAuthToken, authCode); | |||
| JSONObject result = getGenericResponse(response.getHttpBody(),"alipay_system_oauth_token_response"); | |||
| if (null != result) { | |||
| UserAuthData authData = new UserAuthData(); | |||
| authData.setUserId(result.getString("user_id")); | |||
| authData.setAccessToken(result.getString("access_token")); | |||
| return authData; | |||
| //JSONObject result = getGenericResponse(response.getHttpBody(),"alipay_system_oauth_token_response"); | |||
| JSONObject responseResult = JSON.parseObject(response.getHttpBody()); | |||
| if (null != responseResult) { | |||
| JSONObject result = responseResult.getJSONObject("alipay_system_oauth_token_response"); | |||
| if (null != result) { | |||
| UserAuthData authData = new UserAuthData(); | |||
| authData.setUserId(result.getString("user_id")); | |||
| authData.setAccessToken(result.getString("access_token")); | |||
| return authData; | |||
| }else { | |||
| log.error("alipay queryUserAuthData error. "+response.getHttpBody()); | |||
| } | |||
| } | |||
| } catch (Exception e) { | |||
| log.error("alipay queryUserAuthData error. ",e); | |||
| @@ -261,8 +267,8 @@ public class AliPayUtil { | |||
| Map<EnumMemberCardConfig,Object> map = new HashMap<EnumMemberCardConfig,Object>(); | |||
| for (int i = 0 ; i < infosArray.size(); i++) { | |||
| Map<String,Object> jo = (Map<String,Object>)infosArray.get(i); | |||
| String[] keys = (String[]) jo.keySet().toArray(); | |||
| EnumMemberCardConfig config = EnumMemberCardConfig.getEnum(keys[0]); | |||
| Object[] keys = jo.keySet().toArray(); | |||
| EnumMemberCardConfig config = EnumMemberCardConfig.getEnum(String.valueOf(keys[0])); | |||
| if (null != config) { | |||
| map.put(config, jo.get(keys[0])); | |||
| } | |||
| @@ -295,7 +301,7 @@ public class AliPayUtil { | |||
| //商圈消息订阅 | |||
| public boolean smartDistrictTopicSubscribe(String appAuthToken) { | |||
| try { | |||
| AlipayOpenApiGenericResponse response = AliPayApi.topicSubscribe(getConfig(),appAuthToken,appAuthToken, "app_auth", "alipay.open.auth.appauth.cancelled", "HTTP", "BIZ_TAG"); | |||
| AlipayOpenApiGenericResponse response = AliPayApi.topicSubscribe(getConfig(),appAuthToken, "app_auth", "alipay.open.auth.appauth.cancelled", "HTTP", "BIZ_TAG"); | |||
| JSONObject result = getGenericResponse(response.getHttpBody(),"alipay_open_app_message_topic_subscribe_response"); | |||
| if (null != result) { | |||
| return true; | |||
| @@ -329,17 +335,17 @@ public class AliPayUtil { | |||
| return buffer; | |||
| } | |||
| public static void main(String[] args) { | |||
| AliPayUtil util = new AliPayUtil(); | |||
| // String token = getAppAuthToken("Pfbe94a5103a0414db99ce865204ee63"); | |||
| // System.out.println(token); | |||
| //File file = new File("C://logo-img.png"); | |||
| //System.out.println(util.merchantImageUpload("202104BB054c88e950ba4513854e4275ff71cF63", "aa.jpg", File2byte(file))); | |||
| //System.out.println(util.createSmartDistrictMemberCardModel("202104BB054c88e950ba4513854e4275ff71cF63", "会员卡", "OKJx3oOPTUOaINs0AQ_qMgAAACMAAQQD", "OKJx3oOPTUOaINs0AQ_qMgAAACMAAQQD")); | |||
| //System.out.println(util.setSmartDistrictMemberCardModelConfig("202104BB054c88e950ba4513854e4275ff71cF63", "20210417000000002702655000300637")); | |||
| //System.out.println(util.getSmartDistrictMemberCardUrl("202104BB054c88e950ba4513854e4275ff71cF63", "20210417000000002702655000300637", "https://ctest.malls.iformall.com/C/api/alipay/callback", "123")); | |||
| //System.out.println(util.getH5SmartDistrictMallVipPointsUrl("2021002139648762", "https://ctest.malls.iformall.com/C/api/alipay/callback", "123")); | |||
| System.out.println(util.smartDistrictTopicSubscribe("202104BB054c88e950ba4513854e4275ff71cF63")); | |||
| } | |||
| // public static void main(String[] args) { | |||
| // AliPayUtil util = new AliPayUtil(); | |||
| //// String token = getAppAuthToken("Pfbe94a5103a0414db99ce865204ee63"); | |||
| //// System.out.println(token); | |||
| // //File file = new File("C://logo-img.png"); | |||
| // //System.out.println(util.merchantImageUpload("202104BB054c88e950ba4513854e4275ff71cF63", "aa.jpg", File2byte(file))); | |||
| // //System.out.println(util.createSmartDistrictMemberCardModel("202104BB054c88e950ba4513854e4275ff71cF63", "会员卡", "OKJx3oOPTUOaINs0AQ_qMgAAACMAAQQD", "OKJx3oOPTUOaINs0AQ_qMgAAACMAAQQD")); | |||
| // //System.out.println(util.setSmartDistrictMemberCardModelConfig("202104BB054c88e950ba4513854e4275ff71cF63", "20210417000000002702655000300637")); | |||
| // //System.out.println(util.getSmartDistrictMemberCardUrl("202104BB054c88e950ba4513854e4275ff71cF63", "20210417000000002702655000300637", "https://ctest.malls.iformall.com/C/api/alipay/callback", "123")); | |||
| // //System.out.println(util.getH5SmartDistrictMallVipPointsUrl("2021002139648762", "https://ctest.malls.iformall.com/C/api/alipay/callback", "123")); | |||
| // System.out.println(util.smartDistrictTopicSubscribe("202104BB054c88e950ba4513854e4275ff71cF63")); | |||
| // } | |||
| } | |||
| @@ -221,13 +221,10 @@ public class AliPayApi { | |||
| //设置系统参数(OpenAPI中非biz_content里的参数) | |||
| Map<String, String> textParams = new HashMap<String, String>(); | |||
| textParams.put("app_auth_token", appAuthToken); | |||
| //设置业务参数(OpenAPI中biz_content里的参数) | |||
| Map<String, Object> bizParams = new HashMap<String, Object>(); | |||
| bizParams.put("grant_type", "authorization_code"); | |||
| bizParams.put("authorization_code", authCode); | |||
| textParams.put("grant_type", "authorization_code"); | |||
| textParams.put("code", authCode); | |||
| AlipayOpenApiGenericResponse response = Factory.Util.Generic().execute( | |||
| "alipay.system.oauth.token", textParams, bizParams); | |||
| "alipay.system.oauth.token", textParams, null); | |||
| return response; | |||
| } | |||
| @@ -281,6 +278,7 @@ public class AliPayApi { | |||
| Map<String,Object> extInfoMap = new HashMap<String,Object>(); | |||
| extInfoMap.put("open_date", new Date()); | |||
| extInfoMap.put("valid_date", DateUtils.stringToDate("2051-05-01 00:00:00",DateUtils.DATE_TIME_PATTERN)); | |||
| extInfoMap.put("external_card_no",String.valueOf(idWorker.nextId())); | |||
| bizParams.put("card_ext_info", extInfoMap); | |||
| AlipayOpenApiGenericResponse response = Factory.Util.Generic().execute( | |||
| @@ -292,14 +290,18 @@ public class AliPayApi { | |||
| /** | |||
| * https://opendocs.alipay.com/apis/api_9/alipay.open.app.message.topic.subscribe | |||
| * 订阅消息主题 | |||
| * | |||
| * 需要联系BD 给商圈挂载“支付宝商圈交易成功信息订阅”功能包 | |||
| * https://openhome.alipay.com/svr/ability/solution/SC00001010/xxdy (对接会员卡的应用AppId用ISV的应用ID) | |||
| * | |||
| * @return | |||
| * @throws Exception | |||
| */ | |||
| public static AlipayOpenApiGenericResponse topicSubscribe(Config config,String appAuthToken,String authToken,String authType,String topic,String type,String tag) throws Exception { | |||
| public static AlipayOpenApiGenericResponse topicSubscribe(Config config,String authToken,String authType,String topic,String type,String tag) throws Exception { | |||
| Factory.setOptions(config); | |||
| //设置系统参数(OpenAPI中非biz_content里的参数) | |||
| Map<String, String> textParams = new HashMap<String, String>(); | |||
| textParams.put("app_auth_token", appAuthToken); | |||
| //Map<String, String> textParams = new HashMap<String, String>(); | |||
| //textParams.put("app_auth_token", appAuthToken); | |||
| //设置业务参数(OpenAPI中biz_content里的参数) | |||
| Map<String, Object> bizParams = new HashMap<String, Object>(); | |||
| @@ -311,7 +313,7 @@ public class AliPayApi { | |||
| bizParams.put("tag",tag); | |||
| } | |||
| AlipayOpenApiGenericResponse response = Factory.Util.Generic().execute( | |||
| "alipay.open.app.message.topic.subscribe", textParams, bizParams); | |||
| "alipay.open.app.message.topic.subscribe", null, bizParams); | |||
| return response; | |||
| } | |||
| } | |||