From 4f44e520ebb726da83c6bb8ccbb2bb2d2dbf539c Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Fri, 1 Mar 2013 12:26:40 +1100 Subject: [PATCH 1/2] refund implemented --- eway/client.py | 39 +++++++++++-- eway/config.py | 4 ++ eway/fields.py | 12 +++- eway/tests/client.py | 1 + eway/tests/refund.py | 131 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 182 insertions(+), 5 deletions(-) create mode 100644 eway/tests/refund.py diff --git a/eway/client.py b/eway/client.py index 9d91ec0..4d3eff5 100644 --- a/eway/client.py +++ b/eway/client.py @@ -6,7 +6,7 @@ from urllib import urlencode, quote_plus from eway import config -from eway.fields import Payment, Customer, Response +from eway.fields import Payment, Customer, Response, Refund class EwayPaymentError(Exception): pass @@ -18,10 +18,11 @@ class EwayPaymentClient(object): def __init__(self, customer_id=config.EWAY_DEFAULT_CUSTOMER_ID, method=config.EWAY_DEFAULT_PAYMENT_METHOD, live_gateway=config.EWAY_DEFAULT_LIVE_GATEWAY, + refund_password=None, pre_auth=False): self.customer_id = customer_id - + self.refund_password = refund_password # FIXME: Clean up if method == config.REAL_TIME: if live_gateway: @@ -174,7 +175,38 @@ def void(self, amount, transaction_id, reference=None): request_data = self.build_request(data) return self.send_transaction(gateway_url, request_data) - + + def refund(self, amount, original_transaction_number, reference=None, expiry_month="", expiry_year=""): + """ + Void an existing authorisation + + @param amount: The amount to void. + """ + if self.gateway_url == config.EWAY_PAYMENT_LIVE_REAL_TIME or self.gateway_url == config.EWAY_PAYMENT_LIVE_REAL_TIME_CVN: + gateway_url = config.EWAY_PAYMENT_LIVE_REFUND + elif self.gateway_url == config.EWAY_PAYMENT_LIVE_REAL_TIME_TESTING_MODE or self.gateway_url == config.EWAY_PAYMENT_LIVE_REAL_TIME_CVN_TESTING_MODE: + gateway_url = config.EWAY_PAYMENT_SANDBOX_REFUND + else: + raise EwayPaymentError('Could not determine complete method: refund') + + refund = Refund(total_amount=amount, + original_transaction_number=original_transaction_number, + refund_password=self.refund_password, + ) + + data = refund.get_data() + if reference: + data['ewayCustomerInvoiceRef'] = reference + + # this two are not required fields, but these two xml elements + # are required to show up in request + # empty field will help the transaction proceed + data['ewayCardExpiryMonth'] = expiry_month + data['ewayCardExpiryYear'] = expiry_year + + request_data = self.build_request(data) + return self.send_transaction(gateway_url, request_data) + def build_request(self, data): root = ET.Element("ewaygateway") customer_element = ET.Element("ewayCustomerID") @@ -209,4 +241,3 @@ def send_transaction(self, gateway_url, xml): response = Response() response.parse(content) return response - diff --git a/eway/config.py b/eway/config.py index 1aa3379..7cd7633 100644 --- a/eway/config.py +++ b/eway/config.py @@ -29,6 +29,10 @@ EWAY_PAYMENT_LIVE_AUTH_VOID = 'https://www.eway.com.au/gateway/xmlauthvoid.asp' EWAY_PAYMENT_LIVE_AUTH_VOID_TESTING_MODE = 'https://www.eway.com.au/gateway/xmltest/authvoidtestpage.asp' +EWAY_PAYMENT_LIVE_REFUND = 'https://www.eway.com.au/gateway/xmlpaymentrefund.asp' +EWAY_PAYMENT_SANDBOX_REFUND = 'https://sandbox.myeway.com.au/gateway/xmlpaymentrefund.asp' + + CUSTOMER_ID = "87654321" # Set this to your eWAY Customer ID PAYMENT_METHOD = REAL_TIME # Set this to the payment gatway you would like to use (REAL_TIME, REAL_TIME_CVN or GEO_IP_ANTI_FRAUD) USE_LIVE = False # Set this to true to use the live gateway diff --git a/eway/fields.py b/eway/fields.py index 2498f68..4ddb621 100644 --- a/eway/fields.py +++ b/eway/fields.py @@ -162,4 +162,14 @@ class Payment(Validator): # Magic Variables option1 = Field("ewayOption1", default="") option2 = Field("ewayOption2", default="") - option3 = Field("ewayOption3", default="") \ No newline at end of file + option3 = Field("ewayOption3", default="") + + +class Refund(Validator): + total_amount = CentsField("ewayTotalAmount", "Total Amount") + original_transaction_number = Field("ewayOriginalTrxnNumber", "Original Transaction Number") + refund_password = Field("ewayRefundPassword", "Refund Password") + # Magic Variables + option1 = Field("ewayOption1", default="") + option2 = Field("ewayOption2", default="") + option3 = Field("ewayOption3", default="") diff --git a/eway/tests/client.py b/eway/tests/client.py index 9a0f5ee..eee34ae 100644 --- a/eway/tests/client.py +++ b/eway/tests/client.py @@ -8,6 +8,7 @@ from eway.client import EwayPaymentClient from eway.fields import Customer, CreditCard + class ClientTestCase(unittest.TestCase): def setUp(self): self.eway_client = EwayPaymentClient('87654321', diff --git a/eway/tests/refund.py b/eway/tests/refund.py new file mode 100644 index 0000000..9f3fd62 --- /dev/null +++ b/eway/tests/refund.py @@ -0,0 +1,131 @@ +import sys +import unittest +from decimal import Decimal + +sys.path.insert(0, "./../../") + +from eway import config +from eway.client import EwayPaymentClient +from eway.fields import Customer, CreditCard + + +class ClientTestCase(unittest.TestCase): + def setUp(self): + # please change to your eway client + self.eway_client = EwayPaymentClient('91728933', + config.REAL_TIME_CVN, + False, + refund_password='xmlrefund123') + + def get_data(self): + """ + Test the eWAY auth complete functionality. + """ + customer = Customer() + customer.first_name = "Joe" + customer.last_name = "Bloggs" + customer.email = "name@xyz.com.au" + customer.address = "123 Someplace Street, Somewhere ACT" + customer.postcode = "2609" + customer.invoice_description = "Testing" + customer.invoice_reference = "INV120394" + customer.country = "AU" + + credit_card = CreditCard() + credit_card.holder_name = '%s %s' % (customer.first_name, customer.last_name,) + credit_card.number = "4444333322221111" + credit_card.expiry_month = 10 + credit_card.expiry_year = 15 + credit_card.verification_number = "123" + credit_card.ip_address = "127.0.0.1" + + return [customer, credit_card] + + def test_refund(self): + """ + Test the eWAY payment functionality. + """ + customer, credit_card = self.get_data() + + # step 1, make a payment + + response = self.eway_client.payment( + Decimal("10.08"), + credit_card=credit_card, + customer=customer, + reference="123456" + ) + + self.failUnless(response.success) + self.assertIn('Honour With Identification', response.get_message()) + self.failUnlessEqual('08', response.get_code(), 'Response code should be 08') + + resp_refund = self.eway_client.refund( + Decimal('10.08'), + response.transaction_number + ) + + self.failUnless(resp_refund.success) + self.assertIsNotNone(resp_refund.transaction_number) + self.failUnlessEqual('00', resp_refund.get_code()) + + def test_refund_multiple(self): + """ + Test the eWAY payment functionality. + """ + customer, credit_card = self.get_data() + + # step 1, make a payment + + response = self.eway_client.payment( + Decimal("20"), + credit_card=credit_card, + customer=customer, + reference="123456" + ) + + self.failUnless(response.success) + + # reminding: $20 + resp_refund = self.eway_client.refund( + Decimal('10'), + response.transaction_number) + + self.assertTrue(resp_refund.success) + self.assertIsNotNone(resp_refund.transaction_number) + + # reminding: $10. this one should failed. + resp_refund = self.eway_client.refund( + Decimal('15'), + response.transaction_number, + ) + self.assertFalse(resp_refund.success) + + # reminding: $10 + resp_refund = self.eway_client.refund( + Decimal('7'), + response.transaction_number + ) + + self.assertTrue(resp_refund.success) + + # reminding: $3 + resp_refund = self.eway_client.refund( + Decimal('3.01'), + response.transaction_number + ) + + self.assertFalse(resp_refund.success) + + # reminding: $3 + resp_refund = self.eway_client.refund( + Decimal('3.00'), + response.transaction_number + ) + + self.assertTrue(resp_refund.success) + + +if __name__ == '__main__': + unittest.main() + From 226d75ae3e45243b65f2302584d86f46a4ba0d53 Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Fri, 1 Mar 2013 12:29:32 +1100 Subject: [PATCH 2/2] version bumpped --- eway/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eway/__init__.py b/eway/__init__.py index b98eb6f..c607205 100644 --- a/eway/__init__.py +++ b/eway/__init__.py @@ -1 +1 @@ -VERSION = (0, 2, 'alpha',) \ No newline at end of file +VERSION = (0, 3, 'alpha',) \ No newline at end of file