点开链接先去了解一下微信支付模式:
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=2_1

web一般选择都是扫码支付。
类似于这个:

首先要申请一个公共账号,最好申请的时候就是服务号,因为微信支付的前提必须是服务号
如果是公众号需要先申请验证才能升级为服务号。

下面链接可以帮助你去找到这个账号:
http://jingyan.baidu.com/article/22fe7ced23fa183002617fa1.html

在公共号平台上找

下面链接可以帮助你去找到这个账号:
http://jingyan.baidu.com/article/22fe7ced23fa183002617fa1.html

以上两个账号都可以在类型为服务号的公众号上找到,下面的两个账号就必须要在商户平台上才可以找到。
大概步骤:
一、公众号为服务号
http://jingyan.baidu.com/article/fea4511a7eaf2cf7bb9125a7.html
二、申请微信认证
http://kf.qq.com/faq/120911VrYVrA150929Fjqeei.html
三、申请扫码支付
升级完之后左侧菜单会多一个“微信支付”选项

第三步成功之后会收到一个邮件,邮件中会有登录商户平台的账号和密码

申请支付成功之后登录商户平台就可以看到这个值了。

这个必须要登录商户平台去设置。
以上两个账号的值可以参考下面的链接:
http://help.ecmoban.com/article-2085.html

https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5

  1. <!-- 生成二维码所需的jar包 开始 -->
  2. <dependency>
  3. <groupId>com.uqihong</groupId>
  4. <artifactId>qdcode</artifactId>
  5. <version>1.0.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.uqihong</groupId>
  9. <artifactId>qdcodeSwetake</artifactId>
  10. <version>1.0.0</version>
  11. </dependency>
  12. <!-- 生成二维码所需的jar包 结束 -->
  13. <!-- 微信支付需要的jar包 -->
  14. <dependency>
  15. <groupId>xmlpull</groupId>
  16. <artifactId>xmlpull</artifactId>
  17. <version>1.1.3.1</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>xpp3</groupId>
  21. <artifactId>xpp3</artifactId>
  22. <version>1.1.4c</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>com.thoughtworks.xstream</groupId>
  26. <artifactId>xstream</artifactId>
  27. <version>1.4.7</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.apache.httpcomponents</groupId>
  31. <artifactId>fluent-hc</artifactId>
  32. <version>4.3.5</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.apache.httpcomponents</groupId>
  36. <artifactId>httpclient</artifactId>
  37. <version>4.3.5</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.apache.httpcomponents</groupId>
  41. <artifactId>httpclient-cache</artifactId>
  42. <version>4.3.5</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.apache.httpcomponents</groupId>
  46. <artifactId>httpcore</artifactId>
  47. <version>4.3.2</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.apache.httpcomponents</groupId>
  51. <artifactId>httpmime</artifactId>
  52. <version>4.3.5</version>
  53. </dependency>
  1. /**
  2. * 创建预支付订单
  3. *
  4. * @param goodsName 商品名称
  5. * @param orderId 订单id
  6. * @param payPrice 支付价格
  7. *
  8. * @return 微信支付二维码链接
  9. */
  10. public String createPrePayOrder(String goodsName, String orderId, double payPrice) {
  11. return dowithWxReturn(sendReqGetPreOrder(goodsName, orderId, payPrice));
  12. }
  13. /**
  14. * 处理微信返回
  15. * <p>
  16. * 处理的是调用微信预支付订单的返回值
  17. *
  18. * @param result 微信的返回值
  19. *
  20. * @return 二维码链接
  21. */
  22. private String dowithWxReturn(String result) {
  23. String codeUrl = "";
  24. Map<String, Object> weixinPrepayInfo = MapUtil.map();
  25. try {
  26. weixinPrepayInfo = XMLParser.getMapFromXML(result);
  27. String return_code = (String) weixinPrepayInfo.get("return_code");
  28. if ("SUCCESS".equals(return_code)) {
  29. codeUrl = (String) weixinPrepayInfo.get("code_url");
  30. if (Util.isEmpty(codeUrl)) {
  31. throw ExceptionUtil.bEx(ConvertUtil.obj2str(weixinPrepayInfo.get("err_code_des")));
  32. }
  33. return codeUrl;
  34. } else {
  35. throw ExceptionUtil.bEx("预支付失败");
  36. }
  37. } catch (Exception e) {
  38. logger.error(e.getMessage());
  39. ExceptionUtil.bEx("调用微信预支付接口出错");
  40. }
  41. return codeUrl;
  42. }
  43. /**
  44. * 发送预生成订单请求
  45. *
  46. * @param goodsName 商品名称
  47. * @param orderId 订单号
  48. * @param payPrice 支付价格
  49. *
  50. * @return 微信处理结果
  51. */
  52. private String sendReqGetPreOrder(String goodsName, String orderId, double payPrice) {
  53. ExceptionUtil.checkEmpty(orderId, "订单id不能为空");
  54. ExceptionUtil.checkEmpty(goodsName, "商品名称不能为空");
  55. Map<String, Object> params = MapUtil.map();
  56. //公共账号id
  57. params.put("appid", WeixinH5PayConfigure.APPID);
  58. //商户id
  59. params.put("mch_id", WeixinH5PayConfigure.MCH_ID);
  60. //设备号
  61. //ctrl+alt+T
  62. params.put("device_info", "WEB");
  63. //随机字符串
  64. params.put("nonce_str", RandomUtil.randomString(randomNumLength));
  65. //商品描述
  66. params.put("body", goodsName);
  67. //订单编号
  68. params.put("out_trade_no", orderId);
  69. //金额
  70. params.put("total_fee", AmountUtils.changeY2F(payPrice));
  71. //回调地址
  72. params.put("notify_url", WeixinH5PayConfigure.NOTIFY_ACTIVITY_URL);
  73. params.put("trade_type", WeixinH5PayConfigure.TRADE_TYPE);
  74. //签名
  75. String sign = Signature.getSign(params, WeixinH5PayConfigure.API_KEY);
  76. params.put("sign", sign);
  77. return HttpRequest.sendPost(WeixinH5PayConfigure.PAY_UNIFIED_ORDER_API, params);
  78. }
  1. /**
  2. * 获取二维码
  3. *
  4. * @param orderId 订单id
  5. *
  6. * @return 二维码的地址
  7. */
  8. public Object getQrCode(long orderId) {
  9. ExceptionUtil.checkId(orderId, "订单id不能为空");
  10. return getWxCodeImagePath(getWxPayCodeUrl(orderId));
  11. }
  12. /**
  13. * 获取微信二维码支付的链接
  14. *
  15. * @param orderId 订单号
  16. *
  17. * @return 二维码支付的链接
  18. */
  19. private String getWxPayCodeUrl(long orderId) {
  20. OrderEntity order = orderBaseService.getWithEx(orderId);
  21. PackageEntity pk = packageBaseService.getWithEx(order.getPackageId());
  22. //double paymentPrice = 0.1;
  23. double paymentPrice = order.getPaymentPrice();
  24. String wxPayCodeUrl = wxPayService.createPrePayOrder(pk.getName(), ConvertUtil.obj2str(orderId), paymentPrice);
  25. return wxPayCodeUrl;
  26. }
  27. /**
  28. * 获取微信二维码的图片地址
  29. *
  30. * @param wxPayCodeUrl 微信二维码链接
  31. *
  32. * @return 含有微信扫码支付的二维码地址
  33. */
  34. private String getWxCodeImagePath(String wxPayCodeUrl) {
  35. String qrCodePath = QrcodeUtil.getQrCodePath(kvConfig, UqihongUploadFoldType.QRCODE);
  36. QrcodeUtil.encoderQRCode(wxPayCodeUrl, qrCodePath);
  37. return QrcodeUtil.getRelationPath(qrCodePath, kvConfig);
  38. }

