Commit a0caba38 authored by Junling Bu's avatar Junling Bu
Browse files

update[litemall-wx-api]:...

update[litemall-wx-api]: 重新设计配置微信配置方式,配置信息包括:wx.app-id,wx.app-secret,wx.mch-id,wx.mch-key,wx.notify-url
parent 8e81093a
package org.linlinjava.litemall.wx.config; package org.linlinjava.litemall.wx.config;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig; import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig; import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import com.github.binarywang.wxpay.config.WxPayConfig; import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class WeixinConfig { public class WxConfig {
public static final String WX_AppId = "wxa5b486c6b918ecfb"; @Autowired
public static final String WX_Secret = "e04004829d4c383b4db7769d88dfbca1"; private WxProperties properties;
public static final String WX_Token = "";
public static final String WX_AesKey = "";
public static final String WX_MsgDataFormat = "JSON";
public static final String WX_MchId = "";
public static final String WX_MchKey = "";
public static final String WX_KeyPath = "";
@Bean @Bean
public WxMaConfig wxMaConfig() { public WxMaConfig wxMaConfig() {
WxMaInMemoryConfig config = new WxMaInMemoryConfig(); WxMaInMemoryConfig config = new WxMaInMemoryConfig();
config.setAppid(WX_AppId); config.setAppid(properties.getAppId());
config.setSecret(WX_Secret); config.setSecret(properties.getAppSecret());
config.setToken(WX_Token);
config.setAesKey(WX_AesKey);
config.setMsgDataFormat(WX_MsgDataFormat);
return config; return config;
} }
@Bean
public WxMaService wxMaService(WxMaConfig maConfig) {
WxMaService service = new WxMaServiceImpl();
service.setWxMaConfig(maConfig);
return service;
}
@Bean @Bean
public WxPayConfig wxPayConfig() { public WxPayConfig wxPayConfig() {
WxPayConfig payConfig = new WxPayConfig(); WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(WX_AppId); payConfig.setAppId(properties.getAppId());
payConfig.setMchId(WX_MchId); payConfig.setMchId(properties.getMchId());
payConfig.setMchKey(WX_MchKey); payConfig.setMchKey(properties.getMchKey());
payConfig.setSubAppId(null); payConfig.setNotifyUrl(properties.getNotifyUrl());
payConfig.setSubMchId(null); payConfig.setTradeType("JSAPI");
payConfig.setKeyPath(WX_KeyPath); payConfig.setSignType("MD5");
return payConfig; return payConfig;
} }
@Bean
public WxPayService wxPayService(WxPayConfig payConfig) {
WxPayService wxPayService = new WxPayServiceImpl();
wxPayService.setConfig(payConfig);
return wxPayService;
}
} }
\ No newline at end of file
package org.linlinjava.litemall.wx.config;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
import com.google.common.collect.Lists;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Configuration
@ConditionalOnClass(WxMaService.class)
@EnableConfigurationProperties(WxMaProperties.class)
public class WxMaConfiguration {
private static final WxMaMessageHandler templateMsgHandler = (wxMessage, context, service, sessionManager) ->
service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder()
.templateId("此处更换为自己的模板id")
.formId("自己替换可用的formid")
.data(Lists.newArrayList(
new WxMaTemplateMessage.Data("keyword1", "339208499", "#173177")))
.toUser(wxMessage.getFromUser())
.build());
private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> {
System.out.println("收到消息:" + wxMessage.toString());
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson())
.toUser(wxMessage.getFromUser()).build());
};
private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) ->
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息")
.toUser(wxMessage.getFromUser()).build());
private final WxMaMessageHandler picHandler = (wxMessage, context, service, sessionManager) -> {
try {
WxMediaUploadResult uploadResult = service.getMediaService()
.uploadMedia("image", "png",
ClassLoader.getSystemResourceAsStream("tmp.png"));
service.getMsgService().sendKefuMsg(
WxMaKefuMessage
.newImageBuilder()
.mediaId(uploadResult.getMediaId())
.toUser(wxMessage.getFromUser())
.build());
} catch (WxErrorException e) {
e.printStackTrace();
}
};
private final WxMaMessageHandler qrcodeHandler = (wxMessage, context, service, sessionManager) -> {
try {
final File file = service.getQrcodeService().createQrcode("123", 430);
WxMediaUploadResult uploadResult = service.getMediaService().uploadMedia("image", file);
service.getMsgService().sendKefuMsg(
WxMaKefuMessage
.newImageBuilder()
.mediaId(uploadResult.getMediaId())
.toUser(wxMessage.getFromUser())
.build());
} catch (WxErrorException e) {
e.printStackTrace();
}
};
@Autowired
private WxMaProperties properties;
@Bean
@ConditionalOnMissingBean
public WxMaConfig config() {
WxMaInMemoryConfig config = new WxMaInMemoryConfig();
config.setAppid(this.properties.getAppid());
config.setSecret(this.properties.getSecret());
config.setToken(this.properties.getToken());
config.setAesKey(this.properties.getAesKey());
config.setMsgDataFormat(this.properties.getMsgDataFormat());
return config;
}
@Bean
@ConditionalOnMissingBean
public WxMaService wxMaService(WxMaConfig config) {
WxMaService service = new WxMaServiceImpl();
service.setWxMaConfig(config);
return service;
}
@Bean
public WxMaMessageRouter router(WxMaService service) {
final WxMaMessageRouter router = new WxMaMessageRouter(service);
router
.rule().handler(logHandler).next()
.rule().async(false).content("模板").handler(templateMsgHandler).end()
.rule().async(false).content("文本").handler(textHandler).end()
.rule().async(false).content("图片").handler(picHandler).end()
.rule().async(false).content("二维码").handler(qrcodeHandler).end();
return router;
}
}
package org.linlinjava.litemall.wx.config;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@ConfigurationProperties(prefix = "wechat.miniapp")
public class WxMaProperties {
/**
* 设置微信小程序的appid
*/
private String appid;
/**
* 设置微信小程序的Secret
*/
private String secret;
/**
* 设置微信小程序的token
*/
private String token;
/**
* 设置微信小程序的EncodingAESKey
*/
private String aesKey;
/**
* 消息格式,XML或者JSON
*/
private String msgDataFormat;
public String getAppid() {
return this.appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getSecret() {
return this.secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
public String getToken() {
return this.token;
}
public void setToken(String token) {
this.token = token;
}
public String getAesKey() {
return this.aesKey;
}
public void setAesKey(String aesKey) {
this.aesKey = aesKey;
}
public String getMsgDataFormat() {
return msgDataFormat;
}
public void setMsgDataFormat(String msgDataFormat) {
this.msgDataFormat = msgDataFormat;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
}
}
package org.linlinjava.litemall.wx.config;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Binary Wang
*/
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WxPayProperties.class)
public class WxPayConfiguration {
@Autowired
private WxPayProperties properties;
@Bean
@ConditionalOnMissingBean
public WxPayConfig config() {
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(this.properties.getAppId());
payConfig.setMchId(this.properties.getMchId());
payConfig.setMchKey(this.properties.getMchKey());
payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
payConfig.setKeyPath(this.properties.getKeyPath());
return payConfig;
}
@Bean
//@ConditionalOnMissingBean
public WxPayService wxPayService(WxPayConfig payConfig) {
WxPayService wxPayService = new WxPayServiceImpl();
wxPayService.setConfig(payConfig);
return wxPayService;
}
}
package org.linlinjava.litemall.wx.config;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* wxpay pay properties
*
* @author Binary Wang
*/
@ConfigurationProperties(prefix = "wechat.pay")
public class WxPayProperties {
/**
* 设置微信公众号的appid
*/
private String appId;
/**
* 微信支付商户号
*/
private String mchId;
/**
* 微信支付商户密钥
*/
private String mchKey;
/**
* 服务商模式下的子商户公众账号ID,普通模式请不要配置,请在配置文件中将对应项删除
*/
private String subAppId;
/**
* 服务商模式下的子商户号,普通模式请不要配置,最好是请在配置文件中将对应项删除
*/
private String subMchId;
/**
* apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定
*/
private String keyPath;
public String getAppId() {
return this.appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getMchId() {
return mchId;
}
public void setMchId(String mchId) {
this.mchId = mchId;
}
public String getMchKey() {
return mchKey;
}
public void setMchKey(String mchKey) {
this.mchKey = mchKey;
}
public String getSubAppId() {
return subAppId;
}
public void setSubAppId(String subAppId) {
this.subAppId = subAppId;
}
public String getSubMchId() {
return subMchId;
}
public void setSubMchId(String subMchId) {
this.subMchId = subMchId;
}
public String getKeyPath() {
return this.keyPath;
}
public void setKeyPath(String keyPath) {
this.keyPath = keyPath;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this,
ToStringStyle.MULTI_LINE_STYLE);
}
}
package org.linlinjava.litemall.wx.config;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "wx")
public class WxProperties {
private String appId;
private String appSecret;
private String mchId;
private String mchKey;
private String notifyUrl;
public String getNotifyUrl() {
return notifyUrl;
}
public void setNotifyUrl(String notifyUrl) {
this.notifyUrl = notifyUrl;
}
public String getMchKey() {
return mchKey;
}
public void setMchKey(String mchKey) {
this.mchKey = mchKey;
}
public String getAppId() {
return this.appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
public String getMchId() {
return mchId;
}
public void setMchId(String mchId) {
this.mchId = mchId;
}
}
package org.linlinjava.litemall.wx.web;
import com.github.binarywang.wxpay.service.WxPayService;
import org.apache.log4j.Logger;
import org.linlinjava.litemall.db.domain.LitemallOrder;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallOrderGoodsService;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/wx/pay")
public class WxPayController {
private Logger logger = Logger.getLogger(WxPayController.class);
@Autowired
private LitemallOrderService orderService;
@Autowired
private LitemallOrderGoodsService orderGoodsService;
@Autowired
private LitemallUserService userService;
@Autowired
private WxPayService wxService;
/**
* 获取支付的请求参数
*/
@RequestMapping("prepay")
public Object payPrepay(@LoginUser Integer userId, Integer orderId) {
if(userId == null){
return ResponseUtil.fail401();
}
if(orderId == null){
return ResponseUtil.fail402();
}
LitemallOrder order = orderService.findById(orderId);
LitemallUser user = userService.findById(userId);
if(user.getWeixinOpenid() == null){
return ResponseUtil.fail(403, "用户openid不存在");
}
if(order == null){
return ResponseUtil.fail(403, "订单不存在");
}
return ResponseUtil.ok("模拟支付成功");
// if(order.getPayStatus() != 0){
// return ResponseUtil.fail(403, "订单已支付,请不要重复操作");
// }
// WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest();
// WxPayUnifiedOrderResult wxPayUnifiedOrderResult = null;
// try {
// wxPayUnifiedOrderResult = wxService.unifiedOrder(wxPayUnifiedOrderRequest);
// } catch (WxPayException e) {
// e.printStackTrace();
// return ResponseUtil.fail(403, "支付失败");
// }
//
// return ResponseUtil.fail(404, "支付未实现");
}
/**
* 微信订单回调接口
*/
@RequestMapping(value = "/notify", method = RequestMethod.POST)
public Object notify(HttpServletRequest request, HttpServletResponse response) {
return ResponseUtil.fail501();
}
/**
* 订单退款请求
*/
@RequestMapping("refund")
public Object refund(@LoginUser Integer userId, Integer orderId) {
if (userId == null) {
return ResponseUtil.fail401();
}
if (orderId == null) {
return ResponseUtil.fail402();
}
return ResponseUtil.fail501();
}
}
\ No newline at end of file
spring.profiles.active=dev spring.profiles.active=prod
server.port=8082 server.port=8082
logging.level.org.linlinjava.litemall.wx.Application=DEBUG logging.level.org.linlinjava.litemall.wx.Application=DEBUG
package org.linlinjava.litemall.wx;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class WxConfigTest {
@Autowired
private WxPayService wxPayService;
@Test
public void test() {
WxPayConfig wxPayConfig = wxPayService.getConfig();
System.out.println(wxPayConfig.getMchId() + " " + wxPayConfig.getMchKey());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment