def event_callback(request): # 回调过程如果使用了加密, 则需要进行解密 # 如果没有涉及到加密,则直接使用 raw_body = request.body.decode('utf-8') try: if settings.VENDOR_AES_KEY: prpcrypt = Prpcrypt(settings.VENDOR_AES_KEY) raw_body = prpcrypt.decrypt(raw_body) body = json.loads(raw_body) except Exception as e: logging.info(e) return error_json('request data is not valid') event_id = body.get('event_id') subject = body.get('subject') if not event_id or not subject: return error_json('missing params') event, created = Event.objects.get_or_create(event_id=event_id, subject=subject, defaults={ 'content': json.dumps(body.get('payload')), }) if not created and event.status == 'PROCESSED': return error_json('already processed') handler = CallbackHandler(event) resp = handler.process_event() return resp
def process_qr_login(self): content = json.loads(self.event.content) user_id = content.get('user_id') if not user_id: return error_json('missing user id in platform event') qr_uuid = content.get('qr_uuid') if not qr_uuid: return error_json('missing qr uuid in platform event') try: qr_session = QRSession.objects.get(uuid=qr_uuid) except QRSession.DoesNotExist: return error_json('qr code not found') if qr_session.user: return error_json('qr code used') if qr_session.is_expired(): return error_json('qr code expired') else: user, user_info = self.client.fetch_user(user_id) qr_session.user = user qr_session.save() return ok_json(result='success')
def deposit_process(request): deposit_id = request.GET.get('deposit_id') if not deposit_id: return error_json('missing deposit_id') try: deposit = Deposit.objects.get(pk=deposit_id) except Deposit.DoesNotExist: return error_json('deposit request not found') if deposit.status == "SUCCESS": return ok_json(result='success') else: return ok_json(result='pending')
def request_withdraw(request): user = request.bx_user form = WithdrawForm(request.POST, user=user) if form.is_valid(): form.save() resp = 'success' else: errors = form_error_formatter(form.errors) return error_json(error=errors) return ok_json(result=resp)
def process_event(self): handler = self.get_event_handler(self.event.subject) if not handler: return error_json('unknown event %s' % self.event.subject) self.client = Client() with transaction.atomic(): resp = handler() self.event.status = 'PROCESSED' self.event.save() return resp
def process_msg(self): if self.evt.event not in ['msg', 'welcome']: return error_json('not support event: %s'%self.evt.event) if self.evt.event == 'welcome': self.welcome_msg() return ok_json() content_type = self.evt.data['content_type'] if content_type == 'event': self.process_event_msg() elif content_type == 'text': self.process_text_msg() elif content_type == 'image': self.process_image_msg() return ok_json()
def bot_login(request, bot_token): bot = Bot() expire = 60 * 60 * 24 platform_user_id = bot.valid_token(bot_token, expire=expire) if not platform_user_id: logging.warn("Invalid bot_token {} return 401".format(bot_token)) return error_json('Invalid bot token', status=401) c = Client() user = c.get_user(platform_user_id, cache=True) request.session['expire'] = int(time.time()) + expire request.session['site_userid'] = user.id site_userid = user.id return redirect(reverse('account:index'))
def qr_session(request): uuid = request.GET.get('uuid') sys_vendor_name = settings.APP_NAME if uuid: qr_info = uuid.split(':') if len(qr_info) < 2: return error_json('qr code format error') vendor_name, uuid = qr_info[:2] try: qr_session = QRSession.objects.get(uuid=uuid) except QRSession.DoesNotExist: return error_json('qr code not found') if vendor_name != sys_vendor_name: return error_json('vendor not match') elif qr_session.is_expired(): return error_json('qr code expired') elif qr_session.user: request.session['site_userid'] = qr_session.user.id return ok_json(result='success') elif qr_session.user is None: return error_json('no user found') else: return error_json('fail') else: expire_seconds = 60 * 3 qr_session = QRSession.objects.create( expired_at=utc_now() + timedelta(seconds=expire_seconds)) QR_LOGIN_URL = settings.QR_LOGIN_URL protocol = "{}/qrcode/?uuid={}:{}".format(QR_LOGIN_URL, sys_vendor_name, qr_session.uuid) if request.from_device == 'phone': protocol = "bixin://login/confirm?{}".format( urlencode({'url': protocol})) resp = { 'protocol': protocol, 'expired_at': qr_session.expired_at.isoformat() } return ok_json(result=resp)
def request_deposit(request): user = request.bx_user currency = 'BTC' client = Client() resp = client.get_vendor_address_list(currency=currency) vendor_addresses = resp.get('items', []) if len(vendor_addresses) == 0: return error_json(error='vendor does not have address for %s' % currency) vendor_address = vendor_addresses[0] deposit = Deposit.objects.create(user=user) transfer_protocal = format_transfer_protocol( target_addr=vendor_address, currency=currency, category='deposit', args={'deposit_id': deposit.id}) return ok_json(result=transfer_protocal)
def process_main(self, encrypted_data): ''' The main entry for handel the request''' evt = self._get_event(encrypted_data) if not evt: return error_json('auth failed', status=401) return self.process_msg()