2018-02-07 22:42:37 +00:00
|
|
|
from datetime import datetime
|
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
|
2018-02-07 21:46:44 +00:00
|
|
|
from regluit.payment.parameters import (
|
|
|
|
PAYMENT_HOST_CREDIT,
|
|
|
|
PAYMENT_TYPE_AUTHORIZATION,
|
|
|
|
PAYMENT_TYPE_INSTANT,
|
|
|
|
TRANSACTION_STATUS_COMPLETE,
|
|
|
|
TRANSACTION_STATUS_CANCELED,
|
|
|
|
)
|
|
|
|
from regluit.payment.signals import transaction_charged
|
2012-08-31 07:16:04 +00:00
|
|
|
|
2018-02-07 22:42:37 +00:00
|
|
|
def pledge_transaction(t, user, amount):
|
2012-08-31 07:16:04 +00:00
|
|
|
"""commit <amount> from a <user>'s credit to a specified transaction <t>"""
|
2018-02-07 22:42:37 +00:00
|
|
|
|
2012-08-31 07:16:04 +00:00
|
|
|
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)
|
2018-02-07 22:42:37 +00:00
|
|
|
else:
|
2013-12-15 05:31:06 +00:00
|
|
|
success = user.credit.add_to_pledged(amount)
|
|
|
|
if success:
|
2014-03-01 22:13:57 +00:00
|
|
|
t.type = PAYMENT_TYPE_AUTHORIZATION
|
2018-02-07 22:42:37 +00:00
|
|
|
t.max_amount = amount
|
2013-12-15 05:31:06 +00:00
|
|
|
t.set_credit_approved(amount)
|
|
|
|
return success
|
2012-08-31 07:16:04 +00:00
|
|
|
|
2018-02-07 22:42:37 +00:00
|
|
|
def credit_transaction(t, user, amount):
|
2012-09-06 05:01:17 +00:00
|
|
|
'''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'''
|
2018-02-07 22:42:37 +00:00
|
|
|
success = user.credit.transfer_to(to_user, amount)
|
2013-12-15 05:31:06 +00:00
|
|
|
if success:
|
2014-03-01 22:13:57 +00:00
|
|
|
t.type = PAYMENT_TYPE_INSTANT
|
2013-12-15 05:31:06 +00:00
|
|
|
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):
|
|
|
|
'''
|
2018-02-07 22:42:37 +00:00
|
|
|
Cancels an exisiting token.
|
2012-10-03 03:49:19 +00:00
|
|
|
'''
|
2018-02-07 22:42:37 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
def __init__(self, transaction):
|
|
|
|
self.transaction = transaction
|
|
|
|
if transaction.user.credit.add_to_pledged(-transaction.amount):
|
|
|
|
#success
|
2018-02-07 22:42:37 +00:00
|
|
|
transaction.status = TRANSACTION_STATUS_CANCELED
|
2012-10-03 03:49:19 +00:00
|
|
|
transaction.save()
|
|
|
|
else:
|
2018-02-07 22:42:37 +00:00
|
|
|
self.errorMessage = "couldn't cancel the transaction"
|
2012-10-03 03:49:19 +00:00
|
|
|
self.status = 'Credit Cancel Failure'
|
2018-02-07 22:42:37 +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
|
2018-02-07 21:46:44 +00:00
|
|
|
|
|
|
|
class Execute(BasePaymentRequest):
|
|
|
|
'''
|
|
|
|
This Execute function debits the user credits and pledge and credits the recipient.
|
|
|
|
'''
|
|
|
|
def __init__(self, transaction=None):
|
|
|
|
self.transaction = transaction
|
|
|
|
amount = transaction.amount
|
|
|
|
# make sure transaction hasn't already been executed
|
|
|
|
if transaction.status == TRANSACTION_STATUS_COMPLETE:
|
|
|
|
return
|
|
|
|
|
|
|
|
used = transaction.user.credit.use_pledge(amount)
|
|
|
|
if used:
|
|
|
|
user_to_pay = transaction.campaign.user_to_pay
|
|
|
|
credited = user_to_pay.credit.add_to_balance(amount, notify=False)
|
|
|
|
transaction.status = TRANSACTION_STATUS_COMPLETE
|
2018-02-07 22:42:37 +00:00
|
|
|
transaction.date_payment = datetime.now()
|
2018-02-07 21:46:44 +00:00
|
|
|
transaction.save()
|
2018-02-07 22:42:37 +00:00
|
|
|
|
2018-02-07 21:46:44 +00:00
|
|
|
# fire signal for sucessful transaction
|
|
|
|
transaction_charged.send(sender=self, transaction=transaction)
|