提交 0a19d3dc 编写于 作者: zhouweidong's avatar zhouweidong

支持微信支付

上级 e352a8db
......@@ -32,7 +32,9 @@ public class PayTradeAspect {
Object serviceParam =args[0];
if(serviceParam instanceof PayTrade){
PayTrade entity=(PayTrade)serviceParam;
entity.setTradeId((String) entity.getDefaultKey(true));
entity.setTradeName(entity.getSubject());
entity.setTradeStatus("NOTPAY");
payTradeService.save(entity);
}
}
......
......@@ -2,6 +2,7 @@ package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.extensions.service.wechat.WXPay;
import cn.ibizlab.core.extensions.service.wechat.WechatPayConfig;
import cn.ibizlab.core.extensions.service.wechat.WechatPayDomain;
import cn.ibizlab.core.pay.domain.PayOpenAccess;
import cn.ibizlab.core.pay.domain.PayTrade;
import cn.ibizlab.core.pay.service.IPayOpenAccessService;
......@@ -37,7 +38,10 @@ public class PayCoreService {
IPayTradeService tradeService;
@Value("${ibz.pay.ali.pagepay.returnurl:http://127.0.0.1:8080/pay/trade/pagepay/callback}")
private String ali_return_url;
private String ali_notify_url;
@Value("${ibz.pay.wechat.returnurl:http://127.0.0.1:8080/pay/trade/callback}")
private String wechat_notify_url;
@Value("${ibz.pay.ali.gateway:https://openapi.alipaydev.com/gateway.do}")
private String ali_gateway;
......@@ -53,11 +57,12 @@ public class PayCoreService {
*/
@SneakyThrows
public String pagePay(PayTrade trade){
PayOpenAccess openAccess=getOpenAccess(trade);
PayOpenAccess openAccess=getOpenAccess(trade.getAccessId());
trade.setAccessId(openAccess.getId());
AlipayClient alipayClient = getAliPayClient(openAccess);
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
request.setReturnUrl(openAccess.getRedirectUri());
request.setNotifyUrl(ali_return_url);
request.setNotifyUrl(ali_notify_url);
request.setBizContent(
"{\"out_trade_no\":\""+ trade.getOutTradeNo() +"\","
+ "\"total_amount\":\""+ trade.getTotalAmount() +"\","
......@@ -79,7 +84,6 @@ public class PayCoreService {
* @return
*/
public String pagePayCallBack(HttpServletRequest req){
JSONObject paramMap=new JSONObject();
Enumeration names = req.getParameterNames();
while(names.hasMoreElements()) {
......@@ -87,26 +91,18 @@ public class PayCoreService {
String value = req.getParameter(key);
paramMap.put(key,value);
}
// String result_code=paramMap.getString("code");
// String result_sub_code=paramMap.getString("sub_code");
String out_trade_no=paramMap.getString("out_trade_no");
// String app_id=paramMap.getString("app_id");
String total_amount=paramMap.getString("total_amount");
String trade_status=paramMap.getString("trade_status");
if(StringUtils.isEmpty(out_trade_no))
return String.format("没有找到[%s]订单信息",out_trade_no);
// String tradeId= DigestUtils.md5DigestAsHex(String.format(out_trade_no+"||"+app_id).getBytes());
PayTrade trade = tradeService.getOne(Wrappers.<PayTrade>lambdaQuery().eq(PayTrade::getOutTradeNo,out_trade_no).eq(PayTrade::getTradeStatus,"pending"));
PayTrade trade = tradeService.getOne(Wrappers.<PayTrade>lambdaQuery().eq(PayTrade::getOutTradeNo,out_trade_no).eq(PayTrade::getTradeStatus,"NOTPAY"));
if(trade==null)
return String.format("没有找到[%s]待付款订单",out_trade_no);
//确认订单金额
if(!total_amount.equals(trade.getTotalAmount()))
return String.format("交易订单金额[%s]与支付平台金额[%s]不吻合",total_amount,trade.getTotalAmount());
PayOpenAccess openAccess=trade.getOpenaccess();
if(openAccess==null){
openAccess= openAccessService.get(trade.getAccessId());
......@@ -114,7 +110,6 @@ public class PayCoreService {
String notify_url=openAccess.getNotifyUrl();
if(StringUtils.isEmpty(notify_url))
return String.format("无法获取[%s]开放平台回调地址",trade.getAccessId());
//修改订单状态
if(trade_status.equals("TRADE_SUCCESS")){
trade.setTradeStatus("success");
......@@ -129,200 +124,88 @@ public class PayCoreService {
}
/**
* 预创建订单,生成支付二维码
* @param trade
* @return
*/
public JSONObject preCreate(PayTrade trade){
JSONObject rs=new JSONObject();
PayOpenAccess openAccess=getOpenAccess(trade);
switch (openAccess.getOpenType()){
case "aliyun":
// qrcode=aliPayPreCreate(openAccess,trade);
break;
case "wechat":
rs= wechatPreCreate(openAccess,trade);
break;
}
return rs;
}
/**
* 查询订单
* @param trade
* 下订单,获取支付链接
* @param trade 订单信息
* @return
*/
public JSONObject query(PayTrade trade){
JSONObject rs=new JSONObject();
PayOpenAccess openAccess=getOpenAccess(trade);
switch (openAccess.getOpenType()){
case "aliyun":
// aliPayQuery(openAccess,trade);
break;
case "wechat":
rs=wechatPayQuery(openAccess,trade);
break;
}
return rs;
}
/**
* 取消订单
* @param trade
* @return
*/
public JSONObject cancel(PayTrade trade){
JSONObject rs=new JSONObject();
PayOpenAccess openAccess=getOpenAccess(trade);
switch (openAccess.getOpenType()){
case "aliyun":
// aliPayCancel(openAccess,trade);
break;
case "wechat":
rs=wechatPayCancel(openAccess,trade);
break;
}
return rs;
}
// /**
// * 交易预创建,生成正扫二维码
// * @param trade
// */
// @SneakyThrows
// private String aliPayPreCreate(PayOpenAccess openAccess,PayTrade trade){
// AlipayClient alipayClient = getAliPayClient(openAccess);
// AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
// request.setBizContent("{" +
// "\"out_trade_no\":\""+trade.getOutTradeNo()+"\"," +
// "\"subject\":\""+trade.getSubject()+"\"," +
// "\"total_amount\":"+trade.getTotalAmount()+"," +
// " }");
// AlipayTradePrecreateResponse response = alipayClient.execute(request);
// if(response.isSuccess()){
// System.out.println("调用成功");
// } else {
// System.out.println("调用失败");
// }
// return response.getQrCode();
// }
//
// /**
// * 交易查询
// * @param trade
// */
// @SneakyThrows
// private String aliPayQuery(PayOpenAccess openAccess,PayTrade trade){
// AlipayClient alipayClient = getAliPayClient(openAccess);
// AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
// request.setBizContent("{" +
// "\"out_trade_no\":\""+trade.getOutTradeNo()+"\"," +
// " }");
// AlipayTradeQueryResponse response = alipayClient.execute(request);
// if(response.isSuccess()){
// System.out.println("调用成功");
// } else {
// System.out.println("调用失败");
// }
// return response.getBody();
// }
//
// /**
// * 撤销交易
// * @param trade
// */
// @SneakyThrows
// private String aliPayCancel(PayOpenAccess openAccess,PayTrade trade) {
// AlipayClient alipayClient = getAliPayClient(openAccess);
// AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
// request.setBizContent("{" +
// "\"out_trade_no\":\""+trade.getOutTradeNo()+"\"," +
// " }");
// AlipayTradeCancelResponse response = alipayClient.execute(request);
// if(response.isSuccess()){
// System.out.println("调用成功");
// } else {
// System.out.println("调用失败");
// }
// return response.getOutTradeNo();
// }
/**
* 微信预创建订单
* @param trade
*/
@SneakyThrows
private JSONObject wechatPreCreate(PayOpenAccess openAccess,PayTrade trade) {
JSONObject rs=new JSONObject();
public JSONObject preCreate(PayTrade trade){
PayOpenAccess openAccess=getOpenAccess(trade.getAccessId());
trade.setAccessId(openAccess.getId());
WechatPayConfig config = getWechatPayConfig(openAccess);
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
data.put("body", trade.getSubject());
data.put("out_trade_no", trade.getOutTradeNo());
// data.put("device_info", "");
data.put("fee_type", "CNY");
data.put("total_fee",trade.getTotalAmount());
// data.put("spbill_create_ip", "123.12.12.123");
data.put("notify_url", openAccess.getRedirectUri());
data.put("notify_url", wechat_notify_url);
data.put("trade_type", "NATIVE"); // 此处指定为扫码支付
// data.put("product_id", "12");
Map resp = wxpay.unifiedOrder(data);
if(resp!=null){
rs=new JSONObject(resp);
}
return rs;
return getResult(wxpay.unifiedOrder(data));
}
/**
* 微信:订单查询
* @param trade
* 查询订单
* @param accessId 开放平台配置标识
* @param outTradeNo 订单号
* @return
*/
@SneakyThrows
private JSONObject wechatPayQuery(PayOpenAccess openAccess,PayTrade trade) {
JSONObject rs=new JSONObject();
public JSONObject query(String accessId,String outTradeNo){
PayOpenAccess openAccess=getOpenAccess(accessId);
WechatPayConfig config = getWechatPayConfig(openAccess);
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
data.put("out_trade_no", trade.getOutTradeNo());
Map resp = wxpay.orderQuery(data);
if(resp!=null){
rs=new JSONObject(resp);
data.put("out_trade_no", outTradeNo);
Map rsMap=wxpay.orderQuery(data);
JSONObject rs=getResult(rsMap);
String trade_status=rs.getString("trade_state");
if(!StringUtils.isEmpty(trade_status)){
//更新订单状态
PayTrade trade=new PayTrade();
trade.setAccessId(openAccess.getId());
trade.setOutTradeNo(outTradeNo);
trade.setTradeStatus(trade_status);
trade.setTradeId((String) trade.getDefaultKey(true));
tradeService.update(trade);
if("SUCCESS".equals(trade_status)){
//回调业务系统
HttpUtils.post(openAccess.getNotifyUrl(), null, new JSONObject(rsMap));
}
}
return rs;
}
/**
* 微信:取消订单
* @param trade
* 取消订单
* @param accessId 开放平台配置标识
* @param outTradeNo 订单号
* @return
*/
@SneakyThrows
private JSONObject wechatPayCancel(PayOpenAccess openAccess,PayTrade trade) {
JSONObject rs=new JSONObject();
public JSONObject cancel(String accessId,String outTradeNo){
PayOpenAccess openAccess=getOpenAccess(accessId);
WechatPayConfig config = getWechatPayConfig(openAccess);
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
data.put("out_trade_no", trade.getOutTradeNo());
Map resp = wxpay.closeOrder(data);
if(resp!=null){
rs=new JSONObject(resp);
}
return rs;
data.put("out_trade_no", outTradeNo);
return getResult(wxpay.closeOrder(data));
}
public String callBack(HttpServletRequest req){
return "success";
}
/**
* 获取开放平台信息
* @param trade
* @param id
* @return
*/
private PayOpenAccess getOpenAccess(PayTrade trade){
private PayOpenAccess getOpenAccess(String id){
PayOpenAccess openAccess=null;
String accessId=trade.getAccessId();
String appId=trade.getAppId();
if(!StringUtils.isEmpty(accessId))
openAccess=openAccessService.getOne(Wrappers.<PayOpenAccess>lambdaQuery().eq(PayOpenAccess::getAccessKey,appId).or().eq(PayOpenAccess::getId,accessId));
if(!StringUtils.isEmpty(id))
openAccess=openAccessService.getOne(Wrappers.<PayOpenAccess>lambdaQuery().eq(PayOpenAccess::getAccessKey,id).or().eq(PayOpenAccess::getId,id));
if(ObjectUtils.isEmpty(openAccess) || (openAccess.getDisabled()!=null && openAccess.getDisabled()==1))
throw new BadRequestAlertException("获取开放平台配置失败","PayCoreService","pagePay");
trade.setAccessId(openAccess.getId());
return openAccess;
}
......@@ -346,12 +229,34 @@ public class PayCoreService {
* @param openAccess
* @return
*/
@SneakyThrows
private WechatPayConfig getWechatPayConfig(PayOpenAccess openAccess) {
WechatPayConfig config=new WechatPayConfig();
config.setAppID(openAccess.getAccessKey());
config.setKey(openAccess.getAccessKey());
config.setMchID(openAccess.getRegionId());
config.setWXPayDomain(null);
config.setKey(openAccess.getSecretKey());
config.setMchID(openAccess.getAccessToken());
config.setWXPayDomain(new WechatPayDomain());
return config;
}
/**
* 移除敏感信息
* @param resp
* @return
*/
private JSONObject getResult(Map resp){
JSONObject rs=new JSONObject();
if(resp!=null){
rs=new JSONObject(resp);
rs.remove("device_info");
rs.remove("nonce_str");
rs.remove("appid");
rs.remove("sign");
rs.remove("mch_id");
rs.remove("prepay_id");
rs.remove("return_code");
rs.remove("return_msg");
}
return rs;
}
}
package cn.ibizlab.core.extensions.service.wechat;
import lombok.Data;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
@Data
......@@ -22,44 +17,4 @@ public class WechatPayConfig extends WXPayConfig {
private InputStream certStream;
private IWXPayDomain WXPayDomain;
// public WechatPayConfig() throws Exception {
// String certPath = "/path/to/apiclient_cert.p12";
// File file = new File(certPath);
// InputStream certStream = new FileInputStream(file);
// this.certData = new byte[(int) file.length()];
// certStream.read(this.certData);
// certStream.close();
// }
//
// public String getAppID() {
// return "wx8888888888888888";
// }
//
// public String getMchID() {
// return "12888888";
// }
//
// public String getKey() {
// return "88888888888888888888888888888888";
// }
// public InputStream getCertStream() {
// ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
// return certBis;
// }
// public int getHttpConnectTimeoutMs() {
// return 8000;
// }
//
// public int getHttpReadTimeoutMs() {
// return 10000;
// }
// @Override
// IWXPayDomain getWXPayDomain() {
// return null;
// }
}
\ No newline at end of file
package cn.ibizlab.core.extensions.service.wechat;
public class WechatPayDomain implements IWXPayDomain {
@Override
public void report(String domain, long elapsedTimeMillis, Exception ex) {
}
@Override
public DomainInfo getDomain(WXPayConfig config) {
return new DomainInfo("api.mch.weixin.qq.com",true);
}
}
......@@ -10,11 +10,10 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
@RestController
public class PayCoreResource {
......@@ -34,15 +33,23 @@ public class PayCoreResource {
}
@ApiOperation(value = "查询订单", tags = {"查询订单" }, notes = "查询订单")
@RequestMapping(method = RequestMethod.POST,value = "/pay/trade/query")
public ResponseEntity<JSONObject> query(@Validated @RequestBody PayTradeDTO dto){
return ResponseEntity.status(HttpStatus.OK).body(payCoreService.query(payTradeMapping.toDomain(dto)));
@RequestMapping(method = RequestMethod.GET,value = "/pay/trade/query")
public ResponseEntity<JSONObject> query(@Validated @NotBlank(message = "开放平台配置标识不允许为空")@RequestParam("accessid") String accessId,
@NotBlank(message = "订单号不允许为空") @RequestParam("out_trade_no") String outTradeNo){
return ResponseEntity.status(HttpStatus.OK).body(payCoreService.query(accessId,outTradeNo));
}
@ApiOperation(value = "取消订单", tags = {"取消订单" }, notes = "取消订单")
@RequestMapping(method = RequestMethod.POST,value = "/pay/trade/cancel")
public ResponseEntity<JSONObject> cancel(@Validated @RequestBody PayTradeDTO dto){
return ResponseEntity.status(HttpStatus.OK).body(payCoreService.cancel(payTradeMapping.toDomain(dto)));
@RequestMapping(method = RequestMethod.GET,value = "/pay/trade/cancel")
public ResponseEntity<JSONObject> cancel(@Validated @NotBlank(message = "开放平台配置标识不允许为空")@RequestParam("accessid") String accessId,
@NotBlank(message = "订单号不允许为空") @RequestParam("out_trade_no") String outTradeNo){
return ResponseEntity.status(HttpStatus.OK).body(payCoreService.cancel(accessId,outTradeNo));
}
@ApiOperation(value = "支付回调", tags = {"支付回调" }, notes = "支付回调")
@RequestMapping(value = "/pay/trade/callback")
public ResponseEntity<String> callBack(HttpServletRequest req) {
return ResponseEntity.status(HttpStatus.OK).body(payCoreService.callBack(req));
}
@ApiOperation(value = "网页支付", tags = {"网页支付" }, notes = "网页支付")
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册