测试订单系统接口
查詢(xún)訂單接口
接口說(shuō)明:
-
請(qǐng)求方式:GET
-
請(qǐng)求路徑:/order/{id}
-
請(qǐng)求參數(shù):id,訂單編號(hào)
-
返回結(jié)果:Order,訂單的json對(duì)象
測(cè)試:
結(jié)果:
更新訂單狀態(tài)
接口說(shuō)明:
-
請(qǐng)求參數(shù):PUT
-
請(qǐng)求路徑:/order/{id}/{status}
-
請(qǐng)求參數(shù):
-
id:訂單編號(hào),String類(lèi)型,不能為空
-
status:訂單狀態(tài),不能為空
-
-
返回結(jié)果:null
測(cè)試:
結(jié)果:
數(shù)據(jù)庫(kù)中也發(fā)生了改變:
分頁(yè)查詢(xún)訂單
接口說(shuō)明:
-
請(qǐng)求方式:Get
-
請(qǐng)求路徑:/order/list
-
請(qǐng)求參數(shù):
-
page:當(dāng)前頁(yè),Integer類(lèi)型,默認(rèn)為1
-
rows:每頁(yè)大小,Integer類(lèi)型,默認(rèn)為5
-
status:訂單狀態(tài),String類(lèi)型,默認(rèn)查詢(xún)?nèi)繝顟B(tài)訂單
-
-
返回結(jié)果:PageResult 對(duì)象,包含下面屬性:
-
total:總條數(shù)
-
items:當(dāng)前頁(yè)訂單數(shù)組
-
訂單對(duì)象
-
-
測(cè)試:
結(jié)果:
生成微信付款鏈接
接口說(shuō)明:
-
請(qǐng)求方式:Get
-
請(qǐng)求路徑:/order/url/{id}
-
請(qǐng)求參數(shù):id,訂單編號(hào)
-
返回結(jié)果:String類(lèi)型,生成的微信支付鏈接
測(cè)試:
結(jié)果:
微信支付工具
PayHelper
@Component public class PayHelper {private WXPay wxPay;private static final Logger logger = LoggerFactory.getLogger(PayHelper.class);@Autowiredprivate StringRedisTemplate redisTemplate;@Autowiredprivate OrderService orderService;public PayHelper(PayConfig payConfig) {// 真實(shí)開(kāi)發(fā)時(shí)wxPay = new WXPay(payConfig);// 測(cè)試時(shí)// wxPay = new WXPay(payConfig, WXPayConstants.SignType.MD5, true);}public String createPayUrl(Long orderId) {String key = "ly.pay.url." + orderId;try {String url = this.redisTemplate.opsForValue().get(key);if (StringUtils.isNotBlank(url)) {return url;}} catch (Exception e) {logger.error("查詢(xún)緩存付款鏈接異常,訂單編號(hào):{}", orderId, e);}try {Map<String, String> data = new HashMap<>();// 商品描述data.put("body", "商城測(cè)試");// 訂單號(hào)data.put("out_trade_no", orderId.toString());//貨幣data.put("fee_type", "CNY");//金額,單位是分data.put("total_fee", "1");//調(diào)用微信支付的終端IP(estore商城的IP)data.put("spbill_create_ip", "127.0.0.1");//回調(diào)地址data.put("notify_url", "http://test.learn.com/wxpay/notify");// 交易類(lèi)型為掃碼支付data.put("trade_type", "NATIVE");//商品id,使用假數(shù)據(jù)data.put("product_id", "1234567");Map<String, String> result = this.wxPay.unifiedOrder(data);if ("SUCCESS".equals(result.get("return_code"))) {String url = result.get("code_url");// 將付款地址緩存,時(shí)間為10分鐘try {this.redisTemplate.opsForValue().set(key, url, 10, TimeUnit.MINUTES);} catch (Exception e) {logger.error("緩存付款鏈接異常,訂單編號(hào):{}", orderId, e);}return url;} else {logger.error("創(chuàng)建預(yù)交易訂單失敗,錯(cuò)誤信息:{}", result.get("return_msg"));return null;}} catch (Exception e) {logger.error("創(chuàng)建預(yù)交易訂單異常", e);return null;}}/*** 查詢(xún)訂單狀態(tài)** @param orderId* @return*/public PayState queryOrder(Long orderId) {Map<String, String> data = new HashMap<>();// 訂單號(hào)data.put("out_trade_no", orderId.toString());try {Map<String, String> result = this.wxPay.orderQuery(data);if (result == null) {// 未查詢(xún)到結(jié)果,認(rèn)為是未付款return PayState.NOT_PAY;}String state = result.get("trade_state");if ("SUCCESS".equals(state)) {// success,則認(rèn)為付款成功// 修改訂單狀態(tài)this.orderService.updateStatus(orderId, 2);return PayState.SUCCESS;} else if (StringUtils.equals("USERPAYING", state)|| StringUtils.equals("NOTPAY", state)) {// 未付款或正在付款,都認(rèn)為是未付款return PayState.NOT_PAY;} else {// 其它狀態(tài)認(rèn)為是付款失敗return PayState.FAIL;}} catch (Exception e) {logger.error("查詢(xún)訂單狀態(tài)異常", e);return PayState.NOT_PAY;}} }查詢(xún)支付狀態(tài)
接口說(shuō)明:
-
請(qǐng)求方式: Get
-
請(qǐng)求路徑: /state/{id}
-
請(qǐng)求參數(shù): id,訂單編號(hào)
-
返回結(jié)果:0, 未查詢(xún)到支付信息 1,支付成功 2,支付失敗(查詢(xún)失敗,或者訂單過(guò)期)
未付款
未付款時(shí)查詢(xún),測(cè)試:
結(jié)果:
?
總結(jié)