ys.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import logging
  4. from typing import TYPE_CHECKING, Optional
  5. from apilib.monetary import RMB
  6. from apps.web.constant import AppPlatformType
  7. from apps.web.core import YsMixin
  8. from apps.web.core.payment.base import PaymentGateway
  9. from library.ys import YsPayException, YsErrorCode
  10. from library.ys.yspay import YsPay
  11. if TYPE_CHECKING:
  12. from apps.web.core.models import YsPayApp
  13. logger = logging.getLogger(__name__)
  14. class YsPaymentGateway(PaymentGateway, YsMixin):
  15. def __init__(self, app, gateway_type): # type: (YsPayApp, AppPlatformType)->None
  16. super(YsPaymentGateway, self).__init__(app)
  17. self.__gateway_type__ = gateway_type
  18. def __repr__(self):
  19. return '<YsPaymentGateway(channelid = {}, merid = {}, termid = {}, debug = {})>'.format(
  20. self.channel_id, self.mer_id, self.term_id, self.debug)
  21. @property
  22. def __client__(self):
  23. # type: ()->YsPayApp
  24. return YsPay(self.channel_id, self.mer_id, self.term_id, self.work_key, self.debug)
  25. def _to_fen(self, rmb):
  26. # type: (RMB)->long
  27. return int(rmb.amount * 100)
  28. def unified_order(self, out_trade_no, money, openId, notify_url, subject, **data):
  29. pay_opt = self.GATEWAY_TYPE_TO_OPT.get(self.gateway_type)
  30. if not pay_opt:
  31. raise YsPayException(errCode = YsErrorCode.MY_INVALID_PARAMETER, errMsg = u'不支持的支付类型')
  32. return self.client.unified_order(
  33. pay_opt = pay_opt,
  34. openId = openId,
  35. out_trade_no = out_trade_no,
  36. total_fee = self._to_fen(money),
  37. subject = subject,
  38. notify_url = notify_url,
  39. **data)
  40. def api_trade_query(self, out_trade_no = None, trade_no = None):
  41. # type:(Optional[str], Optional[str])->dict
  42. """
  43. 查询订单
  44. :param out_trade_no: 商户订单号
  45. :param trade_no: 微信订单号
  46. :return:
  47. """
  48. return self.client.api_trade_query(out_trade_no = out_trade_no, trade_no = trade_no)