dlb.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import logging
  4. from collections import OrderedDict
  5. from typing import TYPE_CHECKING, Optional
  6. import simplejson as json
  7. from apps.web.constant import AppPlatformType
  8. from apps.web.core import DlbMixin
  9. from apps.web.core.payment.base import PaymentGateway
  10. from library.dlb import DlbPay
  11. if TYPE_CHECKING:
  12. from apps.web.core.models import DlbPayApp
  13. logger = logging.getLogger(__name__)
  14. class DlbPaymentGateway(PaymentGateway, DlbMixin):
  15. def __init__(self, app, gateway_type): # type: (DlbPayApp, AppPlatformType)->None
  16. super(DlbPaymentGateway, self).__init__(app)
  17. self.__gateway_type__ = gateway_type
  18. def __repr__(self):
  19. return \
  20. '<DlbPaymentGateway(occupantId = %s, merchant_no = %s, shop_no = %s, debug = %s)>' % (
  21. str(self.occupantId), self.merchant_no, self.shop_no, self.debug)
  22. @property
  23. def __client__(self):
  24. # type: ()->DlbPay
  25. return DlbPay(self.merchant_no, self.shop_no, self.access_key, self.secret_key, self.machine_no, self.debug)
  26. def create_pay_url(self, order_no, money, notify_url, front_url = '', attach = None):
  27. return self.client.create_pay_url(
  28. requestNum = order_no,
  29. amount = str(money),
  30. notify_url = notify_url,
  31. front_url = front_url,
  32. extraInfo = json.dumps(attach, separators = (',', ':')) if attach else '')
  33. def api_trade_query(self, out_trade_no = None, trade_no = None):
  34. # type:(Optional[str], Optional[str])->dict
  35. """
  36. 查询订单
  37. :param out_trade_no: 商户订单号
  38. :param trade_no: 微信订单号
  39. :return:
  40. """
  41. assert out_trade_no or trade_no, 'must input out_trade_no or trade_no'
  42. return self.client.query_trade_result(trade_no = trade_no, out_trade_no = out_trade_no)
  43. def check(self, data, token):
  44. # type: (dict, str)->bool
  45. sign_data = OrderedDict()
  46. sign_data['secretKey'] = self.secret_key
  47. sign_data['timestamp'] = data['timestamp']
  48. re_token = self.client.token(sign_data)
  49. return re_token == token