|
@@ -5,37 +5,33 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import com.miaxis.common.config.WxpayConfig;
|
|
|
import com.miaxis.common.constant.Constants;
|
|
|
import com.miaxis.common.core.domain.Response;
|
|
|
-import com.miaxis.common.core.domain.entity.UserInfo;
|
|
|
import com.miaxis.common.exception.CustomException;
|
|
|
import com.miaxis.common.utils.AesUtil;
|
|
|
-import com.miaxis.common.utils.SecurityUtils;
|
|
|
import com.miaxis.feign.dto.FilmDTO;
|
|
|
import com.miaxis.film.domain.FilmOrder;
|
|
|
+import com.miaxis.film.domain.RefundRecord;
|
|
|
import com.miaxis.film.dto.*;
|
|
|
import com.miaxis.film.service.IFilmOrderService;
|
|
|
import com.miaxis.film.service.IRefundRecordService;
|
|
|
import io.swagger.annotations.Api;
|
|
|
-import io.swagger.annotations.ApiModelProperty;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
-import io.swagger.annotations.ApiParam;
|
|
|
import lombok.Data;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.joda.time.DateTime;
|
|
|
-import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.security.GeneralSecurityException;
|
|
|
-import java.sql.Date;
|
|
|
-import java.time.ZoneId;
|
|
|
-import java.time.ZonedDateTime;
|
|
|
-import java.util.Locale;
|
|
|
|
|
|
import static com.miaxis.common.utils.OrderCodeFactory.getOrderCode;
|
|
|
|
|
@@ -69,16 +65,80 @@ public class NotifyController {
|
|
|
*/
|
|
|
@PostMapping(value = "/wxpay")
|
|
|
@ApiOperation("微信支付回调")
|
|
|
- public WxNotifyReturnDTO wxpayNotify(@RequestBody FilmWxpayDTO filmWxpayDTO, HttpServletRequest request) throws GeneralSecurityException, IOException {
|
|
|
- AesUtil aesUtil = new AesUtil(wxpayConfig.getV3key().getBytes());
|
|
|
- FilmWxpayDTO.WxpaySource wxpaySource = filmWxpayDTO.getResource();
|
|
|
- String resourceString = aesUtil.decryptToString(wxpaySource.getAssociated_data().getBytes(), wxpaySource.getNonce().getBytes(), wxpaySource.getCiphertext());
|
|
|
+ public WxNotifyReturnDTO wxpayNotify(@RequestBody FilmWxpayDTO filmWxpayDTO, HttpServletRequest request, BufferedReader br) throws GeneralSecurityException, IOException {
|
|
|
+ String bodyString = getBodyString(br);
|
|
|
+ System.out.println(bodyString);
|
|
|
+ Boolean pass = validate(request);
|
|
|
+
|
|
|
+ String resourceString = getSourString(filmWxpayDTO);
|
|
|
log.info(resourceString);
|
|
|
JSONObject jsonObject = JSONObject.parseObject(resourceString);
|
|
|
//将回调数据写入数据库
|
|
|
writeNotifyDataToDb(jsonObject);
|
|
|
|
|
|
+ WxNotifyReturnDTO wxNotifyReturnDTO = new WxNotifyReturnDTO();
|
|
|
+ wxNotifyReturnDTO.setCode("SUCCESS");
|
|
|
+ wxNotifyReturnDTO.setMessage("成功");
|
|
|
+ return wxNotifyReturnDTO;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getBodyString(BufferedReader br) {
|
|
|
+ String inputLine;
|
|
|
+
|
|
|
+ String str = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ while ((inputLine = br.readLine()) != null) {
|
|
|
+ str += inputLine;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ br.close();
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println("IOException: " + e);
|
|
|
+
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Boolean validate(HttpServletRequest request) {
|
|
|
+ String sign = request.getHeader("Wechatpay-Signature");
|
|
|
+ String timestamp = request.getHeader("Wechatpay-Timestamp");
|
|
|
+ String nonce = request.getHeader("Wechatpay-Nonce");
|
|
|
+
|
|
|
+
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ sb.append(timestamp + "\n");
|
|
|
+ sb.append(nonce + "\n");
|
|
|
+ System.out.println(sb);
|
|
|
+ return true;
|
|
|
+
|
|
|
+// File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
|
|
|
+// String realPath =file.getAbsolutePath();
|
|
|
+// PrivateKey privateKey = CommonUtils.getPrivateKey(realPath);
|
|
|
+// // 进行签名服务
|
|
|
+// Signature signature = Signature.getInstance("SHA256withRSA");
|
|
|
+// signature.initSign(privateKey);
|
|
|
+// signature.update(sb.toString().getBytes("UTF-8"));
|
|
|
+// byte[] signedData = signature.sign();
|
|
|
+// String base64Str = Base64.getEncoder().encodeToString(signedData);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ /**
|
|
|
+ * 微信退款回调
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/refund")
|
|
|
+ @ApiOperation("微信退款回调")
|
|
|
+ public WxNotifyReturnDTO refundNotify(@RequestBody FilmWxpayDTO filmWxpayDTO, HttpServletRequest request) throws GeneralSecurityException, IOException {
|
|
|
+ String resourceString = getSourString(filmWxpayDTO);
|
|
|
+ log.info(resourceString);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(resourceString);
|
|
|
+ //将回调数据写入数据库
|
|
|
+ writeRefundNotifyDataToDb(jsonObject);
|
|
|
|
|
|
WxNotifyReturnDTO wxNotifyReturnDTO = new WxNotifyReturnDTO();
|
|
|
wxNotifyReturnDTO.setCode("SUCCESS");
|
|
@@ -86,6 +146,21 @@ public class NotifyController {
|
|
|
|
|
|
return wxNotifyReturnDTO;
|
|
|
}
|
|
|
+
|
|
|
+ private void writeRefundNotifyDataToDb(JSONObject jsonObject) {
|
|
|
+ String refundId = jsonObject.getString("refund_id");
|
|
|
+ RefundRecord refundRecord = refundRecordService.getByRefundId(refundId);
|
|
|
+ if (refundRecord == null) {
|
|
|
+ throw new CustomException("该退款订单不存在");
|
|
|
+ }
|
|
|
+ refundRecord.setTransactionId(jsonObject.getString("transaction_id"));
|
|
|
+ refundRecord.setUserReceivedAccount(jsonObject.getString("user_received_account"));
|
|
|
+ refundRecord.setStatus(jsonObject.getString("refund_status"));
|
|
|
+ refundRecordService.updateById(refundRecord);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 电影回调接口
|
|
|
*/
|
|
@@ -114,27 +189,26 @@ public class NotifyController {
|
|
|
}
|
|
|
filmOrderJsonData.setFilmOrderNotifyDTO(dbFilmOrderNotifyDTO);
|
|
|
filmOrder.setOrderDataJson(JSONObject.toJSONString(filmOrderJsonData));
|
|
|
- UserInfo student = SecurityUtils.getLoginUser().getStudent();
|
|
|
- Long id = student.getId();
|
|
|
- String refundCode = getOrderCode(id);
|
|
|
+ //UserInfo student = SecurityUtils.getLoginUser().getStudent();
|
|
|
//如果回调回来的订单关闭,则调用退款
|
|
|
if ("ORDER_CLOSE".equals(filmOrderNotifyDTO.getEventName())){
|
|
|
- threadPoolTaskExecutor.execute(new Runnable() {
|
|
|
+ String refundCode = getOrderCode(null);
|
|
|
+ filmOrder.setOutRefundNo(refundCode);
|
|
|
+ threadPoolTaskExecutor.execute(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
- try {
|
|
|
- refundRecordService.refund(filmOrder,refundCode);
|
|
|
- } catch (Exception e) {
|
|
|
- log.info(e.getMessage());
|
|
|
+ try {
|
|
|
+
|
|
|
+ refundRecordService.refund(filmOrder,refundCode);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info(e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- });
|
|
|
+ });
|
|
|
|
|
|
}
|
|
|
- filmOrder.setOutRefundNo(refundCode);
|
|
|
- filmOrderService.updateById(filmOrder);
|
|
|
-
|
|
|
|
|
|
+ filmOrderService.updateById(filmOrder);
|
|
|
|
|
|
FilmNotifyReturnDTO filmNotifyReturnDTO = new FilmNotifyReturnDTO();
|
|
|
filmNotifyReturnDTO.setCode("200");
|
|
@@ -241,6 +315,13 @@ public class NotifyController {
|
|
|
filmOrderService.updateById(filmOrder);
|
|
|
}
|
|
|
|
|
|
+ private String getSourString(FilmWxpayDTO filmWxpayDTO) throws GeneralSecurityException, IOException {
|
|
|
+ AesUtil aesUtil = new AesUtil(wxpayConfig.getV3key().getBytes());
|
|
|
+ FilmWxpayDTO.WxpaySource wxpaySource = filmWxpayDTO.getResource();
|
|
|
+ return aesUtil.decryptToString(wxpaySource.getAssociated_data().getBytes(), wxpaySource.getNonce().getBytes(), wxpaySource.getCiphertext());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
@Data
|