base.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import inspect
  4. from .api.base import BaseWeChatAPI
  5. from ..type import WeChatPayType, SignType
  6. def _is_api_endpoint(obj):
  7. return isinstance(obj, BaseWeChatAPI)
  8. class BaseWechatClient(object):
  9. def __new__(cls, *args, **kwargs):
  10. self = super(BaseWechatClient, cls).__new__(cls)
  11. api_endpoints = inspect.getmembers(self, _is_api_endpoint)
  12. for name, api in api_endpoints:
  13. api_cls = type(api)
  14. api = api_cls(self)
  15. setattr(self, name, api)
  16. return self
  17. def __init__(self,
  18. appid,
  19. mchid,
  20. private_key,
  21. cert_serial_no,
  22. apiv3_key,
  23. certificate_str_list = None,
  24. notify_url = None,
  25. wechatpay_type = WeChatPayType.MINIPROG,
  26. logger = None,
  27. partner_mode = False,
  28. proxy = None):
  29. """
  30. :param mchid: 直连商户号,示例值:'1230000109'
  31. :param private_key: 商户证书私钥,示例值:'MIIEvwIBADANBgkqhkiG9w0BAQE...'
  32. :param cert_serial_no: 商户证书序列号,示例值:'444F4864EA9B34415...'
  33. :param appid: 应用ID,示例值:'wxd678efh567hg6787'
  34. :param apiv3_key: 商户APIv3密钥,示例值:'a12d3924fd499edac8a5efc...'
  35. :param wechatpay_type: 微信支付类型,示例值:WeChatPayType.MINIPROG
  36. :param notify_url: 通知地址,示例值:'https://www.weixin.qq.com/wxpay/pay.php'
  37. :param logger: 日志记录器,示例值logging.getLoger('demo')
  38. :param partner_mode: 接入模式,默认False为直连商户模式,True为服务商模式
  39. :param proxy: 代理设置,示例值:{"https": "http://10.10.1.10:1080"}
  40. """
  41. from ..core import Core
  42. self._type = wechatpay_type
  43. self._mchid = mchid
  44. self._appid = appid
  45. self._notify_url = notify_url
  46. self._core = Core(mchid = self._mchid,
  47. cert_serial_no = cert_serial_no,
  48. private_key = private_key,
  49. apiv3_key = apiv3_key,
  50. certificate_str_list = certificate_str_list,
  51. logger = logger,
  52. proxy = proxy)
  53. self._partner_mode = partner_mode
  54. @property
  55. def appid(self):
  56. return self._appid
  57. @property
  58. def mchid(self):
  59. return self._mchid
  60. @property
  61. def core(self):
  62. return self._core
  63. def sign(self, data, sign_type = SignType.RSA_SHA256):
  64. """使用RSAwithSHA256或HMAC_256算法计算签名值供调起支付时使用
  65. :param data: 需要签名的参数清单
  66. :微信支付订单采用RSAwithSHA256算法时,示例值:['wx888','1414561699','5K8264ILTKCH16CQ2502S....','prepay_id=wx201410272009395522657....']
  67. :微信支付分订单采用HMAC_SHA256算法时,示例值:{'mch_id':'1230000109','service_id':'88888888000011','out_order_no':'1234323JKHDFE1243252'}
  68. """
  69. return self._core.sign(data, sign_type)
  70. def decrypt_callback(self, headers, body):
  71. """解密回调接口收到的信息,仅返回resource解密后的参数字符串,此接口为兼容旧版本而保留,建议调用callback()
  72. :param headers: 回调接口收到的headers
  73. :param body: 回调接口收到的body
  74. """
  75. return self._core.decrypt_callback(headers, body)
  76. def callback(self, headers, body):
  77. """解密回调接口收到的信息,返回所有传入的参数
  78. :param headers: 回调接口收到的headers
  79. :param body: 回调接口收到的body
  80. """
  81. return self._core.callback(headers, body)
  82. def decrypt(self, ciphtext):
  83. """解密微信支付平台返回的信息中的敏感字段
  84. :param ciphtext: 加密后的敏感字段,示例值:'Qe41VhP/sGdNeTHMQGlxCWiUyHu6XNO9GCYln2Luv4HhwJzZBfcL12sB+PgZcS5NhePBog30NgJ1xRaK+gbGDKwpg=='
  85. """
  86. return self._core.decrypt(ciphtext)