Example #1
0
    def xml2dict(self, xml):
        sign, params = xml_to_dict(xml)
        if not sign or not params:
            raise ValueError('Convert xml to dict failed, xml: [%s]' % xml)

        if params['appid'] != self.app_id or params['mch_id'] != self.mch_id:
            raise ValueError(
                'Invalid appid or mch_id, appid: [%s], mch_id: [%s]' %
                (params['appid'], params['mch_id']))

        if params['return_code'] != 'SUCCESS':
            raise ValueError(
                'WeChat proccess request failed, return code: [%s], return msg: [%s]'
                % (params['return_code'], params.get('return_msg', '')))

        calc_sign = sign_url(params,
                             self.api_key,
                             key_name='key',
                             upper_case=True)
        if calc_sign != sign:
            raise ValueError('Invalid sign, calculate sign: [%s], sign: [%s]' %
                             (calc_sign, sign))

        if params['result_code'] != 'SUCCESS':
            logger.error(
                'WeChat process request failed, result_code: [%s], err_code: [%s], err_code_des: [%s]'
                % (params['result_code'], params.get(
                    'err_code', ''), params.get('err_code_des', '')))
        return params
Example #2
0
    def post(self, body, out_trade_no, total_fee, spbill_create_ip, notify_url,
             open_id, url):
        # 直接调用基类的post方法查询prepay_id,如果成功,返回一个字典
        unified_order = super(JsAPIOrderPay, self)._post(body,
                                                         out_trade_no,
                                                         total_fee,
                                                         spbill_create_ip,
                                                         notify_url,
                                                         open_id=open_id)
        nonce_str = random_str(length=32)
        time_stamp = time.time()

        pay_params = {
            'appId': self.app_id,
            'timeStamp': '%d' % time_stamp,
            'nonceStr': nonce_str,
            'package': 'prepay_id=%s' % unified_order.get('prepay_id'),
            'signType': 'MD5'
        }
        pay_params['paySign'] = sign_url(pay_params,
                                         self.api_key,
                                         key_name='key',
                                         upper_case=True)

        unified_order.update({
            'pay_params':
            pay_params,
            'config_params':
            get_js_config_params(url, nonce_str, time_stamp)
        })

        return unified_order
Example #3
0
    def get_js_config_params(self, url, nonce_str, time_stamp):
        """
        获取js_config初始化参数
        """
        params = {'noncestr': nonce_str,
                  'jsapi_ticket': self.get_jsapi_ticket(),
                  'timestamp': '%d' % time_stamp,
                  'url': url}

        # params['signature'] = calculate_sign(params, sign_type='sha1',
        # upper_case=False)
        params['signature'] = sign_url(params, '', sign_type='sha1')
        return params
Example #4
0
    def get_js_config_params(self, url, nonce_str, time_stamp):
        """
        获取js_config初始化参数
        """
        params = {
            'noncestr': nonce_str,
            'jsapi_ticket': self.get_jsapi_ticket(),
            'timestamp': '%d' % time_stamp,
            'url': url
        }

        # params['signature'] = calculate_sign(params, sign_type='sha1',
        # upper_case=False)
        params['signature'] = sign_url(params, '', sign_type='sha1')
        return params
Example #5
0
    def post(self, body, out_trade_no, total_fee, spbill_create_ip, notify_url, open_id, url):
        # 直接调用基类的post方法查询prepay_id,如果成功,返回一个字典
        unified_order = super(JsAPIOrderPay, self)._post(body, out_trade_no, total_fee, spbill_create_ip,
                                                         notify_url, open_id=open_id)
        nonce_str = random_str(length=32)
        time_stamp = time.time()

        pay_params = {'appId': self.app_id,
                      'timeStamp': '%d' % time_stamp,
                      'nonceStr': nonce_str,
                      'package': 'prepay_id=%s' % unified_order.get('prepay_id'),
                      'signType': 'MD5'}
        pay_params['paySign'] = sign_url(
            pay_params, self.api_key, key_name='key', upper_case=True)

        unified_order.update({'pay_params': pay_params,
                              'config_params': get_js_config_params(url, nonce_str, time_stamp)})

        return unified_order
Example #6
0
    def xml2dict(self, xml):
        sign, params = xml_to_dict(xml)
        if not sign or not params:
            raise ValueError('Convert xml to dict failed, xml: [%s]' % xml)

        if params['appid'] != self.app_id or params['mch_id'] != self.mch_id:
            raise ValueError('Invalid appid or mch_id, appid: [%s], mch_id: [%s]' % (params['appid'],
                                                                                     params['mch_id']))

        if params['return_code'] != 'SUCCESS':
            raise ValueError('WeChat proccess request failed, return code: [%s], return msg: [%s]' %
                             (params['return_code'], params.get('return_msg', '')))

        calc_sign = sign_url(
            params, self.api_key, key_name='key', upper_case=True)
        if calc_sign != sign:
            raise ValueError(
                'Invalid sign, calculate sign: [%s], sign: [%s]' % (calc_sign, sign))

        if params['result_code'] != 'SUCCESS':
            logger.error('WeChat process request failed, result_code: [%s], err_code: [%s], err_code_des: [%s]' %
                         (params['result_code'], params.get('err_code', ''), params.get('err_code_des', '')))
        return params
Example #7
0
 def dict2xml(self, params, with_sign=True):
     sign = sign_url(
         params, self.api_key, key_name='key', upper_case=True) if with_sign else None
     return dict_to_xml(params, sign)
Example #8
0
 def dict2xml(self, params, with_sign=True):
     sign = sign_url(params, self.api_key, key_name='key',
                     upper_case=True) if with_sign else None
     return dict_to_xml(params, sign)