Skip to content

Commit 607b7a4

Browse files
Merge pull request #11 from 2Checkout/CC-210
Update python2 library for new test orders approach
2 parents 925a414 + aca58c1 commit 607b7a4

File tree

7 files changed

+61
-224
lines changed

7 files changed

+61
-224
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from distutils.core import setup
44
setup(
55
name="twocheckout",
6-
version='0.3.0',
6+
version='0.4.0',
77
description="2Checkout Python Library",
88
author="Craig Christenson",
99
author_email="christensoncraig@gmail.com",

test/test_twocheckout.py

Lines changed: 58 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,25 @@
1212
NOW = datetime.datetime.now()
1313

1414
EXAMPLE_PRODUCT = {
15-
'name': 'Example Product',
16-
'price': 1.00
17-
}
18-
19-
EXAMPLE_OPTION = {
20-
'option_name': 'Example Option',
21-
'option_value_name': 'Test',
22-
'option_value_surcharge': 1.00
23-
}
24-
25-
EXAMPLE_COUPON = {
26-
'date_expire': '2020-12-12',
27-
'type': 'shipping'
15+
'name': 'Python Example Product',
16+
'price': 2.00
2817
}
2918

3019
EXAMPLE_SALE = {
31-
'sale_id': 9093717691800
20+
'sale_id': 250353589267
3221
}
3322

3423
EXAMPLE_COMMENT = {
35-
'sale_comment': "test"
24+
'sale_comment': "python2 test"
3625
}
3726

3827
EXAMPLE_REFUND = {
39-
'comment': "test",
28+
'comment': "Python Refund Sale",
4029
'category': 1
4130
}
4231

4332
EXAMPLE_SHIP = {
44-
'tracking_number': 123
33+
'tracking_number': "test"
4534
}
4635

4736
EXAMPLE_PASSBACK = {
@@ -61,19 +50,22 @@
6150
}
6251

6352
EXAMPLE_AUTH = {
64-
'merchantOrderId': '123',
65-
'token': 'NTQyZTQyOTMtNjA0Ni00NzM4LTkyNDItNjVlMmUzZTU2NTNj',
66-
'currency': 'USD',
67-
'total': '1.00',
68-
'billingAddr': {
69-
'name': 'Testing Tester',
70-
'addrLine1': '123 Test St',
71-
'city': 'Columbus',
72-
'state': 'OH',
73-
'zipCode': '43123',
74-
'country': 'USA',
75-
'email': 'cchristenson@2co.com',
76-
'phoneNumber': '555-555-5555'
53+
"sellerId": "CREDENTIALS_HERE",
54+
"privateKey": "CREDENTIALS_HERE",
55+
"merchantOrderId": "123",
56+
"token": "CUSTOMER-CLIENT-SIDE-TOKEN",
57+
"currency": "USD",
58+
"total": "2.00",
59+
"demo": True,
60+
"billingAddr": {
61+
"name": "John Doe",
62+
"addrLine1": "123 Test St",
63+
"city": "Columbus",
64+
"state": "Ohio",
65+
"zipCode": "43123",
66+
"country": "USA",
67+
"email": "example@2co.com",
68+
"phoneNumber": "5555555555"
7769
}
7870
}
7971

@@ -82,25 +74,40 @@ def setUp(self):
8274
super(TwocheckoutTestCase, self).setUp()
8375

8476
twocheckout.Api.credentials({
85-
'username': 'testlibraryapi901248204',
86-
'password': 'testlibraryapi901248204PASS',
87-
'mode': 'sandbox'
77+
'username': 'CREDENTIALS_HERE',
78+
'password': 'CREDENTIALS_HERE'
8879
})
8980

9081
twocheckout.Api.auth_credentials({
91-
'private_key': 'BE632CB0-BB29-11E3-AFB6-D99C28100996',
92-
'seller_id': '901248204',
93-
'mode': 'sandbox'
82+
'private_key': 'CREDENTIALS_HERE',
83+
'seller_id': 'CREDENTIALS_HERE'
9484
})
9585

86+
87+
class AuthorizationTest(TwocheckoutTestCase):
88+
def setUp(self):
89+
super(AuthorizationTest, self).setUp()
90+
91+
## Place order test
92+
def test_1_auth(self):
93+
params = EXAMPLE_AUTH
94+
try:
95+
result = twocheckout.Charge.authorize(params)
96+
97+
## use OrderNumber for sale_id for next tests
98+
print("OrderNumber: ", result.orderNumber)
99+
self.assertEqual(result.responseCode, "APPROVED")
100+
except TwocheckoutError as error:
101+
self.assertEqual(error.msg, "Unauthorized")
102+
96103
class SaleTest(TwocheckoutTestCase):
97104
def setUp(self):
98105
super(SaleTest, self).setUp()
99106

100107
def test_1_find_sale(self):
101108
try:
102109
sale = twocheckout.Sale.find(EXAMPLE_SALE)
103-
self.assertEqual(int(sale.sale_id), 9093717691800)
110+
self.assertEqual(int(sale.sale_id), 250353589267)
104111
except TwocheckoutError as error:
105112
self.assertEqual(error.message, "Unable to find record.")
106113

@@ -115,26 +122,27 @@ def test_3_refund_sale(self):
115122
result = sale.refund(EXAMPLE_REFUND)
116123
self.assertEqual(result.message, "refund added to invoice")
117124
except TwocheckoutError as error:
118-
self.assertEqual(error.message, "Invoice was already refunded.")
125+
self.assertEqual(error.message, "Amount greater than remaining balance on invoice.")
119126

127+
## If you have already run test_3 then test_4 will fail for the same sale_id.
120128
def test_4_refund_invoice(self):
121129
try:
122130
sale = twocheckout.Sale.find(EXAMPLE_SALE)
123131
invoice = sale.invoices[0]
124132
result = invoice.refund(EXAMPLE_REFUND)
125-
self.assertEqual(result.message, "refund added to invoice")
133+
self.assertEqual(result.response_message, "refund added to invoice")
126134
except TwocheckoutError as error:
127-
self.assertEqual(error.message, "Invoice was already refunded.")
135+
self.assertEqual(error.message, "Amount greater than remaining balance on invoice.")
128136

129137
def test_5_refund_lineitem(self):
130138
try:
131139
sale = twocheckout.Sale.find(EXAMPLE_SALE)
132140
invoice = sale.invoices[0]
133141
lineitem = invoice.lineitems[0]
134142
result = lineitem.refund(EXAMPLE_REFUND)
135-
self.assertEqual(result.message, "lineitem refunded")
143+
self.assertEqual(result.response_message, "lineitem refunded")
136144
except TwocheckoutError as error:
137-
self.assertEqual(error.message, "Lineitem was already refunded.")
145+
self.assertEqual(error.message, "Lineitem amount greater than remaining balance on invoice.")
138146

139147
def test_6_stop_sale(self):
140148
sale = twocheckout.Sale.find(EXAMPLE_SALE)
@@ -147,7 +155,8 @@ def test_7_stop_invoice(self):
147155
result = invoice.stop()
148156
self.assertEqual(result.response_message, "No active recurring lineitems")
149157

150-
def test_6_stop_sale(self):
158+
## If you have already run "test_7_stop_invoice" then "test_8_stop_sale_lineitem" will fail for the same sale_id.
159+
def test_8_stop_sale_lineitem(self):
151160
sale = twocheckout.Sale.find(EXAMPLE_SALE)
152161
invoice = sale.invoices[0]
153162
try:
@@ -156,37 +165,30 @@ def test_6_stop_sale(self):
156165
except TwocheckoutError as error:
157166
self.assertEqual(error.message, "Lineitem is not scheduled to recur.")
158167

159-
def test_7_comment(self):
168+
def test_9_comment(self):
160169
sale = twocheckout.Sale.find(EXAMPLE_SALE)
161170
result = sale.comment(EXAMPLE_COMMENT)
162171
self.assertEqual(result.response_message, "Created comment successfully.")
163172

164-
def test_8_ship(self):
173+
def test_10_ship(self):
165174
try:
166175
sale = twocheckout.Sale.find(EXAMPLE_SALE)
167176
result = sale.ship(EXAMPLE_SHIP)
168177
except TwocheckoutError as error:
169178
self.assertEqual(error.message, "Sale already marked shipped.")
170179

171-
def test_9_reauth(self):
172-
try:
173-
sale = twocheckout.Sale.find(EXAMPLE_SALE)
174-
sale.reauth()
175-
except TwocheckoutError as error:
176-
self.assertEqual(error.message, "Payment is already pending or deposited and cannot be reauthorized.")
177-
178180
class ProductTest(TwocheckoutTestCase):
179181
def setUp(self):
180182
super(ProductTest, self).setUp()
181183

182184
def test_1_create(self):
183185
result = twocheckout.Product.create(EXAMPLE_PRODUCT)
184-
self.assertEqual(result.response_message, "Product successfully created")
186+
self.assertEqual(result.response_message, "Product successfully created.")
185187
EXAMPLE_PRODUCT['product_id'] = result.product_id
186188

187189
def test_2_find(self):
188190
product = twocheckout.Product.find(EXAMPLE_PRODUCT)
189-
self.assertEqual(product.name, "Example Product")
191+
self.assertEqual(product.name, "Python Example Product")
190192

191193
def test_3_update(self):
192194
product = twocheckout.Product.find(EXAMPLE_PRODUCT)
@@ -204,75 +206,21 @@ def test_5_list(self):
204206
list = twocheckout.Product.list(params)
205207
self.assertEqual(len(list), 2)
206208

207-
class OptionTest(TwocheckoutTestCase):
208-
def setUp(self):
209-
super(OptionTest, self).setUp()
210-
211-
def test_1_create(self):
212-
result = twocheckout.Option.create(EXAMPLE_OPTION)
213-
self.assertEqual(result.response_message, "Option created successfully")
214-
EXAMPLE_OPTION['option_id'] = result.option_id
215-
216-
def test_2_find(self):
217-
option = twocheckout.Option.find(EXAMPLE_OPTION)
218-
self.assertEqual(option.option_name, "Example Option")
219-
220-
def test_3_update(self):
221-
option = twocheckout.Option.find(EXAMPLE_OPTION)
222-
params = dict()
223-
params['option_name'] = "Updated Name"
224-
option = option.update(params)
225-
self.assertEqual(option.option_name, "Updated Name")
226-
227-
def test_4_delete(self):
228-
option = twocheckout.Option.find(EXAMPLE_OPTION)
229-
result = option.delete(EXAMPLE_OPTION)
230-
self.assertEqual(result.response_message, "Option deleted successfully")
231-
232-
def test_5_list(self):
233-
params = {'pagesize': 3}
234-
list = twocheckout.Option.list(params)
235-
self.assertEqual(len(list), 3)
236-
237-
class CouponTest(TwocheckoutTestCase):
238-
def setUp(self):
239-
super(CouponTest, self).setUp()
240-
241-
def test_1_create(self):
242-
result = twocheckout.Coupon.create(EXAMPLE_COUPON)
243-
EXAMPLE_COUPON['coupon_code'] = result.coupon_code
244-
self.assertEqual(result.response_message, "Coupon successfully created")
245-
246-
def test_2_find(self):
247-
coupon = twocheckout.Coupon.find(EXAMPLE_COUPON)
248-
self.assertEqual(coupon.coupon_code, EXAMPLE_COUPON['coupon_code'])
249-
250-
def test_3_update(self):
251-
coupon = twocheckout.Coupon.find(EXAMPLE_COUPON)
252-
EXAMPLE_COUPON['date_expire'] = "2020-01-02"
253-
coupon = coupon.update(EXAMPLE_COUPON)
254-
self.assertEqual(coupon.date_expire, "2020-01-02")
255-
256-
def test_4_delete(self):
257-
coupon = twocheckout.Coupon.find(EXAMPLE_COUPON)
258-
result = coupon.delete(EXAMPLE_COUPON)
259-
self.assertEqual(result.response_message, "Coupon successfully deleted.")
260-
261209
class CompanyTest(TwocheckoutTestCase):
262210
def setUp(self):
263211
super(CompanyTest, self).setUp()
264212

265213
def test_1_retrieve(self):
266214
company = twocheckout.Company.retrieve()
267-
self.assertEqual(company.vendor_id, "901248204")
215+
self.assertEqual(company.vendor_id, "250111206876")
268216

269217
class ContactTest(TwocheckoutTestCase):
270218
def setUp(self):
271219
super(ContactTest, self).setUp()
272220

273221
def test_1_create(self):
274222
contact = twocheckout.Contact.retrieve()
275-
self.assertEqual(contact.vendor_id, "901248204")
223+
self.assertEqual(contact.vendor_id, "250111206876")
276224

277225
class PaymentTest(TwocheckoutTestCase):
278226
def setUp(self):
@@ -304,17 +252,5 @@ def test_1_check(self):
304252
result = twocheckout.Notification.check(params)
305253
self.assertEqual(result.response_code, "SUCCESS")
306254

307-
class AuthorizationTest(TwocheckoutTestCase):
308-
def setUp(self):
309-
super(AuthorizationTest, self).setUp()
310-
311-
def test_1_auth(self):
312-
params = EXAMPLE_AUTH
313-
try:
314-
result = twocheckout.Charge.authorize(params)
315-
self.assertEqual(result.responseCode, "APPROVED")
316-
except TwocheckoutError as error:
317-
self.assertEqual(error.msg, "Unauthorized")
318-
319255
if __name__ == '__main__':
320256
unittest.main()

twocheckout/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from passback import Passback
55
from ins import Notification
66
from product import Product
7-
from option import Option
8-
from coupon import Coupon
97
from contact import Contact
108
from company import Company
119
from charge import Charge

twocheckout/api_request.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,17 @@ class Api:
1010
password = None
1111
private_key = None
1212
seller_id = None
13-
mode = None
1413
version = '1'
1514

1615
@classmethod
1716
def credentials(cls, credentials):
1817
Api.username = credentials['username']
1918
Api.password = credentials['password']
20-
if 'mode' in credentials:
21-
Api.mode = credentials['mode']
2219

2320
@classmethod
2421
def auth_credentials(cls, credentials):
2522
Api.private_key = credentials['private_key']
2623
Api.seller_id = credentials['seller_id']
27-
if 'mode' in credentials:
28-
Api.mode = credentials['mode']
2924

3025
@classmethod
3126
def call(cls, method, params=None):
@@ -60,10 +55,7 @@ def set_opts(cls, method, params=None):
6055
else:
6156
username = cls.username
6257
password = cls.password
63-
if cls.mode == 'sandbox':
64-
passwd_url = 'https://sandbox.2checkout.com'
65-
else:
66-
passwd_url = 'https://www.2checkout.com'
58+
passwd_url = 'https://www.2checkout.com'
6759
data = urllib.urlencode(params)
6860
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
6961
password_manager.add_password(
@@ -91,10 +83,7 @@ def build_headers(cls, method):
9183

9284
@classmethod
9385
def build_url(cls, method):
94-
if cls.mode == 'sandbox':
95-
url = 'https://sandbox.2checkout.com'
96-
else:
97-
url = 'https://www.2checkout.com'
86+
url = 'https://www.2checkout.com'
9887
if method == 'authService':
9988
url += '/checkout/api/' + cls.version + '/' + cls.seller_id + '/rs/' + method
10089
else:

0 commit comments

Comments
 (0)