微信支付出现的问题总结--不同域名进行授权解决方案

    1、如果使用的是微信支付的方式,并且使用的是native的支付方式,那么需要在服务器上生成一张二维码,注意二维码的路径问题:
  1. String imgName = UUID.randomUUID().toString().replace("-", "") +".png";
  2. String QRPath = this.getServletContext().getRealPath("/pay/QRIMG/wx") + imgName;
    使用上述的方式可以解决路径问题;在项目中直接引用该文件就可以了
   2、在多个项目中,由于微信的授权登陆只能配置一个,如下图(不同域名进行授权解决方案):
微信支付出现的问题总结--不同域名进行授权解决方案
微信支付出现的问题总结--不同域名进行授权解决方案
此时我们想获得到微信授权到不同的域名,此时的方法为:
微信支付出现的问题总结--不同域名进行授权解决方案


这里贴出2个主要的授权方法:
 
微信支付出现的问题总结--不同域名进行授权解决方案解析方法;
  1. package com.huawei.nser.wap.wxtransformation;
  2. import java.util.Enumeration;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.UUID;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.struts.action.Action;
  9. import org.apache.struts.action.ActionForm;
  10. import org.apache.struts.action.ActionForward;
  11. import org.apache.struts.action.ActionMapping;
  12. import org.jfree.util.Log;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import com.huawei.nser.pub.SJBUtil;
  16. import com.huawei.nser.wap.wxtransformation.model.TransfmMessageBean;
  17. import com.huawei.nser.wap.wxtransformation.service.TransfmMessageService;
  18. /**
  19. *
  20. * @author 获取微信id并跳转
  21. *
  22. */
  23. public class WapTransWXAction extends Action{
  24. private Logger log = LoggerFactory.getLogger(WapTransWXAction.class);
  25. private TransfmMessageService transfmMessageService ;
  26. @Override
  27. public ActionForward execute(ActionMapping mapping, ActionForm form,
  28. HttpServletRequest request, HttpServletResponse response) throws Exception {
  29. log.info("@@@@@ WapTransWXAction.execute enter this method");
  30. transfmMessageService = (TransfmMessageService) SJBUtil.getBean("transfmMessage");
  31. //获取信息并保持内容
  32. String id = UUID.randomUUID().toString().replace("-", "");
  33. String field1 = request.getParameter("field1");
  34. String field2 = request.getParameter("field2");
  35. String field3 = request.getParameter("field3");
  36. String call_back_url = request.getParameter("callBackUrl");
  37. TransfmMessageBean transfmMessageBean = new TransfmMessageBean();
  38. transfmMessageBean.setId(id);
  39. transfmMessageBean.setField1(field1);
  40. transfmMessageBean.setField2(field2);
  41. transfmMessageBean.setField3(field3);
  42. transfmMessageBean.setCall_back_url(call_back_url);
  43. //step1 保存转换信息
  44. transfmMessageService.saveTransFmMessage(transfmMessageBean);
  45. String authorizeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4b19bad466392998&redirect_uri=http://babyhhcsy.imwork.net/wapnew/ngwap/weixinopenid.do?id="+id+"&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
  46. log.info("@@@@@ WapTransWXAction.execute get authorizeurl is :{}",authorizeUrl);
  47. //step 3 调取微信;
  48. response.sendRedirect(authorizeUrl);
  49. return null;
  50. }
  51. public Map<String, String> request2Map(HttpServletRequest request) {
  52. Map<String, String> map = new HashMap<String, String>();
  53. Enumeration<String> names = request.getParameterNames();
  54. while (names.hasMoreElements()) {
  55. String key = names.nextElement();
  56. String value = request.getParameter(key);
  57. if (value == null || value.trim().equals("")) {
  58. continue;
  59. }
  60. map.put(key, value);
  61. }
  62. return map;
  63. }
  64. public String map2String(Map<String,String> map){
  65. StringBuffer sb = new StringBuffer();
  66. for (Map.Entry<String, String> entry : map.entrySet()) {
  67. sb.append(entry.getKey()+"="+entry.getValue()+";");
  68. }
  69. Log.info(sb.toString());
  70. return sb.toString();
  71. }
  72. public TransfmMessageService getTransfmMessageService() {
  73. return transfmMessageService;
  74. }
  75. public void setTransfmMessageService(TransfmMessageService transfmMessageService) {
  76. this.transfmMessageService = transfmMessageService;
  77. }
  78. }
