regluit/payment/credit.py

96 lines
3.4 KiB
Python
Raw Normal View History

2018-02-07 22:42:37 +00:00
from datetime import datetime
from regluit.payment import baseprocessor
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
2018-02-07 22:42:37 +00:00
def pledge_transaction(t, user, amount):
"""commit <amount> from a <user>'s credit to a specified transaction <t>"""
2018-02-07 22:42:37 +00:00
if t.amount and t.host == PAYMENT_HOST_CREDIT:
#changing the pledge_transaction
success = user.credit.add_to_pledged(amount-t.amount)
2018-02-07 22:42:37 +00:00
else:
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
t.set_credit_approved(amount)
return success
2018-02-07 22:42:37 +00:00
def credit_transaction(t, user, amount):
'''user has new credit, use it to fund the transaction'''
# first, credit the user's account
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)
if success:
2014-03-01 22:13:57 +00:00
t.type = PAYMENT_TYPE_INSTANT
t.set_executed()
return success
class Processor(baseprocessor.Processor):
class CancelPreapproval(BasePaymentRequest):
'''
2018-02-07 22:42:37 +00:00
Cancels an exisiting token.
'''
2018-02-07 22:42:37 +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
transaction.save()
else:
2018-02-07 22:42:37 +00:00
self.errorMessage = "couldn't cancel the transaction"
self.status = 'Credit Cancel Failure'
2018-02-07 22:42:37 +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)