添加邮箱工具类及支付完成发送邮件提醒
This commit is contained in:
@@ -31,6 +31,16 @@
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.linlinjava.litemall.core.util;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
public class MailUtils {
|
||||
private static final Log logger = LogFactory.getLog(MailUtils.class);
|
||||
private static MailUtils mailUtils;
|
||||
|
||||
private JavaMailSenderImpl mailSender;
|
||||
|
||||
// TODO 邮箱相关后置后续应移到数据库,在后台配置完成,暂时先完成功能
|
||||
// 通知邮件送达地址
|
||||
private static final String SEND_TO = "ex@qq.com";
|
||||
|
||||
private MailUtils() {
|
||||
mailSender = new JavaMailSenderImpl();
|
||||
// 配置发送邮箱设置,请按照自己邮箱填写
|
||||
mailSender.setHost("smtp.exmail.qq.com");
|
||||
mailSender.setUsername("ex@ex.com.cn");
|
||||
mailSender.setPassword("ex");
|
||||
mailSender.setDefaultEncoding("UTF-8");
|
||||
}
|
||||
|
||||
public static MailUtils getMailUtils() {
|
||||
if (mailUtils == null)
|
||||
mailUtils = new MailUtils();
|
||||
|
||||
return mailUtils;
|
||||
}
|
||||
|
||||
public boolean sendEmail(String setSubject, String setText) {
|
||||
try {
|
||||
final MimeMessage mimeMessage = mailSender.createMimeMessage();
|
||||
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
|
||||
message.setFrom(mailSender.getUsername());
|
||||
message.setTo(SEND_TO);
|
||||
message.setSubject(setSubject);
|
||||
message.setText(setText);
|
||||
mailSender.send(mimeMessage);
|
||||
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
logger.error("通知邮件发送出错" + ex.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,11 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.linlinjava</groupId>
|
||||
<artifactId>litemall-core</artifactId>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.linlinjava.litemall.wx.util;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class IpUtil {
|
||||
public static String client(HttpServletRequest request) {
|
||||
@@ -10,4 +12,42 @@ public class IpUtil {
|
||||
}
|
||||
return xff;
|
||||
}
|
||||
|
||||
public static String getIpAddr(HttpServletRequest request) {
|
||||
String ipAddress = null;
|
||||
try {
|
||||
ipAddress = request.getHeader("x-forwarded-for");
|
||||
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
||||
ipAddress = request.getHeader("Proxy-Client-IP");
|
||||
}
|
||||
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
||||
ipAddress = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
||||
ipAddress = request.getRemoteAddr();
|
||||
if (ipAddress.equals("127.0.0.1")) {
|
||||
// 根据网卡取本机配置的IP
|
||||
InetAddress inet = null;
|
||||
try {
|
||||
inet = InetAddress.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ipAddress = inet.getHostAddress();
|
||||
}
|
||||
}
|
||||
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
||||
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
|
||||
// = 15
|
||||
if (ipAddress.indexOf(",") > 0) {
|
||||
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ipAddress="";
|
||||
}
|
||||
// ipAddress = this.getRequest().getRemoteAddr();
|
||||
|
||||
return ipAddress;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,24 +2,23 @@ package org.linlinjava.litemall.wx.web;
|
||||
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
|
||||
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
|
||||
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
|
||||
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
|
||||
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
|
||||
import com.github.binarywang.wxpay.service.WxPayService;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.linlinjava.litemall.core.util.JacksonUtil;
|
||||
import org.linlinjava.litemall.core.util.MailUtils;
|
||||
import org.linlinjava.litemall.core.util.ResponseUtil;
|
||||
import org.linlinjava.litemall.db.domain.*;
|
||||
import org.linlinjava.litemall.db.service.*;
|
||||
import org.linlinjava.litemall.db.util.OrderHandleOption;
|
||||
import org.linlinjava.litemall.db.util.OrderUtil;
|
||||
import org.linlinjava.litemall.core.util.JacksonUtil;
|
||||
import org.linlinjava.litemall.core.util.ResponseUtil;
|
||||
import org.linlinjava.litemall.wx.annotation.LoginUser;
|
||||
import org.linlinjava.litemall.wx.util.IpUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
@@ -290,7 +289,7 @@ public class WxOrderController {
|
||||
// 根据订单商品总价计算运费,满88则免运费,否则8元;
|
||||
BigDecimal freightPrice = new BigDecimal(0.00);
|
||||
if (checkedGoodsPrice.compareTo(new BigDecimal(88.00)) < 0) {
|
||||
freightPrice = new BigDecimal(8.00);
|
||||
freightPrice = new BigDecimal(0.00);
|
||||
}
|
||||
|
||||
// 可以使用的其他钱,例如用户积分
|
||||
@@ -440,7 +439,7 @@ public class WxOrderController {
|
||||
|
||||
/**
|
||||
* 付款订单的预支付会话标识
|
||||
*
|
||||
* <p>
|
||||
* 1. 检测当前订单是否能够付款
|
||||
* 2. 微信支付平台返回支付订单ID
|
||||
* 3. 设置订单付款状态
|
||||
@@ -452,8 +451,8 @@ public class WxOrderController {
|
||||
* 失败则 { errno: XXX, errmsg: XXX }
|
||||
*/
|
||||
@PostMapping("prepay")
|
||||
public Object prepay(@LoginUser Integer userId, @RequestBody String body) {
|
||||
if(userId == null){
|
||||
public Object prepay(@LoginUser Integer userId, @RequestBody String body, HttpServletRequest request) {
|
||||
if (userId == null) {
|
||||
return ResponseUtil.unlogin();
|
||||
}
|
||||
Integer orderId = JacksonUtil.parseInteger(body, "orderId");
|
||||
@@ -477,7 +476,7 @@ public class WxOrderController {
|
||||
|
||||
LitemallUser user = userService.findById(userId);
|
||||
String openid = user.getWeixinOpenid();
|
||||
if(openid == null){
|
||||
if (openid == null) {
|
||||
return ResponseUtil.fail(403, "订单不能支付");
|
||||
}
|
||||
WxPayMpOrderResult result = null;
|
||||
@@ -488,14 +487,14 @@ public class WxOrderController {
|
||||
// TODO 更有意义的显示名称
|
||||
orderRequest.setBody("litemall小商场-订单测试支付");
|
||||
// 元转成分
|
||||
Integer fee = 1;
|
||||
Integer fee = 0;
|
||||
// 这里演示仅支付1分
|
||||
// 实际项目取消下面两行注释
|
||||
// BigDecimal actualPrice = order.getActualPrice();
|
||||
// fee = actualPrice.multiply(new BigDecimal(100)).intValue();
|
||||
BigDecimal actualPrice = order.getActualPrice();
|
||||
fee = actualPrice.multiply(new BigDecimal(100)).intValue();
|
||||
orderRequest.setTotalFee(fee);
|
||||
// TODO 用户IP地址
|
||||
orderRequest.setSpbillCreateIp("123.12.12.123");
|
||||
orderRequest.setSpbillCreateIp(IpUtil.getIpAddr(request));
|
||||
|
||||
result = wxPayService.createOrder(orderRequest);
|
||||
} catch (Exception e) {
|
||||
@@ -518,7 +517,7 @@ public class WxOrderController {
|
||||
* @return 订单操作结果
|
||||
* 成功则 WxPayNotifyResponse.success的XML内容
|
||||
* 失败则 WxPayNotifyResponse.fail的XML内容
|
||||
*
|
||||
* <p>
|
||||
* 注意,这里pay-notify是示例地址,开发者应该设立一个隐蔽的回调地址
|
||||
*/
|
||||
@PostMapping("pay-notify")
|
||||
@@ -533,19 +532,19 @@ public class WxOrderController {
|
||||
String totalFee = BaseWxPayResult.feeToYuan(result.getTotalFee());
|
||||
|
||||
LitemallOrder order = orderService.findBySn(orderSn);
|
||||
if(order == null){
|
||||
if (order == null) {
|
||||
throw new Exception("订单不存在 sn=" + orderSn);
|
||||
}
|
||||
|
||||
// 检查这个订单是否已经处理过
|
||||
if(OrderUtil.isPayStatus(order) && order.getPayId() != null){
|
||||
if (OrderUtil.isPayStatus(order) && order.getPayId() != null) {
|
||||
return WxPayNotifyResponse.success("处理成功!");
|
||||
}
|
||||
|
||||
// 检查支付订单金额
|
||||
// TODO 这里1分钱需要改成实际订单金额
|
||||
if(!totalFee.equals("0.01")){
|
||||
throw new Exception("支付金额不符合 totalFee=" + totalFee);
|
||||
if (!totalFee.equals(order.getActualPrice().toString())) {
|
||||
throw new Exception(order.getOrderSn() + " : 支付金额不符合 totalFee=" + totalFee);
|
||||
}
|
||||
|
||||
order.setPayId(payId);
|
||||
@@ -553,6 +552,9 @@ public class WxOrderController {
|
||||
order.setOrderStatus(OrderUtil.STATUS_PAY);
|
||||
orderService.updateById(order);
|
||||
|
||||
//TODO 发送邮件通知,这里最好才用异步发送
|
||||
MailUtils.getMailUtils().sendEmail("订单通知", order.toString());
|
||||
|
||||
return WxPayNotifyResponse.success("处理成功!");
|
||||
} catch (Exception e) {
|
||||
logger.error("微信回调结果异常,异常原因 " + e.getMessage());
|
||||
@@ -560,7 +562,6 @@ public class WxOrderController {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 订单申请退款
|
||||
* 1. 检测当前订单是否能够退款
|
||||
|
||||
Reference in New Issue
Block a user