|
@@ -5,12 +5,14 @@ import com.book.dao.cps.entity.Goods;
|
|
|
import com.book.dao.cps.mapper.*;
|
|
|
import com.book.dao.cps.pojo.*;
|
|
|
import com.book.dao.utils.DateUtils;
|
|
|
+import com.book.dao.utils.TimeUtil;
|
|
|
import com.book.server.config.Const;
|
|
|
import com.book.server.service.BookService;
|
|
|
import com.book.server.service.CacheService;
|
|
|
import com.book.server.service.UserService;
|
|
|
import com.book.server.service.WxPayService;
|
|
|
import com.book.server.utils.*;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -18,7 +20,6 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.net.URLEncoder;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
@@ -28,6 +29,7 @@ import java.util.concurrent.TimeUnit;
|
|
|
*
|
|
|
* @author win7
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class WxPayServiceImpl implements WxPayService {
|
|
|
|
|
@@ -66,7 +68,7 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
return null;
|
|
|
}
|
|
|
BigDecimal money = goods.getMoney();
|
|
|
- String amount = money.multiply(new BigDecimal(100)).toString();
|
|
|
+ String amount = String.valueOf(money.multiply(new BigDecimal(100)).intValue());
|
|
|
//todo 支付域名
|
|
|
User userByUserId = userService.getUserByUserId(wxPayRequest.getUserId());
|
|
|
Integer channelId = userByUserId.getChannelId();
|
|
@@ -96,8 +98,8 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
order.setMoney(money);
|
|
|
order.setCategory("wechat");
|
|
|
order.setBusinessLine(0);
|
|
|
- order.setPayid(String.valueOf(orderid).concat(Md5Util.md5(order + "").substring(10, 20)));
|
|
|
order.setOutTradeNo(String.valueOf(orderid));
|
|
|
+ order.setPayid(String.valueOf(orderid).concat(Md5Util.md5(orderid + "").substring(10, 20)));
|
|
|
order.setIp(wxPayRequest.getIp());
|
|
|
order.setGoodsId(goodId);
|
|
|
order.setKandian(goods.getKandian());
|
|
@@ -114,13 +116,12 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
order.setMark(0);
|
|
|
order.setChannelBenefit(adminExtend.getBenefit());
|
|
|
order.setChannelBenefitMoney(money.multiply(adminExtend.getBenefit()));
|
|
|
-
|
|
|
order.setMoneyBenefit(money.multiply(adminExtend.getBenefit()));
|
|
|
order.setBenefit(adminExtend.getBenefit());
|
|
|
|
|
|
+ ordersMapper.insert(order);
|
|
|
HashMap<String, String> map = new HashMap();
|
|
|
- int a = money.multiply(new BigDecimal(100)).intValue();
|
|
|
-
|
|
|
+ String mchOrderid = TimeUtil.getCurrentDate(TimeUtil.YYYYMMDDHHMMSS) + order.getId();
|
|
|
map.put("appid", palmpayAppid);
|
|
|
map.put("mchId", palmpayMchid);
|
|
|
map.put("version", "3.0");
|
|
@@ -128,9 +129,9 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
map.put("signType", "MD5");
|
|
|
map.put("productName", goods.getTitle());
|
|
|
map.put("productDesc", goods.getTitle());
|
|
|
- map.put("outTradeNo", orderid + "");
|
|
|
- map.put("money", a + "");
|
|
|
- map.put("callbackUrl", String.format("http://%s.%s/home.html#/center?userId=%s&channelId=%s",
|
|
|
+ map.put("outTradeNo", mchOrderid);
|
|
|
+ map.put("money", amount);
|
|
|
+ map.put("callbackUrl", String.format("http://%s.%s/pay?userId=%s&channelId=%s",
|
|
|
adminConfig.getAppid(), ophost.getHost(), wxPayRequest.getUserId(), adminConfig.getAdminId()));
|
|
|
map.put("notifyUrl", url + "/api/wxpay/notify");
|
|
|
map.put("ip", wxPayRequest.getIp());
|
|
@@ -138,7 +139,7 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
map.put("api", "1");
|
|
|
|
|
|
|
|
|
- String str = palmpayAppid + "WECHAT_SUB" + a + orderid + palmpayAppkey;
|
|
|
+ String str = palmpayAppid + "WECHAT_SUB" + amount + mchOrderid + palmpayAppkey;
|
|
|
|
|
|
String sign = Md5Util.md5(str);
|
|
|
// sign = generalSign(map,md5key);
|
|
@@ -148,8 +149,8 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
System.out.println("请求:" + PALMPAY_URL + "?" + form);
|
|
|
WxPayInfo wxPayInfo = new WxPayInfo();
|
|
|
wxPayInfo.setUserId(userByUserId.getId() + "");
|
|
|
- wxPayInfo.setOrderId(order.getOutTradeNo());
|
|
|
- if (StringUtils.isNotEmpty(wxpay.getAppid()) && StringUtils.isNotEmpty(wxPayRequest.getOpenid())) {
|
|
|
+ wxPayInfo.setOrderId(String.valueOf(order.getId()));
|
|
|
+ if (StringUtils.isNotEmpty(wxpay.getAppid()) || StringUtils.isNotEmpty(wxPayRequest.getOpenid())) {
|
|
|
map.put("subAppid", wxpay.getAppid());
|
|
|
map.put("openid", wxPayRequest.getOpenid());
|
|
|
|
|
@@ -163,21 +164,21 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
return null;
|
|
|
}
|
|
|
PalmpayResponse.Result r = object.getResult();
|
|
|
- order.setPdorderid(r.getPdorderid());
|
|
|
+ Orders updateOrder = new Orders();
|
|
|
+ updateOrder.setId(order.getId());
|
|
|
+ updateOrder.setPdorderid(r.getPdorderid());
|
|
|
String pay_info = r.getPay_info();
|
|
|
- order.setPayJson(pay_info);
|
|
|
+ updateOrder.setPayJson(pay_info);
|
|
|
|
|
|
Map<String, Object> map1 = JsonUtils.getMap(pay_info);
|
|
|
wxPayInfo.setPayInfo(map1);
|
|
|
+ wxPayInfo.setUrl("");
|
|
|
|
|
|
} else {
|
|
|
wxPayInfo.setUrl(PALMPAY_URL + "?" + form);
|
|
|
}
|
|
|
|
|
|
|
|
|
- ordersMapper.insert(order);
|
|
|
-
|
|
|
-
|
|
|
return wxPayInfo;
|
|
|
}
|
|
|
|
|
@@ -207,17 +208,22 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
String openid = map.get("openid");
|
|
|
String timeEnd = map.get("timeEnd");
|
|
|
if ("success".equals(status)) {
|
|
|
-
|
|
|
- Orders order = ordersMapper.selectByOutTradeNo(outTradeNo);
|
|
|
+ Integer id = Integer.valueOf(outTradeNo.substring(8, outTradeNo.length()));
|
|
|
+ Orders order = ordersMapper.selectByPrimaryKey(id);
|
|
|
+ if (order == null) {
|
|
|
+ log.error("订单不存在");
|
|
|
+ return "error";
|
|
|
+ }
|
|
|
if (!"1".equals(order.getState())) {
|
|
|
Orders orders = new Orders();
|
|
|
+ orders.setId(id);
|
|
|
orders.setOutTradeNo(outTradeNo);
|
|
|
orders.setState(1);
|
|
|
orders.setPdorderid(pdorderid);
|
|
|
orders.setTransactionId(chorderid);
|
|
|
orders.setUpdatetime(DateUtils.getNow());
|
|
|
orders.setFinishtime(DateUtils.getNow());
|
|
|
- ordersMapper.updateByOutTradeNo(outTradeNo);
|
|
|
+ ordersMapper.updateByPrimaryKeySelective(orders);
|
|
|
bookService.insertKandian(order.getUserId(), "1", order.getKandian(), order.getFreeKandian());
|
|
|
}
|
|
|
|
|
@@ -229,7 +235,7 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String chargePage(String userId, String code, String page) {
|
|
|
+ public String chargePage(String userId, String code, String domain) {
|
|
|
User userByUserId = userService.getUserByUserId(Long.parseLong(userId));
|
|
|
Integer channelId = userByUserId.getChannelId();
|
|
|
AdminConfig adminConfig = adminConfigMapper.selectByPrimaryKey(channelId);
|
|
@@ -238,10 +244,10 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
String appid = wxpay.getAppid();
|
|
|
String secret = wxpay.getSecret();
|
|
|
if (StringUtils.isEmpty(code)) {
|
|
|
- String redirectUrl = "http://" + wxpay.getPayHost() + "/api/wxpay/page?userId=" + userId;
|
|
|
+ String redirectUrl = "http://" + domain + "/api/wxpay/login?userId=" + userId;
|
|
|
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=%s#wechat_redirect";
|
|
|
try {
|
|
|
- url = String.format(url, appid, java.net.URLEncoder.encode(redirectUrl, "UTF-8"), page);
|
|
|
+ url = String.format(url, appid, java.net.URLEncoder.encode(redirectUrl, "UTF-8"), userId);
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -255,11 +261,8 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
String s = HttpUtils.sendGet(format);
|
|
|
SnsapiBase object = JsonUtils.getObject(s, SnsapiBase.class);
|
|
|
String openid = object.getOpenid();
|
|
|
- Integer ophostId = adminConfig.getOphostId();
|
|
|
- Ophost ophost = ophostMapper.selectByPrimaryKey(ophostId);
|
|
|
- String url = Const.URL_PAY_CENTER.replace("{appid}", appid)
|
|
|
- .replace("{host}", ophost.getHost())
|
|
|
- .replace("{page}", page)
|
|
|
+ String url = Const.URL_PAY_CENTER
|
|
|
+ .replace("{host}", wxpay.getPayHost())
|
|
|
.replace("userId", userId)
|
|
|
.replace("{openid}", openid);
|
|
|
return "redirect:" + url;
|
|
@@ -270,7 +273,7 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
|
|
|
@Override
|
|
|
public OrderVo queryOrder(String orderId) {
|
|
|
- Orders order = ordersMapper.selectByOutTradeNo(orderId);
|
|
|
+ Orders order = ordersMapper.selectByPrimaryKey(Integer.valueOf(orderId));
|
|
|
if (order == null) {
|
|
|
return null;
|
|
|
}
|