diff --git a/payment/models.py b/payment/models.py index b9669c50..9795d00d 100644 --- a/payment/models.py +++ b/payment/models.py @@ -1,12 +1,17 @@ from django.db import models from django.contrib.auth.models import User from django.conf import settings +from django.db.models import Q +import regluit.payment.manager from regluit.payment.parameters import * from regluit.payment.signals import credit_balance_added, pledge_created from regluit.utils.localdatetime import now + +from django.db.models.signals import post_save, post_delete + from decimal import Decimal -from datetime import timedelta +import datetime import uuid import urllib import logging @@ -136,7 +141,7 @@ class Transaction(models.Model): self.approved=True now_val = now() self.date_authorized = now_val - self.date_expired = now_val + timedelta( days=settings.PREAPPROVAL_PERIOD ) + self.date_expired = now_val + datetime.timedelta( days=settings.PREAPPROVAL_PERIOD ) self.save() pledge_created.send(sender=self, transaction=self) @@ -333,9 +338,6 @@ class Account(models.Model): self.date_deactivated = now() self.save() -from django.db.models.signals import post_save, post_delete -import regluit.payment.manager - # handle any save, updates to a payment.Transaction def handle_transaction_change(sender, instance, created, **kwargs): @@ -355,3 +357,22 @@ def handle_transaction_delete(sender, instance, **kwargs): post_save.connect(handle_transaction_change,sender=Transaction) post_delete.connect(handle_transaction_delete,sender=Transaction) +def recharge_failed_transactions(sender, created, instance, **kwargs): + """When a new Account is saved, check whether this is the new active account for a user. If so, recharge any + outstanding failed transactions + """ + + transactions_to_recharge = instance.user.transaction_set.filter((Q(status=TRANSACTION_STATUS_FAILED) | Q(status=TRANSACTION_STATUS_ERROR)) & Q(campaign__status='UNSUCCESSFUL')).all() + + if transactions_to_recharge: + pm = manager.PaymentManager() + for transaction in transactions_to_recharge: + # check whether we are still within the window to recharge + if (now() - transaction.campaign.deadline) < datetime.timedelta(settings.RECHARGE_WINDOW): + pm.execute_transaction(transaction) + +post_save.connect(recharge_failed_transactions, sender=Account) + +# handle recharging failed transactions + + diff --git a/payment/parameters.py b/payment/parameters.py index 027bb7f6..fac4cedf 100644 --- a/payment/parameters.py +++ b/payment/parameters.py @@ -48,3 +48,5 @@ TRANSACTION_STATUS_REFUNDED = 'Refunded' # The transaction was refused/denied TRANSACTION_STATUS_FAILED = 'Failed' +# Transaction written off -- unable to successfully be charged after campaign succeeded +TRANSACTION_STATUS_WRITTEN_OFF = 'Written-Off' diff --git a/settings/common.py b/settings/common.py index aa3ac0e1..43dca13e 100644 --- a/settings/common.py +++ b/settings/common.py @@ -226,6 +226,9 @@ GLUEJAR_COMMISSION = 0.06 PREAPPROVAL_PERIOD = 365 # days to ask for in a preapproval PREAPPROVAL_PERIOD_AFTER_CAMPAIGN = 90 # if we ask for preapproval time after a campaign deadline +# How many days we will try to collect on failed transactions until they are written off +RECHARGE_WINDOW = 7 + GOODREADS_API_KEY = "" GOODREADS_API_SECRET = ""