2011-09-27 12:48:11 +00:00
|
|
|
from regluit.core.models import Campaign, Wishlist
|
2011-12-14 13:30:37 +00:00
|
|
|
from regluit.payment.models import Transaction, Receiver, PaymentResponse
|
2011-09-27 12:48:11 +00:00
|
|
|
from django.contrib.auth.models import User
|
2012-01-10 20:16:04 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2012-04-18 15:53:13 +00:00
|
|
|
from django.conf import settings
|
2011-09-27 12:48:11 +00:00
|
|
|
from regluit.payment.parameters import *
|
2012-05-29 12:54:57 +00:00
|
|
|
from regluit.payment.signals import transaction_charged, pledge_modified, pledge_created
|
2012-08-31 07:16:04 +00:00
|
|
|
from regluit.payment import credit
|
|
|
|
|
|
|
|
from regluit.payment.baseprocessor import Pay, Finish, Preapproval, ProcessIPN, CancelPreapproval, PaymentDetails, PreapprovalDetails, RefundPayment
|
2012-04-18 15:53:13 +00:00
|
|
|
|
2011-09-27 12:48:11 +00:00
|
|
|
import uuid
|
|
|
|
import traceback
|
2012-03-07 19:42:16 +00:00
|
|
|
from regluit.utils.localdatetime import now
|
2011-10-27 08:05:21 +00:00
|
|
|
from dateutil.relativedelta import relativedelta
|
2011-10-06 00:56:20 +00:00
|
|
|
import logging
|
|
|
|
from decimal import Decimal as D
|
2011-10-27 08:05:21 +00:00
|
|
|
from xml.dom import minidom
|
2012-01-10 20:16:04 +00:00
|
|
|
import urllib, urlparse
|
2012-08-31 07:16:04 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2011-10-06 00:56:20 +00:00
|
|
|
|
2011-12-19 23:34:30 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
2011-10-06 00:56:20 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2011-10-27 08:05:21 +00:00
|
|
|
def append_element(doc, parent, name, text):
|
|
|
|
|
|
|
|
element = doc.createElement(name)
|
|
|
|
parent.appendChild(element)
|
|
|
|
text_node = doc.createTextNode(text)
|
|
|
|
element.appendChild(text_node)
|
|
|
|
|
|
|
|
return element
|
|
|
|
|
2011-10-13 17:28:23 +00:00
|
|
|
# at this point, there is no internal context and therefore, the methods of PaymentManager can be recast into static methods
|
2011-09-27 12:48:11 +00:00
|
|
|
class PaymentManager( object ):
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2012-05-11 11:19:13 +00:00
|
|
|
def processIPN(self, request, module):
|
2012-04-20 19:59:08 +00:00
|
|
|
|
|
|
|
# Forward to our payment processor
|
2012-05-11 11:19:13 +00:00
|
|
|
mod = __import__("regluit.payment." + module, fromlist=[str(module)])
|
|
|
|
method = getattr(mod, "ProcessIPN")
|
|
|
|
return method(request)
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2012-01-11 19:12:12 +00:00
|
|
|
def update_preapproval(self, transaction):
|
2012-01-11 22:46:59 +00:00
|
|
|
"""Update a transaction to hold the data from a PreapprovalDetails on that transaction"""
|
2012-01-11 19:12:12 +00:00
|
|
|
t = transaction
|
2012-05-11 11:19:13 +00:00
|
|
|
method = getattr(transaction.get_payment_class(), "PreapprovalDetails")
|
|
|
|
p = method(t)
|
2012-01-11 19:12:12 +00:00
|
|
|
|
|
|
|
preapproval_status = {'id':t.id, 'key':t.preapproval_key}
|
|
|
|
|
|
|
|
if p.error() or not p.success():
|
|
|
|
logger.info("Error retrieving preapproval details for transaction %d" % t.id)
|
|
|
|
preapproval_status["error"] = "An error occurred while verifying this transaction, see server logs for details"
|
|
|
|
else:
|
|
|
|
|
|
|
|
# Check the transaction status
|
|
|
|
if t.status != p.status:
|
|
|
|
preapproval_status["status"] = {'ours':t.status, 'theirs':p.status}
|
|
|
|
t.status = p.status
|
2012-05-04 14:30:05 +00:00
|
|
|
t.local_status = p.local_status
|
2012-01-11 19:12:12 +00:00
|
|
|
t.save()
|
|
|
|
|
|
|
|
# check the currency code
|
|
|
|
if t.currency != p.currency:
|
|
|
|
preapproval_status["currency"] = {'ours':t.currency, 'theirs':p.currency}
|
|
|
|
t.currency = p.currency
|
|
|
|
t.save()
|
|
|
|
|
|
|
|
# Check the amount
|
|
|
|
if t.max_amount != D(p.amount):
|
|
|
|
preapproval_status["amount"] = {'ours':t.max_amount, 'theirs':p.amount}
|
|
|
|
t.max_amount = p.amount
|
|
|
|
t.save()
|
2012-01-11 21:47:56 +00:00
|
|
|
|
|
|
|
# Check approved
|
|
|
|
if t.approved != p.approved:
|
|
|
|
preapproval_status["approved"] = {'ours':t.approved, 'theirs':p.approved}
|
|
|
|
t.approved = p.approved
|
|
|
|
t.save()
|
2012-04-23 18:34:51 +00:00
|
|
|
|
|
|
|
# In amazon FPS, we may not have a pay_key via the return URL, update here
|
|
|
|
try:
|
|
|
|
if t.pay_key != p.pay_key:
|
|
|
|
preapproval_status['pay_key'] = {'ours':t.pay_key, 'theirs':p.pay_key}
|
|
|
|
t.pay_key = p.pay_key
|
|
|
|
t.save()
|
|
|
|
except:
|
|
|
|
# No problem, p.pay_key is not defined for paypal function
|
|
|
|
blah = "blah"
|
2012-01-11 21:47:56 +00:00
|
|
|
|
2012-01-11 19:12:12 +00:00
|
|
|
|
|
|
|
return preapproval_status
|
|
|
|
|
|
|
|
def update_payment(self, transaction):
|
2012-01-11 22:46:59 +00:00
|
|
|
"""Update a transaction to hold the data from a PaymentDetails on that transaction"""
|
2012-01-11 19:12:12 +00:00
|
|
|
t = transaction
|
|
|
|
payment_status = {'id':t.id}
|
|
|
|
|
2012-05-11 11:19:13 +00:00
|
|
|
method = getattr(transaction.get_payment_class(), "PaymentDetails")
|
|
|
|
p = method(t)
|
2012-01-11 19:12:12 +00:00
|
|
|
|
|
|
|
if p.error() or not p.success():
|
|
|
|
logger.info("Error retrieving payment details for transaction %d" % t.id)
|
|
|
|
payment_status['error'] = "An error occurred while verifying this transaction, see server logs for details"
|
|
|
|
else:
|
|
|
|
|
|
|
|
# Check the transaction status
|
|
|
|
if t.status != p.status:
|
|
|
|
payment_status['status'] = {'ours': t.status, 'theirs': p.status}
|
|
|
|
|
|
|
|
t.status = p.status
|
2012-04-21 05:10:56 +00:00
|
|
|
t.local_status = p.local_status
|
2012-01-11 19:12:12 +00:00
|
|
|
t.save()
|
|
|
|
|
|
|
|
receivers_status = []
|
|
|
|
|
|
|
|
for r in p.transactions:
|
2012-04-21 05:10:56 +00:00
|
|
|
# This is only supported for paypal at this time
|
2012-01-11 19:12:12 +00:00
|
|
|
try:
|
|
|
|
receiver = Receiver.objects.get(transaction=t, email=r['email'])
|
|
|
|
|
|
|
|
receiver_status = {'email':r['email']}
|
|
|
|
|
|
|
|
logger.info(r)
|
|
|
|
logger.info(receiver)
|
|
|
|
|
|
|
|
# Check for updates on each receiver's status. Note that unprocessed delayed chained payments
|
|
|
|
# will not have a status code or txn id code
|
|
|
|
if receiver.status != r['status']:
|
|
|
|
receiver_status['status'] = {'ours': receiver.status, 'theirs': r['status']}
|
|
|
|
receiver.status = r['status']
|
|
|
|
receiver.save()
|
|
|
|
|
|
|
|
if receiver.txn_id != r['txn_id']:
|
|
|
|
receiver_status['txn_id'] = {'ours':receiver.txn_id, 'theirs':r['txn_id']}
|
|
|
|
|
|
|
|
receiver.txn_id = r['txn_id']
|
|
|
|
receiver.save()
|
|
|
|
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
if not set(["status","txn_id"]).isdisjoint(receiver_status.keys()):
|
|
|
|
receivers_status.append(receiver_status)
|
|
|
|
|
|
|
|
if len(receivers_status):
|
|
|
|
payment_status["receivers"] = receivers_status
|
|
|
|
|
|
|
|
return payment_status
|
|
|
|
|
|
|
|
|
2012-01-11 19:50:08 +00:00
|
|
|
def checkStatus(self, past_days=None, transactions=None):
|
2011-10-27 08:05:21 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
Run through all pay transactions and verify that their current status is as we think.
|
2012-01-11 19:50:08 +00:00
|
|
|
|
|
|
|
Allow for a list of transactions to be passed in or for the method to check on all transactions within the
|
|
|
|
given past_days
|
|
|
|
|
2011-10-27 08:05:21 +00:00
|
|
|
'''
|
|
|
|
|
2012-01-11 19:50:08 +00:00
|
|
|
DEFAULT_DAYS_TO_CHECK = 3
|
|
|
|
|
2011-12-23 01:34:24 +00:00
|
|
|
status = {'payments':[], 'preapprovals':[]}
|
|
|
|
|
|
|
|
# look at all PAY transactions for stated number of past days; if past_days is not int, get all Transaction
|
|
|
|
# only PAY transactions have date_payment not None
|
2012-01-11 19:50:08 +00:00
|
|
|
|
|
|
|
if transactions is None:
|
2011-12-08 17:13:23 +00:00
|
|
|
|
2012-01-11 19:50:08 +00:00
|
|
|
if past_days is None:
|
|
|
|
past_days = DEFAULT_DAYS_TO_CHECK
|
2011-10-27 08:05:21 +00:00
|
|
|
|
2012-01-11 19:50:08 +00:00
|
|
|
try:
|
2012-03-07 19:42:16 +00:00
|
|
|
ref_date = now() - relativedelta(days=int(past_days))
|
2012-01-11 19:50:08 +00:00
|
|
|
payment_transactions = Transaction.objects.filter(date_payment__gte=ref_date)
|
|
|
|
except:
|
2012-03-07 19:42:16 +00:00
|
|
|
ref_date = now()
|
2012-01-11 19:50:08 +00:00
|
|
|
payment_transactions = Transaction.objects.filter(date_payment__isnull=False)
|
|
|
|
|
|
|
|
logger.info(payment_transactions)
|
2011-10-27 08:05:21 +00:00
|
|
|
|
2012-01-11 19:50:08 +00:00
|
|
|
# Now look for preapprovals that have not been paid and check on their status
|
|
|
|
preapproval_transactions = Transaction.objects.filter(date_authorized__gte=ref_date, date_payment=None, type=PAYMENT_TYPE_AUTHORIZATION)
|
|
|
|
|
|
|
|
logger.info(preapproval_transactions)
|
2011-12-23 01:34:24 +00:00
|
|
|
|
2012-01-11 19:50:08 +00:00
|
|
|
transactions = payment_transactions | preapproval_transactions
|
2012-06-01 17:13:37 +00:00
|
|
|
|
2011-12-06 14:36:18 +00:00
|
|
|
|
|
|
|
for t in transactions:
|
|
|
|
|
2012-06-13 12:35:18 +00:00
|
|
|
# deal with preapprovals
|
2012-01-11 19:50:08 +00:00
|
|
|
if t.date_payment is None:
|
2012-03-21 21:53:33 +00:00
|
|
|
preapproval_status = self.update_preapproval(t)
|
|
|
|
logger.info("transaction: {0}, preapproval_status: {1}".format(t, preapproval_status))
|
2012-01-11 21:47:56 +00:00
|
|
|
if not set(['status', 'currency', 'amount', 'approved']).isdisjoint(set(preapproval_status.keys())):
|
2012-01-11 19:50:08 +00:00
|
|
|
status["preapprovals"].append(preapproval_status)
|
2012-06-13 12:35:18 +00:00
|
|
|
# update payments
|
2012-01-11 19:50:08 +00:00
|
|
|
else:
|
2012-03-21 21:53:33 +00:00
|
|
|
payment_status = self.update_payment(t)
|
2012-01-11 19:50:08 +00:00
|
|
|
if not set(["status", "receivers"]).isdisjoint(payment_status.keys()):
|
|
|
|
status["payments"].append(payment_status)
|
2012-06-01 17:13:37 +00:00
|
|
|
|
2012-06-13 12:35:18 +00:00
|
|
|
# Clear out older, duplicate preapproval transactions
|
2012-06-01 17:42:48 +00:00
|
|
|
cleared_list = []
|
2012-06-13 12:35:18 +00:00
|
|
|
for p in transactions:
|
2012-06-01 17:13:37 +00:00
|
|
|
|
2012-06-13 12:35:18 +00:00
|
|
|
# pick out only the preapprovals
|
|
|
|
if p.date_payment is None and p.type == PAYMENT_TYPE_AUTHORIZATION and p.status == TRANSACTION_STATUS_ACTIVE and p not in cleared_list:
|
2012-06-01 17:13:37 +00:00
|
|
|
|
2012-06-01 17:42:48 +00:00
|
|
|
# keep only the newest transaction for this user and campaign
|
2012-06-13 12:35:18 +00:00
|
|
|
user_transactions_for_campaign = Transaction.objects.filter(user=p.user, status=TRANSACTION_STATUS_ACTIVE, campaign=p.campaign).order_by('-date_authorized')
|
2012-06-01 17:13:37 +00:00
|
|
|
|
2012-06-13 12:35:18 +00:00
|
|
|
if len(user_transactions_for_campaign) > 1:
|
|
|
|
logger.info("Found %d active transactions for campaign" % len(user_transactions_for_campaign))
|
|
|
|
self.cancel_related_transaction(user_transactions_for_campaign[0], status=TRANSACTION_STATUS_ACTIVE, campaign=transactions[0].campaign)
|
2012-06-01 17:13:37 +00:00
|
|
|
|
2012-06-13 12:35:18 +00:00
|
|
|
cleared_list.extend(user_transactions_for_campaign)
|
2012-06-01 17:42:48 +00:00
|
|
|
|
2012-06-01 17:13:37 +00:00
|
|
|
# Note, we may need to call checkstatus again here
|
2012-01-11 19:50:08 +00:00
|
|
|
|
2011-12-23 01:34:24 +00:00
|
|
|
return status
|
2011-10-27 08:05:21 +00:00
|
|
|
|
2011-10-01 12:01:40 +00:00
|
|
|
|
2012-06-22 03:20:32 +00:00
|
|
|
def run_query(self, transaction_list, summary=True, campaign_total=False, pledged=False, authorized=False, incomplete=False, completed=False, pending=False, error=False, failed=False, **kwargs):
|
2011-10-13 17:28:23 +00:00
|
|
|
'''
|
2012-06-22 03:20:32 +00:00
|
|
|
Generic query handler for returning summary and transaction info, see query_user and query_campaign
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2012-06-22 03:20:32 +00:00
|
|
|
campaign_total=True includes all payment types which should count towards campaign total
|
|
|
|
|
|
|
|
'''
|
|
|
|
if campaign_total:
|
|
|
|
# must double check when adding Paypal or other
|
2012-01-11 22:46:59 +00:00
|
|
|
# return only ACTIVE transactions with approved=True
|
2012-06-22 03:20:32 +00:00
|
|
|
list = transaction_list.filter(type=PAYMENT_TYPE_AUTHORIZATION,
|
|
|
|
approved=True).exclude(status=TRANSACTION_STATUS_CANCELED)
|
|
|
|
list = list | transaction_list.filter(type=PAYMENT_TYPE_INSTANT,
|
|
|
|
status=TRANSACTION_STATUS_COMPLETE)
|
|
|
|
else:
|
|
|
|
list = Transaction.objects.none()
|
|
|
|
if pledged:
|
|
|
|
list = list | transaction_list.filter(type=PAYMENT_TYPE_INSTANT,
|
|
|
|
status=TRANSACTION_STATUS_COMPLETE)
|
2012-01-03 19:49:37 +00:00
|
|
|
|
2012-06-22 03:20:32 +00:00
|
|
|
if authorized:
|
|
|
|
# return only ACTIVE transactions with approved=True
|
|
|
|
list = list | transaction_list.filter(type=PAYMENT_TYPE_AUTHORIZATION,
|
|
|
|
status=TRANSACTION_STATUS_ACTIVE,
|
|
|
|
approved=True)
|
|
|
|
if incomplete:
|
|
|
|
list = list | transaction_list.filter(type=PAYMENT_TYPE_AUTHORIZATION,
|
|
|
|
status=TRANSACTION_STATUS_INCOMPLETE)
|
|
|
|
if completed:
|
|
|
|
list = list | transaction_list.filter(type=PAYMENT_TYPE_AUTHORIZATION,
|
|
|
|
status=TRANSACTION_STATUS_COMPLETE)
|
|
|
|
if pending:
|
|
|
|
list = list | transaction_list.filter(type=PAYMENT_TYPE_AUTHORIZATION,
|
|
|
|
status=TRANSACTION_STATUS_PENDING)
|
|
|
|
if error:
|
|
|
|
list = list | transaction_list.filter(type=PAYMENT_TYPE_AUTHORIZATION,
|
|
|
|
status=TRANSACTION_STATUS_ERROR)
|
|
|
|
if failed:
|
|
|
|
list = list | transaction_list.filter(type=PAYMENT_TYPE_AUTHORIZATION,
|
|
|
|
status=TRANSACTION_STATUS_FAILED)
|
2011-09-27 12:48:11 +00:00
|
|
|
if summary:
|
2012-06-22 03:20:32 +00:00
|
|
|
amount = D('0.00')
|
|
|
|
for t in list:
|
|
|
|
if t.type==PAYMENT_TYPE_INSTANT:
|
|
|
|
for r in t.receiver_set.all():
|
|
|
|
#
|
|
|
|
# Currently receivers are only used for paypal, so keep the paypal status code here
|
|
|
|
#
|
|
|
|
if r.status == IPN_SENDER_STATUS_COMPLETED:
|
|
|
|
# or IPN_SENDER_STATUS_COMPLETED
|
|
|
|
# individual senders may not have been paid due to errors, and disputes/chargebacks only appear here
|
|
|
|
amount += r.amount
|
|
|
|
else:
|
|
|
|
amount += t.amount
|
|
|
|
|
2011-09-27 12:48:11 +00:00
|
|
|
return amount
|
|
|
|
|
|
|
|
else:
|
2012-06-22 03:20:32 +00:00
|
|
|
return list
|
2011-10-13 17:28:23 +00:00
|
|
|
|
2012-06-22 03:20:32 +00:00
|
|
|
def query_user(self, user, **kwargs):
|
2011-10-13 17:28:23 +00:00
|
|
|
'''
|
|
|
|
query_user
|
|
|
|
|
|
|
|
Returns either an amount or list of transactions for a user
|
|
|
|
|
|
|
|
summary: if true, return a float of the total, if false, return a list of transactions
|
|
|
|
|
|
|
|
return value: either a float summary or a list of transactions
|
2012-06-22 03:20:32 +00:00
|
|
|
Note: this method appears to be unused.
|
2011-10-13 17:28:23 +00:00
|
|
|
|
|
|
|
'''
|
2011-10-01 12:01:40 +00:00
|
|
|
|
|
|
|
transactions = Transaction.objects.filter(user=user)
|
2012-06-22 03:20:32 +00:00
|
|
|
return self.run_query(transactions, **kwargs)
|
2011-10-01 12:01:40 +00:00
|
|
|
|
2012-06-22 03:20:32 +00:00
|
|
|
def query_campaign(self, campaign, **kwargs ):
|
2011-10-13 17:28:23 +00:00
|
|
|
'''
|
|
|
|
query_campaign
|
|
|
|
|
|
|
|
Returns either an amount or list of transactions for a campaign
|
|
|
|
|
|
|
|
summary: if true, return a float of the total, if false, return a list of transactions
|
|
|
|
|
|
|
|
return value: either a float summary or a list of transactions
|
2012-06-22 03:20:32 +00:00
|
|
|
|
2011-10-13 17:28:23 +00:00
|
|
|
'''
|
2011-10-01 12:01:40 +00:00
|
|
|
|
|
|
|
transactions = Transaction.objects.filter(campaign=campaign)
|
2012-06-22 03:20:32 +00:00
|
|
|
return self.run_query(transactions, **kwargs)
|
2011-10-01 12:01:40 +00:00
|
|
|
|
2011-10-13 17:28:23 +00:00
|
|
|
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2011-09-29 07:50:07 +00:00
|
|
|
def execute_campaign(self, campaign):
|
2011-10-13 17:28:23 +00:00
|
|
|
'''
|
|
|
|
execute_campaign
|
|
|
|
|
|
|
|
attempts to execute all pending transactions for a campaign.
|
|
|
|
|
|
|
|
return value: returns a list of transactions with the status of each receiver/transaction updated
|
|
|
|
|
|
|
|
'''
|
2011-09-29 07:50:07 +00:00
|
|
|
|
2011-10-01 12:01:40 +00:00
|
|
|
# only allow active transactions to go through again, if there is an error, intervention is needed
|
2012-04-26 19:31:55 +00:00
|
|
|
transactions = Transaction.objects.filter(campaign=campaign, status=TRANSACTION_STATUS_ACTIVE)
|
2011-09-29 07:50:07 +00:00
|
|
|
|
|
|
|
for t in transactions:
|
2012-05-04 14:30:05 +00:00
|
|
|
#
|
|
|
|
# Currently receivers are only used for paypal, so it is OK to leave the paypal info here
|
|
|
|
#
|
2011-12-19 23:34:30 +00:00
|
|
|
receiver_list = [{'email':settings.PAYPAL_GLUEJAR_EMAIL, 'amount':t.amount},
|
|
|
|
{'email':campaign.paypal_receiver, 'amount':D(t.amount) * (D('1.00') - D(str(settings.GLUEJAR_COMMISSION)))}]
|
2011-09-29 07:50:07 +00:00
|
|
|
|
|
|
|
self.execute_transaction(t, receiver_list)
|
|
|
|
|
2012-03-23 18:28:09 +00:00
|
|
|
# TO DO: update campaign status
|
|
|
|
# Should this be done first before executing the transactions?
|
|
|
|
# How does the success/failure of transactions affect states of campaigns
|
|
|
|
|
|
|
|
|
2011-09-29 07:50:07 +00:00
|
|
|
return transactions
|
2011-12-21 17:31:58 +00:00
|
|
|
|
|
|
|
def finish_campaign(self, campaign):
|
|
|
|
'''
|
|
|
|
finish_campaign
|
|
|
|
|
|
|
|
attempts to execute all remaining payment to non-primary receivers
|
|
|
|
|
2012-05-04 14:30:05 +00:00
|
|
|
This is currently only supported for paypal
|
2011-12-21 17:31:58 +00:00
|
|
|
|
|
|
|
return value: returns a list of transactions with the status of each receiver/transaction updated
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
# QUESTION: to figure out which transactions are in a state in which the payment to the primary recipient is done but not to secondary recipient
|
|
|
|
# Consider two possibilities: status=IPN_PAY_STATUS_INCOMPLETE or execution = EXECUTE_TYPE_CHAINED_DELAYED
|
|
|
|
# which one? Let's try the second one
|
|
|
|
# only allow incomplete transactions to go through again, if there is an error, intervention is needed
|
|
|
|
transactions = Transaction.objects.filter(campaign=campaign, execution=EXECUTE_TYPE_CHAINED_DELAYED)
|
|
|
|
|
|
|
|
for t in transactions:
|
|
|
|
result = self.finish_transaction(t)
|
|
|
|
|
2012-03-23 18:28:09 +00:00
|
|
|
# TO DO: update campaign status
|
|
|
|
|
|
|
|
|
2011-12-21 17:31:58 +00:00
|
|
|
return transactions
|
2011-12-21 22:32:08 +00:00
|
|
|
|
2012-06-29 18:11:42 +00:00
|
|
|
def cancel_campaign(self, campaign, reason="UNSUCCESSFUL CAMPAIGN"):
|
2011-12-21 22:32:08 +00:00
|
|
|
'''
|
|
|
|
cancel_campaign
|
|
|
|
|
|
|
|
attempts to cancel active preapprovals related to the campaign
|
|
|
|
|
|
|
|
|
|
|
|
return value: returns a list of transactions with the status of each receiver/transaction updated
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2012-05-12 01:54:21 +00:00
|
|
|
transactions = Transaction.objects.filter(campaign=campaign, status=TRANSACTION_STATUS_ACTIVE)
|
2011-12-21 22:32:08 +00:00
|
|
|
|
|
|
|
for t in transactions:
|
2012-06-29 18:11:42 +00:00
|
|
|
result = self.cancel_transaction(t)
|
|
|
|
if result:
|
|
|
|
t.reason = reason
|
|
|
|
t.save()
|
2012-03-23 18:28:09 +00:00
|
|
|
|
2011-12-21 22:32:08 +00:00
|
|
|
return transactions
|
2011-12-21 17:31:58 +00:00
|
|
|
|
2011-10-13 17:28:23 +00:00
|
|
|
|
2011-12-14 13:30:37 +00:00
|
|
|
def finish_transaction(self, transaction):
|
|
|
|
'''
|
|
|
|
finish_transaction
|
|
|
|
|
2011-12-19 23:34:30 +00:00
|
|
|
calls the paypal API to execute payment to non-primary receivers
|
2011-12-14 13:30:37 +00:00
|
|
|
|
|
|
|
transaction: the transaction we want to complete
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
if transaction.execution != EXECUTE_TYPE_CHAINED_DELAYED:
|
|
|
|
logger.error("FinishTransaction called with invalid execution type")
|
|
|
|
return False
|
|
|
|
|
|
|
|
# mark this transaction as executed
|
2012-03-07 19:42:16 +00:00
|
|
|
transaction.date_executed = now()
|
2011-12-14 13:30:37 +00:00
|
|
|
transaction.save()
|
|
|
|
|
2012-05-11 11:19:13 +00:00
|
|
|
method = getattr(transaction.get_payment_class(), "Finish")
|
|
|
|
p = method(transaction)
|
2011-12-14 13:30:37 +00:00
|
|
|
|
|
|
|
# Create a response for this
|
|
|
|
envelope = p.envelope()
|
|
|
|
|
|
|
|
if envelope:
|
|
|
|
correlation = p.correlation_id()
|
|
|
|
timestamp = p.timestamp()
|
|
|
|
|
|
|
|
r = PaymentResponse.objects.create(api=p.url,
|
|
|
|
correlation_id = correlation,
|
|
|
|
timestamp = timestamp,
|
|
|
|
info = p.raw_response,
|
|
|
|
transaction=transaction)
|
|
|
|
|
|
|
|
if p.success() and not p.error():
|
|
|
|
logger.info("finish_transaction Success")
|
|
|
|
return True
|
|
|
|
|
|
|
|
else:
|
|
|
|
transaction.error = p.error_string()
|
|
|
|
transaction.save()
|
|
|
|
logger.info("finish_transaction error " + p.error_string())
|
|
|
|
return False
|
|
|
|
|
2011-09-29 07:50:07 +00:00
|
|
|
def execute_transaction(self, transaction, receiver_list):
|
2011-10-13 17:28:23 +00:00
|
|
|
'''
|
|
|
|
execute_transaction
|
|
|
|
|
|
|
|
executes a single pending transaction.
|
|
|
|
|
|
|
|
transaction: the transaction object to execute
|
|
|
|
receiver_list: a list of receivers for the transaction, in this format:
|
|
|
|
|
|
|
|
[
|
|
|
|
{'email':'email-1', 'amount':amount1},
|
|
|
|
{'email':'email-2', 'amount':amount2}
|
|
|
|
]
|
|
|
|
|
|
|
|
return value: a bool indicating the success or failure of the process. Please check the transaction status
|
|
|
|
after the IPN has completed for full information
|
|
|
|
|
|
|
|
'''
|
2011-09-29 07:50:07 +00:00
|
|
|
|
2011-10-01 12:01:40 +00:00
|
|
|
if len(transaction.receiver_set.all()) > 0:
|
|
|
|
# we are re-submitting a transaction, wipe the old receiver list
|
|
|
|
transaction.receiver_set.all().delete()
|
|
|
|
|
|
|
|
transaction.create_receivers(receiver_list)
|
2011-10-27 08:05:21 +00:00
|
|
|
|
|
|
|
# Mark as payment attempted so we will poll this periodically for status changes
|
2012-03-07 19:42:16 +00:00
|
|
|
transaction.date_payment = now()
|
2011-10-27 08:05:21 +00:00
|
|
|
transaction.save()
|
|
|
|
|
2012-05-11 11:19:13 +00:00
|
|
|
method = getattr(transaction.get_payment_class(), "Execute")
|
|
|
|
p = method(transaction)
|
2011-09-29 07:50:07 +00:00
|
|
|
|
2011-12-14 13:30:37 +00:00
|
|
|
# Create a response for this
|
|
|
|
envelope = p.envelope()
|
|
|
|
|
|
|
|
if envelope:
|
|
|
|
|
|
|
|
correlation = p.correlation_id()
|
|
|
|
timestamp = p.timestamp()
|
|
|
|
|
|
|
|
r = PaymentResponse.objects.create(api=p.api(),
|
|
|
|
correlation_id = correlation,
|
|
|
|
timestamp = timestamp,
|
|
|
|
info = p.raw_response,
|
|
|
|
transaction=transaction)
|
2011-09-29 07:50:07 +00:00
|
|
|
|
2011-12-14 13:30:37 +00:00
|
|
|
# We will update our transaction status when we receive the IPN
|
|
|
|
if p.success() and not p.error():
|
|
|
|
transaction.pay_key = p.key()
|
|
|
|
transaction.save()
|
|
|
|
logger.info("execute_transaction Success")
|
2012-05-16 02:47:57 +00:00
|
|
|
transaction_charged.send(sender=self, transaction=transaction)
|
2011-09-29 07:50:07 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
else:
|
2011-12-14 13:30:37 +00:00
|
|
|
transaction.error = p.error_string()
|
|
|
|
transaction.save()
|
|
|
|
logger.info("execute_transaction Error: " + p.error_string())
|
2011-09-29 07:50:07 +00:00
|
|
|
return False
|
2011-10-01 12:01:40 +00:00
|
|
|
|
2011-12-21 22:32:08 +00:00
|
|
|
def cancel_transaction(self, transaction):
|
2011-10-13 17:28:23 +00:00
|
|
|
'''
|
|
|
|
cancel
|
|
|
|
|
|
|
|
cancels a pre-approved transaction
|
|
|
|
|
|
|
|
return value: True if successful, false otherwise
|
|
|
|
'''
|
2011-10-01 12:01:40 +00:00
|
|
|
|
2012-05-11 11:19:13 +00:00
|
|
|
method = getattr(transaction.get_payment_class(), "CancelPreapproval")
|
|
|
|
p = method(transaction)
|
2011-10-01 12:01:40 +00:00
|
|
|
|
2011-12-14 13:30:37 +00:00
|
|
|
# Create a response for this
|
|
|
|
envelope = p.envelope()
|
|
|
|
|
|
|
|
if envelope:
|
|
|
|
|
|
|
|
correlation = p.correlation_id()
|
|
|
|
timestamp = p.timestamp()
|
|
|
|
|
|
|
|
r = PaymentResponse.objects.create(api=p.url,
|
|
|
|
correlation_id = correlation,
|
|
|
|
timestamp = timestamp,
|
|
|
|
info = p.raw_response,
|
|
|
|
transaction=transaction)
|
|
|
|
|
|
|
|
if p.success() and not p.error():
|
2011-10-06 00:56:20 +00:00
|
|
|
logger.info("Cancel Transaction " + str(transaction.id) + " Completed")
|
2011-10-01 12:01:40 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
else:
|
2011-12-14 13:30:37 +00:00
|
|
|
transaction.error = p.error_string()
|
|
|
|
transaction.save()
|
|
|
|
logger.info("Cancel Transaction " + str(transaction.id) + " Failed with error: " + p.error_string())
|
2011-10-01 12:01:40 +00:00
|
|
|
return False
|
|
|
|
|
2012-08-31 07:16:04 +00:00
|
|
|
def authorize(self, transaction, expiry= None, return_url=None, paymentReason="unglue.it Pledge", modification=False):
|
2011-10-13 17:28:23 +00:00
|
|
|
'''
|
|
|
|
authorize
|
|
|
|
|
|
|
|
authorizes a set amount of money to be collected at a later date
|
|
|
|
|
2012-03-21 21:53:33 +00:00
|
|
|
return_url: url to redirect supporter to after a successful PayPal transaction
|
2012-05-14 17:15:40 +00:00
|
|
|
paymentReason: a memo line that will show up in the Payer's Amazon (and Paypal?) account
|
2012-08-06 17:34:51 +00:00
|
|
|
modification: whether this authorize call is part of a modification of an existing pledge
|
2011-10-13 17:28:23 +00:00
|
|
|
|
|
|
|
return value: a tuple of the new transaction object and a re-direct url. If the process fails,
|
|
|
|
the redirect url will be None
|
|
|
|
|
2012-08-31 07:16:04 +00:00
|
|
|
'''
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2012-09-19 00:01:07 +00:00
|
|
|
if transaction.host == PAYMENT_HOST_NONE:
|
2012-08-31 07:16:04 +00:00
|
|
|
#TODO send user to select a payment processor
|
|
|
|
pass
|
|
|
|
|
|
|
|
# we might want to not allow for a return_url to be passed in but calculated
|
2012-01-10 20:16:04 +00:00
|
|
|
# here because we have immediate access to the Transaction object.
|
|
|
|
|
2012-01-11 01:15:39 +00:00
|
|
|
|
|
|
|
if return_url is None:
|
|
|
|
return_path = "{0}?{1}".format(reverse('pledge_complete'),
|
2012-09-19 00:01:07 +00:00
|
|
|
urllib.urlencode({'tid':transaction.id}))
|
2012-01-11 01:15:39 +00:00
|
|
|
return_url = urlparse.urljoin(settings.BASE_URL, return_path)
|
2012-01-10 20:16:04 +00:00
|
|
|
|
2012-09-19 00:01:07 +00:00
|
|
|
method = getattr(transaction.get_payment_class(), "Preapproval")
|
2012-08-31 07:16:04 +00:00
|
|
|
p = method(transaction, transaction.max_amount, expiry, return_url=return_url, paymentReason=paymentReason)
|
2012-05-15 20:16:39 +00:00
|
|
|
|
2011-12-14 13:30:37 +00:00
|
|
|
# Create a response for this
|
|
|
|
envelope = p.envelope()
|
|
|
|
|
|
|
|
if envelope:
|
|
|
|
r = PaymentResponse.objects.create(api=p.url,
|
|
|
|
correlation_id = p.correlation_id(),
|
|
|
|
timestamp = p.timestamp(),
|
|
|
|
info = p.raw_response,
|
2012-08-31 07:16:04 +00:00
|
|
|
transaction=transaction)
|
2011-12-14 13:30:37 +00:00
|
|
|
|
|
|
|
if p.success() and not p.error():
|
2012-08-31 07:16:04 +00:00
|
|
|
transaction.preapproval_key = p.key()
|
|
|
|
transaction.save()
|
2011-11-22 11:14:58 +00:00
|
|
|
|
|
|
|
url = p.next_url()
|
|
|
|
|
|
|
|
logger.info("Authorize Success: " + url)
|
2012-05-29 12:54:57 +00:00
|
|
|
|
|
|
|
# modification and initial pledge use different notification templates --
|
|
|
|
# decide which to send
|
2012-05-30 01:16:01 +00:00
|
|
|
# we need to fire notifications at the first point at which we are sure
|
2012-05-29 12:54:57 +00:00
|
|
|
# that the transaction has successfully completed; triggering notifications
|
|
|
|
# when the transaction is initiated risks sending notifications on transactions
|
|
|
|
# that for whatever reason fail. will need other housekeeping to handle those.
|
2012-05-30 01:16:01 +00:00
|
|
|
# sadly this point is not yet late enough in the process -- needs to be moved
|
|
|
|
# until after we are certain.
|
2012-06-15 17:48:05 +00:00
|
|
|
|
2012-06-15 20:59:51 +00:00
|
|
|
if not modification:
|
2012-06-15 17:48:05 +00:00
|
|
|
# BUGBUG:
|
|
|
|
# send the notice here for now
|
|
|
|
# this is actually premature since we're only about to send the user off to the payment system to
|
|
|
|
# authorize a charge
|
2012-08-31 07:16:04 +00:00
|
|
|
pledge_created.send(sender=self, transaction=transaction)
|
2012-06-15 15:38:38 +00:00
|
|
|
|
2012-08-31 07:16:04 +00:00
|
|
|
return transaction, url
|
2011-11-22 11:14:58 +00:00
|
|
|
|
2011-09-29 07:50:07 +00:00
|
|
|
|
|
|
|
else:
|
2012-08-31 07:16:04 +00:00
|
|
|
transaction.error = p.error_string()
|
|
|
|
transaction.save()
|
2011-12-14 13:30:37 +00:00
|
|
|
logger.info("Authorize Error: " + p.error_string())
|
2012-08-31 07:16:04 +00:00
|
|
|
return transaction, None
|
|
|
|
|
2012-09-19 00:01:07 +00:00
|
|
|
def process_transaction(self, currency, amount, host=PAYMENT_HOST_NONE, campaign=None, user=None,
|
2012-09-07 13:46:38 +00:00
|
|
|
return_url=None, paymentReason="unglue.it Pledge", pledge_extra=None,
|
2012-08-31 07:16:04 +00:00
|
|
|
modification=False):
|
|
|
|
'''
|
|
|
|
process
|
|
|
|
|
|
|
|
saves and processes a proposed transaction; decides if the transaction should be processed immediately.
|
|
|
|
|
|
|
|
currency: a 3-letter currency code, i.e. USD
|
|
|
|
amount: the amount to authorize
|
|
|
|
host: the name of the processing module; if none, send user back to decide!
|
|
|
|
campaign: required campaign object
|
|
|
|
user: optional user object
|
|
|
|
return_url: url to redirect supporter to after a successful transaction
|
|
|
|
paymentReason: a memo line that will show up in the Payer's Amazon (and Paypal?) account
|
|
|
|
modification: whether this authorize call is part of a modification of an existing pledge
|
2012-09-07 13:46:38 +00:00
|
|
|
pledge_extra: extra pledge stuff
|
2012-08-31 07:16:04 +00:00
|
|
|
|
|
|
|
return value: a tuple of the new transaction object and a re-direct url. If the process fails,
|
|
|
|
the redirect url will be None
|
|
|
|
'''
|
|
|
|
# set the expiry date based on the campaign deadline
|
|
|
|
expiry = campaign.deadline + timedelta( days=settings.PREAPPROVAL_PERIOD_AFTER_CAMPAIGN )
|
|
|
|
|
2012-09-19 00:01:07 +00:00
|
|
|
t = Transaction.create(amount=0,
|
|
|
|
host = host,
|
2012-08-31 07:16:04 +00:00
|
|
|
max_amount=amount,
|
|
|
|
currency=currency,
|
|
|
|
campaign=campaign,
|
|
|
|
user=user,
|
2012-09-07 13:46:38 +00:00
|
|
|
pledge_extra=pledge_extra
|
2012-08-31 07:16:04 +00:00
|
|
|
)
|
2012-09-07 13:46:38 +00:00
|
|
|
t.save()
|
2012-08-31 07:16:04 +00:00
|
|
|
# does user have enough credit to pledge now?
|
|
|
|
if user.credit.available >= amount :
|
|
|
|
# YES!
|
|
|
|
credit.pledge_transaction(t,user,amount)
|
|
|
|
return_path = "{0}?{1}".format(reverse('pledge_complete'),
|
|
|
|
urllib.urlencode({'tid':t.id}))
|
|
|
|
return_url = urlparse.urljoin(settings.BASE_URL, return_path)
|
|
|
|
pledge_created.send(sender=self, transaction=t)
|
|
|
|
return t, return_url
|
|
|
|
else:
|
2012-09-06 05:01:17 +00:00
|
|
|
# send user to choose payment path
|
|
|
|
return t, reverse('fund_pledge', args=[t.id])
|
2012-08-31 07:16:04 +00:00
|
|
|
|
2011-09-29 07:50:07 +00:00
|
|
|
|
2012-06-01 17:13:37 +00:00
|
|
|
def cancel_related_transaction(self, transaction, status=TRANSACTION_STATUS_ACTIVE, campaign=None):
|
|
|
|
'''
|
|
|
|
Cancels any other similar status transactions for the same campaign. Used with modify code
|
|
|
|
|
|
|
|
Returns the number of transactions successfully canceled
|
|
|
|
'''
|
|
|
|
|
|
|
|
related_transactions = Transaction.objects.filter(status=status, user=transaction.user)
|
|
|
|
|
|
|
|
if len(related_transactions) == 0:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if campaign:
|
|
|
|
related_transactions = related_transactions.filter(campaign=campaign)
|
|
|
|
|
|
|
|
canceled = 0
|
|
|
|
|
|
|
|
for t in related_transactions:
|
|
|
|
|
|
|
|
if t.id == transaction.id:
|
|
|
|
# keep our transaction
|
|
|
|
continue
|
|
|
|
|
2012-06-15 15:38:38 +00:00
|
|
|
if self.cancel_transaction(t):
|
2012-06-01 17:13:37 +00:00
|
|
|
canceled = canceled + 1
|
2012-06-15 15:38:38 +00:00
|
|
|
# send notice about modification of transaction
|
|
|
|
if transaction.amount > t.amount:
|
|
|
|
# this should be the only one that happens
|
|
|
|
up_or_down = "increased"
|
|
|
|
elif transaction.amount < t.amount:
|
|
|
|
# we shouldn't expect any case in which this happens
|
|
|
|
up_or_down = "decreased"
|
|
|
|
else:
|
|
|
|
# we shouldn't expect any case in which this happens
|
|
|
|
up_or_down = None
|
|
|
|
|
|
|
|
pledge_modified.send(sender=self, transaction=transaction, up_or_down=up_or_down)
|
|
|
|
else:
|
2012-06-15 20:59:51 +00:00
|
|
|
logger.error("Failed to cancel transaction {0} for related transaction {1} ".format(t, transaction))
|
2012-06-01 17:13:37 +00:00
|
|
|
|
|
|
|
return canceled
|
|
|
|
|
2012-09-07 13:46:38 +00:00
|
|
|
def modify_transaction(self, transaction, amount, expiry=None, pledge_extra=None,
|
|
|
|
return_url=None, nevermind_url=None, paymentReason=None):
|
2012-01-05 06:42:05 +00:00
|
|
|
'''
|
|
|
|
modify
|
|
|
|
|
2012-08-31 07:16:04 +00:00
|
|
|
Modifies a transaction.
|
|
|
|
2 main situations: if the new amount is less than max_amount, no need to go out to PayPal again
|
|
|
|
if new amount is greater than max_amount...need to go out and get new approval.
|
|
|
|
to start with, we can use the standard pledge_complete, pledge_cancel machinery
|
|
|
|
might have to modify the pledge_complete, pledge_cancel because the messages are going to be
|
|
|
|
different because we're modifying a pledge rather than a new one.
|
2012-01-05 06:42:05 +00:00
|
|
|
|
|
|
|
amount: the new amount
|
|
|
|
expiry: the new expiration date, or if none the current expiration date will be used
|
|
|
|
return_url: the return URL after the preapproval(if needed)
|
2012-05-14 17:15:40 +00:00
|
|
|
paymentReason: a memo line that will show up in the Payer's Amazon (and Paypal?) account
|
2012-01-05 06:42:05 +00:00
|
|
|
|
2012-03-21 21:53:33 +00:00
|
|
|
return value: True if successful, False otherwise. An optional second parameter for the forward URL if a new authorhization is needed
|
|
|
|
'''
|
2012-01-05 06:42:05 +00:00
|
|
|
|
|
|
|
if transaction.type != PAYMENT_TYPE_AUTHORIZATION:
|
|
|
|
logger.info("Error, attempt to modify an invalid transaction type")
|
|
|
|
return False, None
|
|
|
|
|
2012-03-21 21:53:33 +00:00
|
|
|
# Can only modify an active, pending transaction. If it is completed, we need to do a refund. If it is incomplete,
|
|
|
|
# then an IPN may be pending and we cannot touch it
|
2012-04-21 05:10:56 +00:00
|
|
|
if transaction.status != TRANSACTION_STATUS_ACTIVE:
|
2012-01-05 06:42:05 +00:00
|
|
|
logger.info("Error, attempt to modify a transaction that is not active")
|
|
|
|
return False, None
|
|
|
|
|
2012-08-31 07:16:04 +00:00
|
|
|
if transaction.host == PAYMENT_HOST_CREDIT:
|
|
|
|
# does user have enough credit to pledge now?
|
|
|
|
if transaction.user.credit.available >= amount-transaction.amount :
|
|
|
|
# YES!
|
2012-09-07 13:46:38 +00:00
|
|
|
transaction.set_pledge_extra(pledge_extra)
|
2012-08-31 07:16:04 +00:00
|
|
|
credit.pledge_transaction(transaction,transaction.user,amount)
|
|
|
|
return_path = "{0}?{1}".format(reverse('pledge_complete'),
|
|
|
|
urllib.urlencode({'tid':transaction.id}))
|
|
|
|
return_url = urlparse.urljoin(settings.BASE_URL, return_path)
|
|
|
|
|
|
|
|
logger.info("Updated amount of transaction to %f" % amount)
|
|
|
|
pledge_modified.send(sender=self, transaction=transaction,up_or_down="decreased" if amount-transaction.amount<0 else "increased")
|
|
|
|
return transaction, return_url
|
|
|
|
else:
|
2012-09-06 20:55:32 +00:00
|
|
|
# cancel old transaction, send user to choose new payment path
|
|
|
|
# set the expiry date based on the campaign deadline
|
|
|
|
expiry = transaction.campaign.deadline + timedelta( days=settings.PREAPPROVAL_PERIOD_AFTER_CAMPAIGN )
|
2012-09-07 13:46:38 +00:00
|
|
|
t = Transaction.create(amount=0,
|
2012-09-06 20:55:32 +00:00
|
|
|
max_amount=amount,
|
|
|
|
currency=transaction.currency,
|
|
|
|
status=TRANSACTION_STATUS_MODIFIED,
|
|
|
|
campaign=transaction.campaign,
|
|
|
|
user=transaction.user,
|
2012-09-07 13:46:38 +00:00
|
|
|
pledge_extra=pledge_extra
|
2012-09-06 20:55:32 +00:00
|
|
|
)
|
2012-09-07 13:46:38 +00:00
|
|
|
t.save()
|
2012-09-06 20:55:32 +00:00
|
|
|
credit.CancelPreapproval(transaction)
|
|
|
|
return t, reverse('fund_pledge', args=[t.id])
|
|
|
|
|
2012-08-31 07:16:04 +00:00
|
|
|
elif amount > transaction.max_amount or expiry != transaction.date_expired:
|
|
|
|
|
|
|
|
# set the expiry date based on the campaign deadline
|
|
|
|
expiry = transaction.campaign.deadline + timedelta( days=settings.PREAPPROVAL_PERIOD_AFTER_CAMPAIGN )
|
2012-01-05 06:42:05 +00:00
|
|
|
|
2012-08-31 07:16:04 +00:00
|
|
|
# Start a new transaction for the new amount
|
2012-09-07 13:46:38 +00:00
|
|
|
t = Transaction.create(amount=amount,
|
2012-08-31 07:16:04 +00:00
|
|
|
max_amount=amount,
|
|
|
|
host=transaction.host,
|
2012-09-17 23:56:07 +00:00
|
|
|
currency=transaction.currency,
|
2012-08-31 07:16:04 +00:00
|
|
|
status=TRANSACTION_STATUS_CREATED,
|
|
|
|
campaign=transaction.campaign,
|
|
|
|
user=transaction.user,
|
2012-09-07 13:46:38 +00:00
|
|
|
pledge_extra=pledge_extra
|
2012-08-31 07:16:04 +00:00
|
|
|
)
|
2012-09-07 13:46:38 +00:00
|
|
|
t.save()
|
2012-08-31 07:16:04 +00:00
|
|
|
t, url = self.authorize(transaction,
|
|
|
|
expiry=expiry if expiry else transaction.date_expired,
|
|
|
|
return_url=return_url,
|
|
|
|
paymentReason=paymentReason,
|
|
|
|
modification=True
|
|
|
|
)
|
2012-01-05 06:42:05 +00:00
|
|
|
|
|
|
|
if t and url:
|
|
|
|
# Need to re-direct to approve the transaction
|
2012-03-21 21:53:33 +00:00
|
|
|
logger.info("New authorization needed, redirection to url %s" % url)
|
2012-06-08 19:02:57 +00:00
|
|
|
|
2012-06-01 17:13:37 +00:00
|
|
|
# Do not cancel the transaction here, wait until we get confirmation that the transaction is complete
|
|
|
|
# then cancel all other active transactions for this campaign
|
|
|
|
#self.cancel_transaction(transaction)
|
2012-06-08 19:02:57 +00:00
|
|
|
|
2012-05-29 12:54:57 +00:00
|
|
|
# while it would seem to make sense to send a pledge notification change here
|
|
|
|
# if we do, we will also send notifications when we initiate but do not
|
|
|
|
# successfully complete a pledge modification
|
2012-06-08 19:02:57 +00:00
|
|
|
|
2012-01-05 06:42:05 +00:00
|
|
|
return True, url
|
|
|
|
else:
|
2012-03-21 21:53:33 +00:00
|
|
|
# a problem in authorize
|
2012-01-05 06:42:05 +00:00
|
|
|
logger.info("Error, unable to start a new authorization")
|
2012-05-29 12:54:57 +00:00
|
|
|
# should we send a pledge_modified signal with state="failed" and a
|
|
|
|
# corresponding notification to the user? that would go here.
|
2012-01-05 06:42:05 +00:00
|
|
|
return False, None
|
|
|
|
|
2012-01-06 15:32:52 +00:00
|
|
|
elif amount <= transaction.max_amount:
|
2012-03-21 21:53:33 +00:00
|
|
|
# Update transaction but leave the preapproval alone
|
2012-01-05 06:42:05 +00:00
|
|
|
transaction.amount = amount
|
2012-09-07 13:46:38 +00:00
|
|
|
transaction.set_pledge_extra(pledge_extra)
|
2012-03-21 21:53:33 +00:00
|
|
|
|
2012-01-05 06:42:05 +00:00
|
|
|
transaction.save()
|
|
|
|
logger.info("Updated amount of transaction to %f" % amount)
|
2012-05-30 01:16:01 +00:00
|
|
|
# since modifying pledges downwards happens immediately and only within our
|
|
|
|
# db, we don't have to wait until we hear back from amazon to be assured of
|
|
|
|
# success; send the notification immediately
|
2012-05-30 00:50:53 +00:00
|
|
|
pledge_modified.send(sender=self, transaction=transaction, up_or_down="decreased")
|
2012-01-05 06:42:05 +00:00
|
|
|
return True, None
|
|
|
|
else:
|
2012-03-21 21:53:33 +00:00
|
|
|
# this shouldn't happen
|
2012-01-05 06:42:05 +00:00
|
|
|
return False, None
|
|
|
|
|
|
|
|
|
|
|
|
def refund_transaction(self, transaction):
|
|
|
|
'''
|
|
|
|
refund
|
|
|
|
|
|
|
|
Refunds a transaction. The money for the transaction may have gone to a number of places. We can only
|
|
|
|
refund money that is in our account
|
|
|
|
|
|
|
|
return value: True if successful, false otherwise
|
|
|
|
'''
|
|
|
|
|
|
|
|
# First check if a payment has been made. It is possible that some of the receivers may be incomplete
|
|
|
|
# We need to verify that the refund API will cancel these
|
2012-05-04 14:30:05 +00:00
|
|
|
if transaction.status != TRANSACTION_STATUS_COMPLETE:
|
2012-01-05 06:42:05 +00:00
|
|
|
logger.info("Refund Transaction failed, invalid transaction status")
|
|
|
|
return False
|
|
|
|
|
2012-05-11 11:19:13 +00:00
|
|
|
method = getattr(transaction.get_payment_class(), "RefundPayment")
|
|
|
|
p = method(transaction)
|
2012-01-05 06:42:05 +00:00
|
|
|
|
|
|
|
# Create a response for this
|
|
|
|
envelope = p.envelope()
|
|
|
|
|
|
|
|
if envelope:
|
|
|
|
|
|
|
|
correlation = p.correlation_id()
|
|
|
|
timestamp = p.timestamp()
|
|
|
|
|
|
|
|
r = PaymentResponse.objects.create(api=p.url,
|
|
|
|
correlation_id = correlation,
|
|
|
|
timestamp = timestamp,
|
|
|
|
info = p.raw_response,
|
|
|
|
transaction=transaction)
|
|
|
|
|
|
|
|
if p.success() and not p.error():
|
|
|
|
logger.info("Refund Transaction " + str(transaction.id) + " Completed")
|
|
|
|
return True
|
|
|
|
|
|
|
|
else:
|
|
|
|
transaction.error = p.error_string()
|
|
|
|
transaction.save()
|
|
|
|
logger.info("Refund Transaction " + str(transaction.id) + " Failed with error: " + p.error_string())
|
|
|
|
return False
|
|
|
|
|
2012-09-07 13:46:38 +00:00
|
|
|
def pledge(self, currency, campaign=None, user=None,
|
|
|
|
return_url=None, nevermind_url=None, pledge_extra=None):
|
2011-10-13 17:28:23 +00:00
|
|
|
'''
|
|
|
|
pledge
|
|
|
|
|
|
|
|
Performs an instant payment
|
|
|
|
|
|
|
|
currency: a 3-letter paypal currency code, i.e. USD
|
2012-08-31 07:16:04 +00:00
|
|
|
campaign: required campaign object
|
2011-10-13 17:28:23 +00:00
|
|
|
user: optional user object
|
2012-03-21 21:53:33 +00:00
|
|
|
return_url: url to redirect supporter to after a successful PayPal transaction
|
2012-05-18 23:59:48 +00:00
|
|
|
nevermind_url: url to send supporter to if support hits cancel while in middle of PayPal transaction
|
2012-09-07 13:46:38 +00:00
|
|
|
|
2011-10-13 17:28:23 +00:00
|
|
|
return value: a tuple of the new transaction object and a re-direct url. If the process fails,
|
|
|
|
the redirect url will be None
|
|
|
|
|
|
|
|
'''
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2011-10-06 00:56:20 +00:00
|
|
|
amount = D('0.00')
|
2011-10-01 12:01:40 +00:00
|
|
|
|
2012-09-07 13:46:38 +00:00
|
|
|
t = Transaction.create(amount=amount,
|
|
|
|
max_amount=amount,
|
|
|
|
currency=currency,
|
|
|
|
status=TRANSACTION_STATUS_NONE,
|
|
|
|
campaign=campaign,
|
|
|
|
user=user,
|
|
|
|
pledge_extra=pledge_extra
|
|
|
|
)
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2012-09-07 13:45:43 +00:00
|
|
|
t.date_payment=now()
|
|
|
|
t.execution=EXECUTE_TYPE_CHAINED_INSTANT
|
|
|
|
t.type=PAYMENT_TYPE_INSTANT
|
2012-05-11 11:19:13 +00:00
|
|
|
method = getattr(t.get_payment_class(), "Pay")
|
2012-05-18 23:59:48 +00:00
|
|
|
p = method(t,return_url=return_url, nevermind_url=nevermind_url)
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2011-12-14 13:30:37 +00:00
|
|
|
# Create a response for this
|
|
|
|
envelope = p.envelope()
|
2011-12-19 23:34:30 +00:00
|
|
|
logger.info(envelope)
|
2011-12-14 13:30:37 +00:00
|
|
|
|
|
|
|
if envelope:
|
|
|
|
r = PaymentResponse.objects.create(api=p.api(),
|
|
|
|
correlation_id = p.correlation_id(),
|
|
|
|
timestamp = p.timestamp(),
|
|
|
|
info = p.raw_response,
|
|
|
|
transaction=t)
|
|
|
|
|
|
|
|
if p.success() and not p.error():
|
|
|
|
t.pay_key = p.key()
|
2012-04-21 05:10:56 +00:00
|
|
|
t.status = TRANSACTION_STATUS_CREATED
|
2011-09-27 12:48:11 +00:00
|
|
|
t.save()
|
2011-11-22 11:14:58 +00:00
|
|
|
|
2012-08-31 07:12:15 +00:00
|
|
|
url = p.next_url()
|
2011-11-22 11:14:58 +00:00
|
|
|
|
|
|
|
logger.info("Pledge Success: " + url)
|
|
|
|
return t, url
|
2011-09-27 12:48:11 +00:00
|
|
|
|
|
|
|
else:
|
2011-12-14 13:30:37 +00:00
|
|
|
t.error = p.error_string()
|
2011-09-27 12:48:11 +00:00
|
|
|
t.save()
|
2012-04-18 15:53:13 +00:00
|
|
|
logger.info("Pledge Error: %s" % p.error_string())
|
2011-09-27 12:48:11 +00:00
|
|
|
return t, None
|
|
|
|
|
2012-05-15 20:16:39 +00:00
|
|
|
|