jdaggre.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import logging
  4. from typing import TYPE_CHECKING, Optional, Dict, Union
  5. import simplejson as json
  6. from apilib.monetary import RMB, Percent
  7. from apps.web.constant import AppPlatformType, PARTITION_ROLE
  8. from apps.web.core.payment.base import PaymentGateway
  9. from apps.web.utils import testcase_point
  10. from library.jd import JDAggrePay
  11. from library.jd.pay import GatewayMethod
  12. if TYPE_CHECKING:
  13. from apps.web.core.models import JDAggrePayApp
  14. logger = logging.getLogger(__name__)
  15. class JDAggrePaymentGateway(PaymentGateway, JDAggrePayMixin):
  16. def __init__(self, app, gateway_type): # type: (JDAggrePayApp, AppPlatformType)->None
  17. super(JDAggrePaymentGateway, self).__init__(app)
  18. self.__gateway_type__ = gateway_type
  19. def __repr__(self):
  20. return '<JDAggrePaymentGateway(occupantId = %s, merchant_no = %s, debug = %s)>' % (
  21. str(self.occupantId), self.merchant_no, self.debug)
  22. @property
  23. def __client__(self):
  24. # type: ()->JDAggrePay
  25. return JDAggrePay(self.merchant_no, self.desKey, self.saltMd5Key, self.systemId, self.debug)
  26. def _to_fen(self, rmb):
  27. # type: (RMB)->long
  28. return int(rmb.amount * 100)
  29. def create_pay_url(self, order_no, money, notify_url, pi_type, subject, attach = None):
  30. # type: (str, RMB, str, str, str, Optional[dict])->str
  31. return self.client.create_pay_url(
  32. out_trade_no = order_no,
  33. total_fee = self._to_fen(money),
  34. notify_url = notify_url,
  35. piType = pi_type,
  36. tradeName = subject,
  37. returnParams = json.dumps(attach, separators = (',', ':')) if attach else '')
  38. def decrypt_response(self, payload):
  39. return self.client.decrypt_response(payload)
  40. def unified_order(self, out_trade_no, money, notify_url, subject = u'充值', expire = 300, attach = None,
  41. openId = None, gatewayMethod = GatewayMethod.SUBSCRIPTION, **kwargs):
  42. # type: (str, RMB, str, basestring, int, Optional[Dict], Optional[str], str, Dict)->Union[str, Dict]
  43. return self.client.unified_order(piType = self.pi_type, out_trade_no = out_trade_no,
  44. total_fee = str(self._to_fen(money)),
  45. notify_url = notify_url, productName = subject, expire = expire,
  46. returnParams = json.dumps(attach, separators = (',', ':')) if attach else '',
  47. client_ip = '127.0.0.1', openId = openId, gatewayMethod = gatewayMethod,
  48. **kwargs)
  49. def api_trade_query(self, out_trade_no = None, trade_no = None):
  50. # type:(Optional[str], Optional[str])->dict
  51. """
  52. 查询订单
  53. :param out_trade_no: 商户订单号
  54. :param trade_no: 微信订单号
  55. :return:
  56. """
  57. return self.client.api_trade_query(out_trade_no = out_trade_no, trade_no = trade_no)
  58. @testcase_point()
  59. def refund_to_user(self, out_trade_no, out_refund_no, refund_fee, total_fee, refund_reason, **kwargs):
  60. # type:(str, str, RMB, RMB, str, dict)->dict
  61. """
  62. :param out_trade_no:
  63. :param out_refund_no:
  64. :param refund_fee:
  65. :param total_fee:
  66. :param refund_reason:
  67. :return:
  68. """
  69. return self.client.api_trade_refund(outTradeNo = out_trade_no,
  70. outRefundNo = out_refund_no,
  71. amount = self._to_fen(refund_fee),
  72. **kwargs)
  73. def api_refund_query(self, out_refund_no = None, refund_no = None):
  74. return self.client.api_refund_query(out_refund_no=out_refund_no)