Esempio n. 1
0
    async def _request(self, method, url_or_endpoint, **kwargs):
        if not url_or_endpoint.startswith(('http://', 'https://')):
            url = '{base}{endpoint}'.format(base=self.API_BASE_URL,
                                            endpoint=url_or_endpoint)
        else:
            url = url_or_endpoint

        params = kwargs.pop('params', {})
        params = urlencode(dict((k, to_binary(v)) for k, v in params.items()))
        url = '{0}?{1}'.format(url, params)

        data = kwargs.pop('data', {})

        if isinstance(data, dict):
            body = json.dumps(data, ensure_ascii=False)
            body = body.encode('utf-8')
            kwargs['body'] = body

        req = HTTPRequest(url, method=method, **kwargs)
        res = await self._http.fetch(req)

        if res.error is not None:
            raise WeChatOAuthException(errcode=None,
                                       errmsg=None,
                                       client=self,
                                       request=req,
                                       response=res)

        return await self._handle_result(res, method=method, url=url, **kwargs)
Esempio n. 2
0
    async def _request(self, method, url_or_endpoint, **kwargs):
        if not url_or_endpoint.startswith(('http://', 'https://')):
            api_base_url = kwargs.pop('api_base_url', self.API_BASE_URL)
            url = '{base}{endpoint}'.format(base=api_base_url,
                                            endpoint=url_or_endpoint)
        else:
            url = url_or_endpoint

        params = kwargs.pop('params', {})

        if isinstance(kwargs['params'], dict) and \
                        'component_access_token' not in params:
            params['component_access_token'] = await self.access_token()

        params = urlencode(dict((k, to_binary(v)) for k, v in params.items()))
        url = '{0}?{1}'.format(url, params)

        data = kwargs.pop('data', {})

        if isinstance(data, dict):
            kwargs['body'] = json.dumps(data)

        req = HTTPRequest(url, method=method, **kwargs)
        res = await self._http.fetch(req)
        if res.error is not None:
            raise WeChatClientException(errcode=None,
                                        errmsg=None,
                                        client=self,
                                        request=req,
                                        response=res)

        return await self._handle_result(res, method, url, **kwargs)
Esempio n. 3
0
    async def fetch_access_token(self):
        """
        获取 component_access_token
        详情请参考 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list\
        &t=resource/res_list&verify=1&id=open1419318587&token=&lang=zh_CN

        :return: 返回的 JSON 数据包
        """
        url = '{0}{1}'.format(self.API_BASE_URL,
                              '/component/api_component_token')
        return await self._fetch_access_token(url=url,
                                              data=json.dumps({
                                                  'component_appid':
                                                  self.component_appid,
                                                  'component_appsecret':
                                                  self.component_appsecret,
                                                  'component_verify_ticket':
                                                  self.component_verify_ticket
                                              }))
Esempio n. 4
0
    async def _request(self, method, url_or_endpoint, **kwargs):
        if not url_or_endpoint.startswith(('http://', 'https://')):
            api_base_url = kwargs.pop('api_base_url', self.API_BASE_URL)
            url = '{base}{endpoint}'.format(base=api_base_url,
                                            endpoint=url_or_endpoint)
        else:
            url = url_or_endpoint

        params = kwargs.pop('params', {})
        if 'access_token' not in params:
            params['access_token'] = await self.access_token()

        params = urlencode(dict((k, to_binary(v)) for k, v in params.items()))
        url = '{0}?{1}'.format(url, params)

        data = kwargs.pop('data') if 'data' in kwargs else {}

        if isinstance(data, dict):
            body = json.dumps(data, ensure_ascii=False)
            body = body.encode('utf-8')
            kwargs['body'] = body

        kwargs['request_timeout'] = kwargs.pop('timeout', self.timeout)
        result_processor = kwargs.pop('result_processor', None)
        req = HTTPRequest(url, method=method, **kwargs)
        res = await self._http.fetch(req)

        if res.error is not None:
            raise WeChatClientException(errcode=None,
                                        errmsg=None,
                                        client=self,
                                        request=req,
                                        response=res)

        return self._handle_result(res, method, url, result_processor,
                                   **kwargs)
