|
@@ -1,306 +1,305 @@
|
|
|
-package com.book.server.service.impl;
|
|
|
-
|
|
|
-import com.book.dao.VO.PalmpayResponse;
|
|
|
-import com.book.dao.VO.SnsapiBase;
|
|
|
-import com.book.dao.VO.WxPayInfo;
|
|
|
-import com.book.dao.VO.WxPayRequest;
|
|
|
-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.server.config.Const;
|
|
|
-import com.book.server.service.BookService;
|
|
|
-import com.book.server.service.UserService;
|
|
|
-import com.book.server.service.WxPayService;
|
|
|
-import com.book.server.utils.*;
|
|
|
-import com.sun.deploy.net.URLEncoder;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.apache.http.client.utils.URLEncodedUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
-import java.math.BigDecimal;
|
|
|
-import java.util.*;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-/**
|
|
|
- * created in 2021/8/30
|
|
|
- * Project: book-store
|
|
|
- *
|
|
|
- * @author win7
|
|
|
- */
|
|
|
-@Service
|
|
|
-public class WxPayServiceImpl implements WxPayService {
|
|
|
-
|
|
|
- public static final String PALMPAY_APPID = "";
|
|
|
- public static final String PALMPAY_APPKEY = "";
|
|
|
- public static final String PALMPAY_MCHID = "";
|
|
|
- public static final String PALMPAY_URL = "https://pay.palmpay.cn/sdkServer/thirdpays/pay/WECHAT_SUB";
|
|
|
- @Autowired
|
|
|
- private GoodsMapper goodsMapper;
|
|
|
- @Autowired
|
|
|
- private AdminConfigMapper adminConfigMapper;
|
|
|
- @Autowired
|
|
|
- private UserService userService;
|
|
|
- @Autowired
|
|
|
- private ConfigMapper configMapper;
|
|
|
- @Autowired
|
|
|
- private OphostMapper ophostMapper;
|
|
|
- @Autowired
|
|
|
- private OrdersMapper ordersMapper;
|
|
|
- @Autowired
|
|
|
- private RechargeMapper rechargeMapper;
|
|
|
- @Autowired
|
|
|
- private WxpayMapper wxpayMapper;
|
|
|
- @Autowired
|
|
|
- private BookService bookService;
|
|
|
- @Autowired
|
|
|
- private AdminExtendMapper adminExtendMapper;
|
|
|
-
|
|
|
- @Override
|
|
|
- public WxPayInfo createOrder(WxPayRequest wxPayRequest) {
|
|
|
- Integer goodId = wxPayRequest.getGoodId();
|
|
|
- Goods goods = goodsMapper.selectByPrimaryKey(goodId);
|
|
|
- if (goods == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- BigDecimal money = goods.getMoney();
|
|
|
- String amount = money.multiply(new BigDecimal(100)).toString();
|
|
|
- //todo 支付域名
|
|
|
- User userByUserId = userService.getUserByUserId(wxPayRequest.getUserId());
|
|
|
- Integer channelId = userByUserId.getChannelId();
|
|
|
- AdminConfig adminConfig = adminConfigMapper.selectByPrimaryKey(channelId);
|
|
|
- Integer wxpayId = adminConfig.getWxpayId();
|
|
|
- Wxpay wxpay = wxpayMapper.selectByPrimaryKey(wxpayId);
|
|
|
- String appid = wxpay.getAppid();
|
|
|
- String secret = wxpay.getSecret();
|
|
|
- String host = wxpay.getPayHost();
|
|
|
- String palmpayAppid = wxpay.getQuartetAppId();
|
|
|
- String palmpayMchid = wxpay.getQuartetMerchantId();
|
|
|
-
|
|
|
- String palmpayAppkey = wxpay.getQuartetAppKey();
|
|
|
-
|
|
|
-
|
|
|
- Integer ophostId = adminConfig.getOphostId();
|
|
|
- Ophost ophost = ophostMapper.selectByPrimaryKey(ophostId);
|
|
|
- Config url_pay = configMapper.selectByName("url_pay");
|
|
|
- String url = "http://" + url_pay.getValue();
|
|
|
- AdminExtend adminExtend = adminExtendMapper.selectByPrimaryKey(channelId);
|
|
|
- //todo 入库
|
|
|
- long orderid = TimeUnit.NANOSECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS) + wxPayRequest.getUserId();
|
|
|
- Orders order = new Orders();
|
|
|
- order.setUserId(userByUserId.getId());
|
|
|
- order.setAdminId(channelId);
|
|
|
- order.setWxpayId(wxpayId);
|
|
|
- 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.setIp(wxPayRequest.getIp());
|
|
|
- order.setGoodsId(goodId);
|
|
|
- order.setKandian(goods.getKandian());
|
|
|
- order.setFreeKandian(goods.getFreeKandian());
|
|
|
- order.setType(1);
|
|
|
- order.setCreatetime(DateUtils.getNow());
|
|
|
- order.setPaymentMethod(true);
|
|
|
- order.setChannelId(channelId);
|
|
|
- order.setState("0");
|
|
|
- order.setActivityId(0);
|
|
|
- order.setDeduct(false);
|
|
|
- order.setRewardAdminId(0);
|
|
|
- order.setGroup(new Byte((byte) 3));
|
|
|
- order.setMark(0);
|
|
|
- order.setChannelBenefit(adminExtend.getBenefit());
|
|
|
- order.setChannelBenefitMoney(money.multiply(adminExtend.getBenefit()));
|
|
|
-
|
|
|
- order.setMoneyBenefit(money.multiply(adminExtend.getBenefit()));
|
|
|
- order.setBenefit(adminExtend.getBenefit());
|
|
|
-
|
|
|
- HashMap<String, String> map = new HashMap();
|
|
|
- int a = money.multiply(new BigDecimal(100)).intValue();
|
|
|
-
|
|
|
- map.put("appid", palmpayAppid);
|
|
|
- map.put("mchId", palmpayMchid);
|
|
|
- map.put("version", "3.0");
|
|
|
- map.put("charset", "UTF-8");
|
|
|
- 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",
|
|
|
- adminConfig.getAppid(), ophost.getHost(), wxPayRequest.getUserId(), adminConfig.getAdminId()));
|
|
|
- map.put("notifyUrl", url + "/api/wxpay/notify");
|
|
|
- map.put("ip", wxPayRequest.getIp());
|
|
|
-
|
|
|
- map.put("api", "1");
|
|
|
-
|
|
|
-
|
|
|
- String str = palmpayAppid + "WECHAT_SUB" + a + orderid + palmpayAppkey;
|
|
|
-
|
|
|
- String sign = Md5Util.md5(str);
|
|
|
- // sign = generalSign(map,md5key);
|
|
|
- map.put("sign", sign);
|
|
|
-
|
|
|
- String form = DataUtil.maptoform(map) + "";
|
|
|
- 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())){
|
|
|
- map.put("subAppid", wxpay.getAppid());
|
|
|
- map.put("openid", wxPayRequest.getOpenid());
|
|
|
-
|
|
|
- String result = HTTPSUtil.sendGetByHttps(PALMPAY_URL, map);
|
|
|
- System.out.println("响应:" + result);
|
|
|
- PalmpayResponse object = JsonUtils.getObject(result, PalmpayResponse.class);
|
|
|
- if (object == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- if (!"0".equals(object.getErrcode())) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- PalmpayResponse.Result r = object.getResult();
|
|
|
- order.setPdorderid(r.getPdorderid());
|
|
|
- String pay_info = r.getPay_info();
|
|
|
- order.setPayJson(pay_info);
|
|
|
-
|
|
|
- Map<String, Object> map1 = JsonUtils.getMap(pay_info);
|
|
|
- wxPayInfo.setPayInfo(map1);
|
|
|
-
|
|
|
- }else {
|
|
|
- wxPayInfo.setUrl(PALMPAY_URL + "?" + form);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- ordersMapper.insert(order);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- return wxPayInfo;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String PayNotify(HttpServletRequest request) {
|
|
|
- Map<String, String> map = new HashMap();
|
|
|
- Map<String, String[]> params = request.getParameterMap();
|
|
|
- Set<Map.Entry<String, String[]>> entries = params.entrySet();
|
|
|
- for (Map.Entry<String, String[]> entry : entries) {
|
|
|
- try {
|
|
|
- String val = entry.getValue()[0];
|
|
|
- map.put(entry.getKey(), val.equalsIgnoreCase("null") ? "" : val);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- boolean b = signCheck(map, PALMPAY_APPKEY);
|
|
|
-
|
|
|
-
|
|
|
- String appid = map.get("appid");
|
|
|
- String mchId = map.get("mchId");
|
|
|
- String outTradeNo = map.get("outTradeNo");
|
|
|
- String pdorderid = map.get("pdorderid");
|
|
|
- String chorderid = map.get("chorderid");
|
|
|
- String attach = map.get("attach");
|
|
|
- String status = map.get("status");
|
|
|
- String openid = map.get("openid");
|
|
|
- String timeEnd = map.get("timeEnd");
|
|
|
- if ("success".equals(status)) {
|
|
|
-
|
|
|
- Orders order = ordersMapper.selectByOutTradeNo(outTradeNo);
|
|
|
- if (!"1".equals(order.getState())) {
|
|
|
- Orders orders = new Orders();
|
|
|
- orders.setOutTradeNo(outTradeNo);
|
|
|
- orders.setState(1);
|
|
|
- orders.setPdorderid(pdorderid);
|
|
|
- orders.setTransactionId(chorderid);
|
|
|
- orders.setUpdatetime(DateUtils.getNow());
|
|
|
- orders.setFinishtime(DateUtils.getNow());
|
|
|
- ordersMapper.updateByOutTradeNo(outTradeNo);
|
|
|
- bookService.insertKandian(order.getUserId(), "1", order.getKandian(), order.getFreeKandian());
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- return "success";
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String chargePage(String userId, String code,String page) {
|
|
|
- User userByUserId = userService.getUserByUserId(Long.parseLong(userId));
|
|
|
- Integer channelId = userByUserId.getChannelId();
|
|
|
- AdminConfig adminConfig = adminConfigMapper.selectByPrimaryKey(channelId);
|
|
|
- Integer wxpayId = adminConfig.getWxpayId();
|
|
|
- Wxpay wxpay = wxpayMapper.selectByPrimaryKey(wxpayId);
|
|
|
- String appid = wxpay.getAppid();
|
|
|
- String secret = wxpay.getSecret();
|
|
|
- if (StringUtils.isEmpty(code)) {
|
|
|
- String redirectUrl = "http://" + wxpay.getPayHost() + "/api/wxpay/page?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, URLEncoder.encode(redirectUrl, "UTF-8"), page);
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- return "redirect:" + url;
|
|
|
- } else {
|
|
|
-
|
|
|
- String openidUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
|
|
|
- String format = String.format(openidUrl, appid, secret, code);
|
|
|
- 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)
|
|
|
- .replace("userId",userId)
|
|
|
- .replace("{openid}",openid);
|
|
|
- return "redirect:" + url;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- //排序,校验
|
|
|
- private boolean signCheck(Map<String, String> src, String sectKey) {
|
|
|
- List<String> keys = new ArrayList<>(src.keySet());
|
|
|
- Collections.sort(keys);
|
|
|
- StringBuilder builder = new StringBuilder();
|
|
|
- for (String key : keys) {
|
|
|
- if (key.equals("sign")) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- String val = src.get(key);
|
|
|
- builder.append(key).append("=").append(val == null ? "" : val).append("&");
|
|
|
- }
|
|
|
- builder.append("key=").append(sectKey);
|
|
|
- String sign = Md5Util.md5(builder.toString());
|
|
|
- return sign.equals(src.get("sign"));
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- String result = "{\"result\":{\"sign\":\"edda60a1e29f6ed539c0203921548cdb\",\"signType\":\"MD5\",\"money\":1000,\"appid\":\"4110\",\"charset\":\"UTF-8\",\"pay_info\":\"{\\\"appId\\\":\\\"wx4cb1f52885afcb03\\\",\\\"timeStamp\\\":\\\"1630571756\\\",\\\"nonceStr\\\":\\\"8cbff937d4fc4d1cb9e3944febcbb93d\\\",\\\"package\\\":\\\"prepay_id=wx02163556206106625d6f65c721e05b0000\\\",\\\"signType\\\":\\\"RSA\\\",\\\"paySign\\\":\\\"MokP0HszOcPwgu3JaHoBWVNtDhD4GNo44O0jZDr0/9jDtkBcxNn+gXELjk9e66+tqxW9cq0cesCPD7qhkOmQBheBnb+CB/cFCO+6IbDpekfVfzYIlH5kq2EgSKX5a3xEPG9ykR6IK0uBgCTkui5vFeusm24eibhix7xkeYwUPiYFnKm7pgaIjFZuTFKa9Vdm3a56Il3TaU8bjRz4b/iUlTJo+DKvxehjx4RuNG5h/SeN84ysKm39VCB0XHP2v9LkScLJC/4g706pySbqXZyoVPrerzCb+RiX4C1ZWDKSy9ri3LKNxr+Pm8L90M7njvWyq0u+8u+/vTZO1EGGbb7WDQ==\\\"}\",\"outTradeNo\":\"4110test1630571752721\",\"mchId\":\"2142013001\",\"pdorderid\":\"1127504471476173824\",\"version\":\"3.0\"},\"err\":\"success\",\"errcode\":\"0\"}";
|
|
|
- PalmpayResponse object = JsonUtils.getObject(result, PalmpayResponse.class);
|
|
|
- PalmpayResponse.Result r = object.getResult();
|
|
|
- String pay_info = r.getPay_info();
|
|
|
-
|
|
|
- Map<String, Object> map1 = JsonUtils.getMap(pay_info);
|
|
|
- WxPayInfo wxPayInfo = new WxPayInfo();
|
|
|
- wxPayInfo.setPayInfo(map1);
|
|
|
- System.out.println(JsonUtils.toJsonStr(wxPayInfo));
|
|
|
- }
|
|
|
-}
|
|
|
+package com.book.server.service.impl;
|
|
|
+
|
|
|
+import com.book.dao.VO.PalmpayResponse;
|
|
|
+import com.book.dao.VO.SnsapiBase;
|
|
|
+import com.book.dao.VO.WxPayInfo;
|
|
|
+import com.book.dao.VO.WxPayRequest;
|
|
|
+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.server.config.Const;
|
|
|
+import com.book.server.service.BookService;
|
|
|
+import com.book.server.service.UserService;
|
|
|
+import com.book.server.service.WxPayService;
|
|
|
+import com.book.server.utils.*;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * created in 2021/8/30
|
|
|
+ * Project: book-store
|
|
|
+ *
|
|
|
+ * @author win7
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class WxPayServiceImpl implements WxPayService {
|
|
|
+
|
|
|
+ public static final String PALMPAY_APPID = "";
|
|
|
+ public static final String PALMPAY_APPKEY = "";
|
|
|
+ public static final String PALMPAY_MCHID = "";
|
|
|
+ public static final String PALMPAY_URL = "https://pay.palmpay.cn/sdkServer/thirdpays/pay/WECHAT_SUB";
|
|
|
+ @Autowired
|
|
|
+ private GoodsMapper goodsMapper;
|
|
|
+ @Autowired
|
|
|
+ private AdminConfigMapper adminConfigMapper;
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+ @Autowired
|
|
|
+ private ConfigMapper configMapper;
|
|
|
+ @Autowired
|
|
|
+ private OphostMapper ophostMapper;
|
|
|
+ @Autowired
|
|
|
+ private OrdersMapper ordersMapper;
|
|
|
+ @Autowired
|
|
|
+ private RechargeMapper rechargeMapper;
|
|
|
+ @Autowired
|
|
|
+ private WxpayMapper wxpayMapper;
|
|
|
+ @Autowired
|
|
|
+ private BookService bookService;
|
|
|
+ @Autowired
|
|
|
+ private AdminExtendMapper adminExtendMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public WxPayInfo createOrder(WxPayRequest wxPayRequest) {
|
|
|
+ Integer goodId = wxPayRequest.getGoodId();
|
|
|
+ Goods goods = goodsMapper.selectByPrimaryKey(goodId);
|
|
|
+ if (goods == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ BigDecimal money = goods.getMoney();
|
|
|
+ String amount = money.multiply(new BigDecimal(100)).toString();
|
|
|
+ //todo 支付域名
|
|
|
+ User userByUserId = userService.getUserByUserId(wxPayRequest.getUserId());
|
|
|
+ Integer channelId = userByUserId.getChannelId();
|
|
|
+ AdminConfig adminConfig = adminConfigMapper.selectByPrimaryKey(channelId);
|
|
|
+ Integer wxpayId = adminConfig.getWxpayId();
|
|
|
+ Wxpay wxpay = wxpayMapper.selectByPrimaryKey(wxpayId);
|
|
|
+ String appid = wxpay.getAppid();
|
|
|
+ String secret = wxpay.getSecret();
|
|
|
+ String host = wxpay.getPayHost();
|
|
|
+ String palmpayAppid = wxpay.getQuartetAppId();
|
|
|
+ String palmpayMchid = wxpay.getQuartetMerchantId();
|
|
|
+
|
|
|
+ String palmpayAppkey = wxpay.getQuartetAppKey();
|
|
|
+
|
|
|
+
|
|
|
+ Integer ophostId = adminConfig.getOphostId();
|
|
|
+ Ophost ophost = ophostMapper.selectByPrimaryKey(ophostId);
|
|
|
+ Config url_pay = configMapper.selectByName("url_pay");
|
|
|
+ String url = "http://" + url_pay.getValue();
|
|
|
+ AdminExtend adminExtend = adminExtendMapper.selectByPrimaryKey(channelId);
|
|
|
+ //todo 入库
|
|
|
+ long orderid = TimeUnit.NANOSECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS) + wxPayRequest.getUserId();
|
|
|
+ Orders order = new Orders();
|
|
|
+ order.setUserId(userByUserId.getId());
|
|
|
+ order.setAdminId(channelId);
|
|
|
+ order.setWxpayId(wxpayId);
|
|
|
+ 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.setIp(wxPayRequest.getIp());
|
|
|
+ order.setGoodsId(goodId);
|
|
|
+ order.setKandian(goods.getKandian());
|
|
|
+ order.setFreeKandian(goods.getFreeKandian());
|
|
|
+ order.setType(1);
|
|
|
+ order.setCreatetime(DateUtils.getNow());
|
|
|
+ order.setPaymentMethod(true);
|
|
|
+ order.setChannelId(channelId);
|
|
|
+ order.setState("0");
|
|
|
+ order.setActivityId(0);
|
|
|
+ order.setDeduct(false);
|
|
|
+ order.setRewardAdminId(0);
|
|
|
+ order.setGroup(new Byte((byte) 3));
|
|
|
+ order.setMark(0);
|
|
|
+ order.setChannelBenefit(adminExtend.getBenefit());
|
|
|
+ order.setChannelBenefitMoney(money.multiply(adminExtend.getBenefit()));
|
|
|
+
|
|
|
+ order.setMoneyBenefit(money.multiply(adminExtend.getBenefit()));
|
|
|
+ order.setBenefit(adminExtend.getBenefit());
|
|
|
+
|
|
|
+ HashMap<String, String> map = new HashMap();
|
|
|
+ int a = money.multiply(new BigDecimal(100)).intValue();
|
|
|
+
|
|
|
+ map.put("appid", palmpayAppid);
|
|
|
+ map.put("mchId", palmpayMchid);
|
|
|
+ map.put("version", "3.0");
|
|
|
+ map.put("charset", "UTF-8");
|
|
|
+ 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",
|
|
|
+ adminConfig.getAppid(), ophost.getHost(), wxPayRequest.getUserId(), adminConfig.getAdminId()));
|
|
|
+ map.put("notifyUrl", url + "/api/wxpay/notify");
|
|
|
+ map.put("ip", wxPayRequest.getIp());
|
|
|
+
|
|
|
+ map.put("api", "1");
|
|
|
+
|
|
|
+
|
|
|
+ String str = palmpayAppid + "WECHAT_SUB" + a + orderid + palmpayAppkey;
|
|
|
+
|
|
|
+ String sign = Md5Util.md5(str);
|
|
|
+ // sign = generalSign(map,md5key);
|
|
|
+ map.put("sign", sign);
|
|
|
+
|
|
|
+ String form = DataUtil.maptoform(map) + "";
|
|
|
+ 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())){
|
|
|
+ map.put("subAppid", wxpay.getAppid());
|
|
|
+ map.put("openid", wxPayRequest.getOpenid());
|
|
|
+
|
|
|
+ String result = HTTPSUtil.sendGetByHttps(PALMPAY_URL, map);
|
|
|
+ System.out.println("响应:" + result);
|
|
|
+ PalmpayResponse object = JsonUtils.getObject(result, PalmpayResponse.class);
|
|
|
+ if (object == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (!"0".equals(object.getErrcode())) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ PalmpayResponse.Result r = object.getResult();
|
|
|
+ order.setPdorderid(r.getPdorderid());
|
|
|
+ String pay_info = r.getPay_info();
|
|
|
+ order.setPayJson(pay_info);
|
|
|
+
|
|
|
+ Map<String, Object> map1 = JsonUtils.getMap(pay_info);
|
|
|
+ wxPayInfo.setPayInfo(map1);
|
|
|
+
|
|
|
+ }else {
|
|
|
+ wxPayInfo.setUrl(PALMPAY_URL + "?" + form);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ ordersMapper.insert(order);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return wxPayInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String PayNotify(HttpServletRequest request) {
|
|
|
+ Map<String, String> map = new HashMap();
|
|
|
+ Map<String, String[]> params = request.getParameterMap();
|
|
|
+ Set<Map.Entry<String, String[]>> entries = params.entrySet();
|
|
|
+ for (Map.Entry<String, String[]> entry : entries) {
|
|
|
+ try {
|
|
|
+ String val = entry.getValue()[0];
|
|
|
+ map.put(entry.getKey(), val.equalsIgnoreCase("null") ? "" : val);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ boolean b = signCheck(map, PALMPAY_APPKEY);
|
|
|
+
|
|
|
+
|
|
|
+ String appid = map.get("appid");
|
|
|
+ String mchId = map.get("mchId");
|
|
|
+ String outTradeNo = map.get("outTradeNo");
|
|
|
+ String pdorderid = map.get("pdorderid");
|
|
|
+ String chorderid = map.get("chorderid");
|
|
|
+ String attach = map.get("attach");
|
|
|
+ String status = map.get("status");
|
|
|
+ String openid = map.get("openid");
|
|
|
+ String timeEnd = map.get("timeEnd");
|
|
|
+ if ("success".equals(status)) {
|
|
|
+
|
|
|
+ Orders order = ordersMapper.selectByOutTradeNo(outTradeNo);
|
|
|
+ if (!"1".equals(order.getState())) {
|
|
|
+ Orders orders = new Orders();
|
|
|
+ orders.setOutTradeNo(outTradeNo);
|
|
|
+ orders.setState(1);
|
|
|
+ orders.setPdorderid(pdorderid);
|
|
|
+ orders.setTransactionId(chorderid);
|
|
|
+ orders.setUpdatetime(DateUtils.getNow());
|
|
|
+ orders.setFinishtime(DateUtils.getNow());
|
|
|
+ ordersMapper.updateByOutTradeNo(outTradeNo);
|
|
|
+ bookService.insertKandian(order.getUserId(), "1", order.getKandian(), order.getFreeKandian());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String chargePage(String userId, String code,String page) {
|
|
|
+ User userByUserId = userService.getUserByUserId(Long.parseLong(userId));
|
|
|
+ Integer channelId = userByUserId.getChannelId();
|
|
|
+ AdminConfig adminConfig = adminConfigMapper.selectByPrimaryKey(channelId);
|
|
|
+ Integer wxpayId = adminConfig.getWxpayId();
|
|
|
+ Wxpay wxpay = wxpayMapper.selectByPrimaryKey(wxpayId);
|
|
|
+ String appid = wxpay.getAppid();
|
|
|
+ String secret = wxpay.getSecret();
|
|
|
+ if (StringUtils.isEmpty(code)) {
|
|
|
+ String redirectUrl = "http://" + wxpay.getPayHost() + "/api/wxpay/page?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, URLEncoder.encode(redirectUrl, "UTF-8"), page);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return "redirect:" + url;
|
|
|
+ } else {
|
|
|
+
|
|
|
+ String openidUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
|
|
|
+ String format = String.format(openidUrl, appid, secret, code);
|
|
|
+ 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)
|
|
|
+ .replace("userId",userId)
|
|
|
+ .replace("{openid}",openid);
|
|
|
+ return "redirect:" + url;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //排序,校验
|
|
|
+ private boolean signCheck(Map<String, String> src, String sectKey) {
|
|
|
+ List<String> keys = new ArrayList<>(src.keySet());
|
|
|
+ Collections.sort(keys);
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ for (String key : keys) {
|
|
|
+ if (key.equals("sign")) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String val = src.get(key);
|
|
|
+ builder.append(key).append("=").append(val == null ? "" : val).append("&");
|
|
|
+ }
|
|
|
+ builder.append("key=").append(sectKey);
|
|
|
+ String sign = Md5Util.md5(builder.toString());
|
|
|
+ return sign.equals(src.get("sign"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String result = "{\"result\":{\"sign\":\"edda60a1e29f6ed539c0203921548cdb\",\"signType\":\"MD5\",\"money\":1000,\"appid\":\"4110\",\"charset\":\"UTF-8\",\"pay_info\":\"{\\\"appId\\\":\\\"wx4cb1f52885afcb03\\\",\\\"timeStamp\\\":\\\"1630571756\\\",\\\"nonceStr\\\":\\\"8cbff937d4fc4d1cb9e3944febcbb93d\\\",\\\"package\\\":\\\"prepay_id=wx02163556206106625d6f65c721e05b0000\\\",\\\"signType\\\":\\\"RSA\\\",\\\"paySign\\\":\\\"MokP0HszOcPwgu3JaHoBWVNtDhD4GNo44O0jZDr0/9jDtkBcxNn+gXELjk9e66+tqxW9cq0cesCPD7qhkOmQBheBnb+CB/cFCO+6IbDpekfVfzYIlH5kq2EgSKX5a3xEPG9ykR6IK0uBgCTkui5vFeusm24eibhix7xkeYwUPiYFnKm7pgaIjFZuTFKa9Vdm3a56Il3TaU8bjRz4b/iUlTJo+DKvxehjx4RuNG5h/SeN84ysKm39VCB0XHP2v9LkScLJC/4g706pySbqXZyoVPrerzCb+RiX4C1ZWDKSy9ri3LKNxr+Pm8L90M7njvWyq0u+8u+/vTZO1EGGbb7WDQ==\\\"}\",\"outTradeNo\":\"4110test1630571752721\",\"mchId\":\"2142013001\",\"pdorderid\":\"1127504471476173824\",\"version\":\"3.0\"},\"err\":\"success\",\"errcode\":\"0\"}";
|
|
|
+ PalmpayResponse object = JsonUtils.getObject(result, PalmpayResponse.class);
|
|
|
+ PalmpayResponse.Result r = object.getResult();
|
|
|
+ String pay_info = r.getPay_info();
|
|
|
+
|
|
|
+ Map<String, Object> map1 = JsonUtils.getMap(pay_info);
|
|
|
+ WxPayInfo wxPayInfo = new WxPayInfo();
|
|
|
+ wxPayInfo.setPayInfo(map1);
|
|
|
+ System.out.println(JsonUtils.toJsonStr(wxPayInfo));
|
|
|
+ }
|
|
|
+}
|