Commit caa104cd authored by jiaorz's avatar jiaorz

支付宝支付

parent dcda5cee
......@@ -277,24 +277,26 @@ public class OrderPayBiz extends BaseBiz<OrderPayMapper, OrderPay> {
*/
@SuppressWarnings("rawtypes")
private String generateAliPayment(OrderPayVo orderPayVo, String notifyUrl) {
//实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient(SystemConfig.ALIPAY_PAY_BASE_URL + "/gateway.do",
SystemConfig.ALIPAY_APPID, SystemConfig.ALIPAY_PRIVATE_KEY, AlipayConstants.FORMAT_JSON,
"utf-8", SystemConfig.ALIPAY_PUBLIC_KEY, AlipayConstants.SIGN_TYPE_RSA2);
AlipayClient alipayClient = getAlipayClient();
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
try {
return appOrderPay(alipayClient, orderPayVo, notifyUrl);
} catch (Exception e) {
e.printStackTrace();
}
//
return null;
}
private AlipayClient getAlipayClient() {
AlipayClient alipayClient = new DefaultAlipayClient(SystemConfig.ALIPAY_PAY_BASE_URL + "/gateway.do",
SystemConfig.ALIPAY_APPID, SystemConfig.ALIPAY_PRIVATE_KEY, AlipayConstants.FORMAT_JSON,
"utf-8", SystemConfig.ALIPAY_PUBLIC_KEY, AlipayConstants.SIGN_TYPE_RSA2);
return alipayClient;
}
//支付宝APP支付方法
public String appOrderPay(AlipayClient alipayClient, OrderPayVo orderPayVo, String notifyUrl) {
private String appOrderPay(AlipayClient alipayClient, OrderPayVo orderPayVo, String notifyUrl) {
BigDecimal realAmount = new BigDecimal(orderPayVo.getAmount().toString()).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_UP);
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
......@@ -418,6 +420,38 @@ public class OrderPayBiz extends BaseBiz<OrderPayMapper, OrderPay> {
log.info("response: {}" + response.getBody());
}
/**
* APP支付退款
* @param
* @param outTradeNo 订单号
* @param tradNo 流水号
* @param refundAmount 退款金额
* @param refundReason 退款原因
* @return
*/
public boolean alipayOrderRefund(String outTradeNo, String tradNo, Integer refundAmount, String refundReason) {
AlipayClient alipayClient = getAlipayClient();
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
request.setBizContent("{" +
"\"out_trade_no\":\"" + outTradeNo + "\"," +
"\"trade_no\":\"" + tradNo + "\"," +
"\"refund_amount\":" + refundAmount + "," +
"\"out_request_no\":\"" + refundReason + "\"" +
" }");
try{
AlipayTradeRefundResponse response = alipayClient.execute(request);
if(response.isSuccess()){
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
log.info("退款失败请重试");
}
return false;
}
public static void main(String[] args) {
AlipayClient alipayClient = new DefaultAlipayClient(SystemConfig.ALIPAY_PAY_BASE_URL + "/gateway.do",
......
......@@ -19,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
import java.util.Map;
......@@ -34,65 +35,71 @@ import static com.xxfc.platform.universal.constant.DictionaryKey.UNIVERSAL_PAY;
*/
@Service
@Slf4j
public class OrderRefundBiz extends BaseBiz<OrderRefundMapper,OrderRefund> {
public class OrderRefundBiz extends BaseBiz<OrderRefundMapper, OrderRefund> {
@Autowired
private OrderPayBiz payBiz;
private OrderPayBiz payBiz;
@Value("${universal.cert}")
private String APICLIENT_CERT ;
private String APICLIENT_CERT;
@Autowired
DictionaryBiz dictionaryBiz;
//申请退款
public JSONObject refund(OrderRefundVo orderRefundVo)throws Exception{
if(orderRefundVo==null){
public JSONObject refund(OrderRefundVo orderRefundVo) throws Exception {
if (orderRefundVo == null) {
log.error("-----参数为空-----------");
return JsonResultUtil.createFailedResult(ResultCode.NULL_CODE, "参数为空");
}
String order_no=orderRefundVo.getOrderNo();
String appid= SystemConfig.APP_ID;
String mchId=SystemConfig.APP_PARTNER;
String partnerKey=SystemConfig.APP_PARTNER_KEY;
Integer payAmount=orderRefundVo.getAmount();
Integer refundAmount=orderRefundVo.getRefundAmount();
String order_no = orderRefundVo.getOrderNo();
String appid = SystemConfig.APP_ID;
String mchId = SystemConfig.APP_PARTNER;
String partnerKey = SystemConfig.APP_PARTNER_KEY;
Integer payAmount = orderRefundVo.getAmount();
Integer refundAmount = orderRefundVo.getRefundAmount();
//临时处理
Map<String, Dictionary> dictionaryMap = dictionaryBiz.getAll4Map();
Integer demotion = Integer.valueOf(dictionaryMap.get(UNIVERSAL_PAY+ "_"+ PAY_DEMOTION).getDetail());
payAmount = payAmount/demotion;
if(payAmount <= 0) {
Integer demotion = Integer.valueOf(dictionaryMap.get(UNIVERSAL_PAY + "_" + PAY_DEMOTION).getDetail());
payAmount = payAmount / demotion;
if (payAmount <= 0) {
payAmount = 1;
}
refundAmount = refundAmount/demotion;
if(refundAmount <= 0) {
refundAmount = refundAmount / demotion;
if (refundAmount <= 0) {
refundAmount = 1;
}
String refundDesc =StringUtils.isNotBlank(orderRefundVo.getRefundDesc())?orderRefundVo.getRefundDesc(): "审核通过,退款";
String refundDesc = StringUtils.isNotBlank(orderRefundVo.getRefundDesc()) ? orderRefundVo.getRefundDesc() : "审核通过,退款";
String out_refund_no = Snowflake.build() + "";
if(StringUtils.isBlank(order_no)||StringUtils.isBlank(appid)||StringUtils.isBlank(mchId)||StringUtils.isBlank(partnerKey)
||payAmount==null||payAmount==0||refundAmount==null||refundAmount==0){
if (StringUtils.isBlank(order_no) || StringUtils.isBlank(appid) || StringUtils.isBlank(mchId) || StringUtils.isBlank(partnerKey)
|| payAmount == null || payAmount == 0 || refundAmount == null || refundAmount == 0) {
log.error("-----参数为空-----------");
return JsonResultUtil.createFailedResult(ResultCode.NULL_CODE, "参数为空");
}
log.error("-----payAmoun======="+payAmount+"------refundAmount======"+refundAmount);
Example example =new Example(OrderPay.class);
example.createCriteria().andEqualTo("orderNo", order_no).andEqualTo("isDel",0).andEqualTo("status",1);
List<OrderPay> list=payBiz.selectByExample(example);
if(list.size()==0){
log.error("---支付回调---trade_no====="+order_no+"----订单不存在或未支付");
return JsonResultUtil.createFailedResult(ResultCode.FAILED_CODE, order_no+"订单不存在或未支付");
log.error("-----payAmoun=======" + payAmount + "------refundAmount======" + refundAmount);
Example example = new Example(OrderPay.class);
example.createCriteria().andEqualTo("orderNo", order_no).andEqualTo("isDel", 0).andEqualTo("status", 1);
List<OrderPay> list = payBiz.selectByExample(example);
if (list.size() == 0) {
log.error("---支付回调---trade_no=====" + order_no + "----订单不存在或未支付");
return JsonResultUtil.createFailedResult(ResultCode.FAILED_CODE, order_no + "订单不存在或未支付");
}
OrderPay orderPay = list.get(0);
String out_trade_no = orderPay.getTradeNo();
boolean flag = false;
if (orderPay.getPayWay() == 2) {
flag = payBiz.alipayOrderRefund(out_trade_no, orderPay.getSerialNumber(), refundAmount, refundDesc);
} else if(orderPay.getPayWay() == 1){
flag = WxPayRefundUtils.refund(appid, mchId, partnerKey, out_trade_no, out_refund_no, payAmount + "",
refundAmount + "", refundDesc, APICLIENT_CERT);
}
OrderPay orderPay=list.get(0);
String out_trade_no=orderPay.getTradeNo();
boolean flag=WxPayRefundUtils.refund(appid,mchId,partnerKey,out_trade_no,out_refund_no,payAmount+"",
refundAmount+"",refundDesc,APICLIENT_CERT);
if(flag){
OrderRefund orderRefund= new OrderRefund();
BeanUtils.copyProperties(orderRefund,orderRefundVo);
if(StringUtils.isNotBlank(orderRefund.getRefundDesc())){
if (flag) {
OrderRefund orderRefund = new OrderRefund();
BeanUtils.copyProperties(orderRefund, orderRefundVo);
if (StringUtils.isNotBlank(orderRefund.getRefundDesc())) {
orderRefund.setRefundDesc(refundDesc);
}
orderRefund.setUserId(orderPay.getUserId());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment