First pass at writing a signal handler to recharge transaction if account updated
parent
9e8f54ab66
commit
a90f133166
|
@ -1,12 +1,17 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
|
import regluit.payment.manager
|
||||||
from regluit.payment.parameters import *
|
from regluit.payment.parameters import *
|
||||||
from regluit.payment.signals import credit_balance_added, pledge_created
|
from regluit.payment.signals import credit_balance_added, pledge_created
|
||||||
from regluit.utils.localdatetime import now
|
from regluit.utils.localdatetime import now
|
||||||
|
|
||||||
|
from django.db.models.signals import post_save, post_delete
|
||||||
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from datetime import timedelta
|
import datetime
|
||||||
import uuid
|
import uuid
|
||||||
import urllib
|
import urllib
|
||||||
import logging
|
import logging
|
||||||
|
@ -136,7 +141,7 @@ class Transaction(models.Model):
|
||||||
self.approved=True
|
self.approved=True
|
||||||
now_val = now()
|
now_val = now()
|
||||||
self.date_authorized = now_val
|
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()
|
self.save()
|
||||||
pledge_created.send(sender=self, transaction=self)
|
pledge_created.send(sender=self, transaction=self)
|
||||||
|
|
||||||
|
@ -333,9 +338,6 @@ class Account(models.Model):
|
||||||
self.date_deactivated = now()
|
self.date_deactivated = now()
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
from django.db.models.signals import post_save, post_delete
|
|
||||||
import regluit.payment.manager
|
|
||||||
|
|
||||||
# handle any save, updates to a payment.Transaction
|
# handle any save, updates to a payment.Transaction
|
||||||
|
|
||||||
def handle_transaction_change(sender, instance, created, **kwargs):
|
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_save.connect(handle_transaction_change,sender=Transaction)
|
||||||
post_delete.connect(handle_transaction_delete,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
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -48,3 +48,5 @@ TRANSACTION_STATUS_REFUNDED = 'Refunded'
|
||||||
# The transaction was refused/denied
|
# The transaction was refused/denied
|
||||||
TRANSACTION_STATUS_FAILED = 'Failed'
|
TRANSACTION_STATUS_FAILED = 'Failed'
|
||||||
|
|
||||||
|
# Transaction written off -- unable to successfully be charged after campaign succeeded
|
||||||
|
TRANSACTION_STATUS_WRITTEN_OFF = 'Written-Off'
|
||||||
|
|
|
@ -226,6 +226,9 @@ GLUEJAR_COMMISSION = 0.06
|
||||||
PREAPPROVAL_PERIOD = 365 # days to ask for in a preapproval
|
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
|
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_KEY = ""
|
||||||
GOODREADS_API_SECRET = ""
|
GOODREADS_API_SECRET = ""
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue