支持支付宝获取用户手机号

This commit is contained in:
谭凯凯
2024-09-26 17:23:38 +08:00
committed by Tankaikai
parent a97e33984d
commit e7d8d85ea1
4 changed files with 110 additions and 33 deletions

View File

@@ -1,6 +1,7 @@
package com.chaozhanggui.system.cashierservice.controller; package com.chaozhanggui.system.cashierservice.controller;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.chaozhanggui.system.cashierservice.annotation.LimitSubmit; import com.chaozhanggui.system.cashierservice.annotation.LimitSubmit;
import com.chaozhanggui.system.cashierservice.service.PayService; import com.chaozhanggui.system.cashierservice.service.PayService;
import com.chaozhanggui.system.cashierservice.sign.Result; import com.chaozhanggui.system.cashierservice.sign.Result;
@@ -40,8 +41,17 @@ public class PayController {
return Result.fail("订单号不允许为空"); return Result.fail("订单号不允许为空");
} }
if (!map.containsKey("payType")) {
return Result.fail("支付类型不允许为空");
}
String payType = map.get("payType");
if (StrUtil.isEmpty(payType)) {
return Result.fail("支付类型不允许为空");
}
try { try {
return payService.payOrder(openId, map.get("orderId"), IpUtil.getIpAddr(request)); return payService.payOrderTest(openId, map.get("orderId"), payType, IpUtil.getIpAddr(request));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -23,6 +23,8 @@ public class TbMerchantThirdApply implements Serializable {
private String smallAppid; private String smallAppid;
private String alipaySmallAppid;
private String storeId; private String storeId;
@@ -117,4 +119,12 @@ public class TbMerchantThirdApply implements Serializable {
public void setStoreId(String storeId) { public void setStoreId(String storeId) {
this.storeId = storeId; this.storeId = storeId;
} }
public String getAlipaySmallAppid() {
return alipaySmallAppid;
}
public void setAlipaySmallAppid(String alipaySmallAppid) {
this.alipaySmallAppid = alipaySmallAppid;
}
} }

View File

@@ -2,6 +2,7 @@ package com.chaozhanggui.system.cashierservice.service;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.dao.*; import com.chaozhanggui.system.cashierservice.dao.*;
@@ -35,7 +36,6 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource; import javax.annotation.Resource;
@@ -166,9 +166,28 @@ public class PayService {
this.shopSongOrderService = shopSongOrderService; this.shopSongOrderService = shopSongOrderService;
} }
@Transactional(rollbackFor = Exception.class)
public Result payOrderTest(String openId, String orderId, String payType, String ip) throws Exception {
//thirdUrl;
String appId = "66e3dd399a7621f45a6293c1";
String reqbody = "茉莉蜜茶";
BigDecimal money = new BigDecimal("0.10");
long amount = money.setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).longValue();
payType = "ALIPAY";
String smallAppid = "2021004145625815";
openId = "2088812533865205";
ip = "1.80.208.76";
String timesSS = DateUtils.getsdfTimesSS();
String storeId = "S2409148611";
//callFSTBack
//null
String appToken = "jVQs9aBIFOj1mh2ioDp5RLiNBLcQoRnhniH9xlyc3ZKmDcwOpDargyEIYASCePtbA4hyJ5aeH841HfBA4rEKFsxl5AJjGcQa7qT28qU0SPN6v9dNzKYK1eTyvSw6mNBh";
PublicResp<WxScanPayResp> scanpay = thirdPayService.scanpay(thirdUrl, appId, reqbody, reqbody, amount, payType, smallAppid, openId, ip, timesSS, storeId, callFSTBack, null, appToken);
return null;
}
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Result payOrder(String openId, String orderId, String ip) throws Exception { public Result payOrder(String openId, String orderId, String payType, String ip) throws Exception {
if (ObjectUtil.isEmpty(openId) || Objects.isNull(openId)) { if (ObjectUtil.isEmpty(openId) || Objects.isNull(openId)) {
return Result.fail("付款用户[userId]参数不能为空"); return Result.fail("付款用户[userId]参数不能为空");
@@ -205,6 +224,10 @@ public class PayService {
return Result.fail("支付通道不存在"); return Result.fail("支付通道不存在");
} }
if ("aliPay".equals(payType) && StrUtil.isBlank(thirdApply.getAlipaySmallAppid())) {
return Result.fail("店铺未配置支付宝小程序appId");
}
TbOrderPayment payment = tbOrderPaymentMapper.selectByOrderId(orderId); TbOrderPayment payment = tbOrderPaymentMapper.selectByOrderId(orderId);
if (ObjectUtil.isEmpty(payment) || payment == null) { if (ObjectUtil.isEmpty(payment) || payment == null) {
payment = new TbOrderPayment(); payment = new TbOrderPayment();
@@ -212,8 +235,12 @@ public class PayService {
payment.setAmount(orderInfo.getOrderAmount()); payment.setAmount(orderInfo.getOrderAmount());
payment.setPaidAmount(orderInfo.getPayAmount()); payment.setPaidAmount(orderInfo.getPayAmount());
payment.setHasRefundAmount(BigDecimal.ZERO); payment.setHasRefundAmount(BigDecimal.ZERO);
payment.setPayName("微信支付"); if ("wechatPay".equals(payType)) {
payment.setPayType("wechatPay"); payment.setPayName("微信支付");
} else if ("aliPay".equals(payType)) {
payment.setPayName("支付宝支付");
}
payment.setPayType(payType);
payment.setReceived(payment.getAmount()); payment.setReceived(payment.getAmount());
payment.setChangeFee(BigDecimal.ZERO); payment.setChangeFee(BigDecimal.ZERO);
payment.setMemberId(orderInfo.getMemberId()); payment.setMemberId(orderInfo.getMemberId());
@@ -222,6 +249,12 @@ public class PayService {
payment.setCreatedAt(System.currentTimeMillis()); payment.setCreatedAt(System.currentTimeMillis());
tbOrderPaymentMapper.insert(payment); tbOrderPaymentMapper.insert(payment);
} else { } else {
if (payType.equals("wechatPay")) {
payment.setPayName("微信支付");
} else if (payType.equals("aliPay")) {
payment.setPayName("支付宝支付");
}
payment.setPayType(payType);
payment.setUpdatedAt(System.currentTimeMillis()); payment.setUpdatedAt(System.currentTimeMillis());
tbOrderPaymentMapper.updateByPrimaryKey(payment); tbOrderPaymentMapper.updateByPrimaryKey(payment);
} }
@@ -281,10 +314,25 @@ public class PayService {
reqbody = body.toString(); reqbody = body.toString();
} }
PublicResp<WxScanPayResp> publicResp = thirdPayService.scanpay(thirdUrl, thirdApply.getAppId(), String smallAppid = thirdApply.getSmallAppid();
reqbody, reqbody, if ("aliPay".equals(payType)){
smallAppid = thirdApply.getAlipaySmallAppid();
}
String convertPayType = "aliPay".equals(payType) ? "ALIPAY" : "WECHAT";
PublicResp<WxScanPayResp> publicResp = thirdPayService.scanpay(thirdUrl,
thirdApply.getAppId(),
reqbody,
reqbody,
payment.getAmount().setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).longValue(), payment.getAmount().setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).longValue(),
"WECHAT", thirdApply.getSmallAppid(), openId, ip, DateUtils.getsdfTimesSS(), thirdApply.getStoreId(), callFSTBack, null, thirdApply.getAppToken()); convertPayType,
smallAppid,
openId,
ip,
DateUtils.getsdfTimesSS(),
thirdApply.getStoreId(),
callFSTBack,
null,
thirdApply.getAppToken());
if (ObjectUtil.isNotNull(publicResp) && ObjectUtil.isNotEmpty(publicResp)) { if (ObjectUtil.isNotNull(publicResp) && ObjectUtil.isNotEmpty(publicResp)) {
if ("000000".equals(publicResp.getCode())) { if ("000000".equals(publicResp.getCode())) {
WxScanPayResp wxScanPayResp = publicResp.getObjData(); WxScanPayResp wxScanPayResp = publicResp.getObjData();
@@ -407,7 +455,7 @@ public class PayService {
tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed"); tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed");
tbOrderDetailMapper.updateStatusByOrderIdAndStatus(Integer.valueOf(orderId), "closed"); tbOrderDetailMapper.updateStatusByOrderIdAndStatus(Integer.valueOf(orderId), "closed");
outRecordMapper.updateByOrderIdAndStatus(orderInfo.getId(),"closed"); outRecordMapper.updateByOrderIdAndStatus(orderInfo.getId(), "closed");
log.info("更新购物车:{}", cartCount); log.info("更新购物车:{}", cartCount);
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
@@ -427,13 +475,13 @@ public class PayService {
mQUtils.sendStockSaleMsg(data); mQUtils.sendStockSaleMsg(data);
JSONObject baObj=new JSONObject(); JSONObject baObj = new JSONObject();
baObj.put("userId",userInfo.getId()); baObj.put("userId", userInfo.getId());
baObj.put("shopId",user.getShopId()); baObj.put("shopId", user.getShopId());
baObj.put("amount",orderInfo.getOrderAmount()); baObj.put("amount", orderInfo.getOrderAmount());
baObj.put("balance",user.getAmount()); baObj.put("balance", user.getAmount());
baObj.put("type","消费"); baObj.put("type", "消费");
baObj.put("time",flow.getCreateTime()); baObj.put("time", flow.getCreateTime());
producer.balance(baObj.toString()); producer.balance(baObj.toString());
// 为代客下单清楚当前台桌最新订单 // 为代客下单清楚当前台桌最新订单
@@ -528,6 +576,11 @@ public class PayService {
if (ObjectUtil.isEmpty(thirdApply) || ObjectUtil.isNull(thirdApply)) { if (ObjectUtil.isEmpty(thirdApply) || ObjectUtil.isNull(thirdApply)) {
return Result.fail("支付通道不存在"); return Result.fail("支付通道不存在");
} }
if ("aliPay".equals(payType) && StrUtil.isBlank(thirdApply.getAlipaySmallAppid())) {
return Result.fail("店铺未配置支付宝小程序appId");
}
StringBuffer body = new StringBuffer(); StringBuffer body = new StringBuffer();
body.append(orderInfo.getProName()); body.append(orderInfo.getProName());
TbOrderPayment payment = tbOrderPaymentMapper.selectByOrderId(orderInfo.getOrderNo()); TbOrderPayment payment = tbOrderPaymentMapper.selectByOrderId(orderInfo.getOrderNo());
@@ -605,14 +658,19 @@ public class PayService {
} else { } else {
reqbody = body.toString(); reqbody = body.toString();
} }
String smallAppid = thirdApply.getSmallAppid();
if ("aliPay".equals(payType)){
smallAppid = thirdApply.getAlipaySmallAppid();
}
String convertPayType = "aliPay".equals(payType) ? "ALIPAY" : "WECHAT";
PublicResp<WxScanPayResp> publicResp = thirdPayService.scanpay( PublicResp<WxScanPayResp> publicResp = thirdPayService.scanpay(
thirdUrl, thirdUrl,
thirdApply.getAppId(), thirdApply.getAppId(),
reqbody, reqbody,
reqbody, reqbody,
payment.getAmount().setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).longValue(), payment.getAmount().setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).longValue(),
"WECHAT", convertPayType,
thirdApply.getSmallAppid(), smallAppid,
userId, userId,
ip, ip,
// orderInfo.getOrderNo(), // orderInfo.getOrderNo(),
@@ -901,7 +959,7 @@ public class PayService {
//更新子单状态 //更新子单状态
tbOrderDetailMapper.updateStatusByOrderIdAndStatus(orderInfo.getId(), "closed"); tbOrderDetailMapper.updateStatusByOrderIdAndStatus(orderInfo.getId(), "closed");
outRecordMapper.updateByOrderIdAndStatus(orderInfo.getId(),"closed"); outRecordMapper.updateByOrderIdAndStatus(orderInfo.getId(), "closed");
//修改主单状态 //修改主单状态
orderInfo.setStatus("closed"); orderInfo.setStatus("closed");
orderInfo.setPayType("wx_lite"); orderInfo.setPayType("wx_lite");
@@ -962,7 +1020,7 @@ public class PayService {
orderInfo.setPayOrderNo(payOrderNO); orderInfo.setPayOrderNo(payOrderNO);
orderInfo.setPayAmount(orderInfo.getOrderAmount()); orderInfo.setPayAmount(orderInfo.getOrderAmount());
tbOrderInfoMapper.updateByPrimaryKeySelective(orderInfo); tbOrderInfoMapper.updateByPrimaryKeySelective(orderInfo);
outRecordMapper.updateByOrderIdAndStatus(orderInfo.getId(),"closed"); outRecordMapper.updateByOrderIdAndStatus(orderInfo.getId(), "closed");
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("token", 0); jsonObject.put("token", 0);
@@ -1133,7 +1191,7 @@ public class PayService {
flow.setIsReturn("0"); flow.setIsReturn("0");
tbShopUserFlowMapper.insert(flow); tbShopUserFlowMapper.insert(flow);
//会员活动 //会员活动
giveActivate(tbShopUser,memberIn.getAmount(),flow.getId()); giveActivate(tbShopUser, memberIn.getAmount(), flow.getId());
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("shopId", memberIn.getShopId()); jsonObject.put("shopId", memberIn.getShopId());
@@ -1143,14 +1201,14 @@ public class PayService {
return "success"; return "success";
} }
public BigDecimal giveActivate(TbShopUser tbShopUser, BigDecimal memAmount,Integer flowId){ public BigDecimal giveActivate(TbShopUser tbShopUser, BigDecimal memAmount, Integer flowId) {
TbActivate activate = tbActivateMapper.selectByAmount(tbShopUser.getShopId(), memAmount); TbActivate activate = tbActivateMapper.selectByAmount(tbShopUser.getShopId(), memAmount);
if (ObjectUtil.isNotEmpty(activate) && ObjectUtil.isNotNull(activate)) { if (ObjectUtil.isNotEmpty(activate) && ObjectUtil.isNotNull(activate)) {
if (activate.getIsGiftPro() != null && activate.getIsGiftPro() == 1) { if (activate.getIsGiftPro() != null && activate.getIsGiftPro() == 1) {
List<TbActivateProduct> tbActivateProducts = actProductMapper.queryAllByActivateId(activate.getId()); List<TbActivateProduct> tbActivateProducts = actProductMapper.queryAllByActivateId(activate.getId());
List<TbActivateInRecord> actGiveRecords = new ArrayList<>(); List<TbActivateInRecord> actGiveRecords = new ArrayList<>();
for (TbActivateProduct actPro : tbActivateProducts) { for (TbActivateProduct actPro : tbActivateProducts) {
TbActivateInRecord record = new TbActivateInRecord(Integer.valueOf(tbShopUser.getId()), actPro.getProductId(), actPro.getNum(), Integer.valueOf(tbShopUser.getShopId()), activate.getId(),flowId); TbActivateInRecord record = new TbActivateInRecord(Integer.valueOf(tbShopUser.getId()), actPro.getProductId(), actPro.getNum(), Integer.valueOf(tbShopUser.getShopId()), activate.getId(), flowId);
actGiveRecords.add(record); actGiveRecords.add(record);
} }
activateInRecordMapper.insertBatch(actGiveRecords); activateInRecordMapper.insertBatch(actGiveRecords);
@@ -1232,7 +1290,7 @@ public class PayService {
flow.setIsReturn("0"); flow.setIsReturn("0");
tbShopUserFlowMapper.insert(flow); tbShopUserFlowMapper.insert(flow);
//会员活动 //会员活动
BigDecimal awardAmount= giveActivate(tbShopUser,memberIn.getAmount(),flow.getId()); BigDecimal awardAmount = giveActivate(tbShopUser, memberIn.getAmount(), flow.getId());
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("shopId", memberIn.getShopId()); jsonObject.put("shopId", memberIn.getShopId());
jsonObject.put("type", "wxMemberIn"); jsonObject.put("type", "wxMemberIn");
@@ -1240,14 +1298,13 @@ public class PayService {
producer.putOrderCollect(jsonObject.toJSONString()); producer.putOrderCollect(jsonObject.toJSONString());
JSONObject baObj = new JSONObject();
JSONObject baObj=new JSONObject();
baObj.put("userId", tbShopUser.getUserId()); baObj.put("userId", tbShopUser.getUserId());
baObj.put("shopId",tbShopUser.getShopId()); baObj.put("shopId", tbShopUser.getShopId());
baObj.put("amount",ObjectUtil.isNull(awardAmount)?memberIn.getAmount():memberIn.getAmount().add(awardAmount)); baObj.put("amount", ObjectUtil.isNull(awardAmount) ? memberIn.getAmount() : memberIn.getAmount().add(awardAmount));
baObj.put("balance",tbShopUser.getAmount()); baObj.put("balance", tbShopUser.getAmount());
baObj.put("type","充值"); baObj.put("type", "充值");
baObj.put("time",flow.getCreateTime()); baObj.put("time", flow.getCreateTime());
producer.balance(baObj.toString()); producer.balance(baObj.toString());
return "SUCCESS"; return "SUCCESS";
@@ -1282,7 +1339,6 @@ public class PayService {
} }
public Result paySongOrder(String openId, String shopId, Integer orderId, String ip, TbMerchantThirdApply thirdApply) throws JsonProcessingException { public Result paySongOrder(String openId, String shopId, Integer orderId, String ip, TbMerchantThirdApply thirdApply) throws JsonProcessingException {
if (ObjectUtil.isEmpty(openId) || Objects.isNull(openId)) { if (ObjectUtil.isEmpty(openId) || Objects.isNull(openId)) {
return Result.fail("付款用户[openId]参数不能为空"); return Result.fail("付款用户[openId]参数不能为空");

View File

@@ -11,13 +11,14 @@
<result column="created_at" jdbcType="BIGINT" property="createdAt" /> <result column="created_at" jdbcType="BIGINT" property="createdAt" />
<result column="updated_at" jdbcType="BIGINT" property="updatedAt" /> <result column="updated_at" jdbcType="BIGINT" property="updatedAt" />
<result column="small_appid" jdbcType="VARCHAR" property="smallAppid" /> <result column="small_appid" jdbcType="VARCHAR" property="smallAppid" />
<result column="alipay_small_appid" jdbcType="VARCHAR" property="alipaySmallAppid" />
<result column="store_id" jdbcType="VARCHAR" property="storeId" /> <result column="store_id" jdbcType="VARCHAR" property="storeId" />
</resultMap> </resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.chaozhanggui.system.cashierservice.entity.TbMerchantThirdApply"> <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.chaozhanggui.system.cashierservice.entity.TbMerchantThirdApply">
<result column="app_token" jdbcType="LONGVARCHAR" property="appToken" /> <result column="app_token" jdbcType="LONGVARCHAR" property="appToken" />
</resultMap> </resultMap>
<sql id="Base_Column_List"> <sql id="Base_Column_List">
id, type, app_id, status, pay_password, applyment_state, created_at, updated_at,small_appid,store_id id, type, app_id, status, pay_password, applyment_state, created_at, updated_at,small_appid,alipay_small_appid,store_id
</sql> </sql>
<sql id="Blob_Column_List"> <sql id="Blob_Column_List">
app_token app_token