Esempio n. 5
0
    async def add(self, media_type, media_file, title=None, introduction=None):
        """
        新增其它类型永久素材
        详情请参考
        https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html

        :param media_type: 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
        :param media_file: 要上传的文件,一个 File-object
        :param title: 视频素材标题,仅上传视频素材时需要
        :param introduction: 视频素材简介,仅上传视频素材时需要
        :return: 返回的 JSON 数据包
        """
        params = {
            'access_token': await self.access_token(),
            'type': media_type
        }
        if media_type == 'video':
            assert title, 'Video title must be set'
            assert introduction, 'Video introduction must be set'
            description = {'title': title, 'introduction': introduction}
            params['description'] = json.dumps(description)
        return await self._post('material/add_material',
                                params=params,
                                files={'media': media_file})
 def set(self, key, value, ttl=0):
     if value is None:
         return
     key = self.key_name(key)
     value = json.dumps(value)
     self.mc.set(key, value, ttl)
Esempio n. 7
0
    async def create(self,
                     trade_type,
                     body,
                     total_fee,
                     notify_url,
                     client_ip=None,
                     user_id=None,
                     out_trade_no=None,
                     detail=None,
                     attach=None,
                     fee_type='CNY',
                     time_start=None,
                     time_expire=None,
                     goods_tag=None,
                     product_id=None,
                     device_info=None,
                     limit_pay=None,
                     scene_info=None,
                     sub_user_id=None,
                     **kwargs):
        """
        统一下单接口

        :param trade_type: 交易类型,取值如下:JSAPI,NATIVE,APP,WAP, MWEB
        :param body: 商品描述
        :param total_fee: 总金额,单位分
        :param notify_url: 接收微信支付异步通知回调地址
        :param client_ip: 可选,APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP
        :param user_id: 可选,用户在商户appid下的唯一标识。trade_type=JSAPI和appid已设定,此参数必传
        :param sub_user_id: 可选,小程序appid下的唯一标识。trade_type=JSAPI和sub_appid已设定,此参数必传
        :param out_trade_no: 可选,商户订单号,默认自动生成
        :param detail: 可选,商品详情
        :param attach: 可选,附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
        :param fee_type: 可选,符合ISO 4217标准的三位字母代码,默认人民币:CNY
        :param time_start: 可选,订单生成时间,默认为当前时间
        :param time_expire: 可选,订单失效时间,默认为订单生成时间后两小时
        :param goods_tag: 可选,商品标记,代金券或立减优惠功能的参数
        :param product_id: 可选,trade_type=NATIVE,此参数必传。此id为二维码中包含的商品ID,商户自行定义
        :param device_info: 可选,终端设备号(门店号或收银设备ID),注意:PC网页或公众号内支付请传"WEB"
        :param limit_pay: 可选,指定支付方式,no_credit--指定不能使用信用卡支付
        :param scene_info: 可选,上报支付的场景信息
        :param kwargs: 其他未列举在上述参数中的统一下单接口调用参数,例如电子发票入口开放标识receipt
        :type scene_info: dict
        :return: 返回的结果数据
        """
        now = datetime.fromtimestamp(time.time(), tz=timezone('Asia/Shanghai'))
        hours_later = now + timedelta(hours=2)
        if time_start is None:
            time_start = now
        if time_expire is None:
            time_expire = hours_later
        if not out_trade_no:
            out_trade_no = '{0}{1}{2}'.format(self.mch_id,
                                              now.strftime('%Y%m%d%H%M%S'),
                                              random.randint(1000, 10000))
        if scene_info is not None:
            scene_info = json.dumps(scene_info, ensure_ascii=False)
        data = {
            'appid': self.appid,
            'sub_appid': self.sub_appid,
            'device_info': device_info,
            'body': body,
            'detail': detail,
            'attach': attach,
            'out_trade_no': out_trade_no,
            'fee_type': fee_type,
            'total_fee': total_fee,
            'spbill_create_ip': client_ip or get_external_ip(),
            'time_start': time_start.strftime('%Y%m%d%H%M%S'),
            'time_expire': time_expire.strftime('%Y%m%d%H%M%S'),
            'goods_tag': goods_tag,
            'notify_url': notify_url,
            'trade_type': trade_type,
            'limit_pay': limit_pay,
            'product_id': product_id,
            'openid': user_id,
            'sub_openid': sub_user_id,
            'scene_info': scene_info,
        }
        data.update(kwargs)
        return await self._post('pay/unifiedorder', data=data)
Esempio n. 8
0
 def set(self, key, value, ttl=None):
     if value is None:
         return
     key = self.key_name(key)
     value = json.dumps(value)
     self.redis.set(key, value, ex=ttl)