def binary_decode(data, encoding='utf-8', errors='strict'): """Decodes a binary string into a text string using given encoding. Does nothing if data is already a text string (raises on unknown types). """ if isinstance(data, six.text_type): return data else: return encodeutils.safe_decode(data, incoming=encoding, errors=errors)
def _unifiedorder_xml(self, money, oid, timeline, req): overtime = timeline + self.overtime _random_str = random_string() data = { 'appId': self.appid, 'mch_id': self.mchid, 'nonceStr': _random_str, 'signType': 'MD5', 'body': '%s-充值' % encodeutils.safe_decode(self.appname), 'time_start': datetime.datetime.utcfromtimestamp(timeline).strftime('%Y%m%d%H%M%S'), 'time_expire': datetime.datetime.utcfromtimestamp(overtime).strftime('%Y%m%d%H%M%S'), 'out_trade_no': str(oid), 'fee_type': self.currency, 'total_fee': str(money*100), 'spbill_create_ip': AuthFilter.client_addr(req), 'notify_url': req.path_url + '/%d' % oid, 'trade_type': 'APP', } data['sign'] = self.sandbox_sign if self.sandbox else WeiXinApi.calculate_signature(data, self.secret) return self.dict_to_xml_string(data), _random_str
def html(self, **kwargs): _kwargs = copy.copy(kwargs) _kwargs.update({'env': 'sandbox' if self.sandbox else 'production'}) buf = HTMLTEMPLATE % _kwargs return encodeutils.safe_decode(buf, 'utf-8')
def decrypt_xml_to_dict(buf): return xmltodict.parse(encodeutils.safe_decode(buf))['xml']
def calculate_signature(params, api_key=None): url = WeiXinApi.format_url(params, api_key) return encodeutils.safe_decode(hashlib.md5(url).hexdigest().upper())
def format_url(params, api_key=None): data = [encodeutils.safe_decode('{0}={1}'.format(k, params[k])) for k in sorted(params) if params[k]] if api_key: data.append(encodeutils.safe_decode('key={0}'.format(api_key))) return b"&".join(data)