# Path: zipfelchappe/paypal/tasks.py
# def process_payments():
#     """
#     Collects the paypal payments for all successfully financed projects
#     that end within the next 24 hours.
#     """
# 
#     billable_projects = Project.objects.billable()
# 
#     # Pledges that are ready to be payed
#     processing_pledges = Pledge.objects.filter(
#         project__in=billable_projects,
#         provider='paypal',
#         status=Pledge.AUTHORIZED,
#         paypal_preapproval__status='ACTIVE',
#         paypal_preapproval__approved=True,
#     )
# 
#     for pledge in processing_pledges:
#         try:
#             process_pledge(pledge)
#         except PaypalException as e:
#             pledge.status = pledge.FAILED
#             pledge.save()
#             # TODO: use logger
#             print e.message
# 
#     return processing_pledges.count()


from django.core.management.base import BaseCommand
from zipfelchappe.paypal.tasks import process_payments



class Command(BaseCommand):
    help = 'Collect all paypal payments for finished projects (cronjob)'

    def handle(self, *args, **options):

# Path: zipfelchappe/paypal/tasks.py
# def process_payments():
#     """
#     Collects the paypal payments for all successfully financed projects
#     that end within the next 24 hours.
#     """
# 
#     billable_projects = Project.objects.billable()
# 
#     # Pledges that are ready to be payed
#     processing_pledges = Pledge.objects.filter(
#         project__in=billable_projects,
#         provider='paypal',
#         status=Pledge.AUTHORIZED,
#         paypal_preapproval__status='ACTIVE',
#         paypal_preapproval__approved=True,
#     )
# 
#     for pledge in processing_pledges:
#         try:
#             process_pledge(pledge)
#         except PaypalException as e:
#             pledge.status = pledge.FAILED
#             pledge.save()
#             # TODO: use logger
#             print e.message
# 
#     return processing_pledges.count()


# Path: zipfelchappe/paypal/management/commands/paypal_payments.py
from django.core.management.base import BaseCommand
from zipfelchappe.paypal.tasks import process_payments




class Command(BaseCommand):
    help = 'Collect all paypal payments for finished projects (cronjob)'

    def handle(self, *args, **options):

        pledges_processed = process_payments()