# -*- coding: utf-8 -*- # !/usr/bin/env python import inspect from .api.base import BaseWeChatAPI from ..type import WeChatPayType, SignType def _is_api_endpoint(obj): return isinstance(obj, BaseWeChatAPI) class BaseWechatClient(object): def __new__(cls, *args, **kwargs): self = super(BaseWechatClient, cls).__new__(cls) api_endpoints = inspect.getmembers(self, _is_api_endpoint) for name, api in api_endpoints: api_cls = type(api) api = api_cls(self) setattr(self, name, api) return self def __init__(self, appid, mchid, private_key, cert_serial_no, apiv3_key, certificate_str_list = None, notify_url = None, wechatpay_type = WeChatPayType.MINIPROG, logger = None, partner_mode = False, proxy = None): """ :param mchid: 直连商户号,示例值:'1230000109' :param private_key: 商户证书私钥,示例值:'MIIEvwIBADANBgkqhkiG9w0BAQE...' :param cert_serial_no: 商户证书序列号,示例值:'444F4864EA9B34415...' :param appid: 应用ID,示例值:'wxd678efh567hg6787' :param apiv3_key: 商户APIv3密钥,示例值:'a12d3924fd499edac8a5efc...' :param wechatpay_type: 微信支付类型,示例值:WeChatPayType.MINIPROG :param notify_url: 通知地址,示例值:'https://www.weixin.qq.com/wxpay/pay.php' :param logger: 日志记录器,示例值logging.getLoger('demo') :param partner_mode: 接入模式,默认False为直连商户模式,True为服务商模式 :param proxy: 代理设置,示例值:{"https": "http://10.10.1.10:1080"} """ from ..core import Core self._type = wechatpay_type self._mchid = mchid self._appid = appid self._notify_url = notify_url self._core = Core(mchid = self._mchid, cert_serial_no = cert_serial_no, private_key = private_key, apiv3_key = apiv3_key, certificate_str_list = certificate_str_list, logger = logger, proxy = proxy) self._partner_mode = partner_mode @property def appid(self): return self._appid @property def mchid(self): return self._mchid @property def core(self): return self._core def sign(self, data, sign_type = SignType.RSA_SHA256): """使用RSAwithSHA256或HMAC_256算法计算签名值供调起支付时使用 :param data: 需要签名的参数清单 :微信支付订单采用RSAwithSHA256算法时,示例值:['wx888','1414561699','5K8264ILTKCH16CQ2502S....','prepay_id=wx201410272009395522657....'] :微信支付分订单采用HMAC_SHA256算法时,示例值:{'mch_id':'1230000109','service_id':'88888888000011','out_order_no':'1234323JKHDFE1243252'} """ return self._core.sign(data, sign_type) def decrypt_callback(self, headers, body): """解密回调接口收到的信息,仅返回resource解密后的参数字符串,此接口为兼容旧版本而保留,建议调用callback() :param headers: 回调接口收到的headers :param body: 回调接口收到的body """ return self._core.decrypt_callback(headers, body) def callback(self, headers, body): """解密回调接口收到的信息,返回所有传入的参数 :param headers: 回调接口收到的headers :param body: 回调接口收到的body """ return self._core.callback(headers, body) def decrypt(self, ciphtext): """解密微信支付平台返回的信息中的敏感字段 :param ciphtext: 加密后的敏感字段,示例值:'Qe41VhP/sGdNeTHMQGlxCWiUyHu6XNO9GCYln2Luv4HhwJzZBfcL12sB+PgZcS5NhePBog30NgJ1xRaK+gbGDKwpg==' """ return self._core.decrypt(ciphtext)