Showing
14 changed files
with
737 additions
and
211 deletions
1 | package com.yohoufo.dal.order; | 1 | package com.yohoufo.dal.order; |
2 | 2 | ||
3 | +import com.yohoufo.dal.order.model.BuyerOrderMeta; | ||
3 | import com.yohoufo.dal.order.model.SellerOrderMeta; | 4 | import com.yohoufo.dal.order.model.SellerOrderMeta; |
5 | +import org.apache.ibatis.annotations.Param; | ||
4 | 6 | ||
5 | public interface SellerOrderMetaMapper { | 7 | public interface SellerOrderMetaMapper { |
6 | int deleteByPrimaryKey(Integer id); | 8 | int deleteByPrimaryKey(Integer id); |
@@ -14,4 +16,6 @@ public interface SellerOrderMetaMapper { | @@ -14,4 +16,6 @@ public interface SellerOrderMetaMapper { | ||
14 | int updateByPrimaryKeySelective(SellerOrderMeta record); | 16 | int updateByPrimaryKeySelective(SellerOrderMeta record); |
15 | 17 | ||
16 | int updateByPrimaryKey(SellerOrderMeta record); | 18 | int updateByPrimaryKey(SellerOrderMeta record); |
19 | + | ||
20 | + BuyerOrderMeta selectByMetaKey(@Param("uid") int uid, @Param("orderCode") long orderCode, @Param("metaKey") String metaKey); | ||
17 | } | 21 | } |
@@ -13,7 +13,7 @@ public class BuyerOrder { | @@ -13,7 +13,7 @@ public class BuyerOrder { | ||
13 | 13 | ||
14 | private Integer clientType; | 14 | private Integer clientType; |
15 | 15 | ||
16 | - private Byte payment; | 16 | + private Integer payment; |
17 | 17 | ||
18 | private Byte paymentType; | 18 | private Byte paymentType; |
19 | 19 | ||
@@ -23,7 +23,7 @@ public class BuyerOrder { | @@ -23,7 +23,7 @@ public class BuyerOrder { | ||
23 | 23 | ||
24 | private BigDecimal shipFee; | 24 | private BigDecimal shipFee; |
25 | 25 | ||
26 | - private Byte status; | 26 | + private Integer status; |
27 | 27 | ||
28 | private Integer createTime; | 28 | private Integer createTime; |
29 | 29 | ||
@@ -75,11 +75,11 @@ public class BuyerOrder { | @@ -75,11 +75,11 @@ public class BuyerOrder { | ||
75 | this.clientType = clientType; | 75 | this.clientType = clientType; |
76 | } | 76 | } |
77 | 77 | ||
78 | - public Byte getPayment() { | 78 | + public Integer getPayment() { |
79 | return payment; | 79 | return payment; |
80 | } | 80 | } |
81 | 81 | ||
82 | - public void setPayment(Byte payment) { | 82 | + public void setPayment(Integer payment) { |
83 | this.payment = payment; | 83 | this.payment = payment; |
84 | } | 84 | } |
85 | 85 | ||
@@ -115,11 +115,11 @@ public class BuyerOrder { | @@ -115,11 +115,11 @@ public class BuyerOrder { | ||
115 | this.shipFee = shipFee; | 115 | this.shipFee = shipFee; |
116 | } | 116 | } |
117 | 117 | ||
118 | - public Byte getStatus() { | 118 | + public Integer getStatus() { |
119 | return status; | 119 | return status; |
120 | } | 120 | } |
121 | 121 | ||
122 | - public void setStatus(Byte status) { | 122 | + public void setStatus(Integer status) { |
123 | this.status = status; | 123 | this.status = status; |
124 | } | 124 | } |
125 | 125 |
@@ -7,12 +7,12 @@ | @@ -7,12 +7,12 @@ | ||
7 | <result column="order_code" jdbcType="BIGINT" property="orderCode" /> | 7 | <result column="order_code" jdbcType="BIGINT" property="orderCode" /> |
8 | <result column="seller_uid" jdbcType="INTEGER" property="sellerUid" /> | 8 | <result column="seller_uid" jdbcType="INTEGER" property="sellerUid" /> |
9 | <result column="client_type" jdbcType="INTEGER" property="clientType" /> | 9 | <result column="client_type" jdbcType="INTEGER" property="clientType" /> |
10 | - <result column="payment" jdbcType="TINYINT" property="payment" /> | 10 | + <result column="payment" jdbcType="INTEGER" property="payment" /> |
11 | <result column="payment_type" jdbcType="TINYINT" property="paymentType" /> | 11 | <result column="payment_type" jdbcType="TINYINT" property="paymentType" /> |
12 | <result column="is_cancel" jdbcType="TINYINT" property="isCancel" /> | 12 | <result column="is_cancel" jdbcType="TINYINT" property="isCancel" /> |
13 | <result column="amount" jdbcType="DECIMAL" property="amount" /> | 13 | <result column="amount" jdbcType="DECIMAL" property="amount" /> |
14 | <result column="ship_fee" jdbcType="DECIMAL" property="shipFee" /> | 14 | <result column="ship_fee" jdbcType="DECIMAL" property="shipFee" /> |
15 | - <result column="status" jdbcType="TINYINT" property="status" /> | 15 | + <result column="status" jdbcType="INTEGER" property="status" /> |
16 | <result column="create_time" jdbcType="INTEGER" property="createTime" /> | 16 | <result column="create_time" jdbcType="INTEGER" property="createTime" /> |
17 | <result column="update_time" jdbcType="INTEGER" property="updateTime" /> | 17 | <result column="update_time" jdbcType="INTEGER" property="updateTime" /> |
18 | <result column="buyer_order_status" jdbcType="INTEGER" property="buyerOrderStatus" /> | 18 | <result column="buyer_order_status" jdbcType="INTEGER" property="buyerOrderStatus" /> |
@@ -92,9 +92,9 @@ | @@ -92,9 +92,9 @@ | ||
92 | status, create_time, update_time, | 92 | status, create_time, update_time, |
93 | buyer_order_status, seller_order_status, channel_no) | 93 | buyer_order_status, seller_order_status, channel_no) |
94 | values (#{uid,jdbcType=INTEGER}, #{orderCode,jdbcType=BIGINT}, #{sellerUid,jdbcType=INTEGER}, | 94 | values (#{uid,jdbcType=INTEGER}, #{orderCode,jdbcType=BIGINT}, #{sellerUid,jdbcType=INTEGER}, |
95 | - #{clientType,jdbcType=INTEGER}, #{payment,jdbcType=TINYINT}, #{paymentType,jdbcType=TINYINT}, | 95 | + #{clientType,jdbcType=INTEGER}, #{payment,jdbcType=INTEGER}, #{paymentType,jdbcType=TINYINT}, |
96 | #{isCancel,jdbcType=TINYINT}, #{amount,jdbcType=DECIMAL}, #{shipFee,jdbcType=DECIMAL}, | 96 | #{isCancel,jdbcType=TINYINT}, #{amount,jdbcType=DECIMAL}, #{shipFee,jdbcType=DECIMAL}, |
97 | - #{status,jdbcType=TINYINT}, #{createTime,jdbcType=INTEGER}, #{updateTime,jdbcType=INTEGER}, | 97 | + #{status,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}, #{updateTime,jdbcType=INTEGER}, |
98 | #{buyerOrderStatus,jdbcType=INTEGER}, #{sellerOrderStatus,jdbcType=INTEGER}, #{channelNo,jdbcType=VARCHAR}) | 98 | #{buyerOrderStatus,jdbcType=INTEGER}, #{sellerOrderStatus,jdbcType=INTEGER}, #{channelNo,jdbcType=VARCHAR}) |
99 | </insert> | 99 | </insert> |
100 | <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.yohoufo.dal.order.model.BuyerOrder" useGeneratedKeys="true"> | 100 | <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.yohoufo.dal.order.model.BuyerOrder" useGeneratedKeys="true"> |
@@ -160,7 +160,7 @@ | @@ -160,7 +160,7 @@ | ||
160 | #{clientType,jdbcType=TINYINT}, | 160 | #{clientType,jdbcType=TINYINT}, |
161 | </if> | 161 | </if> |
162 | <if test="payment != null"> | 162 | <if test="payment != null"> |
163 | - #{payment,jdbcType=TINYINT}, | 163 | + #{payment,jdbcType=INTEGER}, |
164 | </if> | 164 | </if> |
165 | <if test="paymentType != null"> | 165 | <if test="paymentType != null"> |
166 | #{paymentType,jdbcType=TINYINT}, | 166 | #{paymentType,jdbcType=TINYINT}, |
@@ -175,7 +175,7 @@ | @@ -175,7 +175,7 @@ | ||
175 | #{shipFee,jdbcType=DECIMAL}, | 175 | #{shipFee,jdbcType=DECIMAL}, |
176 | </if> | 176 | </if> |
177 | <if test="status != null"> | 177 | <if test="status != null"> |
178 | - #{status,jdbcType=TINYINT}, | 178 | + #{status,jdbcType=INTEGER}, |
179 | </if> | 179 | </if> |
180 | <if test="createTime != null"> | 180 | <if test="createTime != null"> |
181 | #{createTime,jdbcType=INTEGER}, | 181 | #{createTime,jdbcType=INTEGER}, |
@@ -210,7 +210,7 @@ | @@ -210,7 +210,7 @@ | ||
210 | client_type = #{clientType,jdbcType=TINYINT}, | 210 | client_type = #{clientType,jdbcType=TINYINT}, |
211 | </if> | 211 | </if> |
212 | <if test="payment != null"> | 212 | <if test="payment != null"> |
213 | - payment = #{payment,jdbcType=TINYINT}, | 213 | + payment = #{payment,jdbcType=INTEGER}, |
214 | </if> | 214 | </if> |
215 | <if test="paymentType != null"> | 215 | <if test="paymentType != null"> |
216 | payment_type = #{paymentType,jdbcType=TINYINT}, | 216 | payment_type = #{paymentType,jdbcType=TINYINT}, |
@@ -225,7 +225,7 @@ | @@ -225,7 +225,7 @@ | ||
225 | ship_fee = #{shipFee,jdbcType=DECIMAL}, | 225 | ship_fee = #{shipFee,jdbcType=DECIMAL}, |
226 | </if> | 226 | </if> |
227 | <if test="status != null"> | 227 | <if test="status != null"> |
228 | - status = #{status,jdbcType=TINYINT}, | 228 | + status = #{status,jdbcType=INTEGER}, |
229 | </if> | 229 | </if> |
230 | <if test="createTime != null"> | 230 | <if test="createTime != null"> |
231 | create_time = #{createTime,jdbcType=INTEGER}, | 231 | create_time = #{createTime,jdbcType=INTEGER}, |
@@ -251,12 +251,12 @@ | @@ -251,12 +251,12 @@ | ||
251 | order_code = #{orderCode,jdbcType=BIGINT}, | 251 | order_code = #{orderCode,jdbcType=BIGINT}, |
252 | seller_uid = #{sellerUid,jdbcType=INTEGER}, | 252 | seller_uid = #{sellerUid,jdbcType=INTEGER}, |
253 | client_type = #{clientType,jdbcType=TINYINT}, | 253 | client_type = #{clientType,jdbcType=TINYINT}, |
254 | - payment = #{payment,jdbcType=TINYINT}, | 254 | + payment = #{payment,jdbcType=INTEGER}, |
255 | payment_type = #{paymentType,jdbcType=TINYINT}, | 255 | payment_type = #{paymentType,jdbcType=TINYINT}, |
256 | is_cancel = #{isCancel,jdbcType=TINYINT}, | 256 | is_cancel = #{isCancel,jdbcType=TINYINT}, |
257 | amount = #{amount,jdbcType=DECIMAL}, | 257 | amount = #{amount,jdbcType=DECIMAL}, |
258 | ship_fee = #{shipFee,jdbcType=DECIMAL}, | 258 | ship_fee = #{shipFee,jdbcType=DECIMAL}, |
259 | - status = #{status,jdbcType=TINYINT}, | 259 | + status = #{status,jdbcType=INTEGER}, |
260 | create_time = #{createTime,jdbcType=INTEGER}, | 260 | create_time = #{createTime,jdbcType=INTEGER}, |
261 | update_time = #{updateTime,jdbcType=INTEGER}, | 261 | update_time = #{updateTime,jdbcType=INTEGER}, |
262 | buyer_order_status = #{buyerOrderStatus,jdbcType=INTEGER}, | 262 | buyer_order_status = #{buyerOrderStatus,jdbcType=INTEGER}, |
@@ -27,6 +27,19 @@ | @@ -27,6 +27,19 @@ | ||
27 | values (#{skup,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER}, #{metaKey,jdbcType=VARCHAR}, | 27 | values (#{skup,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER}, #{metaKey,jdbcType=VARCHAR}, |
28 | #{metaValue,jdbcType=VARCHAR}) | 28 | #{metaValue,jdbcType=VARCHAR}) |
29 | </insert> | 29 | </insert> |
30 | + | ||
31 | + | ||
32 | + <select id="selectByMetaKey" resultMap="BaseResultMap"> | ||
33 | + select | ||
34 | + <include refid="Base_Column_List" /> | ||
35 | + from seller_order_meta | ||
36 | + where uid = #{uid,jdbcType=INTEGER} | ||
37 | + and order_code = #{orderCode,jdbcType=BIGINT} | ||
38 | + and meta_key = #{metaKey,jdbcType=VARCHAR} | ||
39 | + limit 1 | ||
40 | + </select> | ||
41 | + | ||
42 | + | ||
30 | <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.yohoufo.dal.order.model.SellerOrderMeta" useGeneratedKeys="true"> | 43 | <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.yohoufo.dal.order.model.SellerOrderMeta" useGeneratedKeys="true"> |
31 | insert into seller_order_meta | 44 | insert into seller_order_meta |
32 | <trim prefix="(" suffix=")" suffixOverrides=","> | 45 | <trim prefix="(" suffix=")" suffixOverrides=","> |
@@ -2,7 +2,7 @@ package com.yohoufo.order.common; | @@ -2,7 +2,7 @@ package com.yohoufo.order.common; | ||
2 | 2 | ||
3 | 3 | ||
4 | 4 | ||
5 | -public enum SellerOrderStatus { | 5 | +public enum SellerOrderStatus { |
6 | 6 | ||
7 | WAITING_PAY(0, "待付保证金", new ButtonShow[]{ButtonShow.PAY_EARNESTMONEY,ButtonShow.NOT_SOLD}), | 7 | WAITING_PAY(0, "待付保证金", new ButtonShow[]{ButtonShow.PAY_EARNESTMONEY,ButtonShow.NOT_SOLD}), |
8 | SELLER_CANCEL_PAY(1, "上架关闭(卖家取消支付)", new ButtonShow[]{ButtonShow.SHOW_DETAIL}), | 8 | SELLER_CANCEL_PAY(1, "上架关闭(卖家取消支付)", new ButtonShow[]{ButtonShow.SHOW_DETAIL}), |
1 | +package com.yohoufo.order.model; | ||
2 | + | ||
3 | + | ||
4 | +import com.yohoufo.order.common.OrderCodeType; | ||
5 | +import lombok.Data; | ||
6 | +import lombok.experimental.Builder; | ||
7 | + | ||
8 | +import java.math.BigDecimal; | ||
9 | + | ||
10 | +@Data | ||
11 | +@Builder | ||
12 | +public class OrderInfo { | ||
13 | + | ||
14 | + | ||
15 | + private Integer uid; | ||
16 | + | ||
17 | + private Long orderCode; | ||
18 | + | ||
19 | + private int skup; | ||
20 | + | ||
21 | + private Integer payment; | ||
22 | + | ||
23 | + private Integer status; | ||
24 | + | ||
25 | + private OrderCodeType orderCodeType; | ||
26 | + | ||
27 | + private BigDecimal amount; | ||
28 | + | ||
29 | + private String statusStr; | ||
30 | +} |
1 | +package com.yohoufo.order.response; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | +import lombok.ToString; | ||
5 | +import lombok.experimental.Builder; | ||
6 | + | ||
7 | +@Data | ||
8 | +@ToString | ||
9 | +@Builder | ||
10 | +public class PaymentConfirmRsp { | ||
11 | + | ||
12 | + | ||
13 | + long orderCode; | ||
14 | + | ||
15 | + String status; | ||
16 | + | ||
17 | + String isPaid; | ||
18 | + String isCancle; | ||
19 | + | ||
20 | +} |
1 | +package com.yohoufo.order.service; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSON; | ||
4 | +import com.alibaba.fastjson.JSONObject; | ||
5 | +import com.yoho.core.common.utils.DateUtil; | ||
6 | +import com.yoho.error.ServiceError; | ||
7 | +import com.yoho.error.exception.ServiceException; | ||
8 | +import com.yohoufo.common.utils.HttpClient; | ||
9 | +import com.yohoufo.common.utils.SignUtils; | ||
10 | +import com.yohoufo.order.common.Payment; | ||
11 | +import com.yohoufo.order.constants.OrderConstant; | ||
12 | +import com.yohoufo.order.model.OrderInfo; | ||
13 | +import com.yohoufo.order.request.PaymentRequest; | ||
14 | +import com.yohoufo.order.response.PrepayResponse; | ||
15 | +import org.slf4j.Logger; | ||
16 | +import org.slf4j.LoggerFactory; | ||
17 | +import org.springframework.beans.factory.annotation.Autowired; | ||
18 | +import org.springframework.beans.factory.annotation.Value; | ||
19 | + | ||
20 | +import java.math.BigDecimal; | ||
21 | +import java.util.Date; | ||
22 | + | ||
23 | +public abstract class AbstractOrderPaymentService { | ||
24 | + | ||
25 | + private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
26 | + | ||
27 | + | ||
28 | + @Value("${unionpay.pay.url}") | ||
29 | + private String unionPayUrl; | ||
30 | + | ||
31 | + @Value("${unionpay.pay.callbackUrl}") | ||
32 | + private String callbackUrl; | ||
33 | + | ||
34 | + @Value("${unionpay.pay.msgSrc}") | ||
35 | + private String msgSrc; | ||
36 | + | ||
37 | + @Value("${unionpay.pay.mid}") | ||
38 | + private String mid; | ||
39 | + | ||
40 | + @Value("${unionpay.pay.tid}") | ||
41 | + private String tid; | ||
42 | + | ||
43 | + @Value("${unionPay.pay.signKey}") | ||
44 | + private String signKey; | ||
45 | + | ||
46 | + | ||
47 | + @Autowired | ||
48 | + HttpClient httpClient; | ||
49 | + | ||
50 | + | ||
51 | + /** | ||
52 | + * 机构商户号 | ||
53 | + */ | ||
54 | + public static final String INST_MID = "APPDEFAULT"; | ||
55 | + | ||
56 | + | ||
57 | + /** | ||
58 | + * 获取订单信息 | ||
59 | + * @param request | ||
60 | + * @return | ||
61 | + */ | ||
62 | + public abstract OrderInfo getOrderInfo(PaymentRequest request); | ||
63 | + | ||
64 | + | ||
65 | + /** | ||
66 | + * 检查状态 | ||
67 | + * @param orderInfo | ||
68 | + */ | ||
69 | + public abstract void checkOrderStatus(OrderInfo orderInfo); | ||
70 | + | ||
71 | + | ||
72 | + /** | ||
73 | + * 预支付方式更新 | ||
74 | + * @param orderInfo | ||
75 | + */ | ||
76 | + public abstract void updatePayment(OrderInfo orderInfo, int payment); | ||
77 | + | ||
78 | + | ||
79 | + /** | ||
80 | + * 获取前回的访问结果 | ||
81 | + * @param request | ||
82 | + * @return | ||
83 | + */ | ||
84 | + public abstract PrepayResponse getAppPayRequest(PaymentRequest request); | ||
85 | + | ||
86 | + | ||
87 | + /** | ||
88 | + * 保存前回预支付结果 | ||
89 | + * @param orderInfo | ||
90 | + * @param response | ||
91 | + */ | ||
92 | + public abstract void saveAppPayRequest(OrderInfo orderInfo, PrepayResponse response); | ||
93 | + | ||
94 | + | ||
95 | + /** | ||
96 | + * 订单是否是待支付状态 | ||
97 | + * @param orderInfo | ||
98 | + * @return | ||
99 | + */ | ||
100 | + public abstract boolean isOrderWaitingPay(OrderInfo orderInfo); | ||
101 | + | ||
102 | + | ||
103 | + /** | ||
104 | + * 检查并更新预支付方式 | ||
105 | + * @param request | ||
106 | + * @return | ||
107 | + */ | ||
108 | + public BigDecimal checkUpdOrderCodePayment(OrderInfo orderInfo,PaymentRequest request) { | ||
109 | + | ||
110 | + // check 订单状态为非付款的状态 | ||
111 | + checkOrderStatus(orderInfo); | ||
112 | + | ||
113 | + // check 金额 | ||
114 | + checkOrderAmount(orderInfo); | ||
115 | + | ||
116 | + // 更新 预支付方式 | ||
117 | + updatePayment(orderInfo, request.getPayment()); | ||
118 | + | ||
119 | + return orderInfo.getAmount(); | ||
120 | + | ||
121 | + } | ||
122 | + | ||
123 | + | ||
124 | + /** | ||
125 | + * 请求银联接口 | ||
126 | + * @param requestObj | ||
127 | + * @return | ||
128 | + */ | ||
129 | + public JSONObject accessUnionPay(JSONObject requestObj){ | ||
130 | + | ||
131 | + try{ | ||
132 | + String result = httpClient.post(unionPayUrl, JSON.toJSONString(requestObj)); | ||
133 | + | ||
134 | + return JSONObject.parseObject(result); | ||
135 | + | ||
136 | + }catch (Exception e){ | ||
137 | + logger.warn("accessUnionPay fail, orderCode is {}, uid is {}"); | ||
138 | + return null; | ||
139 | + } | ||
140 | + | ||
141 | + } | ||
142 | + | ||
143 | + | ||
144 | + /** | ||
145 | + * 生成查询订单支付结果 | ||
146 | + * @param request | ||
147 | + * @param payment | ||
148 | + * @return | ||
149 | + */ | ||
150 | + public JSONObject buildQueryOrderData(PaymentRequest request, Payment payment) { | ||
151 | + JSONObject prepayData = buildCommOrderData(request, payment); | ||
152 | + | ||
153 | + prepayData.put("sign", SignUtils.getSign(prepayData, signKey)); | ||
154 | + return prepayData; | ||
155 | + } | ||
156 | + | ||
157 | + | ||
158 | + /** | ||
159 | + * 银联请求共通参数 | ||
160 | + * @param request | ||
161 | + * @param payment | ||
162 | + * @return | ||
163 | + */ | ||
164 | + private JSONObject buildCommOrderData(PaymentRequest request, Payment payment){ | ||
165 | + JSONObject commData = new JSONObject(); | ||
166 | + commData.put("msgSrc", msgSrc); | ||
167 | + commData.put("msgType", payment.getValue()); | ||
168 | + commData.put("requestTimestamp", DateUtil.date2String(new Date(), OrderConstant.DATE_FORMAT)); | ||
169 | + commData.put("merOrderId", getPayOrderCode(request.getOrderCode(), payment)); | ||
170 | + commData.put("mid", mid); | ||
171 | + commData.put("tid", tid); | ||
172 | + commData.put("instMid", INST_MID); | ||
173 | + | ||
174 | + return commData; | ||
175 | + } | ||
176 | + /** | ||
177 | + * 生成预支付数据 | ||
178 | + * @param request | ||
179 | + * @param payment | ||
180 | + * @return | ||
181 | + */ | ||
182 | + public JSONObject buildPrepayData(PaymentRequest request, Payment payment, BigDecimal amount) { | ||
183 | + | ||
184 | + JSONObject requestData = buildCommOrderData(request, payment); | ||
185 | + requestData.put("notifyUrl", callbackUrl); | ||
186 | + // 单位分 存在点,则签名失败,不支持1.0这种 | ||
187 | + requestData.put("totalAmount", amount.multiply(new BigDecimal(100)).setScale( 0, BigDecimal.ROUND_DOWN ).longValue()); | ||
188 | + | ||
189 | + requestData.put("sign", SignUtils.getSign(requestData, signKey)); | ||
190 | + | ||
191 | + return requestData; | ||
192 | + } | ||
193 | + | ||
194 | + | ||
195 | + private String getPayOrderCode(long orderCode, Payment payment){ | ||
196 | + StringBuilder sb = new StringBuilder(); | ||
197 | + sb.append(OrderConstant.ORDER_CODE_PRE); | ||
198 | + sb.append(orderCode); | ||
199 | + sb.append(OrderConstant.SPLIT_STR); | ||
200 | + sb.append(payment.getCode()); | ||
201 | + return sb.toString(); | ||
202 | + } | ||
203 | + | ||
204 | + | ||
205 | + | ||
206 | + /** | ||
207 | + * 检查金额 | ||
208 | + * @param orderInfo | ||
209 | + * @return | ||
210 | + */ | ||
211 | + public BigDecimal checkOrderAmount(OrderInfo orderInfo){ | ||
212 | + // 检查实付金额 | ||
213 | + if (orderInfo.getAmount() == null || orderInfo.getAmount().compareTo(new BigDecimal(0)) <=0 ){ | ||
214 | + logger.warn("payment amount null, orderCode is {}", orderInfo.getOrderCode()); | ||
215 | + throw new ServiceException(ServiceError.ORDER_NULL); | ||
216 | + } | ||
217 | + | ||
218 | + return orderInfo.getAmount(); | ||
219 | + } | ||
220 | +} |
1 | +package com.yohoufo.order.service; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSON; | ||
4 | +import com.alibaba.fastjson.JSONObject; | ||
5 | +import com.yoho.core.common.utils.DateUtil; | ||
6 | +import com.yoho.error.ServiceError; | ||
7 | +import com.yoho.error.exception.ServiceException; | ||
8 | +import com.yohoufo.dal.order.BuyerOrderMapper; | ||
9 | +import com.yohoufo.dal.order.BuyerOrderMetaMapper; | ||
10 | +import com.yohoufo.dal.order.model.BuyerOrder; | ||
11 | +import com.yohoufo.dal.order.model.BuyerOrderMeta; | ||
12 | +import com.yohoufo.order.common.OrderCodeType; | ||
13 | +import com.yohoufo.order.common.OrderStatus; | ||
14 | +import com.yohoufo.order.common.SellerOrderStatus; | ||
15 | +import com.yohoufo.order.constants.MetaKey; | ||
16 | +import com.yohoufo.order.model.OrderInfo; | ||
17 | +import com.yohoufo.order.request.PaymentRequest; | ||
18 | +import com.yohoufo.order.response.PrepayResponse; | ||
19 | +import org.apache.commons.lang3.StringUtils; | ||
20 | +import org.slf4j.Logger; | ||
21 | +import org.slf4j.LoggerFactory; | ||
22 | +import org.springframework.beans.factory.annotation.Autowired; | ||
23 | +import org.springframework.stereotype.Service; | ||
24 | + | ||
25 | +@Service | ||
26 | +public class BuyerOrderPaymentService extends AbstractOrderPaymentService { | ||
27 | + | ||
28 | + private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
29 | + | ||
30 | + @Autowired | ||
31 | + BuyerOrderMapper buyerOrderMapper; | ||
32 | + | ||
33 | + @Autowired | ||
34 | + BuyerOrderMetaMapper buyerOrderMetaMapper; | ||
35 | + | ||
36 | + | ||
37 | + /** | ||
38 | + * 订单是否是待支付状态 | ||
39 | + * @param orderInfo | ||
40 | + * @return | ||
41 | + */ | ||
42 | + public boolean isOrderWaitingPay(OrderInfo orderInfo){ | ||
43 | + | ||
44 | + if (orderInfo.getStatus() != null && orderInfo.getStatus().intValue() == OrderStatus.WAITING_PAY.getCode()){ | ||
45 | + return true; | ||
46 | + } | ||
47 | + | ||
48 | + return false; | ||
49 | + } | ||
50 | + | ||
51 | + | ||
52 | + /** | ||
53 | + * 保存前回预支付结果 | ||
54 | + * @param orderInfo | ||
55 | + * @param response | ||
56 | + */ | ||
57 | + public void saveAppPayRequest(OrderInfo orderInfo, PrepayResponse response) { | ||
58 | + BuyerOrderMeta buyerOrderMeta = new BuyerOrderMeta(); | ||
59 | + buyerOrderMeta.setUid(orderInfo.getUid()); | ||
60 | + buyerOrderMeta.setOrderCode(orderInfo.getOrderCode()); | ||
61 | + buyerOrderMeta.setMetaKey(MetaKey.APP_PAY_REQUEST); | ||
62 | + buyerOrderMeta.setMetaValue(JSONObject.toJSONString(response)); | ||
63 | + | ||
64 | + // 记录appPayRequest | ||
65 | + buyerOrderMetaMapper.insert(buyerOrderMeta); | ||
66 | + } | ||
67 | + | ||
68 | + | ||
69 | + /** | ||
70 | + * 获取前回的访问结果 | ||
71 | + * @param request | ||
72 | + * @return | ||
73 | + */ | ||
74 | + public PrepayResponse getAppPayRequest(PaymentRequest request) { | ||
75 | + BuyerOrderMeta buyerOrderMeta = buyerOrderMetaMapper.selectByMetaKey(request.getUid(), request.getOrderCode(), MetaKey.APP_PAY_REQUEST); | ||
76 | + | ||
77 | + if (buyerOrderMeta == null || StringUtils.isEmpty(buyerOrderMeta.getMetaValue())){ | ||
78 | + return null; | ||
79 | + } | ||
80 | + try{ | ||
81 | + PrepayResponse response = JSON.parseObject(buyerOrderMeta.getMetaValue(), PrepayResponse.class); | ||
82 | + return response; | ||
83 | + }catch (Exception e){ | ||
84 | + | ||
85 | + } | ||
86 | + return null; | ||
87 | + } | ||
88 | + | ||
89 | + | ||
90 | + /** | ||
91 | + * 检查状态 | ||
92 | + * @param orderInfo | ||
93 | + */ | ||
94 | + public void checkOrderStatus(OrderInfo orderInfo){ | ||
95 | + // 订单不是未付款的状态 | ||
96 | + if (orderInfo.getStatus() == null || orderInfo.getStatus().intValue() != OrderStatus.WAITING_PAY.getCode()){ | ||
97 | + logger.warn("payment status not allow {}, orderCode is {}, uid is {}", orderInfo.getStatus(), | ||
98 | + orderInfo.getOrderCode(), orderInfo.getUid()); | ||
99 | + throw new ServiceException(ServiceError.ORDER_PAY_NOT_ALLOW); | ||
100 | + } | ||
101 | + } | ||
102 | + | ||
103 | + | ||
104 | + /** | ||
105 | + * 预支付方式更新 | ||
106 | + * @param orderInfo | ||
107 | + */ | ||
108 | + public void updatePayment(OrderInfo orderInfo, int payment){ | ||
109 | + | ||
110 | + BuyerOrder buyerOrder = new BuyerOrder(); | ||
111 | + // 更新预支付方式 | ||
112 | + buyerOrder.setPayment(orderInfo.getPayment()); | ||
113 | + buyerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond()); | ||
114 | + buyerOrderMapper.updateByPrimaryKeySelective(buyerOrder); | ||
115 | + | ||
116 | + } | ||
117 | + | ||
118 | + | ||
119 | + /** | ||
120 | + * 获取订单情报 | ||
121 | + * @param request | ||
122 | + * @return | ||
123 | + */ | ||
124 | + public OrderInfo getOrderInfo(PaymentRequest request) { | ||
125 | + | ||
126 | + BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(request.getOrderCode(), request.getUid()); | ||
127 | + | ||
128 | + if (buyerOrder == null){ | ||
129 | + logger.warn("getOrderInfo order not exist, orderCode is {}, uid is {}",request.getOrderCode(), request.getUid()); | ||
130 | + throw new ServiceException(ServiceError.ORDER_NULL); | ||
131 | + } | ||
132 | + | ||
133 | + OrderInfo orderInfo = OrderInfo.builder() | ||
134 | + .uid(request.getUid()) | ||
135 | + .orderCode(request.getOrderCode()) | ||
136 | + .payment(buyerOrder.getPayment()) | ||
137 | + .status(buyerOrder.getStatus()) | ||
138 | + .amount(buyerOrder.getAmount()) | ||
139 | + .orderCodeType(OrderCodeType.BUYER_TYPE) | ||
140 | + .build(); | ||
141 | + return orderInfo; | ||
142 | + } | ||
143 | + | ||
144 | + | ||
145 | +} |
1 | +package com.yohoufo.order.service; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSON; | ||
4 | +import com.alibaba.fastjson.JSONObject; | ||
5 | +import com.yoho.core.common.utils.DateUtil; | ||
6 | +import com.yoho.error.ServiceError; | ||
7 | +import com.yoho.error.exception.ServiceException; | ||
8 | +import com.yohoufo.dal.order.SellerOrderMapper; | ||
9 | +import com.yohoufo.dal.order.SellerOrderMetaMapper; | ||
10 | +import com.yohoufo.dal.order.model.BuyerOrderMeta; | ||
11 | +import com.yohoufo.dal.order.model.SellerOrder; | ||
12 | +import com.yohoufo.dal.order.model.SellerOrderMeta; | ||
13 | +import com.yohoufo.order.common.OrderCodeType; | ||
14 | +import com.yohoufo.order.common.SellerOrderStatus; | ||
15 | +import com.yohoufo.order.constants.MetaKey; | ||
16 | +import com.yohoufo.order.model.OrderInfo; | ||
17 | +import com.yohoufo.order.request.PaymentRequest; | ||
18 | +import com.yohoufo.order.response.PrepayResponse; | ||
19 | +import org.apache.commons.lang3.StringUtils; | ||
20 | +import org.slf4j.Logger; | ||
21 | +import org.slf4j.LoggerFactory; | ||
22 | +import org.springframework.beans.factory.annotation.Autowired; | ||
23 | +import org.springframework.stereotype.Service; | ||
24 | + | ||
25 | + | ||
26 | +@Service | ||
27 | +public class SellerOrderPaymentService extends AbstractOrderPaymentService { | ||
28 | + | ||
29 | + | ||
30 | + private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
31 | + | ||
32 | + @Autowired | ||
33 | + SellerOrderMapper sellerOrderMapper; | ||
34 | + | ||
35 | + @Autowired | ||
36 | + SellerOrderMetaMapper sellerOrderMetaMapper; | ||
37 | + | ||
38 | + | ||
39 | + | ||
40 | + | ||
41 | + | ||
42 | + /** | ||
43 | + * 订单是否是待支付状态 | ||
44 | + * @param orderInfo | ||
45 | + * @return | ||
46 | + */ | ||
47 | + public boolean isOrderWaitingPay(OrderInfo orderInfo){ | ||
48 | + | ||
49 | + if (orderInfo.getStatus() == null || orderInfo.getStatus().intValue() != SellerOrderStatus.WAITING_PAY.getCode()){ | ||
50 | + return true; | ||
51 | + } | ||
52 | + | ||
53 | + return false; | ||
54 | + } | ||
55 | + | ||
56 | + | ||
57 | + /** | ||
58 | + * 保存前回预支付结果 | ||
59 | + * @param orderInfo | ||
60 | + * @param response | ||
61 | + */ | ||
62 | + public void saveAppPayRequest(OrderInfo orderInfo, PrepayResponse response) { | ||
63 | + | ||
64 | + SellerOrderMeta buyerOrderMeta = new SellerOrderMeta(); | ||
65 | + buyerOrderMeta.setUid(orderInfo.getUid()); | ||
66 | + buyerOrderMeta.setSkup(orderInfo.getSkup()); | ||
67 | + buyerOrderMeta.setMetaKey(MetaKey.APP_PAY_REQUEST); | ||
68 | + buyerOrderMeta.setMetaValue(JSONObject.toJSONString(response)); | ||
69 | + | ||
70 | + // 记录appPayRequest | ||
71 | + sellerOrderMetaMapper.insert(buyerOrderMeta); | ||
72 | + } | ||
73 | + | ||
74 | + | ||
75 | + /** | ||
76 | + * 获取前回的访问结果 | ||
77 | + * @param request | ||
78 | + * @return | ||
79 | + */ | ||
80 | + public PrepayResponse getAppPayRequest(PaymentRequest request) { | ||
81 | + BuyerOrderMeta buyerOrderMeta = sellerOrderMetaMapper.selectByMetaKey(request.getUid(), request.getOrderCode(), MetaKey.APP_PAY_REQUEST); | ||
82 | + | ||
83 | + if (buyerOrderMeta == null || StringUtils.isEmpty(buyerOrderMeta.getMetaValue())){ | ||
84 | + return null; | ||
85 | + } | ||
86 | + try{ | ||
87 | + PrepayResponse response = JSON.parseObject(buyerOrderMeta.getMetaValue(), PrepayResponse.class); | ||
88 | + return response; | ||
89 | + }catch (Exception e){ | ||
90 | + | ||
91 | + } | ||
92 | + return null; | ||
93 | + } | ||
94 | + | ||
95 | + | ||
96 | + /** | ||
97 | + * 预支付方式更新 | ||
98 | + * @param orderInfo | ||
99 | + */ | ||
100 | + public void updatePayment(OrderInfo orderInfo, int payment){ | ||
101 | + | ||
102 | + SellerOrder sellerOrder = new SellerOrder(); | ||
103 | + // 更新预支付方式 | ||
104 | + sellerOrder.setPayment(orderInfo.getPayment()); | ||
105 | + sellerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond()); | ||
106 | + sellerOrderMapper.updateByPrimaryKeySelective(sellerOrder); | ||
107 | + | ||
108 | + } | ||
109 | + | ||
110 | + | ||
111 | + /** | ||
112 | + * 检查状态 | ||
113 | + * @param orderInfo | ||
114 | + */ | ||
115 | + public void checkOrderStatus(OrderInfo orderInfo){ | ||
116 | + // 订单不是未付款的状态 | ||
117 | + if (orderInfo.getStatus() == null || orderInfo.getStatus().intValue() != SellerOrderStatus.WAITING_PAY.getCode()){ | ||
118 | + logger.warn("payment status not allow {}, orderCode is {}, uid is {}", orderInfo.getStatus(), | ||
119 | + orderInfo.getOrderCode(), orderInfo.getUid()); | ||
120 | + throw new ServiceException(ServiceError.ORDER_PAY_NOT_ALLOW); | ||
121 | + } | ||
122 | + } | ||
123 | + | ||
124 | + | ||
125 | + /** | ||
126 | + * 获取订单情报 | ||
127 | + * @param request | ||
128 | + * @return | ||
129 | + */ | ||
130 | + public OrderInfo getOrderInfo(PaymentRequest request) { | ||
131 | + | ||
132 | + SellerOrder sellerOrder = sellerOrderMapper.selectByOrderCode(request.getOrderCode(), request.getUid()); | ||
133 | + | ||
134 | + if (sellerOrder == null){ | ||
135 | + logger.warn("getOrderInfo order not exist, orderCode is {}, uid is {}",request.getOrderCode(), request.getUid()); | ||
136 | + throw new ServiceException(ServiceError.ORDER_NULL); | ||
137 | + } | ||
138 | + | ||
139 | + OrderInfo orderInfo = OrderInfo.builder() | ||
140 | + .uid(request.getUid()) | ||
141 | + .orderCode(request.getOrderCode()) | ||
142 | + .payment(sellerOrder.getPayment()) | ||
143 | + .skup(sellerOrder.getSkup()) | ||
144 | + .status(sellerOrder.getStatus()) | ||
145 | + .orderCodeType(OrderCodeType.BUYER_TYPE) | ||
146 | + .amount(sellerOrder.getEarnestMoney()) | ||
147 | + .build(); | ||
148 | + return orderInfo; | ||
149 | + } | ||
150 | + | ||
151 | + | ||
152 | + | ||
153 | +} |
@@ -13,14 +13,17 @@ import com.yohoufo.dal.order.SellerOrderMapper; | @@ -13,14 +13,17 @@ import com.yohoufo.dal.order.SellerOrderMapper; | ||
13 | import com.yohoufo.dal.order.model.BuyerOrder; | 13 | import com.yohoufo.dal.order.model.BuyerOrder; |
14 | import com.yohoufo.dal.order.model.BuyerOrderMeta; | 14 | import com.yohoufo.dal.order.model.BuyerOrderMeta; |
15 | import com.yohoufo.dal.order.model.SellerOrder; | 15 | import com.yohoufo.dal.order.model.SellerOrder; |
16 | -import com.yohoufo.order.common.OrderCodeType; | ||
17 | -import com.yohoufo.order.common.OrderStatus; | ||
18 | -import com.yohoufo.order.common.Payment; | 16 | +import com.yohoufo.order.common.*; |
19 | import com.yohoufo.order.constants.MetaKey; | 17 | import com.yohoufo.order.constants.MetaKey; |
20 | import com.yohoufo.order.constants.OrderConstant; | 18 | import com.yohoufo.order.constants.OrderConstant; |
19 | +import com.yohoufo.order.model.OrderInfo; | ||
21 | import com.yohoufo.order.request.PaymentRequest; | 20 | import com.yohoufo.order.request.PaymentRequest; |
21 | +import com.yohoufo.order.response.PaymentConfirmRsp; | ||
22 | import com.yohoufo.order.response.PrepayResponse; | 22 | import com.yohoufo.order.response.PrepayResponse; |
23 | +import com.yohoufo.order.service.AbstractOrderPaymentService; | ||
24 | +import com.yohoufo.order.service.BuyerOrderPaymentService; | ||
23 | import com.yohoufo.order.service.IPaymentService; | 25 | import com.yohoufo.order.service.IPaymentService; |
26 | +import com.yohoufo.order.service.SellerOrderPaymentService; | ||
24 | import com.yohoufo.order.service.support.codegenerator.OrderCodeGenerator; | 27 | import com.yohoufo.order.service.support.codegenerator.OrderCodeGenerator; |
25 | import com.yohoufo.order.service.support.codegenerator.bean.CodeMeta; | 28 | import com.yohoufo.order.service.support.codegenerator.bean.CodeMeta; |
26 | import org.apache.commons.lang3.StringUtils; | 29 | import org.apache.commons.lang3.StringUtils; |
@@ -39,52 +42,97 @@ public class PaymentServiceImpl implements IPaymentService { | @@ -39,52 +42,97 @@ public class PaymentServiceImpl implements IPaymentService { | ||
39 | private final Logger logger = LoggerFactory.getLogger(getClass()); | 42 | private final Logger logger = LoggerFactory.getLogger(getClass()); |
40 | 43 | ||
41 | @Autowired | 44 | @Autowired |
42 | - BuyerOrderMapper buyerOrderMapper; | 45 | + BuyerOrderMetaMapper buyerOrderMetaMapper; |
43 | 46 | ||
44 | @Autowired | 47 | @Autowired |
45 | - SellerOrderMapper sellerOrderMapper; | 48 | + BuyerOrderPaymentService buyerOrderPaymentService; |
46 | 49 | ||
47 | - @Value("${unionpay.pay.url}") | ||
48 | - private String unionPayUrl; | 50 | + @Autowired |
51 | + SellerOrderPaymentService sellerOrderPaymentService; | ||
49 | 52 | ||
50 | - @Value("${unionpay.pay.callbackUrl}") | ||
51 | - private String callbackUrl; | ||
52 | 53 | ||
53 | - @Value("${unionpay.pay.msgSrc}") | ||
54 | - private String msgSrc; | 54 | + @Autowired |
55 | + OrderCodeGenerator orderCodeGenerator; | ||
55 | 56 | ||
56 | - @Value("${unionpay.pay.mid}") | ||
57 | - private String mid; | ||
58 | 57 | ||
59 | - @Value("${unionpay.pay.tid}") | ||
60 | - private String tid; | 58 | + /** |
59 | + * 获取主场的订单service | ||
60 | + * @param codeMeta | ||
61 | + * @param request | ||
62 | + * @return | ||
63 | + */ | ||
64 | + private AbstractOrderPaymentService getOrderPaymentService(CodeMeta codeMeta, PaymentRequest request){ | ||
61 | 65 | ||
62 | - @Value("${unionPay.pay.signKey}") | ||
63 | - private String signKey; | 66 | + if (!OrderCodeType.isExistOrderCodeType(codeMeta.getType())) { |
67 | + logger.warn("payment orderCode invalidate {}, uid is {}, codeMeta is {}", | ||
68 | + request.getOrderCode(), request.getUid(), codeMeta.getType()); | ||
69 | + throw new ServiceException(ServiceError.ORDER_ORDER_CODE_IS_EMPTY); | ||
70 | + } | ||
64 | 71 | ||
65 | - @Autowired | ||
66 | - HttpClient httpClient; | 72 | + AbstractOrderPaymentService paymentService = null; |
67 | 73 | ||
68 | - @Autowired | ||
69 | - BuyerOrderMetaMapper buyerOrderMetaMapper; | 74 | + // 买家订单 |
75 | + if (codeMeta.getType() == OrderCodeType.BUYER_TYPE.getType()){ | ||
76 | + paymentService = this.buyerOrderPaymentService; | ||
70 | 77 | ||
71 | - /** | ||
72 | - * 机构商户号 | ||
73 | - */ | ||
74 | - public static final String INST_MID = "APPDEFAULT"; | 78 | + }else{ |
79 | + paymentService = this.sellerOrderPaymentService; | ||
75 | 80 | ||
76 | - @Autowired | ||
77 | - OrderCodeGenerator orderCodeGenerator; | 81 | + } |
82 | + | ||
83 | + return paymentService; | ||
84 | + } | ||
85 | + | ||
86 | + | ||
87 | + | ||
88 | + public PaymentConfirmRsp payConfirm(PaymentRequest request){ | ||
89 | + | ||
90 | + // 入口数据检查 | ||
91 | + if (request.getUid() < 0 | ||
92 | + || request.getOrderCode() < 0){ | ||
93 | + logger.warn("payConfirm request empty"); | ||
94 | + throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY); | ||
95 | + } | ||
96 | + | ||
97 | + // 反解订单号 | ||
98 | + // 根据订单号 反解:买家订单号 or 卖家订单号 | ||
99 | + CodeMeta codeMeta = orderCodeGenerator.expId(request.getOrderCode()); | ||
100 | + | ||
101 | + // 获取主场的service | ||
102 | + AbstractOrderPaymentService paymentService = getOrderPaymentService(codeMeta, request); | ||
103 | + | ||
104 | + // 获取订单信息 | ||
105 | + OrderInfo orderInfo = paymentService.getOrderInfo(request); | ||
106 | + | ||
107 | + // 已支付的状态,直接返回 | ||
108 | + | ||
109 | + // payment!=null && 待付款,其他直接返回 | ||
110 | + PaymentConfirmRsp paymentConfirmRsp = PaymentConfirmRsp.builder().orderCode(request.getOrderCode()).build(); | ||
111 | + if (orderInfo.getPayment() == null || !paymentService.isOrderWaitingPay(orderInfo)){ | ||
112 | + | ||
113 | + | ||
114 | + } | ||
78 | 115 | ||
116 | + // 未支付订单 | ||
117 | + Payment payment = Payment.getPayment(orderInfo.getPayment()); | ||
118 | + JSONObject result = paymentService.accessUnionPay(paymentService.buildQueryOrderData(request, payment)); | ||
79 | 119 | ||
120 | + if (result.containsKey("errCode") && "SUCCESS".equals(result.getString("errCode"))){ | ||
121 | + String stauts = result.getString("status"); | ||
122 | + if ("TRADE_SUCCESS".equals(stauts)){ | ||
80 | 123 | ||
81 | - public void payConfirm(PaymentRequest request){ | 124 | + // 支付成功 TODO |
125 | + } | ||
126 | + | ||
127 | + } | ||
82 | 128 | ||
83 | - // | 129 | + return paymentConfirmRsp; |
84 | 130 | ||
85 | } | 131 | } |
86 | 132 | ||
87 | 133 | ||
134 | + | ||
135 | + | ||
88 | /** | 136 | /** |
89 | * 订单退款 | 137 | * 订单退款 |
90 | * 使用场景: [卖家发布商品]页面的[不卖了], | 138 | * 使用场景: [卖家发布商品]页面的[不卖了], |
@@ -113,207 +161,84 @@ public class PaymentServiceImpl implements IPaymentService { | @@ -113,207 +161,84 @@ public class PaymentServiceImpl implements IPaymentService { | ||
113 | */ | 161 | */ |
114 | public PrepayResponse payment(PaymentRequest request){ | 162 | public PrepayResponse payment(PaymentRequest request){ |
115 | 163 | ||
116 | - // 入口数据检查 | ||
117 | - if (request.getUid() < 0 | ||
118 | - || request.getOrderCode() < 0){ | ||
119 | - logger.warn("payment not exist"); | ||
120 | - throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY); | ||
121 | - } | 164 | + Payment payment = checkPaymentRequest(request); |
122 | 165 | ||
123 | - // 不存在的支付方式 | ||
124 | - Payment payment = Payment.getPayment(request.getPayment()); | ||
125 | - if (payment == null){ | ||
126 | - logger.warn("payment payment not exist {}, uid is {}, orderCode is {}" ,request.getPayment(), | ||
127 | - request.getUid(), request.getOrderCode()); | ||
128 | - throw new ServiceException(ServiceError.ORDER_PAYMENT_IS_EMPTY); | ||
129 | - } | 166 | + // 根据订单号 反解:买家订单号 or 卖家订单号 |
167 | + CodeMeta codeMeta = orderCodeGenerator.expId(request.getOrderCode()); | ||
168 | + | ||
169 | + // 获取主场的service | ||
170 | + AbstractOrderPaymentService paymentService = getOrderPaymentService(codeMeta, request); | ||
171 | + | ||
172 | + // check 订单是否存在 | ||
173 | + OrderInfo orderInfo = paymentService.getOrderInfo(request); | ||
130 | 174 | ||
131 | // 卖家or买家订单,支付方式更新,返回实付金额 | 175 | // 卖家or买家订单,支付方式更新,返回实付金额 |
132 | - BigDecimal amount = checkUpdOrderCode(request); | 176 | + BigDecimal amount = paymentService.checkUpdOrderCodePayment(orderInfo, request); |
133 | 177 | ||
134 | // 向银联提下过单的,直接返回结果 | 178 | // 向银联提下过单的,直接返回结果 |
135 | - PrepayResponse oldAppPayRequest = getAppPayRequest(request); | 179 | + PrepayResponse oldAppPayRequest = paymentService.getAppPayRequest(request); |
136 | if (oldAppPayRequest != null){ | 180 | if (oldAppPayRequest != null){ |
137 | - logger.info("payment oldAppPayRequest orderCode is {}, uid is {}, oldAppPayRequest is {}", | ||
138 | - request.getOrderCode(), request.getUid(), oldAppPayRequest); | 181 | + logger.info("payment oldAppPayRequest orderCode is {}, uid is {}, oldAppPayRequest is {}",request.getOrderCode(), request.getUid(), oldAppPayRequest); |
139 | return oldAppPayRequest; | 182 | return oldAppPayRequest; |
140 | } | 183 | } |
141 | 184 | ||
142 | // 生成预支付数据 | 185 | // 生成预支付数据 |
143 | - JSONObject prepayData = buildPrepayData(request, payment, amount); | 186 | + JSONObject prepayData = paymentService.buildPrepayData(request, payment, amount); |
144 | 187 | ||
188 | + // 返回处理结果 | ||
145 | PrepayResponse response = new PrepayResponse(); | 189 | PrepayResponse response = new PrepayResponse(); |
146 | 190 | ||
147 | - try{ | ||
148 | - String result = httpClient.post(unionPayUrl, JSON.toJSONString(prepayData)); | ||
149 | - JSONObject resultJSON = JSONObject.parseObject(result); | ||
150 | - if (resultJSON.containsKey("errCode") && "SUCCESS".equals(resultJSON.getString("errCode"))){ | ||
151 | - response.setPrepayResult(PrepayResponse.SUCCESS); | ||
152 | - response.setJsonObj(resultJSON.getJSONObject("appPayRequest")); | ||
153 | - | ||
154 | - // 保存支付成功结果 | ||
155 | - saveAppPayRequest(request, response); | ||
156 | - | ||
157 | - logger.info("payment success orderCode is {}", request.getOrderCode() ); | ||
158 | - return response; | ||
159 | - }else { | ||
160 | - response.setPrepayResult(PrepayResponse.FAILED); | ||
161 | - response.setJsonObj(resultJSON); | ||
162 | - logger.warn("payment fail, orderCode is {}, result is {}", request.getOrderCode(), result); | ||
163 | - return response; | ||
164 | - } | ||
165 | - }catch (Exception e) { | ||
166 | - logger.warn("payment error orderCode is {}, e is {}", request.getOrderCode()); | ||
167 | - } | ||
168 | - response.setPrepayResult(PrepayResponse.FAILED); | ||
169 | - return response; | 191 | + JSONObject resultJSON = paymentService.accessUnionPay(prepayData); |
170 | 192 | ||
171 | - } | 193 | + if (resultJSON.containsKey("errCode") && "SUCCESS".equals(resultJSON.getString("errCode"))){ |
194 | + response.setPrepayResult(PrepayResponse.SUCCESS); | ||
195 | + response.setJsonObj(resultJSON.getJSONObject("appPayRequest")); | ||
172 | 196 | ||
173 | - /** | ||
174 | - * 获取前回的访问结果 | ||
175 | - * @param request | ||
176 | - * @return | ||
177 | - */ | ||
178 | - private PrepayResponse getAppPayRequest(PaymentRequest request) { | ||
179 | - BuyerOrderMeta buyerOrderMeta = buyerOrderMetaMapper.selectByMetaKey(request.getUid(), request.getOrderCode(), getMetaKey(request.getPayment())); | 197 | + // 保存支付成功结果 |
198 | + paymentService.saveAppPayRequest(orderInfo, response); | ||
180 | 199 | ||
181 | - if (buyerOrderMeta == null || StringUtils.isEmpty(buyerOrderMeta.getMetaValue())){ | ||
182 | - return null; | ||
183 | - } | ||
184 | - try{ | ||
185 | - PrepayResponse response = JSON.parseObject(buyerOrderMeta.getMetaValue(), PrepayResponse.class); | 200 | + logger.info("payment success orderCode is {}", request.getOrderCode() ); |
186 | return response; | 201 | return response; |
187 | - }catch (Exception e){ | 202 | + }else{ |
188 | 203 | ||
204 | + response.setPrepayResult(PrepayResponse.FAILED); | ||
205 | + response.setJsonObj(resultJSON); | ||
206 | + return response; | ||
189 | } | 207 | } |
190 | - return null; | ||
191 | - } | ||
192 | - | ||
193 | - private void saveAppPayRequest(PaymentRequest request, PrepayResponse response) { | ||
194 | - BuyerOrderMeta buyerOrderMeta = new BuyerOrderMeta(); | ||
195 | - buyerOrderMeta.setUid(request.getUid()); | ||
196 | - buyerOrderMeta.setOrderCode(request.getOrderCode()); | ||
197 | - buyerOrderMeta.setMetaKey(getMetaKey(request.getPayment())); | ||
198 | - buyerOrderMeta.setMetaValue(JSONObject.toJSONString(response)); | ||
199 | - | ||
200 | - // 记录appPayRequest | ||
201 | - buyerOrderMetaMapper.insert(buyerOrderMeta); | ||
202 | - } | ||
203 | - | ||
204 | - public String getMetaKey(int payment){ | ||
205 | - return MetaKey.APP_PAY_REQUEST + OrderConstant.SPLIT_STR +payment; | ||
206 | } | 208 | } |
207 | 209 | ||
208 | /** | 210 | /** |
209 | - * 生成预支付数据 | 211 | + * 入口数据检查 |
210 | * @param request | 212 | * @param request |
211 | - * @param paymentVal | ||
212 | * @return | 213 | * @return |
213 | */ | 214 | */ |
214 | - private JSONObject buildPrepayData(PaymentRequest request, Payment payment, BigDecimal amount) { | ||
215 | - JSONObject prepayData = new JSONObject(); | ||
216 | - prepayData.put("msgSrc", msgSrc); | ||
217 | - prepayData.put("msgType", payment.getValue()); | ||
218 | - prepayData.put("requestTimestamp", DateUtil.date2String(new Date(), OrderConstant.DATE_FORMAT)); | ||
219 | - prepayData.put("merOrderId", getPayOrderCode(request.getOrderCode(), payment)); | ||
220 | - prepayData.put("mid", mid); | ||
221 | - prepayData.put("tid", tid); | ||
222 | - prepayData.put("instMid", INST_MID); | ||
223 | - prepayData.put("notifyUrl", callbackUrl); | ||
224 | - // 单位分 存在点,则签名失败,不支持1.0这种 | ||
225 | - prepayData.put("totalAmount", amount.multiply(new BigDecimal(100)).setScale( 0, BigDecimal.ROUND_DOWN ).longValue()); | ||
226 | - | ||
227 | - prepayData.put("sign", SignUtils.getSign(prepayData, signKey)); | ||
228 | - return prepayData; | ||
229 | - } | ||
230 | - | ||
231 | - | ||
232 | - private String getPayOrderCode(long orderCode, Payment payment){ | ||
233 | - StringBuilder sb = new StringBuilder(); | ||
234 | - sb.append(OrderConstant.ORDER_CODE_PRE); | ||
235 | - sb.append(orderCode); | ||
236 | - sb.append(OrderConstant.SPLIT_STR); | ||
237 | - sb.append(payment.getCode()); | ||
238 | - return sb.toString(); | ||
239 | - } | ||
240 | - | ||
241 | - private BigDecimal checkUpdOrderCode(PaymentRequest request) { | ||
242 | - // 根据订单号 反解:买家订单号 or 卖家订单号 | ||
243 | - CodeMeta codeMeta = orderCodeGenerator.expId(request.getOrderCode()); | ||
244 | - if (!OrderCodeType.isExistOrderCodeType(codeMeta.getType())) { | ||
245 | - logger.warn("payment orderCode invalidate {}, uid is {}, codeMeta is {}", request.getOrderCode(), request.getUid(), codeMeta.getType()); | ||
246 | - throw new ServiceException(ServiceError.ORDER_ORDER_CODE_IS_EMPTY); | 215 | + private Payment checkPaymentRequest(PaymentRequest request) { |
216 | + // 入口数据检查 | ||
217 | + if (request.getUid() < 0 | ||
218 | + || request.getOrderCode() < 0){ | ||
219 | + logger.warn("payment request empty"); | ||
220 | + throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY); | ||
247 | } | 221 | } |
248 | 222 | ||
249 | - logger.info("payment orderCode is {} ,uid is {}, type is {}",request.getOrderCode(), request.getUid(), codeMeta.getType()); | ||
250 | - | ||
251 | - // 买家订单号 | ||
252 | - if (codeMeta.getType() == OrderCodeType.BUYER_TYPE.getType()){ | ||
253 | - return checkUpdBuyerOrderCode(request); | ||
254 | - }else{ | ||
255 | - return checkUpdSellerOrderCode(request); | 223 | + // 不存在的支付方式 |
224 | + Payment payment = Payment.getPayment(request.getPayment()); | ||
225 | + if (payment == null){ | ||
226 | + logger.warn("payment payment not exist {}, uid is {}, orderCode is {}" ,request.getPayment(), | ||
227 | + request.getUid(), request.getOrderCode()); | ||
228 | + throw new ServiceException(ServiceError.ORDER_PAYMENT_IS_EMPTY); | ||
256 | } | 229 | } |
230 | + return payment; | ||
257 | } | 231 | } |
258 | 232 | ||
259 | 233 | ||
260 | - private BigDecimal checkUpdSellerOrderCode(PaymentRequest request) { | ||
261 | - // 订单状态 | ||
262 | - SellerOrder sellerOrder = sellerOrderMapper.selectByOrderCode(request.getOrderCode(), request.getUid()); | ||
263 | - if (sellerOrder == null){ | ||
264 | - logger.warn("payment orderCode not exist, orderCode is {}", request.getOrderCode(), request.getUid()); | ||
265 | - throw new ServiceException(ServiceError.ORDER_NULL); | ||
266 | - } | ||
267 | 234 | ||
268 | - // 订单不是未付款的状态 | ||
269 | - if (sellerOrder.getStatus() == null || sellerOrder.getStatus().intValue() != OrderStatus.WAITING_PAY.getCode()){ | ||
270 | - logger.warn("payment status not allow {}, orderCode is {}, uid is {}", sellerOrder.getStatus(), request.getOrderCode(), request.getUid()); | ||
271 | - throw new ServiceException(ServiceError.ORDER_PAY_NOT_ALLOW); | ||
272 | - } | ||
273 | 235 | ||
274 | - // 检查实付金额 | ||
275 | - if (sellerOrder.getEarnestMoney() == null || sellerOrder.getEarnestMoney().compareTo(new BigDecimal(0)) <=0 ){ | ||
276 | - logger.warn("payment amount null, orderCode is {}", request.getOrderCode()); | ||
277 | - throw new ServiceException(ServiceError.ORDER_NULL); | ||
278 | - } | ||
279 | 236 | ||
280 | - // 更新预支付方式 | ||
281 | - sellerOrder.setPayment(request.getPayment()); | ||
282 | - sellerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond()); | ||
283 | - sellerOrderMapper.updateByPrimaryKey(sellerOrder); | ||
284 | 237 | ||
285 | - return sellerOrder.getEarnestMoney(); | ||
286 | - } | ||
287 | 238 | ||
288 | - /** | ||
289 | - * 检查买家订单状态,更新支付方式 | ||
290 | - * @param request | ||
291 | - */ | ||
292 | - private BigDecimal checkUpdBuyerOrderCode(PaymentRequest request) { | ||
293 | - // 订单状态 | ||
294 | - BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(request.getOrderCode(), request.getUid()); | ||
295 | - if (buyerOrder == null){ | ||
296 | - logger.warn("payment orderCode not exist, orderCode is {}", request.getOrderCode(), request.getUid()); | ||
297 | - throw new ServiceException(ServiceError.ORDER_NULL); | ||
298 | - } | ||
299 | 239 | ||
300 | - // 订单不是未付款的状态 | ||
301 | - if (buyerOrder.getStatus() == null || buyerOrder.getStatus().intValue() != OrderStatus.WAITING_PAY.getCode()){ | ||
302 | - logger.warn("payment status not allow {}, orderCode is {}, uid is {}", buyerOrder.getStatus(), request.getOrderCode(), request.getUid()); | ||
303 | - throw new ServiceException(ServiceError.ORDER_PAY_NOT_ALLOW); | ||
304 | - } | ||
305 | 240 | ||
306 | - // 检查实付金额 | ||
307 | - if (buyerOrder.getAmount() == null || buyerOrder.getAmount().compareTo(new BigDecimal(0)) <= 0){ | ||
308 | - logger.warn("payment amount null, orderCode is {}", request.getOrderCode()); | ||
309 | - throw new ServiceException(ServiceError.ORDER_NULL); | ||
310 | - } | ||
311 | 241 | ||
312 | - // 更新预支付方式 | ||
313 | - buyerOrder.setPayment((byte)request.getPayment()); | ||
314 | - buyerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond()); | ||
315 | - buyerOrderMapper.updateByPrimaryKey(buyerOrder); | ||
316 | 242 | ||
317 | - return buyerOrder.getAmount(); | ||
318 | - } | 243 | + |
319 | } | 244 | } |
@@ -161,7 +161,7 @@ public class SubmitOrderServiceImpl implements ISubmitOrderService { | @@ -161,7 +161,7 @@ public class SubmitOrderServiceImpl implements ISubmitOrderService { | ||
161 | buyerOrder.setClientType(ClientType.getClientType(orderBuilder.getClientType())); | 161 | buyerOrder.setClientType(ClientType.getClientType(orderBuilder.getClientType())); |
162 | buyerOrder.setCreateTime(now); | 162 | buyerOrder.setCreateTime(now); |
163 | buyerOrder.setUpdateTime(now); | 163 | buyerOrder.setUpdateTime(now); |
164 | - buyerOrder.setStatus((byte) OrderStatus.WAITING_PAY.getCode()); | 164 | + buyerOrder.setStatus(OrderStatus.WAITING_PAY.getCode()); |
165 | buyerOrderMapper.insert(buyerOrder); | 165 | buyerOrderMapper.insert(buyerOrder); |
166 | } | 166 | } |
167 | 167 |
@@ -187,7 +187,7 @@ public class PayTest { | @@ -187,7 +187,7 @@ public class PayTest { | ||
187 | // 支付宝 | 187 | // 支付宝 |
188 | data.put("msgType", "query"); // 微信:wx.unifiedOrder, 支付宝:trade.precreate, 全民付:qmf.order, 银联云闪付:uac.appOrder | 188 | data.put("msgType", "query"); // 微信:wx.unifiedOrder, 支付宝:trade.precreate, 全民付:qmf.order, 银联云闪付:uac.appOrder |
189 | data.put("requestTimestamp", DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss")); | 189 | data.put("requestTimestamp", DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss")); |
190 | - data.put("merOrderId", "319450751581111111"); | 190 | + data.put("merOrderId", "31949756868190273_1"); |
191 | data.put("mid", "898310148160568"); //商户号 仅用于支付宝和云闪付 | 191 | data.put("mid", "898310148160568"); //商户号 仅用于支付宝和云闪付 |
192 | data.put("tid","00000001"); //终端号 | 192 | data.put("tid","00000001"); //终端号 |
193 | data.put("instMid", "APPDEFAULT"); // 机构商户号 | 193 | data.put("instMid", "APPDEFAULT"); // 机构商户号 |
-
Please register or login to post a comment