Skip to content

Commit

Permalink
Merge pull request johnboxall#44 from tarkatronic/do-reference-transa…
Browse files Browse the repository at this point in the history
…ction

Added support for DoReferenceTransaction
  • Loading branch information
spookylukey committed Oct 6, 2014
2 parents 11dacb3 + 7f7ddbd commit 637b7df
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -434,6 +434,21 @@ def create_billing_agreement_view(request):
`SetExpressCheckout` API call, the billing agreement is created when you
call the `DoExpressCheckoutPayment` API operation.

* doReferenceTransaction

The DoReferenceTransaction API operation processes a payment from a buyer's
account, which is identified by a previous transaction.
```python
from paypal.pro.helpers import PayPalWPP

def do_reference_transaction_view(request):
wpp = PayPalWPP(request)
reference_id = request.POST.get('reference_id')
amount = request.POST.get('amount')
wpp.doReferenceTransaction({'referenceid': reference_id, 'amt': amount})
...
```

* getExpressCheckoutDetails

The GetExpressCheckoutDetails API operation obtains information about a
Expand Down
16 changes: 16 additions & 0 deletions paypal/pro/helpers.py
Expand Up @@ -227,6 +227,22 @@ def manangeRecurringPaymentsProfileStatus(self, params, fail_silently=False):
def refundTransaction(self, params):
raise NotImplementedError

def doReferenceTransaction(self, params):
"""
Process a payment from a buyer's account, identified by a previous
transaction.
The `paymentaction` param defaults to "Sale", but may also contain the
values "Authorization" or "Order".
"""
defaults = {"method": "DoReferenceTransaction",
"paymentaction": "Sale"}
required = ["referenceid", "amt"]

nvp_obj = self._fetch(params, required, defaults)
if nvp_obj.flag:
raise PayPalFailure(nvp_obj.flag_info)
return nvp_obj

def _is_recurring(self, params):
"""Returns True if the item passed is a recurring transaction."""
return 'billingfrequency' in params
Expand Down
9 changes: 6 additions & 3 deletions paypal/pro/models.py
Expand Up @@ -83,9 +83,12 @@ class Meta:

def init(self, request, paypal_request, paypal_response):
"""Initialize a PayPalNVP instance from a HttpRequest."""
self.ipaddress = request.META.get('REMOTE_ADDR', '').split(':')[0]
if hasattr(request, "user") and request.user.is_authenticated():
self.user = request.user
if request is not None:
self.ipaddress = request.META.get('REMOTE_ADDR', '').split(':')[0]
if hasattr(request, "user") and request.user.is_authenticated():
self.user = request.user
else:
self.ipaddress = ''

# No storing credit card info.
query_data = dict((k, v) for k, v in paypal_request.items() if k not in self.RESTRICTED_FIELDS)
Expand Down
28 changes: 28 additions & 0 deletions paypal/pro/tests.py
@@ -1,5 +1,6 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from decimal import Decimal
import mock

from django.conf import settings
Expand Down Expand Up @@ -118,6 +119,33 @@ def test_createBillingAgreement(self, mock_request_object):
with self.assertRaises(PayPalFailure):
nvp = wpp.createBillingAgreement({'token': 'dummy token'})

@mock.patch.object(PayPalWPP, '_request', autospec=True)
def test_doReferenceTransaction_valid(self, mock_request_object):
reference_id = 'B-1234'
amount = Decimal('10.50')
mock_request_object.return_value = 'ack=Success&paymentstatus=Completed&amt=%s&version=%s&billingagreementid=%s' % \
(amount, VERSION, reference_id)
wpp = PayPalWPP(REQUEST)
nvp = wpp.doReferenceTransaction({'referenceid': reference_id,
'amt': amount})
call_args = mock_request_object.call_args
self.assertIn('VERSION=%s' % VERSION, call_args[0][1])
self.assertIn('METHOD=DoReferenceTransaction', call_args[0][1])
self.assertIn('REFERENCEID=%s' % reference_id, call_args[0][1])
self.assertIn('AMT=%s' % amount, call_args[0][1])
self.assertEquals(nvp.method, 'DoReferenceTransaction')
self.assertEquals(nvp.ack, 'Success')

@mock.patch.object(PayPalWPP, '_request', autospec=True)
def test_doReferenceTransaction_invalid(self, mock_request_object):
reference_id = 'B-1234'
amount = Decimal('10.50')
mock_request_object.return_value = 'ack=Failure&l_errorcode=42&l_longmessage0=Broken'
wpp = PayPalWPP(REQUEST)
with self.assertRaises(PayPalFailure):
nvp = wpp.doReferenceTransaction({'referenceid': reference_id,
'amt': amount})


### DoExpressCheckoutPayment
# PayPal Request:
Expand Down

0 comments on commit 637b7df

Please sign in to comment.