|
|
|
@ -1,10 +1,17 @@
|
|
|
|
|
package com.payment.djypaymentplat.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.alipay.api.*;
|
|
|
|
|
import com.alipay.api.domain.AlipayTradeAppPayModel;
|
|
|
|
|
import com.alipay.api.domain.AlipayTradePagePayModel;
|
|
|
|
|
import com.alipay.api.internal.util.AlipaySignature;
|
|
|
|
|
import com.alipay.api.request.AlipayTradePagePayRequest;
|
|
|
|
|
import com.alipay.api.response.AlipayTradePagePayResponse;
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import com.google.zxing.BarcodeFormat;
|
|
|
|
|
import com.google.zxing.WriterException;
|
|
|
|
|
import com.google.zxing.common.BitMatrix;
|
|
|
|
|
import com.google.zxing.qrcode.QRCodeWriter;
|
|
|
|
|
import com.payment.djypaymentplat.config.AliPayConfig;
|
|
|
|
|
import com.payment.djypaymentplat.config.WeixinPayConfig;
|
|
|
|
|
import com.payment.djypaymentplat.domain.*;
|
|
|
|
|
import com.payment.djypaymentplat.mapper.PayOrderInfoMapper;
|
|
|
|
@ -12,12 +19,14 @@ import com.payment.djypaymentplat.mapper.PayOrderQrcodeMapper;
|
|
|
|
|
import com.payment.djypaymentplat.model.PayOrderInfo;
|
|
|
|
|
import com.payment.djypaymentplat.model.PayOrderQrcode;
|
|
|
|
|
import com.payment.djypaymentplat.service.ICompanyChargeService;
|
|
|
|
|
import com.payment.djypaymentplat.util.AliPayUtil;
|
|
|
|
|
import com.payment.djypaymentplat.util.HttpUtils;
|
|
|
|
|
import com.payment.djypaymentplat.util.WechatPay2ValidatorForRequest;
|
|
|
|
|
import com.payment.djypaymentplat.util.WeixinPayUtil;
|
|
|
|
|
import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
|
|
|
|
|
import com.wechat.pay.java.service.payments.model.Transaction;
|
|
|
|
|
import com.wechat.pay.java.service.payments.nativepay.model.*;
|
|
|
|
|
import okhttp3.*;
|
|
|
|
|
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
@ -30,6 +39,8 @@ import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.security.GeneralSecurityException;
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
@ -69,6 +80,11 @@ public class CompanyChargeServiceImpl implements ICompanyChargeService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private WeixinPayUtil weixinPayUtil;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private AliPayUtil aliPayUtil;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private AliPayConfig aliPayConfig;
|
|
|
|
|
|
|
|
|
|
@PostMapping("/createorder")
|
|
|
|
|
@Override
|
|
|
|
@ -82,13 +98,24 @@ public class CompanyChargeServiceImpl implements ICompanyChargeService {
|
|
|
|
|
|
|
|
|
|
orderInfo.setPkId(UUID.randomUUID().toString().replace("-", ""));
|
|
|
|
|
orderInfo.setAmount(model.main.amount);
|
|
|
|
|
orderInfo.setAppId(wexinConfig.Appid());
|
|
|
|
|
orderInfo.setMchId(wexinConfig.MerchantId());
|
|
|
|
|
|
|
|
|
|
if(model.main.payApiCode.equals("WEIXIN"))
|
|
|
|
|
{
|
|
|
|
|
orderInfo.setAppId(wexinConfig.Appid());
|
|
|
|
|
orderInfo.setMchId(wexinConfig.MerchantId());
|
|
|
|
|
}
|
|
|
|
|
else if(model.main.payApiCode.equals("ALIPAY"))
|
|
|
|
|
{
|
|
|
|
|
orderInfo.setAppId(aliPayConfig.AppId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
orderInfo.setDescription(model.main.notes);
|
|
|
|
|
orderInfo.setCurrency(model.main.currency);
|
|
|
|
|
orderInfo.setCreateduserid(model.main.operatorId);
|
|
|
|
|
orderInfo.setCreatedusername(model.main.operatorName);
|
|
|
|
|
orderInfo.setStatus("WAIT");
|
|
|
|
|
orderInfo.setSenderKey(model.head.senderKey);
|
|
|
|
|
orderInfo.setSenderPkid(model.main.businessId);
|
|
|
|
|
|
|
|
|
|
Date nowDate = new Date();
|
|
|
|
|
try {
|
|
|
|
@ -102,71 +129,108 @@ public class CompanyChargeServiceImpl implements ICompanyChargeService {
|
|
|
|
|
payOrderInfoMapper.insert(orderInfo);
|
|
|
|
|
|
|
|
|
|
PayOrderInfo payOrder = payOrderInfoMapper.selectByPrimaryKey(orderInfo.getPkId());
|
|
|
|
|
/*
|
|
|
|
|
1、测试充值0.1元,能正常触发下单。
|
|
|
|
|
2、生成支付二维码。
|
|
|
|
|
3、部署接收端接收充值成功回执
|
|
|
|
|
*/
|
|
|
|
|
// 初始化商户配置
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Config config =
|
|
|
|
|
new RSAAutoCertificateConfig.Builder()
|
|
|
|
|
.merchantId(wexinConfig.MerchantId())
|
|
|
|
|
// 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
|
|
|
|
|
.privateKeyFromPath(wexinConfig.PrivateKeyPath())
|
|
|
|
|
.merchantSerialNumber(wexinConfig.MerchantSerialNumber())
|
|
|
|
|
.apiV3Key(wexinConfig.ApiV3Key())
|
|
|
|
|
.build();
|
|
|
|
|
String getCodeUrl = "";
|
|
|
|
|
String alipayFormAct = "";
|
|
|
|
|
|
|
|
|
|
if(model.main.payApiCode.equals("WEIXIN")) {
|
|
|
|
|
PrepayRequest request = new PrepayRequest();
|
|
|
|
|
|
|
|
|
|
request.setAppid(wexinConfig.Appid());
|
|
|
|
|
request.setMchid(wexinConfig.MerchantId());
|
|
|
|
|
request.setDescription(model.main.notes);
|
|
|
|
|
request.setOutTradeNo(payOrder.getOrderNo());
|
|
|
|
|
request.setNotifyUrl(wexinConfig.NoticeUrl());
|
|
|
|
|
|
|
|
|
|
Amount amount = new Amount();
|
|
|
|
|
|
|
|
|
|
amount.setTotal(model.main.amount.multiply(new BigDecimal(100)).intValue());
|
|
|
|
|
amount.setCurrency(model.main.currency);
|
|
|
|
|
request.setAmount(amount);
|
|
|
|
|
|
|
|
|
|
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
|
|
|
|
|
// 调用接口
|
|
|
|
|
PrepayResponse preRlt = weixinPayUtil.getNativePayService().prepay(request);
|
|
|
|
|
|
|
|
|
|
service = new NativePayService.Builder().config(config).build();
|
|
|
|
|
*/
|
|
|
|
|
log.info("支付返回={}", gson.toJson(preRlt));
|
|
|
|
|
|
|
|
|
|
if (preRlt != null && preRlt.getCodeUrl() != "") {
|
|
|
|
|
getCodeUrl = preRlt.getCodeUrl();
|
|
|
|
|
}
|
|
|
|
|
}else if(model.main.payApiCode.equals("ALIPAY")){
|
|
|
|
|
|
|
|
|
|
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
|
|
|
|
|
|
|
|
|
|
AlipayTradePagePayModel aliPayModel = new AlipayTradePagePayModel();
|
|
|
|
|
//aliPayModel.setBody("我是测试数据");
|
|
|
|
|
aliPayModel.setSubject(model.main.notes);
|
|
|
|
|
aliPayModel.setOutTradeNo(payOrder.getOrderNo());
|
|
|
|
|
aliPayModel.setTimeoutExpress("60m");
|
|
|
|
|
aliPayModel.setTotalAmount(model.main.amount.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
|
|
|
|
|
|
|
|
|
|
PrepayRequest request = new PrepayRequest();
|
|
|
|
|
aliPayModel.setProductCode("FAST_INSTANT_TRADE_PAY");
|
|
|
|
|
aliPayModel.setQrPayMode("4");
|
|
|
|
|
|
|
|
|
|
request.setAppid(wexinConfig.Appid());
|
|
|
|
|
request.setMchid(wexinConfig.MerchantId());
|
|
|
|
|
request.setDescription(model.main.notes);
|
|
|
|
|
request.setOutTradeNo(payOrder.getOrderNo());
|
|
|
|
|
request.setNotifyUrl(wexinConfig.NoticeUrl());
|
|
|
|
|
alipayRequest.setBizModel(aliPayModel);
|
|
|
|
|
alipayRequest.setNotifyUrl(aliPayConfig.DjyNoticeUrl());
|
|
|
|
|
|
|
|
|
|
Amount amount = new Amount();
|
|
|
|
|
CertAlipayRequest certAlipayRequest = new CertAlipayRequest ();
|
|
|
|
|
|
|
|
|
|
amount.setTotal(model.main.amount.multiply(new BigDecimal(100)).intValue());
|
|
|
|
|
amount.setCurrency(model.main.currency);
|
|
|
|
|
request.setAmount(amount);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
AlipayClient alipayClient = aliPayUtil.getAlipayClient();
|
|
|
|
|
AlipayTradePagePayResponse aliPayResult = alipayClient.pageExecute(alipayRequest,"GET");
|
|
|
|
|
|
|
|
|
|
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
|
|
|
|
|
// 调用接口
|
|
|
|
|
PrepayResponse preRlt = weixinPayUtil.getNativePayService().prepay(request);
|
|
|
|
|
log.info("请求支付宝下单后返回结果,body={}",aliPayResult.getBody());
|
|
|
|
|
|
|
|
|
|
if(aliPayResult.isSuccess())
|
|
|
|
|
{
|
|
|
|
|
alipayFormAct = aliPayResult.getBody();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (AlipayApiException e)
|
|
|
|
|
{
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompanyChargeResult chargeRlt = new CompanyChargeResult();
|
|
|
|
|
|
|
|
|
|
chargeRlt.excuteDate = sdf.format(nowDate);
|
|
|
|
|
chargeRlt.orderNo = payOrder.getOrderNo();
|
|
|
|
|
|
|
|
|
|
log.info("支付返回={}", gson.toJson(preRlt));
|
|
|
|
|
if(!StringUtils.isEmpty(getCodeUrl) || !StringUtils.isEmpty(alipayFormAct)){
|
|
|
|
|
|
|
|
|
|
if(preRlt != null && preRlt.getCodeUrl() != ""){
|
|
|
|
|
if(!StringUtils.isEmpty(getCodeUrl)){
|
|
|
|
|
chargeRlt.qRCodeUrl = getCodeUrl;
|
|
|
|
|
chargeRlt.imgBase64 = GetQRCodeFile(getCodeUrl).getData();
|
|
|
|
|
payOrder.setCallbackUrl(getCodeUrl);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
payOrder.setUpdatedtime(sdf.parse(sdf.format(new Date())));
|
|
|
|
|
}catch (ParseException e)
|
|
|
|
|
{
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chargeRlt.qRCodeUrl = preRlt.getCodeUrl();
|
|
|
|
|
chargeRlt.imgBase64 = GetQRCodeFile(preRlt.getCodeUrl()).getData();
|
|
|
|
|
payOrder.setCallbackUrl(preRlt.getCodeUrl());
|
|
|
|
|
if(!StringUtils.isEmpty(alipayFormAct)){
|
|
|
|
|
chargeRlt.alipayFormAction = alipayFormAct;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
payOrder.setUpdatedtime(sdf.parse(sdf.format(new Date())));
|
|
|
|
|
}catch (ParseException e)
|
|
|
|
|
{
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
payOrderInfoMapper.updateByPrimaryKey(payOrder);
|
|
|
|
|
|
|
|
|
|
PayOrderQrcode qrcode = new PayOrderQrcode();
|
|
|
|
|
qrcode.setPkId(payOrder.getPkId());
|
|
|
|
|
qrcode.setQrcodeImg(chargeRlt.imgBase64);
|
|
|
|
|
|
|
|
|
|
if(!StringUtils.isEmpty(getCodeUrl)) {
|
|
|
|
|
qrcode.setQrcodeImg(chargeRlt.imgBase64);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!StringUtils.isEmpty(alipayFormAct)) {
|
|
|
|
|
qrcode.setFormActionUrl(alipayFormAct);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
payOrderQrcodeMapper.insert(qrcode);
|
|
|
|
|
|
|
|
|
@ -395,12 +459,30 @@ public class CompanyChargeServiceImpl implements ICompanyChargeService {
|
|
|
|
|
//解密报文
|
|
|
|
|
String plainText = decryptFromResource(bodyMap);
|
|
|
|
|
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
log.info("解密报文 bodyMap={}",plainText);
|
|
|
|
|
//将明文转换成map
|
|
|
|
|
//Gson gson = new Gson();
|
|
|
|
|
HashMap plainTextMap = gson.fromJson(plainText, HashMap.class);
|
|
|
|
|
String orderNo = (String) plainTextMap.get("out_trade_no");
|
|
|
|
|
|
|
|
|
|
//更新本地记录
|
|
|
|
|
PayOrderInfo chargeOrder = payOrderInfoMapper.selectByOrderNo(orderNo);
|
|
|
|
|
|
|
|
|
|
if(chargeOrder != null)
|
|
|
|
|
{
|
|
|
|
|
//PAY_SUCC-支付成功
|
|
|
|
|
chargeOrder.setStatus("PAY_SUCC");
|
|
|
|
|
chargeOrder.setIsComplete(true);
|
|
|
|
|
chargeOrder.setCompleteType("WEIXIN_NOTICE");
|
|
|
|
|
chargeOrder.setUpdatedtime(new Date());
|
|
|
|
|
|
|
|
|
|
payOrderInfoMapper.updateByPrimaryKey(chargeOrder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//触发大简云回写
|
|
|
|
|
SendNoticeToDaJanYun(chargeOrder.getSenderPkid(),chargeOrder.getAmount());
|
|
|
|
|
|
|
|
|
|
/*在对业务数据进行状态检查和处理之前,
|
|
|
|
|
要采用数据锁进行并发控制,
|
|
|
|
@ -434,6 +516,48 @@ public class CompanyChargeServiceImpl implements ICompanyChargeService {
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SendNoticeToDaJanYun(String senderPkId,BigDecimal amount){
|
|
|
|
|
OkHttpClient client = new OkHttpClient().newBuilder()
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
|
|
|
|
|
log.info("充值成功回写大简云 act={} uid={} amount={}","charge",senderPkId,amount.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
|
|
|
|
|
MultipartBody formBody = new MultipartBody.Builder()
|
|
|
|
|
.setType(MultipartBody.FORM)
|
|
|
|
|
.addFormDataPart("act", "charge")
|
|
|
|
|
.addFormDataPart("uid", senderPkId)
|
|
|
|
|
.addFormDataPart("amount", amount.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
//MediaType mediaType = MediaType.parse("application/json");
|
|
|
|
|
//RequestBody body = RequestBody.create(mediaType, json);
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(wexinConfig.DjyNoticeUrl())
|
|
|
|
|
.method("POST", formBody)
|
|
|
|
|
.addHeader("Content-Type", "application/json")
|
|
|
|
|
.build();
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if(response.isSuccessful())
|
|
|
|
|
{
|
|
|
|
|
String msg = response.body().string();
|
|
|
|
|
|
|
|
|
|
log.info("发送大简云完成,返回结果={}",msg);
|
|
|
|
|
|
|
|
|
|
R<String> djyRlt = gson.fromJson(msg,R.class);
|
|
|
|
|
|
|
|
|
|
if(djyRlt.getCode() == Constants.SUCCESS){
|
|
|
|
|
log.info("充值成功发送大简云回写成功,消息={}",djyRlt.getMsg());
|
|
|
|
|
}else {
|
|
|
|
|
log.info("充值成功发送大简云回写失败,消息={}",djyRlt.getMsg());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String decryptFromResource(Map<String, Object> bodyMap) throws GeneralSecurityException {
|
|
|
|
|
|
|
|
|
|
log.info("密文解密");
|
|
|
|
@ -456,6 +580,13 @@ public class CompanyChargeServiceImpl implements ICompanyChargeService {
|
|
|
|
|
return plainText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TestSendDJY()
|
|
|
|
|
{
|
|
|
|
|
//更新本地记录
|
|
|
|
|
PayOrderInfo chargeOrder = payOrderInfoMapper.selectByOrderNo("PWX202312080002");
|
|
|
|
|
|
|
|
|
|
SendNoticeToDaJanYun(chargeOrder.getSenderPkid(),chargeOrder.getAmount());
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
public OutResultInfo PushPaymentNotice(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
|
|
|
|
|
|
|