# -*- coding: utf-8 -*- # !/usr/bin/env python import logging from typing import TYPE_CHECKING, Optional from apilib.monetary import RMB from apps.web.constant import AppPlatformType from apps.web.core import YsMixin from apps.web.core.payment.base import PaymentGateway from library.ys import YsPayException, YsErrorCode from library.ys.yspay import YsPay if TYPE_CHECKING: from apps.web.core.models import YsPayApp logger = logging.getLogger(__name__) class YsPaymentGateway(PaymentGateway, YsMixin): def __init__(self, app, gateway_type): # type: (YsPayApp, AppPlatformType)->None super(YsPaymentGateway, self).__init__(app) self.__gateway_type__ = gateway_type def __repr__(self): return ''.format( self.channel_id, self.mer_id, self.term_id, self.debug) @property def __client__(self): # type: ()->YsPayApp return YsPay(self.channel_id, self.mer_id, self.term_id, self.work_key, self.debug) def _to_fen(self, rmb): # type: (RMB)->long return int(rmb.amount * 100) def unified_order(self, out_trade_no, money, openId, notify_url, subject, **data): pay_opt = self.GATEWAY_TYPE_TO_OPT.get(self.gateway_type) if not pay_opt: raise YsPayException(errCode = YsErrorCode.MY_INVALID_PARAMETER, errMsg = u'不支持的支付类型') return self.client.unified_order( pay_opt = pay_opt, openId = openId, out_trade_no = out_trade_no, total_fee = self._to_fen(money), subject = subject, notify_url = notify_url, **data) def api_trade_query(self, out_trade_no = None, trade_no = None): # type:(Optional[str], Optional[str])->dict """ 查询订单 :param out_trade_no: 商户订单号 :param trade_no: 微信订单号 :return: """ return self.client.api_trade_query(out_trade_no = out_trade_no, trade_no = trade_no)