X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/48a8d1d73adefae7f0c58015494ce956d7cd70df..ca336bd1f9658cf713681d1412d4153e5c4d9c93:/src/club/payu/pos.py diff --git a/src/club/payu/pos.py b/src/club/payu/pos.py new file mode 100644 index 000000000..c9dca99e7 --- /dev/null +++ b/src/club/payu/pos.py @@ -0,0 +1,47 @@ +import requests + + +class POS: + ACCESS_TOKEN_URL = '/pl/standard/user/oauth/authorize' + API_BASE = '/api/v2_1/' + + def __init__(self, pos_id, client_secret, secondary_key, sandbox=False, currency_code='PLN'): + self.pos_id = pos_id + self.client_secret = client_secret + self.secondary_key = secondary_key + self.sandbox = sandbox + self.currency_code = currency_code + + def get_api_host(self): + if self.sandbox: + return 'https://secure.snd.payu.com' + else: + return 'https://secure.payu.com' + + def get_access_token(self): + response = requests.post( + self.get_api_host() + self.ACCESS_TOKEN_URL, + data={ + 'grant_type': 'client_credentials', + 'client_id': self.pos_id, + 'client_secret': self.client_secret + }, + ) + assert response.status_code == 200 + data = response.json() + assert data['token_type'] == 'bearer' + assert data['grant_type'] == 'client_credentials' + return data['access_token'] + + def request(self, method, url, data): + access_token = self.get_access_token() + + full_url = self.get_api_host() + self.API_BASE + url + response = requests.request( + method, full_url, json=data, headers={ + 'Authorization': 'Bearer ' + access_token, + }, + allow_redirects=False + ) + return response.json() +