2011-09-27 12:48:11 +00:00
|
|
|
from regluit.payment.manager import PaymentManager
|
|
|
|
from regluit.payment.paypal import IPN
|
|
|
|
from regluit.payment.models import Transaction
|
|
|
|
from regluit.core.models import Campaign, Wishlist
|
2011-10-07 16:53:30 +00:00
|
|
|
from django.conf import settings
|
2012-04-11 16:51:18 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.shortcuts import render_to_response
|
2011-09-27 12:48:11 +00:00
|
|
|
from django.contrib.auth.models import User
|
2012-04-11 16:51:18 +00:00
|
|
|
from django.contrib.sites.models import RequestSite
|
2011-09-27 12:48:11 +00:00
|
|
|
from regluit.payment.parameters import *
|
2012-04-18 20:22:00 +00:00
|
|
|
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect, HttpResponseBadRequest
|
2011-09-27 12:48:11 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2011-10-04 11:55:39 +00:00
|
|
|
from django.test.utils import setup_test_environment
|
2012-04-11 16:51:18 +00:00
|
|
|
from django.template import RequestContext
|
|
|
|
|
2011-10-04 11:55:39 +00:00
|
|
|
from unittest import TestResult
|
|
|
|
from regluit.payment.tests import PledgeTest, AuthorizeTest
|
2012-04-11 16:51:18 +00:00
|
|
|
from regluit.payment.urls import amazon_fps_obj, fps_recur_obj
|
2012-04-16 14:00:46 +00:00
|
|
|
import billing
|
2012-04-17 03:58:00 +00:00
|
|
|
import uuid
|
|
|
|
from decimal import Decimal as D
|
2012-04-16 14:00:46 +00:00
|
|
|
|
|
|
|
from regluit.utils.localdatetime import now
|
2011-09-27 12:48:11 +00:00
|
|
|
import traceback
|
|
|
|
|
2012-04-17 03:58:00 +00:00
|
|
|
|
2011-10-06 00:56:20 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# parameterize some test recipients
|
2012-01-05 06:42:05 +00:00
|
|
|
TEST_RECEIVERS = ['seller_1317463643_biz@gmail.com', 'Buyer6_1325742408_per@gmail.com']
|
2011-12-14 13:30:37 +00:00
|
|
|
#TEST_RECEIVERS = ['glueja_1317336101_biz@gluejar.com', 'rh1_1317336251_biz@gluejar.com', 'RH2_1317336302_biz@gluejar.com']
|
2011-10-06 00:56:20 +00:00
|
|
|
|
|
|
|
|
2011-09-29 07:50:07 +00:00
|
|
|
'''
|
|
|
|
http://BASE/querycampaign?id=2
|
|
|
|
|
|
|
|
Example that returns a summary total for a campaign
|
|
|
|
'''
|
2011-09-27 12:48:11 +00:00
|
|
|
def queryCampaign(request):
|
|
|
|
|
|
|
|
id = request.GET['id']
|
|
|
|
campaign = Campaign.objects.get(id=id)
|
|
|
|
|
|
|
|
p = PaymentManager()
|
|
|
|
|
|
|
|
transactions = p.query_campaign(campaign)
|
|
|
|
|
|
|
|
total = p.query_campaign(campaign, summary=True)
|
|
|
|
|
|
|
|
return HttpResponse(str(total))
|
2011-09-29 07:50:07 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
http://BASE/testexecute?campaign=2
|
|
|
|
|
|
|
|
Example that executes a set of transactions that are pre-approved
|
|
|
|
'''
|
|
|
|
def testExecute(request):
|
|
|
|
|
|
|
|
p = PaymentManager()
|
|
|
|
|
|
|
|
if 'campaign' in request.GET.keys():
|
|
|
|
campaign_id = request.GET['campaign']
|
|
|
|
campaign = Campaign.objects.get(id=int(campaign_id))
|
|
|
|
else:
|
|
|
|
campaign = None
|
|
|
|
|
|
|
|
output = ''
|
|
|
|
|
|
|
|
if campaign:
|
|
|
|
result = p.execute_campaign(campaign)
|
|
|
|
|
|
|
|
for t in result:
|
|
|
|
output += str(t)
|
2011-10-06 00:56:20 +00:00
|
|
|
logger.info(str(t))
|
2011-09-29 07:50:07 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
transactions = Transaction.objects.filter(status='ACTIVE')
|
|
|
|
|
|
|
|
for t in transactions:
|
|
|
|
|
|
|
|
# Note, set this to 1-5 different receivers with absolute amounts for each
|
2011-10-27 08:05:21 +00:00
|
|
|
receiver_list = [{'email':TEST_RECEIVERS[0], 'amount':float(t.amount) * 0.80},
|
|
|
|
{'email':TEST_RECEIVERS[1], 'amount':float(t.amount) * 0.20}]
|
2011-09-29 07:50:07 +00:00
|
|
|
|
|
|
|
p.execute_transaction(t, receiver_list)
|
|
|
|
output += str(t)
|
2011-10-06 00:56:20 +00:00
|
|
|
logger.info(str(t))
|
2011-09-29 07:50:07 +00:00
|
|
|
|
|
|
|
return HttpResponse(output)
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
http://BASE/testauthorize?amount=20
|
|
|
|
|
|
|
|
Example that initiates a pre-approval for a set amount
|
|
|
|
'''
|
|
|
|
def testAuthorize(request):
|
|
|
|
|
|
|
|
p = PaymentManager()
|
|
|
|
|
|
|
|
if 'campaign' in request.GET.keys():
|
|
|
|
campaign_id = request.GET['campaign']
|
|
|
|
else:
|
|
|
|
campaign_id = None
|
|
|
|
|
|
|
|
if 'amount' in request.GET.keys():
|
|
|
|
amount = float(request.GET['amount'])
|
|
|
|
else:
|
|
|
|
return HttpResponse("Error, no amount in request")
|
|
|
|
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2011-09-29 07:50:07 +00:00
|
|
|
# Note, set this to 1-5 different receivers with absolute amounts for each
|
2011-10-06 00:56:20 +00:00
|
|
|
receiver_list = [{'email': TEST_RECEIVERS[0], 'amount':20.00},
|
|
|
|
{'email': TEST_RECEIVERS[1], 'amount':10.00}]
|
2011-09-29 07:50:07 +00:00
|
|
|
|
2012-04-18 20:22:00 +00:00
|
|
|
# Set the return url for the processor
|
|
|
|
if settings.PAYMENT_PROCESSOR == 'amazon':
|
|
|
|
return_url = settings.BASE_URL + reverse('AmazonPaymentReturn')
|
|
|
|
else:
|
|
|
|
return_url = None
|
|
|
|
|
2011-09-29 07:50:07 +00:00
|
|
|
if campaign_id:
|
|
|
|
campaign = Campaign.objects.get(id=int(campaign_id))
|
2012-04-18 20:22:00 +00:00
|
|
|
t, url = p.authorize('USD', TARGET_TYPE_CAMPAIGN, amount, campaign=campaign, return_url=return_url, list=None, user=None)
|
2011-09-29 07:50:07 +00:00
|
|
|
|
|
|
|
else:
|
2012-04-18 20:22:00 +00:00
|
|
|
t, url = p.authorize('USD', TARGET_TYPE_NONE, amount, campaign=None, return_url=return_url, list=None, user=None)
|
2011-09-29 07:50:07 +00:00
|
|
|
|
|
|
|
if url:
|
2011-10-06 00:56:20 +00:00
|
|
|
logger.info("testAuthorize: " + url)
|
2011-09-29 07:50:07 +00:00
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
|
|
else:
|
2012-01-05 06:42:05 +00:00
|
|
|
response = t.error
|
|
|
|
logger.info("testAuthorize: Error " + str(t.error))
|
2011-09-29 07:50:07 +00:00
|
|
|
return HttpResponse(response)
|
|
|
|
|
2011-10-01 12:01:40 +00:00
|
|
|
'''
|
|
|
|
http://BASE/testcancel?transaction=2
|
|
|
|
|
|
|
|
Example that cancels a preapproved transaction
|
|
|
|
'''
|
|
|
|
def testCancel(request):
|
|
|
|
|
|
|
|
if "transaction" not in request.GET.keys():
|
|
|
|
return HttpResponse("No Transaction in Request")
|
|
|
|
|
|
|
|
t = Transaction.objects.get(id=int(request.GET['transaction']))
|
|
|
|
p = PaymentManager()
|
2011-12-21 22:32:08 +00:00
|
|
|
if p.cancel_transaction(t):
|
2011-10-01 12:01:40 +00:00
|
|
|
return HttpResponse("Success")
|
|
|
|
else:
|
|
|
|
message = "Error: " + t.error
|
|
|
|
return HttpResponse(message)
|
2011-12-14 13:30:37 +00:00
|
|
|
|
2012-01-05 06:42:05 +00:00
|
|
|
'''
|
|
|
|
http://BASE/testrefund?transaction=2
|
|
|
|
|
|
|
|
Example that refunds a transaction
|
|
|
|
'''
|
|
|
|
def testRefund(request):
|
|
|
|
|
|
|
|
if "transaction" not in request.GET.keys():
|
|
|
|
return HttpResponse("No Transaction in Request")
|
|
|
|
|
|
|
|
t = Transaction.objects.get(id=int(request.GET['transaction']))
|
|
|
|
p = PaymentManager()
|
|
|
|
if p.refund_transaction(t):
|
|
|
|
return HttpResponse("Success")
|
|
|
|
else:
|
|
|
|
if t.error:
|
|
|
|
message = "Error: " + t.error
|
|
|
|
else:
|
|
|
|
message = "Error"
|
|
|
|
|
|
|
|
return HttpResponse(message)
|
|
|
|
|
|
|
|
'''
|
2012-01-06 15:32:52 +00:00
|
|
|
http://BASE/testmodify?transaction=2
|
2012-01-05 06:42:05 +00:00
|
|
|
|
|
|
|
Example that modifies the amount of a transaction
|
|
|
|
'''
|
|
|
|
def testModify(request):
|
|
|
|
|
|
|
|
if "transaction" not in request.GET.keys():
|
|
|
|
return HttpResponse("No Transaction in Request")
|
|
|
|
|
|
|
|
if "amount" in request.GET.keys():
|
|
|
|
amount = float(request.GET['amount'])
|
|
|
|
else:
|
|
|
|
amount = 200.0
|
|
|
|
|
|
|
|
t = Transaction.objects.get(id=int(request.GET['transaction']))
|
|
|
|
p = PaymentManager()
|
|
|
|
|
|
|
|
status, url = p.modify_transaction(t, amount)
|
|
|
|
|
|
|
|
if url:
|
|
|
|
logger.info("testModify: " + url)
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
|
|
if status:
|
|
|
|
return HttpResponse("Success")
|
|
|
|
else:
|
|
|
|
return HttpResponse("Error")
|
|
|
|
|
|
|
|
|
2011-12-14 13:30:37 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
http://BASE/testfinish?transaction=2
|
|
|
|
|
|
|
|
Example that finishes a delayed chained transaction
|
|
|
|
'''
|
|
|
|
def testFinish(request):
|
|
|
|
|
|
|
|
if "transaction" not in request.GET.keys():
|
|
|
|
return HttpResponse("No Transaction in Request")
|
|
|
|
|
|
|
|
t = Transaction.objects.get(id=int(request.GET['transaction']))
|
|
|
|
p = PaymentManager()
|
|
|
|
if p.finish_transaction(t):
|
|
|
|
return HttpResponse("Success")
|
|
|
|
else:
|
|
|
|
message = "Error: " + t.error
|
|
|
|
return HttpResponse(message)
|
|
|
|
|
2011-10-01 12:01:40 +00:00
|
|
|
|
|
|
|
|
2011-09-29 07:50:07 +00:00
|
|
|
'''
|
|
|
|
http://BASE/testpledge?campaign=2
|
|
|
|
|
|
|
|
Example that initiates an instant payment for a campaign
|
|
|
|
'''
|
2011-09-27 12:48:11 +00:00
|
|
|
def testPledge(request):
|
|
|
|
|
|
|
|
p = PaymentManager()
|
|
|
|
|
2011-10-11 17:03:40 +00:00
|
|
|
if 'campaign' in request.REQUEST.keys():
|
|
|
|
campaign_id = request.REQUEST['campaign']
|
2011-09-27 12:48:11 +00:00
|
|
|
else:
|
|
|
|
campaign_id = None
|
|
|
|
|
2011-10-11 17:03:40 +00:00
|
|
|
# see whether there is a user logged in.
|
|
|
|
if request.user.is_authenticated():
|
|
|
|
user = request.user
|
|
|
|
else:
|
|
|
|
user = None
|
2011-09-27 12:48:11 +00:00
|
|
|
|
|
|
|
# Note, set this to 1-5 different receivers with absolute amounts for each
|
2011-10-06 00:56:20 +00:00
|
|
|
#receiver_list = [{'email':TEST_RECEIVERS[0], 'amount':20.00},{'email':TEST_RECEIVERS[1], 'amount':10.00}]
|
2011-10-11 17:03:40 +00:00
|
|
|
|
|
|
|
if 'pledge_amount' in request.REQUEST.keys():
|
|
|
|
pledge_amount = request.REQUEST['pledge_amount']
|
|
|
|
receiver_list = [{'email':TEST_RECEIVERS[0], 'amount':pledge_amount}]
|
|
|
|
else:
|
|
|
|
receiver_list = [{'email':TEST_RECEIVERS[0], 'amount':78.90}, {'email':TEST_RECEIVERS[1], 'amount':34.56}]
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2012-04-18 15:53:13 +00:00
|
|
|
# Set the return url for the processor
|
|
|
|
if settings.PAYMENT_PROCESSOR == 'amazon':
|
|
|
|
return_url = settings.BASE_URL + reverse('AmazonPaymentReturn')
|
|
|
|
else:
|
|
|
|
return_url = None
|
|
|
|
|
2011-09-27 12:48:11 +00:00
|
|
|
if campaign_id:
|
|
|
|
campaign = Campaign.objects.get(id=int(campaign_id))
|
2012-04-18 15:53:13 +00:00
|
|
|
t, url = p.pledge('USD', TARGET_TYPE_CAMPAIGN, receiver_list, campaign=campaign, list=None, user=user, return_url=return_url)
|
2011-09-27 12:48:11 +00:00
|
|
|
|
|
|
|
else:
|
2012-04-18 15:53:13 +00:00
|
|
|
t, url = p.pledge('USD', TARGET_TYPE_NONE, receiver_list, campaign=None, list=None, user=user, return_url=return_url)
|
2011-09-27 12:48:11 +00:00
|
|
|
|
|
|
|
if url:
|
2011-10-06 00:56:20 +00:00
|
|
|
logger.info("testPledge: " + url)
|
2011-09-27 12:48:11 +00:00
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
|
|
else:
|
2012-04-18 15:53:13 +00:00
|
|
|
response = t.error
|
|
|
|
logger.info("testPledge: Error " + str(t.error))
|
2011-09-27 12:48:11 +00:00
|
|
|
return HttpResponse(response)
|
2011-10-04 11:55:39 +00:00
|
|
|
|
|
|
|
def runTests(request):
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Setup the test environement. We need to run these tests on a live server
|
|
|
|
# so our code can receive IPN notifications from paypal
|
|
|
|
setup_test_environment()
|
|
|
|
result = TestResult()
|
|
|
|
|
|
|
|
# Run the authorize test
|
|
|
|
test = AuthorizeTest('test_authorize')
|
|
|
|
test.run(result)
|
|
|
|
|
|
|
|
# Run the pledge test
|
|
|
|
test = PledgeTest('test_pledge_single_receiver')
|
|
|
|
test.run(result)
|
|
|
|
|
|
|
|
# Run the pledge failure test
|
|
|
|
test = PledgeTest('test_pledge_too_much')
|
|
|
|
test.run(result)
|
|
|
|
|
|
|
|
output = "Tests Run: " + str(result.testsRun) + str(result.errors) + str(result.failures)
|
2011-10-06 00:56:20 +00:00
|
|
|
logger.info(output)
|
2011-10-04 11:55:39 +00:00
|
|
|
|
|
|
|
return HttpResponse(output)
|
2011-09-27 12:48:11 +00:00
|
|
|
|
2011-10-04 11:55:39 +00:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2011-09-27 12:48:11 +00:00
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def paypalIPN(request):
|
|
|
|
# Handler for paypal IPN notifications
|
|
|
|
|
|
|
|
p = PaymentManager()
|
|
|
|
p.processIPN(request)
|
|
|
|
|
2011-10-06 00:56:20 +00:00
|
|
|
logger.info(str(request.POST))
|
2011-09-27 12:48:11 +00:00
|
|
|
return HttpResponse("ipn")
|
2012-04-18 20:22:00 +00:00
|
|
|
|
2011-10-06 00:56:20 +00:00
|
|
|
|
|
|
|
def paymentcomplete(request):
|
|
|
|
# pick up all get and post parameters and display
|
|
|
|
output = "payment complete"
|
|
|
|
output += request.method + "\n" + str(request.REQUEST.items())
|
|
|
|
return HttpResponse(output)
|
2011-10-27 08:05:21 +00:00
|
|
|
|
|
|
|
def checkStatus(request):
|
|
|
|
# Check the status of all PAY transactions and flag any errors
|
|
|
|
p = PaymentManager()
|
|
|
|
error_data = p.checkStatus()
|
|
|
|
|
|
|
|
return HttpResponse(error_data, mimetype="text/xml")
|
|
|
|
|
2012-04-11 16:51:18 +00:00
|
|
|
# https://raw.github.com/agiliq/merchant/master/example/app/views.py
|
2011-10-27 08:05:21 +00:00
|
|
|
|
2012-04-11 16:51:18 +00:00
|
|
|
def _render(request, template, template_vars={}):
|
|
|
|
return render_to_response(template, template_vars, RequestContext(request))
|
|
|
|
|
|
|
|
def testfps(request):
|
|
|
|
url_scheme = "http"
|
|
|
|
if request.is_secure():
|
|
|
|
url_scheme = "https"
|
2012-04-17 03:58:00 +00:00
|
|
|
|
|
|
|
# generate a callerReference to use in our call
|
|
|
|
callerReference = str(uuid.uuid4())
|
|
|
|
# model stores TransactionAmount as string! We won't be doing this in our long-term model
|
|
|
|
transactionAmount = str(D(100.00))
|
|
|
|
|
|
|
|
fields = {"transactionAmount": transactionAmount,
|
2012-04-11 16:51:18 +00:00
|
|
|
"pipelineName": "SingleUse",
|
|
|
|
"paymentReason": "Merchant Test",
|
|
|
|
"paymentPage": request.build_absolute_uri(),
|
2012-04-17 03:58:00 +00:00
|
|
|
"callerReference": callerReference,
|
2012-04-11 16:51:18 +00:00
|
|
|
"returnURL": "%s://%s%s" % (url_scheme,
|
|
|
|
RequestSite(request).domain,
|
|
|
|
reverse("fps_return_url"))
|
|
|
|
}
|
|
|
|
# Save the fps.fields["callerReference"] in the db along with
|
|
|
|
# the amount to be charged or use the user's unique id as
|
|
|
|
# the callerReference so that the amount to be charged is known
|
|
|
|
# Or save the callerReference in the session and send the user
|
|
|
|
# to FPS and then use the session value when the user is back.
|
|
|
|
amazon_fps_obj.add_fields(fields)
|
2012-04-16 14:00:46 +00:00
|
|
|
|
|
|
|
# let's save this object to the model that django-merchant has set up for amazon transactions
|
|
|
|
# billing.models.AmazonFPSResponse.objects.all()
|
|
|
|
fps_transaction = billing.models.AmazonFPSResponse()
|
|
|
|
fps_transaction.transactionDate = now()
|
|
|
|
fps_transaction.transactionAmount = 100.00
|
2012-04-17 03:58:00 +00:00
|
|
|
fps_transaction.callerReference = callerReference
|
|
|
|
|
2012-04-16 14:00:46 +00:00
|
|
|
# force a calculation of the link_url so that callerReference gets calculated
|
|
|
|
logger.info("amazon_fps_obj.link_url: {0}".format(amazon_fps_obj.link_url))
|
|
|
|
|
|
|
|
fps_transaction.save()
|
|
|
|
|
2012-04-11 16:51:18 +00:00
|
|
|
fields.update({"transactionAmount": "100",
|
|
|
|
"pipelineName": "Recurring",
|
|
|
|
"recurringPeriod": "1 Hour",
|
|
|
|
})
|
|
|
|
fps_recur_obj.add_fields(fields)
|
2012-04-16 14:00:46 +00:00
|
|
|
|
|
|
|
|
2012-04-11 16:51:18 +00:00
|
|
|
template_vars = {'title': 'Amazon Flexible Payment Service',
|
|
|
|
"fps_recur_obj": fps_recur_obj,
|
|
|
|
"fps_obj": amazon_fps_obj}
|
|
|
|
return _render(request, 'amazon_fps.html', template_vars)
|
2011-10-06 00:56:20 +00:00
|
|
|
|
2011-09-27 12:48:11 +00:00
|
|
|
|