|
6 | 6 |
|
7 | 7 | class Api: |
8 | 8 |
|
9 | | - username = [] |
10 | | - password = [] |
| 9 | + username = None |
| 10 | + password = None |
| 11 | + private_key = None |
| 12 | + seller_id = None |
| 13 | + mode = None |
| 14 | + version = '1' |
11 | 15 |
|
12 | 16 | @classmethod |
13 | 17 | def credentials(cls, credentials): |
14 | 18 | Api.username = credentials['username'] |
15 | 19 | Api.password = credentials['password'] |
16 | 20 |
|
| 21 | + @classmethod |
| 22 | + def auth_credentials(cls, credentials): |
| 23 | + Api.private_key = credentials['private_key'] |
| 24 | + Api.seller_id = credentials['seller_id'] |
| 25 | + Api.mode = credentials['mode'] |
| 26 | + |
17 | 27 | @classmethod |
18 | 28 | def call(cls, method, params=None): |
19 | | - username = cls.username |
20 | | - password = cls.password |
21 | | - headers = {'Accept': 'application/json', |
22 | | - 'User-Agent': '2Checkout Python/0.1.0/%s' |
23 | | - } |
24 | | - base_url = 'https://www.2checkout.com/api/' |
25 | | - url = base_url + method |
26 | | - data = urllib.urlencode(params) |
27 | | - password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() |
28 | | - password_manager.add_password( |
29 | | - None, 'https://www.2checkout.com', username, password |
30 | | - ) |
31 | | - auth_handler = urllib2.HTTPBasicAuthHandler(password_manager) |
32 | | - opener = urllib2.build_opener(auth_handler) |
33 | | - urllib2.install_opener(opener) |
| 29 | + data = cls.set_opts(method, params) |
| 30 | + url = cls.build_url(method) |
| 31 | + headers = cls.build_headers(method) |
34 | 32 | try: |
35 | 33 | req = urllib2.Request(url, data, headers) |
36 | 34 | result = urllib2.urlopen(req).read() |
37 | 35 | return json.loads(result) |
38 | 36 | except urllib2.HTTPError, e: |
39 | 37 | exception = json.loads(e.read()) |
40 | | - raise TwocheckoutError(exception['errors'][0]['code'], exception['errors'][0]['message']) |
| 38 | + if method == 'authService': |
| 39 | + raise TwocheckoutError(exception['exception']['errorCode'], exception['exception']['errorMsg']) |
| 40 | + else: |
| 41 | + raise TwocheckoutError(exception['errors'][0]['code'], exception['errors'][0]['message']) |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def set_opts(cls, method, params=None): |
| 45 | + if method == 'authService': |
| 46 | + params['sellerId'] = cls.seller_id |
| 47 | + params['privateKey'] = cls.private_key |
| 48 | + data = json.dumps(params) |
| 49 | + else: |
| 50 | + username = cls.username |
| 51 | + password = cls.password |
| 52 | + data = urllib.urlencode(params) |
| 53 | + password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() |
| 54 | + password_manager.add_password( |
| 55 | + None, 'https://www.2checkout.com', username, password |
| 56 | + ) |
| 57 | + auth_handler = urllib2.HTTPBasicAuthHandler(password_manager) |
| 58 | + opener = urllib2.build_opener(auth_handler) |
| 59 | + urllib2.install_opener(opener) |
| 60 | + return data |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def build_headers(cls, method): |
| 64 | + if method == 'authService': |
| 65 | + headers = { |
| 66 | + 'Accept': 'application/json', |
| 67 | + 'User-Agent': '2Checkout Python/0.1.0/%s', |
| 68 | + 'Content-Type': 'application/JSON' |
| 69 | + } |
| 70 | + else: |
| 71 | + headers = { |
| 72 | + 'Accept': 'application/json', |
| 73 | + 'User-Agent': '2Checkout Python/0.1.0/%s' |
| 74 | + } |
| 75 | + return headers |
| 76 | + |
| 77 | + @classmethod |
| 78 | + def build_url(cls, method): |
| 79 | + if method == 'authService': |
| 80 | + if cls.mode == 'sandbox': |
| 81 | + url = 'https://sandbox.2checkout.com/checkout/api/' |
| 82 | + else: |
| 83 | + url = 'https://www.2checkout.com/checkout/api/' |
| 84 | + url += cls.version + '/' + cls.seller_id + '/rs/' + method |
| 85 | + else: |
| 86 | + url = 'https://www.2checkout.com/api/' + method |
| 87 | + return url |
0 commit comments