Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eway/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = (0, 2, 'alpha',)
VERSION = (0, 3, 'alpha',)
39 changes: 35 additions & 4 deletions eway/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -209,4 +241,3 @@ def send_transaction(self, gateway_url, xml):
response = Response()
response.parse(content)
return response

4 changes: 4 additions & 0 deletions eway/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion eway/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,14 @@ class Payment(Validator):
# Magic Variables
option1 = Field("ewayOption1", default="")
option2 = Field("ewayOption2", default="")
option3 = Field("ewayOption3", default="")
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="")
1 change: 1 addition & 0 deletions eway/tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
131 changes: 131 additions & 0 deletions eway/tests/refund.py
Original file line number Diff line number Diff line change
@@ -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()