package com.ruoyi.web.controller.api;
|
|
import com.alipay.api.AlipayApiException;
|
import com.alipay.api.internal.util.AlipaySignature;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.system.domain.TCourse;
|
import com.ruoyi.system.domain.TOrder;
|
import com.ruoyi.system.service.TCourseService;
|
import com.ruoyi.system.service.TOrderService;
|
import com.ruoyi.system.vx.WeChatConfig;
|
import com.ruoyi.system.vx.WeChatUtil;
|
import com.ruoyi.web.controller.tool.AlipayTradePagePay;
|
import com.ruoyi.web.controller.tool.AlipayTradeQuery;
|
import com.ruoyi.web.controller.tool.PayMoneyUtil;
|
import com.wechat.pay.java.core.notification.NotificationParser;
|
import com.wechat.pay.java.service.partnerpayments.app.model.Transaction;
|
import com.wechat.pay.java.service.refund.RefundService;
|
import com.wechat.pay.java.service.refund.model.AmountReq;
|
import com.wechat.pay.java.service.refund.model.CreateRequest;
|
import com.wechat.pay.java.service.refund.model.Refund;
|
import io.swagger.annotations.ApiOperation;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.xml.bind.ValidationException;
|
import java.math.BigDecimal;
|
import java.time.LocalDateTime;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.Map;
|
|
@RestController
|
@RequestMapping("/call-back")
|
public class CallBackController {
|
|
|
@Resource
|
private NotificationParser notificationParser;
|
@Resource
|
private TOrderService orderService;
|
|
@Resource
|
private WeChatConfig weChatConfig;
|
@Resource
|
private RefundService refundService;
|
@Resource
|
private PayMoneyUtil payMoneyUtil;
|
|
@ApiOperation(value = "支付回调",tags = {"支付宝轮询"})
|
@RequestMapping (value = "/ali/buy")
|
@Transactional
|
public R alipayCallback(Long orderId) throws AlipayApiException {
|
|
// String outTradeNo = stringStringMap.get("out_trade_no");
|
TOrder one = orderService.getById(orderId);
|
Boolean check = AlipayTradeQuery.check(one.getCode());
|
if (check) {
|
one.setPaymentStatus(2);
|
orderService.updateById(one);
|
if (one.getGoodType()==1){
|
try {
|
TCourse byId = courseService.getById(one.getGoodId());
|
byId.setBuyNum(byId.getBuyNum()+1);
|
courseService.updateById(byId);
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
|
}
|
return R.ok();
|
}else {
|
return R.fail("请支付");
|
}
|
|
}
|
|
@Resource
|
private TCourseService courseService;
|
|
@ApiOperation(value = "支付回调",tags = {"微信支付回调"})
|
@RequestMapping (value = "/buy")
|
@Transactional
|
public R payNotify(HttpServletRequest request) throws Exception{
|
System.err.println("======回调开始");
|
Transaction transaction;
|
transaction = notificationParser.parse(WeChatUtil.handleNodifyRequestParam(request), Transaction.class);
|
if (transaction.getTradeState() == Transaction.TradeStateEnum.SUCCESS) {
|
TOrder one = orderService.lambdaQuery().eq(TOrder::getCode, transaction.getOutTradeNo()).one();
|
one.setPaymentStatus(2);
|
one.setSerialNumber(transaction.getTransactionId());
|
one.setPayTime(LocalDateTime.now());
|
|
one.setPaymentType(1);
|
orderService.updateById(one);
|
if (one.getGoodType()==1){
|
try {
|
TCourse byId = courseService.getById(one.getGoodId());
|
byId.setBuyNum(byId.getBuyNum()+1);
|
courseService.updateById(byId);
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
|
}
|
}
|
return R.ok(null,"SUCCESS");
|
}
|
|
@ApiOperation(value = "退款",tags = {"统一退款接口"})
|
@PostMapping (value = "/refund")
|
@Transactional
|
public R refund(@RequestParam Long orderId) {
|
TOrder byId = orderService.getById(orderId);
|
if (byId.getPaymentType() == 1) {
|
try {
|
CreateRequest createRequest = new CreateRequest();
|
AmountReq amountReq = new AmountReq();
|
amountReq.setRefund(byId.getPaymentAmount().multiply(BigDecimal.valueOf(100)).longValue());
|
amountReq.setTotal(byId.getPaymentAmount().multiply(BigDecimal.valueOf(100)).longValue());
|
amountReq.setCurrency("CNY");
|
createRequest.setAmount(amountReq);
|
createRequest.setNotifyUrl("http://www.zhipingwang.com.cn:8081/call-back/buy");
|
createRequest.setOutTradeNo(byId.getCode());
|
createRequest.setOutRefundNo("TK" + WeChatUtil.generateTradeNumber());
|
|
Refund refund = refundService.create(createRequest);
|
byId.setPaymentStatus(3);
|
orderService.updateById(byId);
|
}catch (Exception e){
|
e.printStackTrace();
|
return R.fail("当前服务器异常,请联系客服");
|
}
|
|
}else {
|
try {
|
AlipayTradePagePay.refund(byId.getPaymentAmount().toString(),byId.getCode());
|
}catch (Exception e) {
|
e.printStackTrace();
|
return R.fail("当前服务器异常,请联系客服");
|
}finally {
|
byId.setPaymentStatus(3);
|
orderService.updateById(byId);
|
}
|
}
|
return R.ok();
|
}
|
|
|
}
|