12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- # coding=utf-8
- import datetime
- import logging
- from typing import TYPE_CHECKING
- from apps.web.exceptions import PostPayOrderError
- if TYPE_CHECKING:
- from apps.web.user.models import RechargeRecord
- from apps.web.user.utils2 import OrderContext
- logger = logging.getLogger(__name__)
- class PostPay(object):
- def __init__(self, order): # type: (RechargeRecord)-> None
- self._order = order
- def do(self): # type:() -> None
- try:
- with PostPayOrderContextManager() as manager:
- context = manager.init_context(order=self._order) # type: OrderContext
- if not context.terminal:
- logger.warning('[PostPay] {} check failure. terminal is not exist.'.format(self._order))
- return
- if not context.terminal.owner:
- logger.warning('[PostPay] {} check failure. owner is not exist.'.format(self._order))
- return
- if self._order.ownerId != context.terminal.ownerId:
- logger.warning('[PostPay] {} check failure. ownerId of records != ownerId of group.'.format(self._order))
- return
- if not context.user:
- logger.warning('[PostPay] {} check failure. openid is null.'.format(self._order))
- return
- diff_ts = (datetime.datetime.now() - self._order.dateTimeAdded).total_seconds()
- logger.debug(
- '[PostPay] {} in ({}) via={} attachParas={} record={} diff={}'.format(
- repr(self._order.user), self._order.gateway, self._order.via, self._order.attachParas, self._order, diff_ts)
- )
- context.goods.attach_to(self._order, self._order.user)
- except PostPayOrderError as pe:
- # 此错误一定是attach_to 执行成功之前发送 此时用户尚未获取商品 所以可执行金额退还的操作
- logger.error('[PostPay] do, order = {}, error = {}'.format(self._order, pe))
- # TODO zjl 执行退款操作 并将订单设置为关闭状态
- return
- # 执行统一的后续流程
- self.deal_with_order()
- def deal_with_order(self):
- """
- TODO 处理订单的后续问题 订单总金额累加 发送金额通知等
- """
- def post_pay(order):
- logger.info('[post_pay] post pay order = {}'.format(order))
- PostPay(order).do()
|