Example #1
0
 def _upload_image(self, appid, ticket, picurl, filename, width=0):
     img = Image.open(io.BytesIO(urllib.request.urlopen(picurl).read()))
     w, h = img.size
     nw = min(width, w) if width != 0 else w
     nh = int(float(nw * h) / w)
     tmp_file = '/tmp/' + security.nonce_str()
     fd = open(tmp_file, 'w')
     img.resize((nw, nh)).convert('RGB').save(fd, 'JPEG', quality=85)
     fd.close()
     fd = open(tmp_file, 'rb')
     post_url = httputils.build_url(
         url.mp_upload, {
             'ticket_id': 'sevengram',
             'ticket': ticket,
             'f': 'json',
             'token': self.tokens[appid]['token'],
             'lang': 'zh_CN',
             'writetype': 'doublewrite',
             'group_id': 1,
             'action': 'upload_material'
         })
     referer = httputils.build_url(
         url.mp_appmsg, {
             't': 'media/appmsg_edit',
             'action': 'edit',
             'type': '10',
             'isMul': 0,
             'isNew': 1,
             'lang': 'zh_CN',
             'token': self.tokens[appid]['token']
         })
     content_type, data = httputils.encode_multipart_formdata(
         fields={
             'Filename': filename,
             'folder': '/cgi-bin/uploads',
             'Upload': 'Submit Query'
         },
         files=(filename, fd.read()))
     fd.close()
     os.remove(tmp_file)
     try:
         resp = yield self._post_data(appid,
                                      post_url,
                                      data,
                                      content_type,
                                      referer=referer)
     except tornado.httpclient.HTTPError:
         raise tornado.gen.Return({'err_code': 7000})
     raise tornado.gen.Return(_parse_mp_resp(resp))
Example #2
0
 def _upload_image(self, appid, ticket, picurl, filename, width=0):
     img = Image.open(io.BytesIO(urllib.request.urlopen(picurl).read()))
     w, h = img.size
     nw = min(width, w) if width != 0 else w
     nh = int(float(nw * h) / w)
     tmp_file = '/tmp/' + security.nonce_str()
     fd = open(tmp_file, 'w')
     img.resize((nw, nh)).convert('RGB').save(fd, 'JPEG', quality=85)
     fd.close()
     fd = open(tmp_file, 'rb')
     post_url = httputils.build_url(url.mp_upload, {
         'ticket_id': 'sevengram',
         'ticket': ticket,
         'f': 'json',
         'token': self.tokens[appid]['token'],
         'lang': 'zh_CN',
         'writetype': 'doublewrite',
         'group_id': 1,
         'action': 'upload_material'})
     referer = httputils.build_url(url.mp_appmsg, {
         't': 'media/appmsg_edit',
         'action': 'edit',
         'type': '10',
         'isMul': 0,
         'isNew': 1,
         'lang': 'zh_CN',
         'token': self.tokens[appid]['token']})
     content_type, data = httputils.encode_multipart_formdata(
         fields={'Filename': filename,
                 'folder': '/cgi-bin/uploads',
                 'Upload': 'Submit Query'},
         files=(filename, fd.read()))
     fd.close()
     os.remove(tmp_file)
     try:
         resp = yield self._post_data(appid, post_url, data, content_type, referer=referer)
     except tornado.httpclient.HTTPError:
         raise tornado.gen.Return({'err_code': 7000})
     raise tornado.gen.Return(_parse_mp_resp(resp))