第二步中的getQrCode()方法返回的就是图片的地址,直接在页面中使用img标签展示给用户就行。

  1. /**
  2. * 微信提醒
  3. * <P>
  4. * 微信支付成功的回调
  5. *
  6. * @throws Exception
  7. */
  8. public void wxPayFinishNotify() throws Exception {
  9. HttpServletRequest request = Mvcs.getReq();
  10. HttpServletResponse response = Mvcs.getResp();
  11. String responseString = getWeiXinResponseContent(request);
  12. PrintWriter out = response.getWriter();
  13. String resp = "";
  14. String signKey = WeixinH5PayConfigure.API_KEY;
  15. boolean verify = Signature.checkIsSignValidFromResponseString(responseString, signKey);
  16. if (!verify) {
  17. resp = "签名验证失败";
  18. out.write(resp);
  19. out.close();
  20. return;
  21. }
  22. Map<String, Object> map = XMLParser.getMapFromXML(responseString);
  23. String result_code = ConvertUtil.obj2str(map.get("result_code"));
  24. if (!"SUCCESS".equalsIgnoreCase(result_code)) {
  25. resp = PayCommonUtil.getResponseXML("ERROR", "ERROR");
  26. out.write(resp);
  27. out.close();
  28. return;
  29. }
  30. resp = handleOrder(map);
  31. out.write(resp);
  32. out.close();
  33. }
  34. @Aop("txDb")
  35. private String handleOrder(Map<String, Object> map) throws Exception {
  36. String resp = PayCommonUtil.getResponseXML("SUCCESS", "OK");
  37. //微信支付订单号
  38. String transaction_id = ConvertUtil.obj2str(map.get("transaction_id"));
  39. //支付完成时间
  40. String time_end = ConvertUtil.obj2str(map.get("time_end"));
  41. //订单号
  42. String out_trade_no = (String) map.get("out_trade_no");
  43. if (Util.isEmpty(transaction_id) || Util.isEmpty(time_end) || Util.isEmpty(out_trade_no)) {
  44. resp = PayCommonUtil.getResponseXML("ERROR", "参数错误,微信支付订单号、支付完成时间、订单号均不能为空");
  45. return resp;
  46. }
  47. OrderEntity order = orderBaseService.get(ConvertUtil.obj2long(out_trade_no));
  48. if (Util.isEmpty(order)) {
  49. resp = PayCommonUtil.getResponseXML("ERROR", "订单不存在");
  50. return resp;
  51. }
  52. //判断订单状态,避免重复处理
  53. int orderStatus = order.getStatus();
  54. if (OrderStatusEnum.FINISHED.intKey() == orderStatus) {
  55. return resp;
  56. }
  57. if (OrderStatusEnum.WAITING_PAY.intKey() == orderStatus) {
  58. //在这里编写你的逻辑即可
  59. 。。。。。。
  60. }
  61. return resp;
  62. }

https://my.oschina.net/xiaohui249/blog/656511 扫码支付的demo
http://www.cnblogs.com/zhengbin/p/6777639.html 生成二维码

版权声明:本文为wangsen原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wangsen/p/8257635.html