| @@ -0,0 +1,98 @@ | |||
| package com.iformall.controller; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.common.ResultData; | |||
| import com.iformall.config.PayProperty; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.domain.vo.WxBillAll; | |||
| import com.iformall.enums.EnumMerchantStatus; | |||
| import com.iformall.enums.EnumPayWay; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.service.*; | |||
| import com.iformall.utils.IPUtil; | |||
| import io.swagger.annotations.Api; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.web.bind.annotation.*; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import java.math.BigDecimal; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| /** | |||
| * @author Stormeye Wu wuguoqiang@iformall.com | |||
| */ | |||
| @RestController | |||
| @Api(description = "刷卡支付") | |||
| @RequestMapping("/api/micropay") | |||
| public class WxMicroPaylController extends BaseController { | |||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||
| @Autowired | |||
| private PayProperty payProperty; | |||
| @Autowired | |||
| WxMerchantBUserService wxMerchantBUserService; | |||
| @Autowired | |||
| WxMerchantService wxMerchantService; | |||
| @Autowired | |||
| WxOrderService wxOrderService; | |||
| @Autowired | |||
| WxPayOrderService wxPayOrderService; | |||
| @ApiOperation(value = "刷卡支付订单", notes = "{\"authCode\":\"String\",\"totalFee\":\"String\"}") | |||
| @PostMapping("order_create") | |||
| public ResultData saveMicopayOrder(@RequestBody Map<String, String> paramMap, HttpServletRequest request) { | |||
| logger.info("saveMicopayOrder: " + paramMap.toString()); | |||
| //Assert.notNull(wxOrders.getName(), "角色名不能为空"); | |||
| //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名"); | |||
| String authCode = paramMap.get("authCode"); | |||
| String totalFeeStr = paramMap.get("totalFee"); | |||
| if (StringUtils.isBlank(authCode)) { | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "authCode不能为空"); | |||
| } | |||
| if (StringUtils.isBlank(totalFeeStr)) { | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "totalFee不能为空"); | |||
| } | |||
| WxMerchantBUser user = getUser(); | |||
| WxOrder order = null; | |||
| try { | |||
| order = wxOrderService.saveMicroOrder(user, totalFeeStr); | |||
| } catch (MallinkException e) { | |||
| logger.error(e.getMessage()); | |||
| return new ResultData(e.getErrorCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| return new ResultData(ErrorCode.ORDER_IS_FAIL, e.getMessage()); | |||
| } | |||
| WxAppinfo appInfo = getAppInfo(user.getAppId()); | |||
| WxPayOrder record = new WxPayOrder(); | |||
| record.setOrderId(order.getId()); | |||
| record.setAuthCode(authCode); | |||
| record.setPayAmount(order.getPayment()); | |||
| try { | |||
| record.setIp(IPUtil.getIpAddr(request)); | |||
| return wxPayOrderService.createMicroPayOrder(payProperty.isReal(), appInfo, user, record, EnumPayWay.PAY_WAY_WECHAT); | |||
| } catch (MallinkException e) { | |||
| logger.error("payment wechat, order create error, req 2: " + record.toString() + ", e:" + e.getMessage()); | |||
| return new ResultData(e.getErrorCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| logger.error("payment wechat, order create error, req 3: " + record.toString() + ", e:" + e.getMessage()); | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR, e.getMessage()); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,38 @@ | |||
| package com.iformall.enums; | |||
| /** | |||
| * @author gongbiao | |||
| */ | |||
| public enum EnumOrderType { | |||
| COUPON(0,"券"), | |||
| MICROPAY(1,"刷卡支付金") | |||
| ; | |||
| public static EnumOrderType getEnum(Integer code) { | |||
| for (EnumOrderType value : values()) { | |||
| if (value.getCode().equals(code)) { | |||
| return value; | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| private Integer code; | |||
| private String message; | |||
| EnumOrderType(Integer code, String message) { | |||
| this.code = code; | |||
| this.message = message; | |||
| } | |||
| public Integer getCode() { | |||
| return code; | |||
| } | |||
| public String getMessage() { | |||
| return message; | |||
| } | |||
| } | |||
| @@ -0,0 +1,135 @@ | |||
| package com.iformall.pay; | |||
| /** | |||
| * Created by Stormeye on 2018/11/7. | |||
| * 普通商户模式 | |||
| */ | |||
| public class WxMicroPayOrderP { | |||
| private String appid; // 小程序ID | |||
| private String mch_id; // 商户号 | |||
| private String device_info; // 终端设备号 门店编号 | |||
| private String nonce_str; // 随机字符串 | |||
| private String sign; // 签名 | |||
| private String body; // 商品简单描述 128 | |||
| private String out_trade_no; // 商户订单号 | |||
| private Integer total_fee; // 支付金额 | |||
| private String spbill_create_ip; // 支付IP | |||
| private String time_start; // 开始时间 | |||
| private String time_expire; // 失效时间 | |||
| private String auth_code; // 扫码支付授权码 | |||
| public String getTime_start() { | |||
| return time_start; | |||
| } | |||
| public void setTime_start(String time_start) { | |||
| this.time_start = time_start; | |||
| } | |||
| public String getTime_expire() { | |||
| return time_expire; | |||
| } | |||
| public void setTime_expire(String time_expire) { | |||
| this.time_expire = time_expire; | |||
| } | |||
| public String getAppid() { | |||
| return appid; | |||
| } | |||
| public void setAppid(String appid) { | |||
| this.appid = appid; | |||
| } | |||
| public String getMch_id() { | |||
| return mch_id; | |||
| } | |||
| public void setMch_id(String mch_id) { | |||
| this.mch_id = mch_id; | |||
| } | |||
| public String getDevice_info() { | |||
| return device_info; | |||
| } | |||
| public void setDevice_info(String device_info) { | |||
| this.device_info = device_info; | |||
| } | |||
| public String getNonce_str() { | |||
| return nonce_str; | |||
| } | |||
| public void setNonce_str(String nonce_str) { | |||
| this.nonce_str = nonce_str; | |||
| } | |||
| public String getSign() { | |||
| return sign; | |||
| } | |||
| public void setSign(String sign) { | |||
| this.sign = sign; | |||
| } | |||
| public String getBody() { | |||
| return body; | |||
| } | |||
| public void setBody(String body) { | |||
| this.body = body; | |||
| } | |||
| public String getOut_trade_no() { | |||
| return out_trade_no; | |||
| } | |||
| public void setOut_trade_no(String out_trade_no) { | |||
| this.out_trade_no = out_trade_no; | |||
| } | |||
| public Integer getTotal_fee() { | |||
| return total_fee; | |||
| } | |||
| public void setTotal_fee(Integer total_fee) { | |||
| this.total_fee = total_fee; | |||
| } | |||
| public String getSpbill_create_ip() { | |||
| return spbill_create_ip; | |||
| } | |||
| public void setSpbill_create_ip(String spbill_create_ip) { | |||
| this.spbill_create_ip = spbill_create_ip; | |||
| } | |||
| public String getAuth_code() { | |||
| return auth_code; | |||
| } | |||
| public void setAuth_code(String auth_code) { | |||
| this.auth_code = auth_code; | |||
| } | |||
| @Override | |||
| public String toString() { | |||
| final StringBuilder sb = new StringBuilder("WxPayOrderP{"); | |||
| sb.append("appid='").append(appid).append('\''); | |||
| sb.append(", mch_id='").append(mch_id).append('\''); | |||
| sb.append(", device_info='").append(device_info).append('\''); | |||
| sb.append(", nonce_str='").append(nonce_str).append('\''); | |||
| sb.append(", sign='").append(sign).append('\''); | |||
| sb.append(", body='").append(body).append('\''); | |||
| sb.append(", out_trade_no='").append(out_trade_no).append('\''); | |||
| sb.append(", total_fee=").append(total_fee); | |||
| sb.append(", spbill_create_ip='").append(spbill_create_ip).append('\''); | |||
| sb.append(", time_start='").append(time_start).append('\''); | |||
| sb.append(", time_expire='").append(time_expire).append('\''); | |||
| sb.append(", auth_code='").append(auth_code).append('\''); | |||
| sb.append('}'); | |||
| return sb.toString(); | |||
| } | |||
| } | |||
| @@ -0,0 +1,174 @@ | |||
| package com.iformall.pay; | |||
| /** | |||
| * Created by Stormeye on 2018/11/7. | |||
| * 服务商模式 | |||
| */ | |||
| public class WxMicroPayOrderSP { | |||
| private String appid; // 公众账号ID | |||
| private String sub_appid; // 小程序ID | |||
| private String mch_id; // 服务商号 | |||
| private String sub_mch_id; // 特约商户号 | |||
| private String device_info; // 终端设备号 - 门店编号 | |||
| private String nonce_str; // 随机字符串 | |||
| private String sign; // 签名 | |||
| private String sign_type; // 签名类型 | |||
| private String body; // 商品简单描述 128 | |||
| private String out_trade_no; // 商户订单号 | |||
| private Integer total_fee; // 支付金额 | |||
| private String spbill_create_ip; // 支付IP | |||
| private String time_start; // 开始时间 | |||
| private String time_expire; // 失效时间 | |||
| private String auth_code; // 扫码支付授权码 | |||
| private String profit_sharing; // 是否开启分账 | |||
| public String getTime_start() { | |||
| return time_start; | |||
| } | |||
| public void setTime_start(String time_start) { | |||
| this.time_start = time_start; | |||
| } | |||
| public String getTime_expire() { | |||
| return time_expire; | |||
| } | |||
| public void setTime_expire(String time_expire) { | |||
| this.time_expire = time_expire; | |||
| } | |||
| public String getAppid() { | |||
| return appid; | |||
| } | |||
| public void setAppid(String appid) { | |||
| this.appid = appid; | |||
| } | |||
| public String getSub_appid() { | |||
| return sub_appid; | |||
| } | |||
| public void setSub_appid(String sub_appid) { | |||
| this.sub_appid = sub_appid; | |||
| } | |||
| public String getMch_id() { | |||
| return mch_id; | |||
| } | |||
| public void setMch_id(String mch_id) { | |||
| this.mch_id = mch_id; | |||
| } | |||
| public String getSub_mch_id() { | |||
| return sub_mch_id; | |||
| } | |||
| public void setSub_mch_id(String sub_mch_id) { | |||
| this.sub_mch_id = sub_mch_id; | |||
| } | |||
| public String getDevice_info() { | |||
| return device_info; | |||
| } | |||
| public void setDevice_info(String device_info) { | |||
| this.device_info = device_info; | |||
| } | |||
| public String getNonce_str() { | |||
| return nonce_str; | |||
| } | |||
| public void setNonce_str(String nonce_str) { | |||
| this.nonce_str = nonce_str; | |||
| } | |||
| public String getSign() { | |||
| return sign; | |||
| } | |||
| public void setSign(String sign) { | |||
| this.sign = sign; | |||
| } | |||
| public String getSign_type() { | |||
| return sign_type; | |||
| } | |||
| public void setSign_type(String sign_type) { | |||
| this.sign_type = sign_type; | |||
| } | |||
| public String getBody() { | |||
| return body; | |||
| } | |||
| public void setBody(String body) { | |||
| this.body = body; | |||
| } | |||
| public String getOut_trade_no() { | |||
| return out_trade_no; | |||
| } | |||
| public void setOut_trade_no(String out_trade_no) { | |||
| this.out_trade_no = out_trade_no; | |||
| } | |||
| public Integer getTotal_fee() { | |||
| return total_fee; | |||
| } | |||
| public void setTotal_fee(Integer total_fee) { | |||
| this.total_fee = total_fee; | |||
| } | |||
| public String getSpbill_create_ip() { | |||
| return spbill_create_ip; | |||
| } | |||
| public void setSpbill_create_ip(String spbill_create_ip) { | |||
| this.spbill_create_ip = spbill_create_ip; | |||
| } | |||
| public String getAuth_code() { | |||
| return auth_code; | |||
| } | |||
| public void setAuth_code(String auth_code) { | |||
| this.auth_code = auth_code; | |||
| } | |||
| public String getProfit_sharing() { | |||
| return profit_sharing; | |||
| } | |||
| public void setProfit_sharing(String profit_sharing) { | |||
| this.profit_sharing = profit_sharing; | |||
| } | |||
| @Override | |||
| public String toString() { | |||
| final StringBuilder sb = new StringBuilder("WxPayOrderSP{"); | |||
| sb.append("appid='").append(appid).append('\''); | |||
| sb.append(", sub_appid='").append(sub_appid).append('\''); | |||
| sb.append(", mch_id='").append(mch_id).append('\''); | |||
| sb.append(", sub_mch_id='").append(sub_mch_id).append('\''); | |||
| sb.append(", device_info='").append(device_info).append('\''); | |||
| sb.append(", nonce_str='").append(nonce_str).append('\''); | |||
| sb.append(", sign='").append(sign).append('\''); | |||
| sb.append(", body='").append(body).append('\''); | |||
| sb.append(", out_trade_no='").append(out_trade_no).append('\''); | |||
| sb.append(", total_fee=").append(total_fee); | |||
| sb.append(", spbill_create_ip='").append(spbill_create_ip).append('\''); | |||
| sb.append(", time_start='").append(time_start).append('\''); | |||
| sb.append(", time_expire='").append(time_expire).append('\''); | |||
| sb.append(", auth_code='").append(auth_code).append('\''); | |||
| sb.append(", profit_sharing='").append(profit_sharing).append('\''); | |||
| sb.append('}'); | |||
| return sb.toString(); | |||
| } | |||
| } | |||
| @@ -6,6 +6,7 @@ import com.github.pagehelper.PageInfo; | |||
| import com.iformall.common.ResultData; | |||
| import com.iformall.domain.po.WxCUser; | |||
| import com.iformall.domain.po.WxCouponOrder; | |||
| import com.iformall.domain.po.WxMerchantBUser; | |||
| import com.iformall.domain.po.WxOrder; | |||
| import com.iformall.domain.vo.WxOrderCVo; | |||
| import com.iformall.enums.EnumOrderStatus; | |||
| @@ -31,6 +32,14 @@ public interface WxOrderService { | |||
| */ | |||
| WxOrder saveOrder(WxCUser user, Long couponChannelId, Long couponId); | |||
| /** | |||
| * 刷卡支付 | |||
| * @param user | |||
| * @param totalFeeStr | |||
| * @return 订单id | |||
| */ | |||
| WxOrder saveMicroOrder(WxMerchantBUser user, String totalFeeStr); | |||
| /** | |||
| * 免费券订单接口 | |||
| * @param userId | |||
| @@ -6,6 +6,7 @@ import com.github.pagehelper.PageInfo; | |||
| import com.iformall.common.ResultData; | |||
| import com.iformall.domain.po.WxAppinfo; | |||
| import com.iformall.domain.po.WxCUser; | |||
| import com.iformall.domain.po.WxMerchantBUser; | |||
| import com.iformall.domain.po.WxPayOrder; | |||
| import com.iformall.enums.EnumPayWay; | |||
| @@ -20,6 +21,15 @@ public interface WxPayOrderService { | |||
| */ | |||
| ResultData createPayOrder(boolean isReal, WxAppinfo appInfo, WxCUser user, WxPayOrder record, EnumPayWay payWay); | |||
| /** | |||
| * 刷卡支付订单 | |||
| * | |||
| * @param record 支付订单请求 | |||
| * @param payWay | |||
| * @return | |||
| */ | |||
| ResultData createMicroPayOrder(boolean isReal, WxAppinfo appInfo, WxMerchantBUser user, WxPayOrder record, EnumPayWay payWay); | |||
| /** | |||
| * 微信支付订单查询 | |||
| * @param appInfo 支付订单Appinfo | |||
| @@ -20,6 +20,7 @@ import org.springframework.stereotype.Service; | |||
| import org.springframework.transaction.annotation.Propagation; | |||
| import org.springframework.transaction.annotation.Transactional; | |||
| import java.math.BigDecimal; | |||
| import java.util.*; | |||
| @Service | |||
| @@ -86,6 +87,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||
| WxOrder orderQ = new WxOrder(); | |||
| orderQ.setTenantId(user.getTenantId()); | |||
| orderQ.setCouponId(counpon.getId()); | |||
| orderQ.setType(EnumOrderType.COUPON.getCode()); | |||
| orderQ.setCUserId(user.getId()); | |||
| orderQ.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()); | |||
| try { | |||
| @@ -267,6 +269,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||
| record.setTenantId(user.getTenantId()); | |||
| record.setOrderNumber(orderNumber); | |||
| record.setCouponId(couponId); | |||
| record.setType(EnumOrderType.COUPON.getCode()); | |||
| record.setCUserId(user.getId()); | |||
| record.setMerchantId(coupon.getMerchantId()); | |||
| record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode()); | |||
| @@ -298,6 +301,64 @@ public class WxOrderServiceImpl implements WxOrderService { | |||
| return record; | |||
| } | |||
| @Override | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public WxOrder saveMicroOrder(WxMerchantBUser user, String totalFeeStr) { | |||
| // 检查用户 | |||
| if (user == null) { | |||
| logger.error("用户不存在"); | |||
| throw new MallinkException(ErrorCode.USER_IS_EMPTY); | |||
| } | |||
| WxMerchant wxMerchant = wxMerchantMapper.selectByPrimaryKey(user.getMerchantId()); | |||
| if (wxMerchant == null) { | |||
| logger.error("商户不存在, merchantId: " + user.getMerchantId()); | |||
| throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_FOUND); | |||
| } | |||
| if (wxMerchant.getStatus() == EnumMerchantStatus.NOT_VALID.getCode()) { | |||
| logger.error("商户已禁用, merchantId: " + user.getMerchantId()); | |||
| throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_VALID); | |||
| } | |||
| Date curr = new Date(); | |||
| BigDecimal totalFee = new BigDecimal(totalFeeStr); | |||
| totalFee.setScale(2, BigDecimal.ROUND_HALF_UP); // 第一个变量是小数位数,第二个变量是取舍方法(四舍五入) | |||
| int payment = totalFee.multiply(new BigDecimal(100)).intValue(); | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| Long orderNumber = idWorker.nextId(); | |||
| // body | |||
| // tenant_id + merchant_id + title + subtitle | |||
| String bodyStr = "刷卡支付, 金额:" + totalFeeStr; | |||
| WxOrder record = new WxOrder(); | |||
| record.setId(orderNumber); | |||
| record.setTenantId(user.getTenantId()); | |||
| record.setOrderNumber(orderNumber); | |||
| record.setType(EnumOrderType.MICROPAY.getCode()); | |||
| record.setBUserId(user.getId()); | |||
| record.setMerchantId(user.getMerchantId()); | |||
| record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode()); | |||
| record.setPayment(payment); | |||
| record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()); | |||
| record.setDetail(bodyStr); | |||
| record.setCreateDate(curr); | |||
| record.setUpdateDate(curr); | |||
| try { | |||
| // 保存订单 | |||
| wxOrderMapper.insertSelective(record); | |||
| } catch (RuntimeException e) { | |||
| logger.error("保存订单:" + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); | |||
| } | |||
| return record; | |||
| } | |||
| /** | |||
| * 创建 couponOrder | |||
| * | |||
| @@ -351,6 +351,248 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| } | |||
| @Override | |||
| public ResultData createMicroPayOrder(boolean isReal, WxAppinfo appInfo, WxMerchantBUser user, WxPayOrder record, EnumPayWay payWay) { | |||
| final IdWorker idworker = IdWorker.get(); | |||
| EnumPayShare isShare = EnumPayShare.NO; | |||
| try { | |||
| // 1. check 订单 | |||
| WxOrder order = wxOrderMapper.selectByPrimaryKey(record.getOrderId()); | |||
| if (order == null) { | |||
| logger.error("pay order, order not allow, repaymentReq: " + record.toString() + ", payWay: " + payWay.toString()); | |||
| throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND); | |||
| } | |||
| if (order.getOrderStatus() != EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()) { | |||
| logger.error("pay order, order status not allow, repaymentReq: " + order.toString() + " , payWay: " + payWay.toString()); | |||
| throw new MallinkException(ErrorCode.ORDER_IS_NOT_PAY); | |||
| } | |||
| if (order.getPaymentType() != EnumPayType.PAY_PAYMENT.getCode()) { | |||
| return new ResultData(ErrorCode.PAY_ORDER_IS_NOT_PAYMENT); | |||
| } | |||
| WxPayAccount payAccount = wxPayAccountMapper.selectByPrimaryKey(appInfo.getPayId()); | |||
| if (payAccount.isShare()) { | |||
| isShare = EnumPayShare.YES; | |||
| } | |||
| // 1.5 check 分账 receive | |||
| if (isShare == EnumPayShare.YES) { | |||
| WxProfitSharingReceiver receiver = new WxProfitSharingReceiver(); | |||
| receiver.setMerchantId(order.getMerchantId()); | |||
| List<WxProfitSharingReceiver> merList = wxProfitSharingReceiverMapper.findList(receiver); | |||
| if (merList.size() > 0) { | |||
| isShare = EnumPayShare.YES; | |||
| } else { | |||
| isShare = EnumPayShare.NO; | |||
| } | |||
| } | |||
| // 2. check 是否有支付订单 | |||
| Date currentDate = new Date(); | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_WAY_WAIT.getCode()); | |||
| List<WxPayOrder> list = wxPayOrderMapper.findList(record); | |||
| String payOrderNo = ""; | |||
| if (list.size() <= 0) { | |||
| // 3. 创建支付订单 | |||
| Long id = idworker.nextId(); | |||
| payOrderNo = String.valueOf(id); | |||
| record.setId(id); | |||
| record.setTenantId(user.getTenantId()); | |||
| record.setbUserId(user.getId()); | |||
| record.setCreateTime(currentDate); | |||
| record.setUpdateTime(currentDate); | |||
| record.setPayTimeStart(currentDate); | |||
| record.setPayTimeEnd(currentDate); | |||
| // 支付单号 | |||
| record.setPayOrderNo(payOrderNo); | |||
| record.setPayAmount(order.getPayment()); | |||
| record.setPayVendor(EnumPayWay.PAY_WAY_WEAPP.getCode()); | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_WAY_WAIT.getCode()); | |||
| record.setShare(isShare.getCode()); | |||
| if (isShare == EnumPayShare.YES) { | |||
| // 分账金额 | |||
| Double dChargeFee = Math.ceil(record.getPayAmount() * 1.0D * payAccount.getRate() / 1000); | |||
| Integer share_amount = record.getPayAmount() - dChargeFee.intValue(); | |||
| record.setShareAmount(share_amount); | |||
| } | |||
| int sqlRow = wxPayOrderMapper.insertSelective(record); | |||
| if (sqlRow != 1) { | |||
| logger.error("pay order insert error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| } else { | |||
| record = list.get(0); | |||
| record.setShare(isShare.getCode()); | |||
| payOrderNo = String.valueOf(record.getId()); | |||
| } | |||
| if (isReal) { | |||
| // 微信实际支付 | |||
| if (payAccount.getType() == EnumPayMode.MCH.getCode()) { | |||
| // 刷卡支付 普通商户模式 | |||
| String noncestr = Utility.generate32UUID(); | |||
| WxMicroPayOrderP wxPayOrderP = new WxMicroPayOrderP(); | |||
| wxPayOrderP.setAppid(user.getAppId()); | |||
| wxPayOrderP.setMch_id(payAccount.getMchId()); | |||
| wxPayOrderP.setDevice_info(user.getPhone()); | |||
| wxPayOrderP.setNonce_str(noncestr); | |||
| wxPayOrderP.setBody(order.getDetail()); | |||
| wxPayOrderP.setOut_trade_no(record.getPayOrderNo()); | |||
| wxPayOrderP.setTotal_fee(order.getPayment()); | |||
| wxPayOrderP.setSpbill_create_ip(record.getIp()); // 终端IP | |||
| wxPayOrderP.setAuth_code(record.getAuthCode()); | |||
| wxPayOrderP.setTime_start(Utility.getDataFormatStringYYYYMMDDHHmmss(currentDate)); | |||
| Date futureDate = new Date(); | |||
| futureDate.setTime(currentDate.getTime() + 15 * 60 * 1000); | |||
| wxPayOrderP.setTime_expire(Utility.getDataFormatStringYYYYMMDDHHmmss(futureDate)); // 15分钟后结束 | |||
| Map<String, String> payOrderMap = BeanUtils.toStringMap(wxPayOrderP); | |||
| wxPayOrderP.setSign(WxPayment.createSign(payOrderMap, payAccount.getApiKey())); | |||
| String response = WxPay.micropay(BeanUtils.toStringMap(wxPayOrderP)); | |||
| logger.info("pay order, wechat micropay, " + wxPayOrderP.toString() + ", response: " + response.toString()); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| returnMap.put("payOrderId", payOrderNo); | |||
| String return_code = returnMap.get("return_code"); | |||
| String result_code = returnMap.get("result_code"); | |||
| if("SUCCESS".equalsIgnoreCase(return_code)) { | |||
| if ("SUCCESS".equals(result_code)) { | |||
| String openId = returnMap.get("openid"); | |||
| String isSubscribe = returnMap.get("is_subscribe"); | |||
| String transactionId = returnMap.get("transaction_id"); | |||
| String err_code = returnMap.get("err_code"); | |||
| String errMsg = returnMap.get("err_code_des"); | |||
| if(errMsg.length() <= 0) { | |||
| errMsg = returnMap.get("return_msg"); | |||
| } | |||
| record.setTransactionId(transactionId); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateByPrimaryKeySelective(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(Result.SUCCESS, errMsg, returnMap); | |||
| } else { | |||
| String err_code = returnMap.get("err_code"); | |||
| String errMsg = returnMap.get("err_code_des"); | |||
| if(errMsg.length() <= 0) { | |||
| errMsg = returnMap.get("return_msg"); | |||
| } | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateByPrimaryKeySelective(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errMsg, returnMap); | |||
| } | |||
| } | |||
| return new ResultData(Result.ERROR, "刷卡支付异常", returnMap); | |||
| } else { | |||
| // 统一下单 // 服务商模式 | |||
| String noncestr = Utility.generate32UUID(); | |||
| WxMicroPayOrderSP wxPayOrderSP = new WxMicroPayOrderSP(); | |||
| wxPayOrderSP.setAppid(appInfo.getParentAppId()); | |||
| wxPayOrderSP.setMch_id(payAccount.getMchId()); | |||
| wxPayOrderSP.setSub_appid(user.getAppId()); | |||
| wxPayOrderSP.setSub_mch_id(payAccount.getSubMchId()); | |||
| wxPayOrderSP.setDevice_info(user.getPhone()); | |||
| wxPayOrderSP.setNonce_str(noncestr); | |||
| wxPayOrderSP.setBody(order.getDetail()); | |||
| wxPayOrderSP.setOut_trade_no(record.getPayOrderNo()); | |||
| wxPayOrderSP.setTotal_fee(order.getPayment()); | |||
| wxPayOrderSP.setSpbill_create_ip(record.getIp()); | |||
| wxPayOrderSP.setAuth_code(record.getAuthCode()); | |||
| wxPayOrderSP.setTime_start(Utility.getDataFormatStringYYYYMMDDHHmmss(currentDate)); | |||
| Date futureDate = new Date(); | |||
| futureDate.setTime(currentDate.getTime() + 15 * 60 * 1000); | |||
| wxPayOrderSP.setTime_expire(Utility.getDataFormatStringYYYYMMDDHHmmss(futureDate)); // 15分钟后结束 | |||
| wxPayOrderSP.setSign_type("HMAC-SHA256"); | |||
| wxPayOrderSP.setProfit_sharing(null); | |||
| if (isShare == EnumPayShare.YES) { | |||
| wxPayOrderSP.setProfit_sharing("Y"); | |||
| } | |||
| Map<String, String> payOrderMap = BeanUtils.toStringMap(wxPayOrderSP); | |||
| wxPayOrderSP.setSign(WxPayment.createSignHMAC(payOrderMap, payAccount.getApiKey())); | |||
| String response = WxPay.micropay(BeanUtils.toStringMap(wxPayOrderSP)); | |||
| logger.info("pay order, wechat micropay, " + wxPayOrderSP.toString() + ", response: " + response.toString()); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| returnMap.put("payOrderId", payOrderNo); | |||
| String return_code = returnMap.get("return_code"); | |||
| String result_code = returnMap.get("result_code"); | |||
| if("SUCCESS".equalsIgnoreCase(return_code)) { | |||
| if ("SUCCESS".equals(result_code)) { | |||
| String openId = returnMap.get("openid"); | |||
| String isSubscribe = returnMap.get("is_subscribe"); | |||
| String transactionId = returnMap.get("transaction_id"); | |||
| String err_code = returnMap.get("err_code"); | |||
| String errMsg = returnMap.get("err_code_des"); | |||
| if(errMsg.length() <= 0) { | |||
| errMsg = returnMap.get("return_msg"); | |||
| } | |||
| record.setTransactionId(transactionId); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateByPrimaryKeySelective(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| // TODO update order | |||
| return new ResultData(Result.SUCCESS, errMsg, returnMap); | |||
| } else { | |||
| String err_code = returnMap.get("err_code"); | |||
| String errMsg = returnMap.get("err_code_des"); | |||
| if(errMsg.length() <= 0) { | |||
| errMsg = returnMap.get("return_msg"); | |||
| } | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateByPrimaryKeySelective(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errMsg, returnMap); | |||
| } | |||
| } | |||
| return new ResultData(Result.ERROR, "刷卡支付异常", returnMap); | |||
| } | |||
| } else { | |||
| // 虚拟支付 | |||
| WxMicroPayOrderP wxPayOrderP = new WxMicroPayOrderP(); | |||
| wxPayOrderP.setAppid(user.getAppId()); | |||
| wxPayOrderP.setMch_id(payAccount.getMchId()); | |||
| wxPayOrderP.setBody(order.getDetail()); | |||
| wxPayOrderP.setOut_trade_no(record.getPayOrderNo()); | |||
| wxPayOrderP.setTotal_fee(order.getPayment()); | |||
| wxPayOrderP.setSpbill_create_ip(record.getIp()); // 终端IP | |||
| wxPayOrderP.setDevice_info(user.getPhone()); | |||
| wxPayOrderP.setAuth_code(record.getAuthCode()); | |||
| wxPayOrderP.setTime_start(Utility.getDataFormatStringYYYYMMDDHHmmss(currentDate)); | |||
| Date futureDate = new Date(); | |||
| futureDate.setTime(currentDate.getTime() + 15 * 60 * 1000); | |||
| wxPayOrderP.setTime_expire(Utility.getDataFormatStringYYYYMMDDHHmmss(futureDate)); // 15分钟后结束 | |||
| wxPayOrderP.setSign(WxPayment.createSign(BeanUtils.toStringMap(wxPayOrderP), payAccount.getApiKey())); | |||
| Map<String, String> returnMap = BeanUtils.toStringMap(wxPayOrderP); | |||
| return new ResultData(Result.SUCCESS, "创建刷卡支付订单成功", returnMap); | |||
| } | |||
| } catch (RuntimeException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } | |||
| private String wechatPayOrderQuery(WxAppinfo appInfo, WxPayOrder record) { | |||
| // get payAccount | |||