标签:崩溃 前端 ble close ret 订单号 sed 支付宝 tran
public order scanPay()throws PayException{ try{ beginTransaction order = createOrder result = wechat.scanPay order.makeSuccess commit } catch(Exception e){ rollback throw new PayException("pay fail") } return order }
public order scanPay()throws PayException{ try{ beginTransaction order = createOrder order.makePaying commit } catch(Exception e){ rollback throw new PayException("pay fail") } try{ result = wechat.scanPay if (result == fail) { ..... } ....... } catch(Exception e){ order.makeFail throw new PayException("pay fail") } try{ beginTransaction order.makePaySuccess commit } catch(Exception e){ rollback throw new PayException("pay fail") } return order }
//创建订单,然后返回给调用者 public order createOrder(){ order = createOrder return order } //调用者发起支付请求,由于异步操作,不会返回任何结果 public void reqeustScanPay(orderId,amount,code){ mqclient.sendMessage(order); } //调用者轮询支付结果 public void checkOrder(orderId,firstQueryTime) throws PayException{ currentTime = System.currentTime if (currentTime > firstQueryTime + 2min) { //轮询2分钟还没有获得结果,终止轮询,订单等待支付服务自动处理 } order = getOrderbyId(orderId) if (order == null) { throw new PayException("order not exist") } if (order.isPaying) { //如果是支付中结束本次查询 } else if (order.isPaySuccess) { //支付成功.... //终止轮询 } else if (order.isPayFail) { //支付失败.... //终止轮询 } }
//当接收到请求扫码支付时调用。扫码返回结果可能有多种,需要一一判断,根据不同条件来决定继续查询或者是撤销支付或支付成功、失败 public void processScanPay(message){ try{ result = wechat.scanPay if (result == userpaying) { send query memsage into MQ 延迟3s接收消息 delete message return } else if (result == error){ send cancel message into MQ delete message return } else if (result == duplicate pay){ send paySuccess message into MQ delete message return } else if (result == paySuccess){ send paySuccess message into MQ delete message return } //其他情况不举例了,根据实际情况来发送查询或者撤销消息 } catch(Exception e){ send query memsage into MQ 延迟3s接收消息 delete message } } //当支付结果不明确或状态为支付中、用户正在输入密码时调用。微信支付有可能不会立刻返回支付结果,例如用户需要输入密码等情况,就需要多次查询才可以获得结果,但是查询也需要有限制,例如2分钟如果超过,就强制撤销支付,这样就不会造成支付单边 public void processQueryPay(message){ orderId = message.orderId firstTime = message.firstTime currentTime = time.currentTime if (currentTime - firstTime > 2min) { send cancel message into MO delete message return } result = wechat.query(orderId) if (result == userpaying) { //用户还在输入密码,或者微信正在跟银行交互 send query memsage into MQ 延迟3s接收消息 delete message return } else if (result == paySuccess) { //支付成功是最终结果 send success message into MQ delete message return } else if (result == payFail) { //支付失败,是最终结果 send fail message into MQ delete message return } else if (result == unknown) { //微信系统异常经常遇到,因此对未知结果进行再次查询处理 send query memsage into MQ 延迟3s接收消息 delete message return } else if (result == orderNotExist) { //为什么订单会不存在,刚才明明发起了支付?原因就是发起支付后,如果timeout,请求有可能没有被微信处理或者根本没有接受到请求 send cancel message into MO delete message return } //其他情况不举例了 } //接收到支付结果时调用 public void processPay(paySuccess){ if (paySuccess) { beginTransaction lock order order.makePaySuccess commit } else{ beginTransaction lock order order.makePayFail commit } delete message } //撤销支付,直到撤销成功才删除消息,否则有可能出现支付单边 public void processCancel(){ orderId=message.orderId wechat.cancel(orderId) delete message }
public void cancelOrder(orderId)throw CancelException{ beginTransaction order = db.getOrderId(orderId) if (order == null) { throw CancelException("order not exist") } order.makeCancel commit }
//根据订单号进行支付撤销,不管订单是否存在,只要不是关闭或者失败状态,都可以进行撤销 public void cancelOrder(orderId)throws CancelException{ beginTransaction order = db.getOrderId(orderId) if (order != null) { if (order.isPayFail) { throw new CancelException("order is fail"); } else if (order.isClosed) { throw new CancelException("order is fail"); } ....... order.makeCancel//把钱退回 } makeCancelByOrderId(orderId) commit }
那么 makeCancelByOrderId(orderId) 里面做了什么呢,这里大致的表结构如下 table:tb_order_cancel{ orderId varchar(50), //orderId是唯一索引或主键 createTime bigint } //记录了被撤销的订单号 public void makeCancelByOrderId(orderId){ try{ insert into tb_order_cancel value(orderId,createTime); }catch(DuplicateKeyException ignore){ //ignore exception } } 支付请求到达时,会先判断是否订单号已经被撤销,如果被撤销,就不再进行支付 public order scanPay(orderId,amount) throws PayException{ cancelOrder = select from tb_order_cancel if (cancelOrder != null) { //说明被撤销了,不应该完成支付 throw new PayException("order was canceled") } do pay ...... }
标签:崩溃 前端 ble close ret 订单号 sed 支付宝 tran
原文地址:http://www.cnblogs.com/akwei/p/6783862.html