|  |  |  | 
|---|
|  |  |  | package com.doumee.core.wx; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
|---|
|  |  |  | import com.doumee.core.constants.Constants; | 
|---|
|  |  |  | import com.doumee.core.constants.ResponseStatus; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.doumee.core.utils.ID; | 
|---|
|  |  |  | import com.doumee.dao.business.RefundMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.TransactionsMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Locks; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Refund; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Transactions; | 
|---|
|  |  |  | import com.doumee.dao.business.web.request.RefundDTO; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemDictData; | 
|---|
|  |  |  | import com.doumee.service.business.RefundService; | 
|---|
|  |  |  | import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest; | 
|---|
|  |  |  | import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest; | 
|---|
|  |  |  | import com.github.binarywang.wxpay.bean.result.WxPayRefundResult; | 
|---|
|  |  |  | import com.github.binarywang.wxpay.exception.WxPayException; | 
|---|
|  |  |  | import io.netty.handler.codec.base64.Base64Decoder; | 
|---|
|  |  |  | import io.netty.handler.codec.base64.Base64Encoder; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import okhttp3.OkHttpClient; | 
|---|
|  |  |  | import okhttp3.Request; | 
|---|
|  |  |  | import okhttp3.Response; | 
|---|
|  |  |  | import org.apache.commons.io.IOUtils; | 
|---|
|  |  |  | import org.checkerframework.checker.units.qual.A; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import sun.misc.BASE64Decoder; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.io.ByteArrayInputStream; | 
|---|
|  |  |  | import java.io.ByteArrayOutputStream; | 
|---|
|  |  |  | import java.io.FileOutputStream; | 
|---|
|  |  |  | import java.io.InputStream; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 微信小程序-公共方法 | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private TransactionsMapper transactionsMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SystemDictDataBiz systemDictDataBiz; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Transactional(rollbackFor = {BusinessException.class,Exception.class}) | 
|---|
|  |  |  | public Refund wxRefund(RefundDTO refundDTO) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | // 发送退款请求 | 
|---|
|  |  |  | 
|---|
|  |  |  | WxPayRefundRequest request = new WxPayRefundRequest(); | 
|---|
|  |  |  | request.setOutTradeNo(refundDTO.getOrderId()); | 
|---|
|  |  |  | request.setOutRefundNo(refNum); | 
|---|
|  |  |  | request.setTotalFee(BaseWxPayRequest.yuanToFen(refundDTO.getTotalAmount().toString())); | 
|---|
|  |  |  | request.setRefundFee(BaseWxPayRequest.yuanToFen(refundDTO.getRefundAmount().toString())); | 
|---|
|  |  |  | System.out.println("实际总金额" + BaseWxPayRequest.yuanToFen(refundDTO.getTotalAmount().toString())); | 
|---|
|  |  |  | System.out.println("实际退款金额" + BaseWxPayRequest.yuanToFen(refundDTO.getRefundAmount().toString())); | 
|---|
|  |  |  | request.setTotalFee(refundDTO.getTotalAmount().intValue()); | 
|---|
|  |  |  | request.setRefundFee(refundDTO.getRefundAmount().intValue()); | 
|---|
|  |  |  | System.out.println("实际总金额" + refundDTO.getTotalAmount()); | 
|---|
|  |  |  | System.out.println("实际退款金额" + refundDTO.getRefundAmount()); | 
|---|
|  |  |  | //            request.setTotalFee(1); | 
|---|
|  |  |  | //            request.setRefundFee(1); | 
|---|
|  |  |  | WxPayRefundResult response = WxMiniConfig.wxPayService.refund(request); | 
|---|
|  |  |  | 
|---|
|  |  |  | refund.setMoney(refundDTO.getRefundAmount()); | 
|---|
|  |  |  | refund.setOnlineOrderid(refNum); | 
|---|
|  |  |  | refund.setPayWay(Constants.ZERO); | 
|---|
|  |  |  | refund.setStatus(Constants.TWO); | 
|---|
|  |  |  | refund.setDoneDate(new Date()); | 
|---|
|  |  |  | refund.setCreator(refundDTO.getCreator()); | 
|---|
|  |  |  | refund.setType(refundDTO.getType()); | 
|---|
|  |  |  | refund.setObjId(refundDTO.getOrderId()); | 
|---|
|  |  |  | refund.setReason(refundDTO.getReason()); | 
|---|
|  |  |  | 
|---|
|  |  |  | transactions.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | transactions.setOrderId(refundDTO.getOrderId()); | 
|---|
|  |  |  | transactions.setMoney(refundDTO.getRefundAmount()); | 
|---|
|  |  |  | transactions.setType(refundDTO.getType()==Constants.TRANSACTIONS_TYPE.REFUND.getKey()?Constants.TRANSACTIONS_TYPE.PLATFORMREFUND.getKey():Constants.REFUND_TYPE.BACK.getKey()); | 
|---|
|  |  |  | transactions.setPreOrderid(refundDTO.getOrderId()); | 
|---|
|  |  |  | transactions.setOnlineOrderid(refNum); | 
|---|
|  |  |  | transactions.setDoneDate(new Date()); | 
|---|
|  |  |  | transactions.setTitle(Constants.TRANSACTIONS_TYPE.get(transactions.getType()).getName()); | 
|---|
|  |  |  | transactions.setContent(Constants.TRANSACTIONS_TYPE.get(transactions.getType()).getInfo()); | 
|---|
|  |  |  | if(refund.getType().equals(Constants.REFUND_TYPE.PLAT_AUTO.getKey())||refund.getType().equals(Constants.REFUND_TYPE.PLAT_FORCE.getKey())){ | 
|---|
|  |  |  | //平台自动退款 或 强制退款 | 
|---|
|  |  |  | transactions.setType(Constants.TRANSACTIONS_TYPE.REFUND.getKey()); | 
|---|
|  |  |  | transactions.setTitle(Constants.REFUND_TYPE.PLAT_AUTO.getInfo()); | 
|---|
|  |  |  | transactions.setContent(Constants.REFUND_TYPE.PLAT_AUTO.getInfo()); | 
|---|
|  |  |  | }else if(refund.getType().equals(Constants.REFUND_TYPE.NORMAL.getKey())){ | 
|---|
|  |  |  | //用户主动退款 | 
|---|
|  |  |  | transactions.setType(Constants.TRANSACTIONS_TYPE.REFUND.getKey()); | 
|---|
|  |  |  | transactions.setTitle(Constants.REFUND_TYPE.NORMAL.getInfo()); | 
|---|
|  |  |  | transactions.setContent(Constants.REFUND_TYPE.NORMAL.getInfo()); | 
|---|
|  |  |  | }else if(refund.getType().equals(Constants.REFUND_TYPE.BACK.getKey())){ | 
|---|
|  |  |  | //结算后退款 | 
|---|
|  |  |  | transactions.setType(Constants.TRANSACTIONS_TYPE.REFUND.getKey()); | 
|---|
|  |  |  | transactions.setTitle(Constants.REFUND_TYPE.BACK.getInfo()); | 
|---|
|  |  |  | transactions.setContent(Constants.REFUND_TYPE.BACK.getInfo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | transactions.setBalance(BigDecimal.ZERO); | 
|---|
|  |  |  | transactions.setObjId(refund.getId()); | 
|---|
|  |  |  | transactions.setObjType(Constants.ONE); | 
|---|
|  |  |  | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"退款发生异常请联系管理员"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 生成小程序码 | 
|---|
|  |  |  | * https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void generateWXMiniCode(Locks locks,SystemDictData systemDictData){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(Objects.isNull(systemDictData)){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //生成图片上传OSS | 
|---|
|  |  |  | Map<String,Object> body = new HashMap<>(); | 
|---|
|  |  |  | // 场景码,与前端约定,最终是需要前端解析 | 
|---|
|  |  |  | body.put("scene", locks.getSiteId() + "/" +locks.getCode() ); | 
|---|
|  |  |  | // 正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。 | 
|---|
|  |  |  | body.put("env_version", "release"); | 
|---|
|  |  |  | // 透明,根据你的场景自行设置body参数 | 
|---|
|  |  |  | body.put("is_hyaline", false); | 
|---|
|  |  |  | body.put("check_path", false); | 
|---|
|  |  |  | body.put("page","pages/index/index"); | 
|---|
|  |  |  | OkHttpClient client = new OkHttpClient().newBuilder().build(); | 
|---|
|  |  |  | okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json"); | 
|---|
|  |  |  | okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(mediaType, JSONObject.toJSONString(body)); | 
|---|
|  |  |  | Request request = new Request.Builder().url("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+systemDictData.getCode()).method("POST", requestBody).build(); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Response response = client.newCall(request).execute(); | 
|---|
|  |  |  | if (response.isSuccessful()) { | 
|---|
|  |  |  | InputStream inputStream = new ByteArrayInputStream(response.body().bytes()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 得到文件大小 | 
|---|
|  |  |  | int i = inputStream.available(); | 
|---|
|  |  |  | byte[] mFileBody = new byte[i]; | 
|---|
|  |  |  | // 读数据 | 
|---|
|  |  |  | inputStream.read(mFileBody); | 
|---|
|  |  |  | locks.setInfo("data:image/Jpeg;base64," + Base64.getEncoder().encodeToString(mFileBody)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|