微信支付出现的问题总结--不同域名进行授权解决方案解析方法:
  1. package com.huawei.nser.wap.wxtransformation;
  2. import java.io.IOException;
  3. import java.util.Enumeration;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import net.sf.json.JSONObject;
  10. import org.apache.commons.lang.StringUtils;
  11. import org.apache.struts.action.Action;
  12. import org.apache.struts.action.ActionForm;
  13. import org.apache.struts.action.ActionForward;
  14. import org.apache.struts.action.ActionMapping;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import com.huawei.nser.pub.SJBUtil;
  18. import com.huawei.nser.wap.bankPay.util.HttpsRequestUtil;
  19. import com.huawei.nser.wap.wxtransformation.model.TransfmMessageBean;
  20. import com.huawei.nser.wap.wxtransformation.service.TransfmMessageService;
  21. /**
  22. * 微信转换类,获得code内容
  23. * @author thero
  24. *
  25. */
  26. public class WXTransAction extends Action {
  27. private Logger log = LoggerFactory.getLogger(WapTransWXAction.class);
  28. private static final String code2openId = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=myappid&secret=mysecret&code=mycode&grant_type=authorization_code";
  29. private static final long serialVersionUID = 1L;
  30. @Override
  31. public ActionForward execute(ActionMapping mapping, ActionForm form,
  32. HttpServletRequest request, HttpServletResponse response) throws Exception {
  33. String code = request.getParameter("code");
  34. log.info("AuthorizeUserServlet.doPost get code is :{},get paramter is {}",code,request.getQueryString());
  35. //step 1 获得id ;
  36. String id = request.getParameter("id");
  37. log.info("@@@@@ get url from wx is :{},and id is",request.getRequestURL(),id);
  38. //step 2 从数据库查询id并获得url;
  39. TransfmMessageService transfmMessageService = (TransfmMessageService) SJBUtil.getBean("transfmMessage");
  40. TransfmMessageBean dbtransfmMessage = transfmMessageService.querytransfmMessage(id);
  41. if(dbtransfmMessage!=null){
  42. String call_back_url = dbtransfmMessage.getCall_back_url();
  43. String openid = this.code2OpenId(request, code, response);
  44. dbtransfmMessage.setOpenid(openid);
  45. transfmMessageService.saveTransFmMessage2(dbtransfmMessage);
  46. StringBuffer sb = new StringBuffer();
  47. sb.append(call_back_url);
  48. sb.append("?openid="+openid);
  49. if(null!=dbtransfmMessage.getField1()&& !"".equals(dbtransfmMessage.getField1())){
  50. sb.append("&fields1="+dbtransfmMessage.getField1());
  51. }
  52. if(null!=dbtransfmMessage.getField2()&& !"".equals(dbtransfmMessage.getField2())){
  53. sb.append("&fields2="+dbtransfmMessage.getField2());
  54. }
  55. if(null!=dbtransfmMessage.getField3()&& !"".equals(dbtransfmMessage.getField3())){
  56. sb.append("&fields3="+dbtransfmMessage.getField3());
  57. }
  58. response.sendRedirect(sb.toString());
  59. return null;
  60. }
  61. return null;
  62. }
  63. public String code2OpenId(HttpServletRequest request,String code, HttpServletResponse response) throws ServletException, IOException{
  64. String openId = null;
  65. String tempUrl = code2openId.replace("myappid", "wxXXXXX8").replace("mysecret", "13XXXXXXc66").replace("mycode",code);
  66. //String tempUrl = code2openId.replace("myappid", "wXXXXXf").replace("mysecret", "7aXXXXXc28").replace("mycode",code);
  67. log.info("AuthorizeUserServlet.code2OpenId get tempUrl is :{}",tempUrl);
  68. JSONObject result = HttpsRequestUtil.httpsRequest(tempUrl, HttpsRequestUtil.POST,"");
  69. log.info("AuthorizeUserServlet.code2OpenId get openid is :{}",result.toString());
  70. if(!(null!=result && result.get("openid")!=null)){
  71. return null;
  72. }else{
  73. return result.get("openid").toString();
  74. }
  75. }
  76. public Map<String, String> request2Map(HttpServletRequest request) {
  77. Map<String, String> map = new HashMap<String, String>();
  78. Enumeration<String> names = request.getParameterNames();
  79. while (names.hasMoreElements()) {
  80. String key = names.nextElement();
  81. String value = request.getParameter(key);
  82. if (value == null || value.trim().equals("")) {
  83. continue;
  84. }
  85. map.put(key, value);
  86. }
  87. return map;
  88. }
  89. public String map2String(Map<String, String> map) {
  90. StringBuffer sb = new StringBuffer();
  91. for (Map.Entry<String, String> entry : map.entrySet()) {
  92. sb.append(entry.getKey() + "=" + entry.getValue() + ";");
  93. }
  94. log.info(sb.toString());
  95. return sb.toString();
  96. }
  97. }