Example #3
0
    def post(self, siteid):
        parse_args = self.assign_arguments(
            essential=['appid',
                       'title',
                       'out_trade_no',
                       'total_fee',
                       'spbill_create_ip',
                       'trade_type'],
            extra=[('detail', ''),
                   ('unionid', ''),
                   ('openid', '')]
        )
        if not parse_args.get('unionid') and not parse_args.get('openid'):
            raise tornado.web.HTTPError(400)

        req_data = dtools.transfer(
            parse_args,
            copys=['appid',
                   'out_trade_no',
                   'detail',
                   'total_fee',
                   'spbill_create_ip',
                   'trade_type',
                   'openid'],
            renames=[('title', 'body')]
        )
        if not req_data.get('openid'):
            req_data['openid'] = self.storage.get_user_info(appid=parse_args['appid'],
                                                            unionid=parse_args['unionid'],
                                                            select_key='openid')
        appinfo = self.storage.get_app_info(appid=req_data['appid'])
        if not appinfo:
            self.send_response(err_code=3201)
            raise tornado.gen.Return()

        req_data.update(
            {
                'attach': 'siteid=' + siteid,
                'mch_id': appinfo.get('mch_id'),
                'notify_url': self.storage.get_site_info(siteid, select_key='pay_notify_url')
            }
        )
        req_key = appinfo['apikey']
        security.add_sign(req_data, req_key)

        try:
            resp = yield httputils.post_dict(url=url.mch_order_add, data=req_data, data_type='xml')
        except tornado.httpclient.HTTPError:
            self.send_response(err_code=1001)
            raise tornado.gen.Return()

        resp_data = self.parse_payment_resp(resp, req_key)
        if resp_data:
            real_sign_data = {
                'appId': resp_data['appid'],
                'timeStamp': str(int(time.time())),
                'nonceStr': security.nonce_str(),
                'package': 'prepay_id=' + resp_data['prepay_id'],
                'signType': 'MD5'
            }
            post_resp_data = {
                'appid': real_sign_data['appId'],
                'timestamp': real_sign_data['timeStamp'],
                'noncestr': real_sign_data['nonceStr'],
                'prepay_id': resp_data['prepay_id'],
                'sign_type': real_sign_data['signType'],
                'pay_sign': security.build_sign(real_sign_data, req_key)
            }
            self.send_response(post_resp_data)
Example #4
0
    def post(self, siteid):
        parse_args = self.assign_arguments(essential=[
            'appid', 'title', 'out_trade_no', 'total_fee', 'spbill_create_ip',
            'trade_type'
        ],
                                           extra=[('detail', ''),
                                                  ('unionid', ''),
                                                  ('openid', '')])
        if not parse_args.get('unionid') and not parse_args.get('openid'):
            raise tornado.web.HTTPError(400)

        req_data = dtools.transfer(parse_args,
                                   copys=[
                                       'appid', 'out_trade_no', 'detail',
                                       'total_fee', 'spbill_create_ip',
                                       'trade_type', 'openid'
                                   ],
                                   renames=[('title', 'body')])
        if not req_data.get('openid'):
            req_data['openid'] = self.storage.get_user_info(
                appid=parse_args['appid'],
                unionid=parse_args['unionid'],
                select_key='openid')
        appinfo = self.storage.get_app_info(appid=req_data['appid'])
        if not appinfo:
            self.send_response(err_code=3201)
            raise tornado.gen.Return()

        req_data.update({
            'attach':
            'siteid=' + siteid,
            'mch_id':
            appinfo.get('mch_id'),
            'notify_url':
            self.storage.get_site_info(siteid, select_key='pay_notify_url')
        })
        req_key = appinfo['apikey']
        security.add_sign(req_data, req_key)

        try:
            resp = yield httputils.post_dict(url=url.mch_order_add,
                                             data=req_data,
                                             data_type='xml')
        except tornado.httpclient.HTTPError:
            self.send_response(err_code=1001)
            raise tornado.gen.Return()

        resp_data = self.parse_payment_resp(resp, req_key)
        if resp_data:
            real_sign_data = {
                'appId': resp_data['appid'],
                'timeStamp': str(int(time.time())),
                'nonceStr': security.nonce_str(),
                'package': 'prepay_id=' + resp_data['prepay_id'],
                'signType': 'MD5'
            }
            post_resp_data = {
                'appid': real_sign_data['appId'],
                'timestamp': real_sign_data['timeStamp'],
                'noncestr': real_sign_data['nonceStr'],
                'prepay_id': resp_data['prepay_id'],
                'sign_type': real_sign_data['signType'],
                'pay_sign': security.build_sign(real_sign_data, req_key)
            }
            self.send_response(post_resp_data)