From 9168779a57750c62f60a0e87e76918a96b86d13b Mon Sep 17 00:00:00 2001 From: "German M. Bravo" Date: Sat, 14 Mar 2015 12:56:16 -0600 Subject: [PATCH] Api is now an object. Fixes #4 --- README.md | 19 ++++---- test/test_twocheckout.py | 88 ++++++++++++++++++++++---------------- twocheckout/api_request.py | 70 ++++++++++++++---------------- twocheckout/charge.py | 42 ++++++++---------- twocheckout/company.py | 9 +--- twocheckout/contact.py | 8 +--- twocheckout/coupon.py | 24 +++++------ twocheckout/ins.py | 25 +++++------ twocheckout/option.py | 24 +++++------ twocheckout/passback.py | 25 +++++------ twocheckout/payment.py | 12 ++---- twocheckout/product.py | 24 +++++------ twocheckout/sale.py | 53 ++++++++++++----------- twocheckout/twocheckout.py | 7 +-- twocheckout/util.py | 3 +- 15 files changed, 214 insertions(+), 219 deletions(-) mode change 100755 => 100644 twocheckout/api_request.py mode change 100755 => 100644 twocheckout/charge.py diff --git a/README.md b/README.md index b9a37d2..1550226 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,9 @@ Example Purchase API Usage import twocheckout -twocheckout.Api.auth_credentials({ +api = twocheckout.Api() + +api.auth_credentials({ 'private_key': '3508079E-5383-44D4-BF69-DC619C0D9811', 'seller_id': '1817037', 'mode': 'production' @@ -63,7 +65,7 @@ params = { } try: - result = twocheckout.Charge.authorize(params) + result = twocheckout.Charge.authorize(api, params) print result.responseCode except TwocheckoutError as error: print error.msg @@ -136,8 +138,9 @@ Example Admin API Usage ```python import twocheckout +api = twocheckout.Api() -twocheckout.Api.credentials({'username':'APIuser1817037', 'password':'APIpass1817037'}) +api.credentials({'username':'APIuser1817037', 'password':'APIpass1817037'}) params = { 'sale_id': 4774467596, @@ -145,7 +148,7 @@ params = { 'comment': "Refunding Sale" } -sale = twocheckout.Sale.find(params) +sale = twocheckout.Sale.find(api, params) sale.refund(params); ``` @@ -170,7 +173,7 @@ params = { 'total': 1.00 } -form = twocheckout.Charge.submit(params) +form = twocheckout.Charge.submit(api, params) ``` *Example Response:* @@ -193,7 +196,7 @@ Example Return Usage: ```python params = web.input() # using web.py params['secret'] = 'tango' -result = twocheckout.Passback.check(params) +result = twocheckout.Passback.check(api, params) ``` *Example Response:* @@ -213,7 +216,7 @@ Example INS Usage: ```python params = web.input() # using web.py params['secret'] = 'tango' -result = twocheckout.Notification.check(params) +result = twocheckout.Notification.check(api, params) ``` *Example Response:* @@ -235,7 +238,7 @@ TwocheckoutError exceptions are thrown by if an error has returned. It is best t ```python try: - sale = twocheckout.Sale.find(EXAMPLE_SALE) + sale = twocheckout.Sale.find(api, EXAMPLE_SALE) invoice = sale.invoices[0] lineitem = invoice.lineitems[0] result = lineitem.refund(EXAMPLE_REFUND) diff --git a/test/test_twocheckout.py b/test/test_twocheckout.py index 35d1430..ecf74ae 100755 --- a/test/test_twocheckout.py +++ b/test/test_twocheckout.py @@ -1,11 +1,13 @@ +#!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys import datetime import unittest -from twocheckout import TwocheckoutError sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + +from twocheckout import TwocheckoutError import twocheckout @@ -77,41 +79,45 @@ } } + class TwocheckoutTestCase(unittest.TestCase): def setUp(self): super(TwocheckoutTestCase, self).setUp() - twocheckout.Api.credentials({ + self.api = twocheckout.Api() + + self.api.credentials({ 'username': 'testlibraryapi901248204', 'password': 'testlibraryapi901248204PASS', 'mode': 'sandbox' }) - twocheckout.Api.auth_credentials({ + self.api.auth_credentials({ 'private_key': 'BE632CB0-BB29-11E3-AFB6-D99C28100996', 'seller_id': '901248204', 'mode': 'sandbox' }) + class SaleTest(TwocheckoutTestCase): def setUp(self): super(SaleTest, self).setUp() def test_1_find_sale(self): try: - sale = twocheckout.Sale.find(EXAMPLE_SALE) + sale = twocheckout.Sale.find(self.api, EXAMPLE_SALE) self.assertEqual(int(sale.sale_id), 9093717691800) except TwocheckoutError as error: self.assertEqual(error.message, "Unable to find record.") def test_2_list_sale(self): params = {'pagesize': 2} - list = twocheckout.Sale.list(params) + list = twocheckout.Sale.list(self.api, params) self.assertEqual(len(list), 2) def test_3_refund_sale(self): try: - sale = twocheckout.Sale.find(EXAMPLE_SALE) + sale = twocheckout.Sale.find(self.api, EXAMPLE_SALE) result = sale.refund(EXAMPLE_REFUND) self.assertEqual(result.message, "refund added to invoice") except TwocheckoutError as error: @@ -119,7 +125,7 @@ def test_3_refund_sale(self): def test_4_refund_invoice(self): try: - sale = twocheckout.Sale.find(EXAMPLE_SALE) + sale = twocheckout.Sale.find(self.api, EXAMPLE_SALE) invoice = sale.invoices[0] result = invoice.refund(EXAMPLE_REFUND) self.assertEqual(result.message, "refund added to invoice") @@ -128,7 +134,7 @@ def test_4_refund_invoice(self): def test_5_refund_lineitem(self): try: - sale = twocheckout.Sale.find(EXAMPLE_SALE) + sale = twocheckout.Sale.find(self.api, EXAMPLE_SALE) invoice = sale.invoices[0] lineitem = invoice.lineitems[0] result = lineitem.refund(EXAMPLE_REFUND) @@ -137,18 +143,18 @@ def test_5_refund_lineitem(self): self.assertEqual(error.message, "Lineitem was already refunded.") def test_6_stop_sale(self): - sale = twocheckout.Sale.find(EXAMPLE_SALE) + sale = twocheckout.Sale.find(self.api, EXAMPLE_SALE) result = sale.stop() self.assertEqual(result.response_message, "No active recurring lineitems") def test_7_stop_invoice(self): - sale = twocheckout.Sale.find(EXAMPLE_SALE) + sale = twocheckout.Sale.find(self.api, EXAMPLE_SALE) invoice = sale.invoices[0] result = invoice.stop() self.assertEqual(result.response_message, "No active recurring lineitems") - def test_6_stop_sale(self): - sale = twocheckout.Sale.find(EXAMPLE_SALE) + def test_6_stop_sale2(self): + sale = twocheckout.Sale.find(self.api, EXAMPLE_SALE) invoice = sale.invoices[0] try: lineitem = invoice.lineitems[0] @@ -157,153 +163,162 @@ def test_6_stop_sale(self): self.assertEqual(error.message, "Lineitem is not scheduled to recur.") def test_7_comment(self): - sale = twocheckout.Sale.find(EXAMPLE_SALE) + sale = twocheckout.Sale.find(self.api, EXAMPLE_SALE) result = sale.comment(EXAMPLE_COMMENT) self.assertEqual(result.response_message, "Created comment successfully.") def test_8_ship(self): try: - sale = twocheckout.Sale.find(EXAMPLE_SALE) + sale = twocheckout.Sale.find(self.api, EXAMPLE_SALE) result = sale.ship(EXAMPLE_SHIP) except TwocheckoutError as error: self.assertEqual(error.message, "Sale already marked shipped.") def test_9_reauth(self): try: - sale = twocheckout.Sale.find(EXAMPLE_SALE) + sale = twocheckout.Sale.find(self.api, EXAMPLE_SALE) sale.reauth() except TwocheckoutError as error: self.assertEqual(error.message, "Payment is already pending or deposited and cannot be reauthorized.") + class ProductTest(TwocheckoutTestCase): def setUp(self): super(ProductTest, self).setUp() def test_1_create(self): - result = twocheckout.Product.create(EXAMPLE_PRODUCT) + result = twocheckout.Product.create(self.api, EXAMPLE_PRODUCT) self.assertEqual(result.response_message, "Product successfully created") EXAMPLE_PRODUCT['product_id'] = result.product_id def test_2_find(self): - product = twocheckout.Product.find(EXAMPLE_PRODUCT) + product = twocheckout.Product.find(self.api, EXAMPLE_PRODUCT) self.assertEqual(product.name, "Example Product") def test_3_update(self): - product = twocheckout.Product.find(EXAMPLE_PRODUCT) + product = twocheckout.Product.find(self.api, EXAMPLE_PRODUCT) EXAMPLE_PRODUCT['name'] = "Updated Name" product = product.update(EXAMPLE_PRODUCT) self.assertEqual(product.name, "Updated Name") def test_4_delete(self): - product = twocheckout.Product.find(EXAMPLE_PRODUCT) + product = twocheckout.Product.find(self.api, EXAMPLE_PRODUCT) result = product.delete(EXAMPLE_PRODUCT) self.assertEqual(result.response_message, "Product successfully deleted.") def test_5_list(self): params = {'pagesize': 2} - list = twocheckout.Product.list(params) + list = twocheckout.Product.list(self.api, params) self.assertEqual(len(list), 2) + class OptionTest(TwocheckoutTestCase): def setUp(self): super(OptionTest, self).setUp() def test_1_create(self): - result = twocheckout.Option.create(EXAMPLE_OPTION) + result = twocheckout.Option.create(self.api, EXAMPLE_OPTION) self.assertEqual(result.response_message, "Option created successfully") EXAMPLE_OPTION['option_id'] = result.option_id def test_2_find(self): - option = twocheckout.Option.find(EXAMPLE_OPTION) + option = twocheckout.Option.find(self.api, EXAMPLE_OPTION) self.assertEqual(option.option_name, "Example Option") def test_3_update(self): - option = twocheckout.Option.find(EXAMPLE_OPTION) + option = twocheckout.Option.find(self.api, EXAMPLE_OPTION) params = dict() params['option_name'] = "Updated Name" option = option.update(params) self.assertEqual(option.option_name, "Updated Name") def test_4_delete(self): - option = twocheckout.Option.find(EXAMPLE_OPTION) + option = twocheckout.Option.find(self.api, EXAMPLE_OPTION) result = option.delete(EXAMPLE_OPTION) self.assertEqual(result.response_message, "Option deleted successfully") def test_5_list(self): params = {'pagesize': 3} - list = twocheckout.Option.list(params) + list = twocheckout.Option.list(self.api, params) self.assertEqual(len(list), 3) + class CouponTest(TwocheckoutTestCase): def setUp(self): super(CouponTest, self).setUp() def test_1_create(self): - result = twocheckout.Coupon.create(EXAMPLE_COUPON) + result = twocheckout.Coupon.create(self.api, EXAMPLE_COUPON) EXAMPLE_COUPON['coupon_code'] = result.coupon_code self.assertEqual(result.response_message, "Coupon successfully created") def test_2_find(self): - coupon = twocheckout.Coupon.find(EXAMPLE_COUPON) + coupon = twocheckout.Coupon.find(self.api, EXAMPLE_COUPON) self.assertEqual(coupon.coupon_code, EXAMPLE_COUPON['coupon_code']) def test_3_update(self): - coupon = twocheckout.Coupon.find(EXAMPLE_COUPON) + coupon = twocheckout.Coupon.find(self.api, EXAMPLE_COUPON) EXAMPLE_COUPON['date_expire'] = "2020-01-02" coupon = coupon.update(EXAMPLE_COUPON) self.assertEqual(coupon.date_expire, "2020-01-02") def test_4_delete(self): - coupon = twocheckout.Coupon.find(EXAMPLE_COUPON) + coupon = twocheckout.Coupon.find(self.api, EXAMPLE_COUPON) result = coupon.delete(EXAMPLE_COUPON) self.assertEqual(result.response_message, "Coupon successfully deleted.") + class CompanyTest(TwocheckoutTestCase): def setUp(self): super(CompanyTest, self).setUp() def test_1_retrieve(self): - company = twocheckout.Company.retrieve() + company = twocheckout.Company.retrieve(self.api) self.assertEqual(company.vendor_id, "901248204") + class ContactTest(TwocheckoutTestCase): def setUp(self): super(ContactTest, self).setUp() def test_1_create(self): - contact = twocheckout.Contact.retrieve() + contact = twocheckout.Contact.retrieve(self.api) self.assertEqual(contact.vendor_id, "901248204") + class PaymentTest(TwocheckoutTestCase): def setUp(self): super(PaymentTest, self).setUp() def test_1_pending(self): - payment = twocheckout.Payment.pending() + payment = twocheckout.Payment.pending(self.api) self.assertEqual(payment.release_level, "300") def test_2_list(self): - payments = twocheckout.Payment.list() + payments = twocheckout.Payment.list(self.api) self.assertEqual(len(payments), 0) + class PassbackTest(TwocheckoutTestCase): def setUp(self): super(PassbackTest, self).setUp() def test_1_check(self): params = EXAMPLE_PASSBACK - result = twocheckout.Passback.check(params) + result = twocheckout.Passback.check(self.api, params) self.assertEqual(result.response_code, "SUCCESS") + class NotificationTest(TwocheckoutTestCase): def setUp(self): super(NotificationTest, self).setUp() def test_1_check(self): params = EXAMPLE_NOTIFICATION - result = twocheckout.Notification.check(params) + result = twocheckout.Notification.check(self.api, params) self.assertEqual(result.response_code, "SUCCESS") + class AuthorizationTest(TwocheckoutTestCase): def setUp(self): super(AuthorizationTest, self).setUp() @@ -311,10 +326,11 @@ def setUp(self): def test_1_auth(self): params = EXAMPLE_AUTH try: - result = twocheckout.Charge.authorize(params) + result = twocheckout.Charge.authorize(self.api, params) self.assertEqual(result.responseCode, "APPROVED") except TwocheckoutError as error: self.assertEqual(error.msg, "Unauthorized") + if __name__ == '__main__': unittest.main() diff --git a/twocheckout/api_request.py b/twocheckout/api_request.py old mode 100755 new mode 100644 index 592a6cf..f8eac6f --- a/twocheckout/api_request.py +++ b/twocheckout/api_request.py @@ -1,45 +1,44 @@ import urllib import urllib2 import json -from error import TwocheckoutError +from error import TwocheckoutError -class Api: - username = None - password = None - private_key = None - seller_id = None - mode = None +class Api(object): version = '1' - @classmethod - def credentials(cls, credentials): - Api.username = credentials['username'] - Api.password = credentials['password'] + def __init__(self, seller_id=None, private_key=None, username=None, password=None, mode=None): + self.seller_id = seller_id + self.private_key = private_key + self.username = username + self.password = password + self.mode = mode + + def credentials(self, credentials): + self.username = credentials['username'] + self.password = credentials['password'] if 'mode' in credentials: - Api.mode = credentials['mode'] + self.mode = credentials['mode'] - @classmethod - def auth_credentials(cls, credentials): - Api.private_key = credentials['private_key'] - Api.seller_id = credentials['seller_id'] + def auth_credentials(self, credentials): + self.private_key = credentials['private_key'] + self.seller_id = credentials['seller_id'] if 'mode' in credentials: - Api.mode = credentials['mode'] + self.mode = credentials['mode'] - @classmethod - def call(cls, method, params=None): - data = cls.set_opts(method, params) - url = cls.build_url(method) - headers = cls.build_headers(method) + def call(self, method, params=None): + data = self.set_opts(method, params) + url = self.build_url(method) + headers = self.build_headers(method) try: req = urllib2.Request(url, data, headers) result = urllib2.urlopen(req).read() - result_safe=None + result_safe = None try: result_safe = unicode(result) except UnicodeDecodeError: - result_safe = unicode( str(result).decode('utf-8', 'ignore') ) + result_safe = unicode(str(result).decode('utf-8', 'ignore')) return json.loads(result_safe) except urllib2.HTTPError, e: if not hasattr(e, 'read'): @@ -51,16 +50,15 @@ def call(cls, method, params=None): else: raise TwocheckoutError(exception['errors'][0]['code'], exception['errors'][0]['message']) - @classmethod - def set_opts(cls, method, params=None): + def set_opts(self, method, params=None): if method == 'authService': - params['sellerId'] = cls.seller_id - params['privateKey'] = cls.private_key + params['sellerId'] = self.seller_id + params['privateKey'] = self.private_key data = json.dumps(params) else: - username = cls.username - password = cls.password - if cls.mode == 'sandbox': + username = self.username + password = self.password + if self.mode == 'sandbox': passwd_url = 'https://sandbox.2checkout.com' else: passwd_url = 'https://www.2checkout.com' @@ -74,8 +72,7 @@ def set_opts(cls, method, params=None): urllib2.install_opener(opener) return data - @classmethod - def build_headers(cls, method): + def build_headers(self, method): if method == 'authService': headers = { 'Accept': 'application/json', @@ -89,14 +86,13 @@ def build_headers(cls, method): } return headers - @classmethod - def build_url(cls, method): - if cls.mode == 'sandbox': + def build_url(self, method): + if self.mode == 'sandbox': url = 'https://sandbox.2checkout.com' else: url = 'https://www.2checkout.com' if method == 'authService': - url += '/checkout/api/' + cls.version + '/' + cls.seller_id + '/rs/' + method + url += '/checkout/api/' + self.version + '/' + self.seller_id + '/rs/' + method else: url += '/api/' + method return url diff --git a/twocheckout/charge.py b/twocheckout/charge.py old mode 100755 new mode 100644 index 6766561..4047704 --- a/twocheckout/charge.py +++ b/twocheckout/charge.py @@ -1,54 +1,46 @@ import urllib -from api_request import Api + from twocheckout import Twocheckout class Charge(Twocheckout): - checkout_url = "https://www.2checkout.com/checkout/purchase" - def __init__(self, dict_): - super(self.__class__, self).__init__(dict_) - - @classmethod - def mode(cls, mode): - if mode == 'sandbox': - Charge.checkout_url = 'https://sandbox.2checkout.com/checkout/purchase' + def __init__(self, dict_, api=None): + super(Charge, self).__init__(dict_, api=api) + if api.mode == 'sandbox': + self.checkout_url = 'https://sandbox.2checkout.com/checkout/purchase' else: - Charge.checkout_url = 'https://www.2checkout.com/checkout/purchase' + self.checkout_url = 'https://www.2checkout.com/checkout/purchase' - @classmethod - def form(cls, params=None, text='Proceed to Checkout'): + def form(self, params=None, text='Proceed to Checkout'): if params is None: params = dict() - form = "
" + form = "" for param in params: form = form + "" return form + "
" - @classmethod - def submit(cls, params=None, text='Proceed to Checkout'): + def submit(self, params=None, text='Proceed to Checkout'): if params is None: params = dict() - form = "
" + form = "" for param in params: form = form + "" return form + "
" + \ "" - @classmethod - def direct(cls, params=None, text='Proceed to Checkout'): + def direct(self, params=None, text='Proceed to Checkout'): if params is None: params = dict() - form = "
" + form = "" for param in params: form = form + "" return form + "
" + \ "" - @classmethod - def link(cls, params=None): - url = Charge.checkout_url + '?' + def link(self, params=None): + url = self.checkout_url + '?' if params is None: params = dict() param = urllib.urlencode(params) @@ -56,6 +48,6 @@ def link(cls, params=None): return url @classmethod - def authorize(cls, params=None): - response = Charge(Api.call('authService', params)) - return response.response \ No newline at end of file + def authorize(cls, api, params=None): + response = cls(api.call('authService', params), api=api) + return response.response diff --git a/twocheckout/company.py b/twocheckout/company.py index a8adf12..5666b56 100644 --- a/twocheckout/company.py +++ b/twocheckout/company.py @@ -1,16 +1,11 @@ -from api_request import Api from twocheckout import Twocheckout class Company(Twocheckout): - def __init__(self, dict_): - super(self.__class__, self).__init__(dict_) - @classmethod - def retrieve(cls, params=None): + def retrieve(cls, api, params=None): if params is None: params = dict() url = 'acct/detail_company_info' - response = cls(Api.call(url, params)) + response = cls(api.call(url, params), api=api) return response.vendor_company_info - diff --git a/twocheckout/contact.py b/twocheckout/contact.py index 15f70fd..54a2a63 100644 --- a/twocheckout/contact.py +++ b/twocheckout/contact.py @@ -1,15 +1,11 @@ -from api_request import Api from twocheckout import Twocheckout class Contact(Twocheckout): - def __init__(self, dict_): - super(self.__class__, self).__init__(dict_) - @classmethod - def retrieve(cls, params=None): + def retrieve(cls, api, params=None): if params is None: params = dict() url = 'acct/detail_contact_info' - response = cls(Api.call(url, params)) + response = cls(api.call(url, params), api=api) return response.vendor_contact_info diff --git a/twocheckout/coupon.py b/twocheckout/coupon.py index e179674..54eb1ee 100644 --- a/twocheckout/coupon.py +++ b/twocheckout/coupon.py @@ -1,40 +1,38 @@ -from api_request import Api from twocheckout import Twocheckout class Coupon(Twocheckout): - def __init__(self, dict_): - super(self.__class__, self).__init__(dict_) - @classmethod - def create(cls, params=None): + def create(cls, api, params=None): if params is None: params = dict() - return cls(Api.call('products/create_coupon', params)) + return cls(api.call('products/create_coupon', params), api=api) @classmethod - def find(cls, params=None): + def find(cls, api, params=None): if params is None: params = dict() - result = cls(Api.call('products/detail_coupon', params)) + result = cls(api.call('products/detail_coupon', params), api=api) return result.coupon @classmethod - def list(cls, params=None): + def list(cls, api, params=None): if params is None: params = dict() - return cls(Api.call('products/list_coupons', params)) + return cls(api.call('products/list_coupons', params), api=api) def update(self, params=None): + api = self.api if params is None: params = dict() params['coupon_code'] = self.coupon_code - Api.call('products/update_coupon', params) - coupon = Coupon(Api.call('products/detail_coupon', params)) + api.call('products/update_coupon', params) + coupon = Coupon(api.call('products/detail_coupon', params), api=api) return coupon.coupon def delete(self, params=None): + api = self.api if params is None: params = dict() params['coupon_code'] = self.coupon_code - return Coupon(Api.call('products/delete_coupon', params)) + return Coupon(api.call('products/delete_coupon', params), api=api) diff --git a/twocheckout/ins.py b/twocheckout/ins.py index 2c57084..63d0430 100644 --- a/twocheckout/ins.py +++ b/twocheckout/ins.py @@ -1,10 +1,8 @@ import hashlib from twocheckout import Twocheckout -class Notification(Twocheckout): - def __init__(self, dict_): - super(self.__class__, self).__init__(dict_) +class Notification(Twocheckout): @classmethod def check_hash(cls, params=None): m = hashlib.md5() @@ -20,21 +18,24 @@ def check_hash(cls, params=None): return False @classmethod - def check(cls, params=None): + def check(cls, api, params=None): if params is None: params = dict() if 'sale_id' in params and 'invoice_id' in params: - check = Notification.check_hash(params) + check = cls.check_hash(params) if check: - response = { "response_code": "SUCCESS", - "response_message": "Hash Matched" + response = { + "response_code": "SUCCESS", + "response_message": "Hash Matched" } else: - response = { "response_code": "FAILED", - "response_message": "Hash Mismatch" + response = { + "response_code": "FAILED", + "response_message": "Hash Mismatch" } else: - response = { "response_code": "ERROR", - "response_message": "You must pass sale_id, vendor_id, invoice_id, secret word." + response = { + "response_code": "ERROR", + "response_message": "You must pass sale_id, vendor_id, invoice_id, secret word." } - return cls(response) \ No newline at end of file + return cls(response, api=api) diff --git a/twocheckout/option.py b/twocheckout/option.py index f53c4ba..602e01d 100644 --- a/twocheckout/option.py +++ b/twocheckout/option.py @@ -1,41 +1,39 @@ -from api_request import Api from twocheckout import Twocheckout class Option(Twocheckout): - def __init__(self, dict_): - super(self.__class__, self).__init__(dict_) - @classmethod - def create(cls, params=None): + def create(cls, api, params=None): if params is None: params = dict() - return cls(Api.call('products/create_option', params)) + return cls(api.call('products/create_option', params), api=api) @classmethod - def find(cls, params=None): + def find(cls, api, params=None): if params is None: params = dict() - option = cls(Api.call('products/detail_option', params)) + option = cls(api.call('products/detail_option', params), api=api) return option.option[0] @classmethod - def list(cls, params=None): + def list(cls, api, params=None): if params is None: params = dict() - list = cls(Api.call('products/list_options', params)) + list = cls(api.call('products/list_options', params), api=api) return list.options def update(self, params=None): + api = self.api if params is None: params = dict() params['option_id'] = self.option_id - Api.call('products/update_option', params) - option = Option(Api.call('products/detail_option', params)) + api.call('products/update_option', params) + option = Option(api.call('products/detail_option', params), api=api) return option.option[0] def delete(self, params=None): + api = self.api if params is None: params = dict() params['option_id'] = self.option_id - return Option(Api.call('products/delete_option', params)) + return Option(api.call('products/delete_option', params), api=api) diff --git a/twocheckout/passback.py b/twocheckout/passback.py index a95c1df..3478b0a 100644 --- a/twocheckout/passback.py +++ b/twocheckout/passback.py @@ -1,10 +1,8 @@ import hashlib from twocheckout import Twocheckout -class Passback(Twocheckout): - def __init__(self, dict_): - super(self.__class__, self).__init__(dict_) +class Passback(Twocheckout): @classmethod def check_hash(cls, params=None): m = hashlib.md5() @@ -20,21 +18,24 @@ def check_hash(cls, params=None): return False @classmethod - def check(cls, params=None): + def check(cls, api, params=None): if params is None: params = dict() if 'order_number' in params and 'total' in params: - check = Passback.check_hash(params) + check = cls.check_hash(params) if check: - response = { "response_code": "SUCCESS", - "response_message":"Hash Matched" + response = { + "response_code": "SUCCESS", + "response_message": "Hash Matched" } else: - response = { "response_code": "FAILED", - "response_message": "Hash Mismatch" + response = { + "response_code": "FAILED", + "response_message": "Hash Mismatch" } else: - return { "response_code": "ERROR", - "response_message": "You must pass secret word, sid, order_number, total" + return { + "response_code": "ERROR", + "response_message": "You must pass secret word, sid, order_number, total" } - return cls(response) + return cls(response, api=api) diff --git a/twocheckout/payment.py b/twocheckout/payment.py index 6b24802..d119c85 100644 --- a/twocheckout/payment.py +++ b/twocheckout/payment.py @@ -1,23 +1,19 @@ -from .api_request import Api from .twocheckout import Twocheckout class Payment(Twocheckout): - def __init__(self, dict_): - super(self.__class__, self).__init__(dict_) - @classmethod - def pending(cls, params=None): + def pending(cls, api, params=None): if params is None: params = dict() url = 'acct/detail_pending_payment' - response = cls(Api.call(url, params)) + response = cls(api.call(url, params), api=api) return response.payment @classmethod - def list(cls, params=None): + def list(cls, api, params=None): if params is None: params = dict() url = 'acct/list_payments' - response = cls(Api.call(url, params)) + response = cls(api.call(url, params), api=api) return response.payments diff --git a/twocheckout/product.py b/twocheckout/product.py index 41f1ab4..24f37b0 100644 --- a/twocheckout/product.py +++ b/twocheckout/product.py @@ -1,41 +1,39 @@ -from api_request import Api from twocheckout import Twocheckout class Product(Twocheckout): - def __init__(self, dict_): - super(self.__class__, self).__init__(dict_) - @classmethod - def create(cls, params=None): + def create(cls, api, params=None): if params is None: params = dict() - return cls(Api.call('products/create_product', params)) + return cls(api.call('products/create_product', params), api=api) @classmethod - def find(cls, params=None): + def find(cls, api, params=None): if params is None: params = dict() - result = cls(Api.call('products/detail_product', params)) + result = cls(api.call('products/detail_product', params), api=api) return result.product @classmethod - def list(cls, params=None): + def list(cls, api, params=None): if params is None: params = dict() - result = cls(Api.call('products/list_products', params)) + result = cls(api.call('products/list_products', params), api=api) return result.products def update(self, params=None): + api = self.api if params is None: params = dict() params['product_id'] = self.product_id - Api.call('products/update_product', params) - product = Product(Api.call('products/detail_product', params)) + api.call('products/update_product', params) + product = Product(api.call('products/detail_product', params), api=api) return product.product def delete(self, params=None): + api = self.api if params is None: params = dict() params['product_id'] = self.product_id - return Product(Api.call('products/delete_product', params)) + return Product(api.call('products/delete_product', params), api=api) diff --git a/twocheckout/sale.py b/twocheckout/sale.py index d32446d..c262e56 100644 --- a/twocheckout/sale.py +++ b/twocheckout/sale.py @@ -1,27 +1,24 @@ -from api_request import Api from util import Util from twocheckout import Twocheckout class Sale(Twocheckout): - def __init__(self, dict_): - super(self.__class__, self).__init__(dict_) - @classmethod - def find(cls, params=None): + def find(cls, api, params=None): if params is None: params = dict() - response = cls(Api.call('sales/detail_sale', params)) + response = cls(api.call('sales/detail_sale', params), api=api) return response.sale @classmethod - def list(cls, params=None): + def list(cls, api, params=None): if params is None: params = dict() - response = cls(Api.call('sales/list_sales', params)) + response = cls(api.call('sales/list_sales', params), api=api) return response.sale_summary def refund(self, params=None): + api = self.api if params is None: params = dict() if hasattr(self, 'lineitem_id'): @@ -33,14 +30,15 @@ def refund(self, params=None): else: params['sale_id'] = self.sale_id url = 'sales/refund_invoice' - return Sale(Api.call(url, params)) + return Sale(api.call(url, params), api=api) def stop(self, params=None): + api = self.api if params is None: params = dict() if hasattr(self, 'lineitem_id'): params['lineitem_id'] = self.lineitem_id - return Api.call('sales/stop_lineitem_recurring', params) + return api.call('sales/stop_lineitem_recurring', params) elif hasattr(self, 'sale_id'): active_lineitems = Util.active(self) if dict(active_lineitems): @@ -49,10 +47,11 @@ def stop(self, params=None): for k, v in active_lineitems.items(): lineitem_id = v params = {'lineitem_id': lineitem_id} - result[i] = Api.call('sales/stop_lineitem_recurring', params) + result[i] = api.call('sales/stop_lineitem_recurring', params) i += 1 - response = { "response_code": "OK", - "response_message": str(len(result)) + " lineitems stopped successfully" + response = { + "response_code": "OK", + "response_message": str(len(result)) + " lineitems stopped successfully" } else: response = { @@ -60,12 +59,14 @@ def stop(self, params=None): "response_message": "No active recurring lineitems" } else: - response = { "response_code": "NOTICE", - "response_message": "This method can only be called on a sale or lineitem" + response = { + "response_code": "NOTICE", + "response_message": "This method can only be called on a sale or lineitem" } - return Sale(response) + return Sale(response, api=api) def active(self): + api = self.api active_lineitems = Util.active(self) if dict(active_lineitems): result = dict() @@ -74,29 +75,33 @@ def active(self): lineitem_id = v result[i] = lineitem_id i += 1 - response = { "response_code": "ACTIVE", - "response_message": str(len(result)) + " active recurring lineitems" + response = { + "response_code": "ACTIVE", + "response_message": str(len(result)) + " active recurring lineitems" } else: response = { - "response_code": "NOTICE","response_message": - "No active recurring lineitems" + "response_code": "NOTICE", + "response_message": "No active recurring lineitems" } - return Sale(response) + return Sale(response, api=api) def comment(self, params=None): + api = self.api if params is None: params = dict() params['sale_id'] = self.sale_id - return Sale(Api.call('sales/create_comment', params)) + return Sale(api.call('sales/create_comment', params), api=api) def ship(self, params=None): + api = self.api if params is None: params = dict() params['sale_id'] = self.sale_id - return Sale(Api.call('sales/mark_shipped', params)) + return Sale(api.call('sales/mark_shipped', params), api=api) def reauth(self): + api = self.api params = dict() params['sale_id'] = self.sale_id - return Sale(Api.call('sales/reauth', params)) + return Sale(api.call('sales/reauth', params), api=api) diff --git a/twocheckout/twocheckout.py b/twocheckout/twocheckout.py index d57c850..8af929e 100644 --- a/twocheckout/twocheckout.py +++ b/twocheckout/twocheckout.py @@ -1,14 +1,15 @@ class Twocheckout(dict): - def __init__(self, dict_): + def __init__(self, dict_, api=None): super(Twocheckout, self).__init__(dict_) + self.api = api for key in self: item = self[key] if isinstance(item, list): for id, it in enumerate(item): if isinstance(it, dict): - item[id] = self.__class__(it) + item[id] = self.__class__(it, api=api) elif isinstance(item, dict): - self[key] = self.__class__(item) + self[key] = self.__class__(item, api=api) def __getattr__(self, key): return self[key] diff --git a/twocheckout/util.py b/twocheckout/util.py index ec1c4e2..f79fc3f 100644 --- a/twocheckout/util.py +++ b/twocheckout/util.py @@ -1,5 +1,4 @@ class Util: - @classmethod def active(cls, sale): i = 0 @@ -18,4 +17,4 @@ def active(cls, sale): if lineitem_id.billing.recurring_status == 'active': lineitems[i] = lineitem_id['lineitem_id'] i += 1 - return lineitems \ No newline at end of file + return lineitems