示例#1
0
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'))