forked from fixmycode/pykhipu
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit(deps): bump actions/checkout from 2 to 4 (#1)
* commit(deps): bump actions/checkout from 2 to 4 Bumps [actions/checkout](https://github.com/actions/checkout) from 2 to 4. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](actions/checkout@v2...v4) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
8f04e64
commit 1331037
Showing
7 changed files
with
213 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
import requests | ||
from pykhipu.responses import BanksResponse | ||
|
||
|
||
class Banks(object): | ||
ENDPOINT = '/banks' | ||
class Banks: | ||
ENDPOINT = "/banks" | ||
|
||
def __init__(self, client): | ||
self.client = client | ||
|
||
def get(self): | ||
response = self.client.make_request('GET', self.ENDPOINT) | ||
response = self.client.make_request("GET", self.ENDPOINT) | ||
return BanksResponse.from_response(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,135 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
import hmac | ||
import logging | ||
import os | ||
import sys | ||
|
||
import requests | ||
import hmac | ||
import logging | ||
|
||
if sys.version_info.major < 3 or sys.version_info.minor < 9: | ||
from importlib_metadata import version | ||
else: | ||
from importlib.metadata import version | ||
|
||
from hashlib import sha256 | ||
from six.moves.urllib.parse import urlencode, quote | ||
#from urllib.parse import urlencode, quote | ||
from pykhipu.payments import Payments | ||
|
||
from pykhipu.banks import Banks | ||
|
||
# from urllib.parse import urlencode, quote | ||
from pykhipu.payments import Payments | ||
from pykhipu.receivers import Receivers | ||
from urllib.parse import quote, urlencode | ||
|
||
API_BASE = "https://khipu.com/api/2.0" | ||
TRUE_LIST = [True, "True", "true", "TRUE", 1, "1"] | ||
FALSE_LIST = [False, "False", "false", "FALSE", 0, "0"] | ||
|
||
API_BASE = 'https://khipu.com/api/2.0' | ||
TRUE_LIST = [True, 'True', 'true', 'TRUE', 1, '1'] | ||
FALSE_LIST = [False, 'False', 'false', 'FALSE', 0, '0'] | ||
|
||
class Client(object): | ||
class Client: | ||
def __init__(self, receiver_id=None, secret=None, debug=False): | ||
self.receiver_id = receiver_id | ||
self.secret = secret | ||
self.is_debug = debug | ||
|
||
@property | ||
def receiver_id(self): | ||
return self._receiver_id or os.getenv('KHIPU_RECEIVER_ID') | ||
return self._receiver_id or os.getenv("KHIPU_RECEIVER_ID") | ||
|
||
@receiver_id.setter | ||
def receiver_id(self, value): | ||
self._receiver_id = value | ||
|
||
@property | ||
def secret(self): | ||
return self._secret or os.getenv('KHIPU_SECRET') | ||
return self._secret or os.getenv("KHIPU_SECRET") | ||
|
||
@secret.setter | ||
def secret(self, value): | ||
self._secret = value | ||
|
||
@property | ||
def is_debug(self): | ||
return self._debug or (os.getenv('KHIPU_DEBUG') in TRUE_LIST) | ||
return self._debug or (os.getenv("KHIPU_DEBUG") in TRUE_LIST) | ||
|
||
@is_debug.setter | ||
def is_debug(self, value): | ||
self._debug = ( | ||
value in TRUE_LIST | ||
and value not in FALSE_LIST | ||
) | ||
|
||
self._debug = value in TRUE_LIST and value not in FALSE_LIST | ||
|
||
@property | ||
def payments(self): | ||
if not hasattr(self, '_payments'): | ||
if not hasattr(self, "_payments"): | ||
self._payments = Payments(self) | ||
return self._payments | ||
|
||
@property | ||
def banks(self): | ||
if not hasattr(self, '_banks'): | ||
if not hasattr(self, "_banks"): | ||
self._banks = Banks(self) | ||
return self._banks | ||
|
||
@property | ||
def receivers(self): | ||
if not hasattr(self, '_receivers'): | ||
if not hasattr(self, "_receivers"): | ||
self._receivers = Receivers(self) | ||
return self._receivers | ||
|
||
def __make_signature(self, method, url, params=None, data=None): | ||
method_name = method.upper() | ||
to_sign = '&'.join([method_name, quote(url, safe='')]) | ||
to_sign = "&".join([method_name, quote(url, safe="")]) | ||
|
||
def quote_items(tuples): | ||
return ['='.join([quote(str(pair[0]), safe=''), | ||
quote(str(pair[1]), safe='')]) for pair in tuples] | ||
return [ | ||
"=".join([quote(str(pair[0]), safe=""), quote(str(pair[1]), safe="")]) | ||
for pair in tuples | ||
] | ||
|
||
if params: | ||
sorted_items = sorted(params.items(), key = lambda item: item[0]) | ||
to_sign = '&'.join([to_sign,] + quote_items(sorted_items)) | ||
sorted_items = sorted(params.items(), key=lambda item: item[0]) | ||
to_sign = "&".join( | ||
[ | ||
to_sign, | ||
] | ||
+ quote_items(sorted_items) | ||
) | ||
if data: | ||
sorted_items = sorted(data.items(), key = lambda item: item[0]) | ||
to_sign = '&'.join([to_sign,] + quote_items(sorted_items)) | ||
|
||
hasher = hmac.new(self.secret.encode(), to_sign.encode('UTF-8'), digestmod=sha256) | ||
signature = "{id}:{hash}".format(id=self.receiver_id, | ||
hash=hasher.hexdigest()) | ||
sorted_items = sorted(data.items(), key=lambda item: item[0]) | ||
to_sign = "&".join( | ||
[ | ||
to_sign, | ||
] | ||
+ quote_items(sorted_items) | ||
) | ||
|
||
hasher = hmac.new( | ||
self.secret.encode(), to_sign.encode("UTF-8"), digestmod=sha256 | ||
) | ||
signature = f"{self.receiver_id}:{hasher.hexdigest()}" | ||
|
||
if self.is_debug: | ||
logging.debug('a firmar: %s', to_sign) | ||
logging.debug('firma: %s', signature) | ||
logging.debug("a firmar: %s", to_sign) | ||
logging.debug("firma: %s", signature) | ||
|
||
return signature | ||
|
||
def make_request(self, method, endpoint, params=None, data=None): | ||
url = API_BASE + endpoint | ||
signature = self.__make_signature(method, url, params, data) | ||
payload = { | ||
'headers': { | ||
'Authorization': signature, | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'User-Agent': 'pykhipu/{version}'.format(version=version('pykhipu')), | ||
'Accept': 'application/json' | ||
"headers": { | ||
"Authorization": signature, | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"User-Agent": "pykhipu/{version}".format(version=version("pykhipu")), | ||
"Accept": "application/json", | ||
} | ||
} | ||
|
||
if params: | ||
payload.update({ 'params': params }) | ||
payload.update({"params": params}) | ||
if data: | ||
payload.update({ 'data': data }) | ||
payload.update({"data": data}) | ||
|
||
response = requests.request(method, url, **payload) | ||
|
||
if self.is_debug: | ||
logging.debug('Response: %s', response.text) | ||
logging.debug("Response: %s", response.text) | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.