2012-08-31 07:16:04 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from django.conf import settings
|
2013-06-03 16:31:39 +00:00
|
|
|
from django.contrib.auth.models import User
|
2012-08-31 07:16:04 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
from regluit.payment import baseprocessor
|
2012-08-31 07:16:04 +00:00
|
|
|
from regluit.payment.baseprocessor import BasePaymentRequest
|
2013-06-03 16:31:39 +00:00
|
|
|
from regluit.payment.parameters import *
|
2012-08-31 07:16:04 +00:00
|
|
|
|
|
|
|
def pledge_transaction(t,user,amount):
|
|
|
|
"""commit <amount> from a <user>'s credit to a specified transaction <t>"""
|
|
|
|
|
|
|
|
if t.amount and t.host == PAYMENT_HOST_CREDIT:
|
|
|
|
#changing the pledge_transaction
|
2013-12-15 05:31:06 +00:00
|
|
|
success = user.credit.add_to_pledged(amount-t.amount)
|
2012-08-31 07:16:04 +00:00
|
|
|
else:
|
2013-12-15 05:31:06 +00:00
|
|
|
success = user.credit.add_to_pledged(amount)
|
|
|
|
if success:
|
|
|
|
t.max_amount=amount
|
|
|
|
t.set_credit_approved(amount)
|
|
|
|
return success
|
2012-08-31 07:16:04 +00:00
|
|
|
|
2012-09-06 05:01:17 +00:00
|
|
|
def credit_transaction(t,user,amount):
|
|
|
|
'''user has new credit, use it to fund the transaction'''
|
|
|
|
# first, credit the user's account
|
2013-12-15 05:31:06 +00:00
|
|
|
success = user.credit.add_to_balance(amount)
|
|
|
|
if success:
|
|
|
|
# now pledge to the transaction
|
|
|
|
pledge_amount = t.max_amount if t.max_amount <= user.credit.available else amount
|
|
|
|
success = user.credit.add_to_pledged(pledge_amount)
|
|
|
|
if success:
|
|
|
|
t.set_credit_approved(pledge_amount)
|
|
|
|
return success
|
|
|
|
|
|
|
|
def pay_transaction(t, user, to_user, amount):
|
|
|
|
'''user has credit, transfer it to rh account'''
|
|
|
|
success = user.credit.transfer_to(to_user , amount)
|
|
|
|
if success:
|
|
|
|
t.set_executed()
|
|
|
|
return success
|
2012-09-06 05:01:17 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
class Processor(baseprocessor.Processor):
|
|
|
|
class CancelPreapproval(BasePaymentRequest):
|
|
|
|
'''
|
|
|
|
Cancels an exisiting token.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, transaction):
|
|
|
|
self.transaction = transaction
|
|
|
|
if transaction.user.credit.add_to_pledged(-transaction.amount):
|
|
|
|
#success
|
|
|
|
transaction.status=TRANSACTION_STATUS_CANCELED
|
|
|
|
transaction.save()
|
|
|
|
else:
|
|
|
|
self.errorMessage="couldn't cancel the transaction"
|
|
|
|
self.status = 'Credit Cancel Failure'
|
2012-08-31 07:16:04 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
class PreapprovalDetails(BasePaymentRequest):
|
|
|
|
status = None
|
|
|
|
approved = None
|
|
|
|
currency = None
|
|
|
|
amount = None
|
|
|
|
def __init__(self, transaction):
|
|
|
|
self.status = transaction.status
|
|
|
|
self.approved = transaction.approved
|
|
|
|
self.currency = transaction.currency
|
|
|
|
self.amount = transaction.amount
|