|
@@ -1,20 +1,26 @@
|
|
|
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.DataUtil;
|
|
|
-import com.book.server.utils.Md5Util;
|
|
|
+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;
|
|
@@ -69,6 +75,7 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
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();
|
|
@@ -79,7 +86,7 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
Integer ophostId = adminConfig.getOphostId();
|
|
|
Ophost ophost = ophostMapper.selectByPrimaryKey(ophostId);
|
|
|
Config url_pay = configMapper.selectByName("url_pay");
|
|
|
- String url = url_pay.getValue();
|
|
|
+ String url = "http://" + url_pay.getValue();
|
|
|
AdminExtend adminExtend = adminExtendMapper.selectByPrimaryKey(channelId);
|
|
|
//todo 入库
|
|
|
long orderid = TimeUnit.NANOSECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS) + wxPayRequest.getUserId();
|
|
@@ -129,9 +136,6 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
map.put("notifyUrl", url + "/api/wxpay/notify");
|
|
|
map.put("ip", wxPayRequest.getIp());
|
|
|
|
|
|
- // todo 修改
|
|
|
- /* map.put("subAppid", adminConfig.getAppid());
|
|
|
- map.put("openid", userByUserId.getOpenid());*/
|
|
|
map.put("api", "1");
|
|
|
|
|
|
|
|
@@ -143,24 +147,43 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
|
|
|
String form = DataUtil.maptoform(map) + "";
|
|
|
System.out.println("请求:" + PALMPAY_URL + "?" + form);
|
|
|
- /* 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());
|
|
|
- order.setPayJson(r.getPay_info());*/
|
|
|
-
|
|
|
- ordersMapper.insert(order);
|
|
|
WxPayInfo wxPayInfo = new WxPayInfo();
|
|
|
wxPayInfo.setUserId(userByUserId.getId() + "");
|
|
|
- wxPayInfo.setUrl(PALMPAY_URL + "?" + form);
|
|
|
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;
|
|
|
}
|
|
|
|
|
@@ -205,13 +228,50 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
|
|
|
- return "success";
|
|
|
}
|
|
|
|
|
|
|
|
@@ -231,4 +291,16 @@ public class WxPayServiceImpl implements WxPayService {
|
|
|
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));
|
|
|
+ }
|
|
|
}
|