コード例 #1
0
ファイル: cron2.py プロジェクト: dperezrada/payparrot
def process_payment(db, raw_message, payment_message):
    parrot = Parrots.findOne(db, {'_id': ObjectId(payment_message.get('parrot_id'))})
    if parrot:
        message = get_message_to_share(db, payment_message)
        twitter_json = tweet_message(parrot, message, raw_message.id)
        payment = store_payment(db, twitter_json, payment_message, message, raw_message)
        if payment.success:
            if Queue.delete_message('payments', raw_message):
                log('cron2', 'Payments succeded', payment_message.get('subscription_id'))
                send_notification(db, payment, 'payment_success')
                log('cron2', 'Notification sent', payment_message.get('subscription_id'))
                create_next_payment(db, payment)
                log('cron2', 'Next payment created', payment_message.get('subscription_id'))
                add_payment_to_parrot(db, parrot, payment_message, twitter_json)
            else:
                log('cron2', 'ERROR: Couldnt delete message', payment_message.get('subscription_id'))
        else:
            total_payments_attempts = Payments.find(db, {'message_id_sqs': raw_message.id}).count()
            subscription = Subscriptions.findOne(db, {'account_id': payment.account_id, 'parrot_id': payment.parrot_id})
            if subscription:
                if total_payments_attempts > 3:
                    log('cron2', 'Too many payments attempts', payment_message.get('subscription_id'))
                    subscription.update({'active': False})
                    if Queue.delete_message('payments', raw_message):
                        send_notification(db, payment,'subscription_deactivated')
                    else:
                        log('cron2', 'ERROR: Couldnt delete message', payment_message.get('subscription_id'))
                else:
                    log('cron2', 'Attempt %s' % total_payments_attempts, payment_message.get('subscription_id'))
コード例 #2
0
ファイル: cron3.py プロジェクト: dperezrada/payparrot
def notify(db, notification_raw, notification_message):
    notification = Notifications.findOne(db, notification_message.get('notification_id'))
    account = Accounts.findOne(db, notification.account_id)
    if notification:
        log('cron3', 'Notifying remote customer', notification_message.get('subscription_id'))
        if not notification_message.get('type') in VALID_NOTIFICATIONS:
            log('cron3', 'ERROR: Unknown notification', notification_message.get('subscription_id'))
            # TODO: Check what to do in this case
            return
        if notification.request_url:
            query_data = {
                'subscription_id': str(notification.subscription_id),
                'account_id': str(notification.account_id),
                'parrot_id': str(notification.parrot_id),
                'type': notification_message.get('type'),
                'external_id': str(notification.external_id),
                'notification_id': str(notification.id),
            }
            log('cron3', 'Notification URL: %s' % notification.request_url, notification_message.get('subscription_id'))
            utf8_query_data = dict([(key,val.encode('utf-8')) for key, val in query_data.items() if isinstance(val, basestring)])
            delete_message = False
            try:
                if account.notification_active:
                    http_client = Http()
                    headers, body = http_client.request(uri = notification.request_url, body = urlencode(utf8_query_data), method = 'POST')
                    if int(headers.status) >= 200 and int(headers.status) < 300:
                        notification.update({
                            'response_status': headers.status,
                            'response_headers': headers,
                            'response_body': body,
                            'status': 'sent'
                        })
                        log('cron3', 'Remote notification succeded', notification_message.get('subscription_id'))
                    else:
                        log('cron3', "Failed. Notification response not 2XX (received %s) from url %s" % (
                            headers.status,
                            notification.request_url
                        ), notification_message.get('subscription_id'))
                else:
                    delete_message = True
                    notification.update({
                            'status': 'off'
                    })
                if delete_message:
                    Queue.delete_message('notifications', notification_raw)

            except Exception, e:
                log('cron3', "Failed. Exception %specified" % e)
        else:
            log('cron3', 'ERROR: No remote url specified', notification_message.get('subscription_id'))
コード例 #3
0
ファイル: __init__.py プロジェクト: dperezrada/payparrot
def tear_down(db, app=None, queue = False):
    db.accounts.drop()
    db.messages.drop()
    db.accounts_sessions.drop()
    db.subscriptions.drop()
    db.sessions.drop()
    db.parrots.drop()
    db.notifications.drop()
    db.payments.drop()
    if app:
        app.get('/logout')
    if queue:
        sleep(1)
        for queue_name in ['notifications', 'payments']:
            queue = Queue.get_queue(queue_name)
            while queue.count():
                for message in queue.get_messages(num_messages=10, visibility_timeout=60):
                    Queue.delete_message(queue_name, message)