Created
September 6, 2018 23:06
-
-
Save Bernix01/1ba73194a7d615c180bbdec66d8d1988 to your computer and use it in GitHub Desktop.
Paymentez Toolbox
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
import os | |
import requests | |
import sys | |
import pprint | |
''' | |
Description: | |
Allows to list and delete user pm cards using Paymentez API. For development testing purposes only. | |
Usage: | |
Make sure you have requests installed with pip install requests. | |
Use as follows: | |
python pm.py get <uid> | |
python pm.py del <uid> <token> | |
''' | |
def get_auth_token(): | |
''' | |
Generates an auth token used in each request, as of API design it's only valid for 15s | |
so it must be wrapped in a function for reuse. | |
''' | |
import time | |
import hashlib | |
from base64 import b64encode | |
paymentez_server_application_code = os.getenv("PAYMENTEZ_AUTH_CODE", "=") | |
paymentez_server_app_key = os.getenv("PAYMENTEZ_AUTH_KEY", "=") | |
unix_timestamp = str(int(time.time())) | |
uniq_token_string = paymentez_server_app_key + unix_timestamp | |
uniq_token_hash = hashlib.sha256( | |
uniq_token_string.encode('utf-8')).hexdigest() | |
auth_token = b64encode(bytes('%s;%s;%s' % (paymentez_server_application_code, | |
unix_timestamp, uniq_token_hash), 'utf-8')) | |
return auth_token | |
def get_cards(uid): | |
''' | |
Get all card from a uid and pretty print it. See https://paymentez.github.io/api-doc/#get-all-cards | |
''' | |
r_params = {'uid': str(uid)} | |
headers = {'Auth-Token': get_auth_token()} | |
request_response = requests.get( | |
"https://ccapi-stg.paymentez.com/v2/card/list", params=r_params, headers=headers) | |
if request_response is None or request_response.status_code != 200: | |
print(" ~> " + str(request_response.text)) | |
return [] | |
cards_result = request_response.json() | |
return cards_result | |
def delet_card(uid, token): | |
''' | |
Removes a card from a user (uid). See https://paymentez.github.io/api-doc/#delete-a-card | |
''' | |
r_params = {'user': {'id': str(uid)}, 'card': {'token': token}} | |
headers = {'Auth-Token': get_auth_token()} | |
request_response = requests.post( | |
"https://ccapi-stg.paymentez.com/v2/card/delete/", json=r_params, headers=headers) | |
if request_response is None or request_response.status_code != 200: | |
return False | |
print(" ~> " + str(request_response.json())) | |
return True | |
def main(): | |
args = sys.argv | |
if args.__len__() <= 2: | |
print("Not enough commands pcli.py action[get,del] uid token(if del)") | |
return | |
print("#############################") | |
print("> Cards for " + str(args[2])) | |
pprint.pprint(get_cards(args[2])) | |
print("#############################") | |
if "del" == args[1]: | |
if args.__len__() == 4: | |
print("Card #" + args[3] + "will be removed") | |
print("Removed? " + str(delet_card(args[2], args[3]))) | |
else: | |
print("provide the token!") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment