|
@@ -0,0 +1,237 @@
|
|
|
|
|
+package com.jpsoft.enterprise.modules.pay.alipay;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.alipay.api.AlipayApiException;
|
|
|
|
|
+import com.alipay.api.AlipayClient;
|
|
|
|
|
+import com.alipay.api.DefaultAlipayClient;
|
|
|
|
|
+import com.alipay.api.internal.util.AlipaySignature;
|
|
|
|
|
+import com.alipay.api.request.AlipaySystemOauthTokenRequest;
|
|
|
|
|
+import com.alipay.api.request.AlipayTradeCreateRequest;
|
|
|
|
|
+import com.alipay.api.request.AlipayTradePrecreateRequest;
|
|
|
|
|
+import com.alipay.api.response.AlipaySystemOauthTokenResponse;
|
|
|
|
|
+import com.alipay.api.response.AlipayTradeCreateResponse;
|
|
|
|
|
+import com.alipay.api.response.AlipayTradePrecreateResponse;
|
|
|
|
|
+import com.ijpay.alipay.AliPayApi;
|
|
|
|
|
+import com.jpsoft.enterprise.config.AliPayJpsoftConfig;
|
|
|
|
|
+import com.jpsoft.enterprise.modules.base.entity.OrderInfo;
|
|
|
|
|
+import com.jpsoft.enterprise.modules.base.service.OrderInfoService;
|
|
|
|
|
+import com.jpsoft.enterprise.modules.common.dto.MessageResult;
|
|
|
|
|
+import com.jpsoft.enterprise.modules.wechat.vo.UserInfo;
|
|
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author 墨鱼_mo
|
|
|
|
|
+ * @date 2019-11-26 9:48
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@RequestMapping("/aliPay")
|
|
|
|
|
+@RestController
|
|
|
|
|
+public class AlipayController {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private AliPayJpsoftConfig aliPayJpsoftConfig;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private OrderInfoService orderInfoService;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping(value="/payNotify")
|
|
|
|
|
+ public String payNotify(HttpServletRequest request) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 获取支付宝POST过来反馈信息
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, String> params = AliPayApi.toMap(request);
|
|
|
|
|
+
|
|
|
|
|
+ for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
|
|
+ System.out.println(entry.getKey() + " = " + entry.getValue());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ boolean verifyResult = AlipaySignature.rsaCheckV1(params, aliPayJpsoftConfig.getZfbPublicKey(), "UTF-8", "RSA2");
|
|
|
|
|
+
|
|
|
|
|
+ if (verifyResult) {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ OrderInfo orderInfo = orderInfoService.findByOutOrderNo(params.get("out_trade_no"));
|
|
|
|
|
+ String payFee = params.get("total_amount");
|
|
|
|
|
+ if (orderInfo == null) {
|
|
|
|
|
+ throw new Exception("支付已完成,但未找到此订单号的流水");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ orderInfo.setUpdateTime(new Date());
|
|
|
|
|
+ orderInfo.setUpdateBy(params.get("buyer_logon_id"));
|
|
|
|
|
+ orderInfo.setPayName("alipay");
|
|
|
|
|
+ orderInfo.setPayStatus(20);
|
|
|
|
|
+ orderInfo.setPayFee(new BigDecimal(payFee));
|
|
|
|
|
+ orderInfo.setTransactionId(params.get("trade_no"));
|
|
|
|
|
+ orderInfo.setPayTime(DateUtil.parse(params.get("gmt_payment")));
|
|
|
|
|
+ orderInfoService.update(orderInfo);
|
|
|
|
|
+ // orderLogService.insert2(new OrderLog(orderInfo.getId(),"支付完成",orderInfo.getCreateBy()));
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ new Thread(() -> {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ try{
|
|
|
|
|
+ //根据订单类型做各自处理
|
|
|
|
|
+ // orderInfoService.orderNotifyLogical(orderInfo);
|
|
|
|
|
+ }catch (Exception ex){
|
|
|
|
|
+ /*SysLog sysLog = new SysLog();
|
|
|
|
|
+ sysLog.setRemark(ex.getMessage());
|
|
|
|
|
+ sysLog.setData(orderInfo.toString());
|
|
|
|
|
+ sysLog.setPointcut("支付");
|
|
|
|
|
+ sysLog.setCreateTime(new Date());
|
|
|
|
|
+ sysLogService.insert(sysLog);*/
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }).start();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ return "success";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("notify_url 验证失败");
|
|
|
|
|
+ return "failure";
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
|
+ return "failure";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @GetMapping(value="findUserInfo/{authCode}")
|
|
|
|
|
+ public MessageResult findUserInfo(@PathVariable(name="authCode") String authCode){
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println("获取支付宝的userId");
|
|
|
|
|
+ try{
|
|
|
|
|
+
|
|
|
|
|
+ AlipayClient alipayClient = new DefaultAlipayClient(aliPayJpsoftConfig.getServiceUrl(),aliPayJpsoftConfig.getAppId(),aliPayJpsoftConfig.getPrivateKey(),"json",aliPayJpsoftConfig.getInputCharset(),aliPayJpsoftConfig.getZfbPublicKey(),aliPayJpsoftConfig.getSignType());
|
|
|
|
|
+
|
|
|
|
|
+ AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
|
|
|
|
|
+ request.setCode(authCode);
|
|
|
|
|
+ request.setGrantType("authorization_code");
|
|
|
|
|
+
|
|
|
|
|
+ AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println(oauthTokenResponse.getUserId());
|
|
|
|
|
+
|
|
|
|
|
+ HashMap<String,Object> dataMap = new HashMap<String,Object>();
|
|
|
|
|
+
|
|
|
|
|
+ UserInfo userInfo = new UserInfo();
|
|
|
|
|
+ userInfo.setOpenid(oauthTokenResponse.getUserId());
|
|
|
|
|
+ // dataMap.put("openId", oauthTokenResponse.getUserId());
|
|
|
|
|
+
|
|
|
|
|
+ // HashMap<String,Object> resultMap = new HashMap<String,Object>();
|
|
|
|
|
+
|
|
|
|
|
+ // resultMap.put("userInfo", dataMap);
|
|
|
|
|
+
|
|
|
|
|
+ return new MessageResult(true, "获取支付宝信息成功", userInfo,200);
|
|
|
|
|
+ } catch (AlipayApiException e) {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //处理异常
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ return new MessageResult(false, "获取支付宝授权失败", "",400);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ //处理异常
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ return new MessageResult(false, "获取支付宝授权失败", "",400);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @ApiOperation(value = "支付宝扫码支付")
|
|
|
|
|
+ @GetMapping("/tradePrecreatePay")
|
|
|
|
|
+ public MessageResult tradePrecreatePay(String orderId) {
|
|
|
|
|
+
|
|
|
|
|
+ MessageResult result = new MessageResult();
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ AlipayClient alipayClient = new DefaultAlipayClient(aliPayJpsoftConfig.getServiceUrl(), aliPayJpsoftConfig.getAppId(), aliPayJpsoftConfig.getPrivateKey(), "json", aliPayJpsoftConfig.getInputCharset(), aliPayJpsoftConfig.getZfbPublicKey(), aliPayJpsoftConfig.getSignType());
|
|
|
|
|
+ AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
|
|
|
|
|
+ request.setNotifyUrl(aliPayJpsoftConfig.getNotifyUrl());
|
|
|
|
|
+
|
|
|
|
|
+ OrderInfo orderInfo = orderInfoService.get(orderId);
|
|
|
|
|
+
|
|
|
|
|
+ if (orderInfo == null) {
|
|
|
|
|
+ throw new Exception("订单不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (orderInfo.getTotalFee().compareTo(BigDecimal.ZERO) != 1) {
|
|
|
|
|
+ throw new Exception("金额有误");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (orderInfo.getPayStatus() != 10) {
|
|
|
|
|
+ throw new Exception("订单状态有误");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ orderInfoService.update(orderInfo);
|
|
|
|
|
+
|
|
|
|
|
+ String appAuthToken = aliPayJpsoftConfig.getAppAuthToken();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> maps = new HashMap<>();
|
|
|
|
|
+ maps.put("out_trade_no", orderInfo.getOutOrderNo());
|
|
|
|
|
+ maps.put("total_amount", String.valueOf(orderInfo.getTotalFee()));
|
|
|
|
|
+ maps.put("subject", orderInfo.getBody());
|
|
|
|
|
+ maps.put("timeout_express", "5m");
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject extendParams = new JSONObject();
|
|
|
|
|
+
|
|
|
|
|
+ extendParams.put("sys_service_provider_id", aliPayJpsoftConfig.getMchId());
|
|
|
|
|
+ maps.put("extend_params", extendParams);
|
|
|
|
|
+ request.putOtherTextParam("app_auth_token",appAuthToken);
|
|
|
|
|
+ //把订单信息转换为json对象的字符串
|
|
|
|
|
+ String postdata = JSONObject.toJSONString(maps);
|
|
|
|
|
+ request.setBizContent(postdata);
|
|
|
|
|
+ AlipayTradePrecreateResponse response = alipayClient.execute(request);
|
|
|
|
|
+ String body = response.getBody();
|
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(body);
|
|
|
|
|
+ String alipayTradePrecreateResponse = jsonObject.getString("alipay_trade_precreate_response");
|
|
|
|
|
+ JSONObject jsonResponse = JSONObject.parseObject(alipayTradePrecreateResponse);
|
|
|
|
|
+
|
|
|
|
|
+ if (!jsonResponse.getString("code").equals("10000")) {
|
|
|
|
|
+ throw new Exception(jsonResponse.getString("sub_msg"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String qr_code = jsonObject.getJSONObject("alipay_trade_precreate_response").getString("qr_code");
|
|
|
|
|
+ result.setResult(true);
|
|
|
|
|
+
|
|
|
|
|
+ log.warn("qr_code=" + qr_code);
|
|
|
|
|
+ result.setData(qr_code);
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
|
+ result.setResult(false);
|
|
|
|
|
+ result.setMessage(e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+}
|