数据库脚本:
  1. prompt PL/SQL Developer import file
  2. prompt Created on 2015717 星期五 by wangjirong
  3. set feedback off
  4. set define off
  5. prompt Creating EC_GET_OPEN_ID...
  6. create table EC_GET_OPEN_ID
  7. (
  8. id VARCHAR2(32) not null,
  9. call_back_url VARCHAR2(300),
  10. openid VARCHAR2(28),
  11. session_id VARCHAR2(24),
  12. field1 VARCHAR2(30),
  13. field2 VARCHAR2(30),
  14. field3 VARCHAR2(30)
  15. )
  16. tablespace ECARE
  17. pctfree 10
  18. initrans 20
  19. maxtrans 255
  20. storage
  21. (
  22. initial 1M
  23. next 2M
  24. minextents 1
  25. maxextents unlimited
  26. );
  27. comment on table EC_GET_OPEN_ID
  28. is ‘微信统一获得openid数据库‘;
  29. comment on column EC_GET_OPEN_ID.id
  30. is ‘id主键‘;
  31. comment on column EC_GET_OPEN_ID.call_back_url
  32. is ‘回调url地址‘;
  33. comment on column EC_GET_OPEN_ID.openid
  34. is ‘openid微信‘;
  35. comment on column EC_GET_OPEN_ID.session_id
  36. is ‘请求session位置‘;
  37. comment on column EC_GET_OPEN_ID.field1
  38. is ‘备用字段1‘;
  39. comment on column EC_GET_OPEN_ID.field2
  40. is ‘备用字段2‘;
  41. comment on column EC_GET_OPEN_ID.field3
  42. is ‘备用字段3‘;
  43. alter table EC_GET_OPEN_ID
  44. add constraint PK primary key (ID)
  45. using index
  46. tablespace ECARE
  47. pctfree 10
  48. initrans 2
  49. maxtrans 255
  50. storage
  51. (
  52. initial 64K
  53. next 1M
  54. minextents 1
  55. maxextents unlimited
  56. );
  57. prompt Disabling triggers for EC_GET_OPEN_ID...
  58. alter table EC_GET_OPEN_ID disable all triggers;
  59. prompt Loading EC_GET_OPEN_ID...
  60. insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
  61. values (‘121‘, ‘http://192.168.88.210:8071/weixin/pay/pay.jsp‘, ‘oznhbuPT3DkbBHmZCWbU4Gr4cbjw‘, null, ‘123‘, ‘321‘, null);
  62. insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
  63. values (‘feb0aaed1cda4c868b569bb2c6202a68‘, ‘http://192.168.88.210:8071/weixin/pay/pay.jsp‘, null, null, ‘123‘, ‘321‘, null);
  64. insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
  65. values (‘078ed17fb5f14665af5c5abef8570bf9‘, ‘http://192.168.88.210:8071/weixin/pay/pay.jsp‘, null, null, ‘123‘, ‘321‘, null);
  66. insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
  67. values (‘020e9039d75d4ea88b9a8e79e8310bc5‘, ‘http://192.168.88.210:8071/weixin/pay/pay.jsp‘, null, null, ‘123‘, ‘321‘, null);
  68. insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
  69. values (‘65a063d899e44349a906d652228d7759‘, ‘http://192.168.88.210:8071/weixin/pay/pay.jsp‘, null, null, ‘123‘, ‘321‘, null);
  70. insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
  71. values (‘cb9e40134a3043319af8ef70fff208af‘, ‘http://192.168.88.210:8071/weixin/pay/pay.jsp‘, null, null, ‘123‘, ‘321‘, null);
  72. insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
  73. values (‘a789ee91f5d749a89a20fe102f93dddb‘, ‘http://192.168.88.210:8071/weixin/pay/pay.jsp‘, null, null, ‘123‘, ‘321‘, null);
  74. commit;
  75. prompt 7 records loaded
  76. prompt Enabling triggers for EC_GET_OPEN_ID...
  77. alter table EC_GET_OPEN_ID enable all triggers;
  78. set feedback on
  79. set define on
  80. prompt Done.
3、微信openid的区别:
    在开发微信的时候,如果你使用了测试账户,测试账户值得是:在公众平台设置的测试账号;测试你用过授权得到的openid,和实际使用的openid是不一致的!,需要特别的注意,在正式上线的时候,需要使用真实的openid;
    使用测试账号的openid和测试的首选域,是可以完成支付测试的,需要注意:只微信支付出现的问题总结--不同域名进行授权解决方案
公众平台设置了测试授权目录才能进行相应的支付测试,负责,不能完成支付;














微信支付出现的问题总结--不同域名进行授权解决方案

上一篇:微信错误提示code= -4/微信发送被拒绝


下一篇:django搭建微信公众号后台——验证(7.19)