kohpriwniranam

easyslip

Nov 4th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.74 KB | None | 0 0
  1. """
  2. created on 04-Nov-24 && powered by kohpriw/niranam
  3.  
  4. ตัวอย่างการเรียกใช้
  5. token = 'YOUR_ACCESS_TOKEN'
  6. image_path = 'slip.jpg'
  7. payload = 'YOUR_PAYLOAD'
  8. _____________________________
  9. functions
  10. get_account_info(token)
  11. verify_bank_slip_by_payload(token, payload)
  12. verify_bank_slip_by_image(token, image_path)
  13. verify_bank_slip_by_base64(token, image_path)
  14. verify_truewallet_slip_by_image(token, image_path)
  15. """
  16.  
  17. import requests
  18. import base64
  19.  
  20. def get_account_info(token):
  21.     url = 'https://developer.easyslip.com/api/v1/me'
  22.     headers = {
  23.         'Authorization': f'Bearer {token}'
  24.     }
  25.     try:
  26.         response = requests.get(url, headers=headers)
  27.         response.raise_for_status()                    
  28.         data = response.json()         
  29.         if response.status_code == 200:
  30.             print("Account Info:")
  31.             print(f"Application: {data['data']['application']}")
  32.             print(f"Used Quota: {data['data']['usedQuota']}")
  33.             print(f"Max Quota: {data['data']['maxQuota']}")
  34.             print(f"Remaining Quota: {data['data']['remainingQuota']}")
  35.             print(f"Expired At: {data['data']['expiredAt']}")
  36.             print(f"Current Credit: {data['data']['currentCredit']}")
  37.         else:
  38.             print(f"Unexpected status code: {response.status_code}")
  39.     except requests.exceptions.HTTPError as http_err:
  40.         if response.status_code == 401:
  41.             print("Error: Unauthorized (401) - Please check your access token.")
  42.         elif response.status_code == 403:
  43.             print("Error: Access Denied (403) - You do not have permission to access this resource.")
  44.         elif response.status_code == 500:
  45.             print("Error: Server Error (500) - Please try again later.")
  46.         else:
  47.             print(f"HTTP error occurred: {http_err}")
  48.     except Exception as err:
  49.         print(f"An error occurred: {err}")
  50.        
  51. def verify_bank_slip_by_payload(token, payload):
  52.     url = f'https://developer.easyslip.com/api/v1/verify?payload={payload}'
  53.     headers = {
  54.         'Authorization': f'Bearer {token}'
  55.     }
  56.     try:
  57.         response = requests.get(url, headers=headers)
  58.         response.raise_for_status()
  59.         data = response.json()         
  60.         if response.status_code == 200:
  61.             print("Slip Verification Successful:")
  62.             print(f"Transaction Reference: {data['data']['transRef']}")
  63.             print(f"Date: {data['data']['date']}")
  64.             print(f"Country Code: {data['data']['countryCode']}")
  65.             print(f"Amount: {data['data']['amount']['amount']}")
  66.             print(f"Sender Bank: {data['data']['sender']['bank']['name']}")
  67.             print(f"Receiver Bank: {data['data']['receiver']['bank']['name']}")
  68.         else:
  69.             print(f"Unexpected status code: {response.status_code}")
  70.     except requests.exceptions.HTTPError as http_err:
  71.         if response.status_code == 400:
  72.             print("Error: Invalid Payload (400) - Please check the payload.")
  73.         elif response.status_code == 401:
  74.             print("Error: Unauthorized (401) - Please check your access token.")
  75.         elif response.status_code == 403:
  76.             error_message = data.get('message', 'Access Denied (403)')
  77.             print(f"Error: {error_message}")
  78.         elif response.status_code == 404:
  79.             print("Error: Slip Not Found (404) - The specified slip was not found.")
  80.         elif response.status_code == 500:
  81.             error_message = data.get('message', 'Server Error (500)')
  82.             print(f"Error: {error_message}")
  83.         else:
  84.             print(f"HTTP error occurred: {http_err}")
  85.     except Exception as err:
  86.         print(f"An error occurred: {err}")
  87.  
  88. def verify_bank_slip_by_image(token, image_path):
  89.     url = 'https://developer.easyslip.com/api/v1/verify'
  90.     headers = {
  91.         'Authorization': f'Bearer {token}'
  92.     }
  93.     files = {
  94.         'file': open(image_path, 'rb')
  95.     }
  96.     try:
  97.         response = requests.post(url, headers=headers, files=files)
  98.         response.raise_for_status()                    
  99.         data = response.json()         
  100.         if response.status_code == 200:
  101.             print("Slip Verification Successful:")
  102.             print(f"Transaction Reference: {data['data']['transRef']}")
  103.             print(f"Date: {data['data']['date']}")
  104.             print(f"Country Code: {data['data']['countryCode']}")
  105.             print(f"Amount: {data['data']['amount']['amount']}")
  106.             print(f"Sender Bank: {data['data']['sender']['bank']['name']}")
  107.             print(f"Receiver Bank: {data['data']['receiver']['bank']['name']}")
  108.         else:
  109.             print(f"Unexpected status code: {response.status_code}")
  110.     except requests.exceptions.HTTPError as http_err:
  111.         if response.status_code == 400:
  112.             error_message = data.get('message', 'Invalid Request (400)')
  113.             print(f"Error: {error_message}")
  114.         elif response.status_code == 401:
  115.             print("Error: Unauthorized (401) - Please check your access token.")
  116.         elif response.status_code == 403:
  117.             error_message = data.get('message', 'Access Denied (403)')
  118.             print(f"Error: {error_message}")
  119.         elif response.status_code == 404:
  120.             error_message = data.get('message', 'Slip Not Found (404)')
  121.             print(f"Error: {error_message}")
  122.         elif response.status_code == 500:
  123.             error_message = data.get('message', 'Server Error (500)')
  124.             print(f"Error: {error_message}")
  125.         else:
  126.             print(f"HTTP error occurred: {http_err}")
  127.     except Exception as err:
  128.         print(f"An error occurred: {err}")
  129.     finally:
  130.         files['file'].close()
  131.  
  132. def verify_bank_slip_by_base64(token, image_path):
  133.     url = 'https://developer.easyslip.com/api/v1/verify'
  134.     headers = {
  135.         'Content-Type': 'application/json',
  136.         'Authorization': f'Bearer {token}'
  137.     }
  138.     try:
  139.         with open(image_path, 'rb') as image_file:
  140.             base64_image = base64.b64encode(image_file.read()).decode('utf-8')         
  141.         payload = {
  142.             "image": base64_image
  143.         }  
  144.         response = requests.post(url, headers=headers, json=payload)
  145.         response.raise_for_status()            
  146.         data = response.json()
  147.         if response.status_code == 200:
  148.             print("Slip Verification Successful:")
  149.             print(f"Transaction Reference: {data['data']['transRef']}")
  150.             print(f"Date: {data['data']['date']}")
  151.             print(f"Country Code: {data['data']['countryCode']}")
  152.             print(f"Amount: {data['data']['amount']['amount']}")
  153.             print(f"Sender Bank: {data['data']['sender']['bank']['name']}")
  154.             print(f"Receiver Bank: {data['data']['receiver']['bank']['name']}")
  155.         else:
  156.             print(f"Unexpected status code: {response.status_code}")
  157.     except requests.exceptions.HTTPError as http_err:
  158.         if response.status_code == 400:
  159.             error_message = data.get('message', 'Invalid Request (400)')
  160.             print(f"Error: {error_message}")
  161.         elif response.status_code == 401:
  162.             print("Error: Unauthorized (401) - Please check your access token.")
  163.         elif response.status_code == 403:
  164.             error_message = data.get('message', 'Access Denied (403)')
  165.             print(f"Error: {error_message}")
  166.         elif response.status_code == 404:
  167.             error_message = data.get('message', 'Slip Not Found (404)')
  168.             print(f"Error: {error_message}")
  169.         elif response.status_code == 500:
  170.             error_message = data.get('message', 'Server Error (500)')
  171.             print(f"Error: {error_message}")
  172.         else:
  173.             print(f"HTTP error occurred: {http_err}")
  174.     except Exception as err:
  175.         print(f"An error occurred: {err}")
  176.  
  177. def verify_truewallet_slip_by_image(token, image_path):
  178.     url = 'https://developer.easyslip.com/api/v1/verify/truewallet'
  179.     headers = {
  180.         'Authorization': f'Bearer {token}'
  181.     }
  182.     files = {
  183.         'file': open(image_path, 'rb')
  184.     }
  185.     try:
  186.         response = requests.post(url, headers=headers, files=files)
  187.         response.raise_for_status()
  188.         data = response.json()
  189.         if response.status_code == 200:
  190.             print("Truemoney Wallet Slip Verification Successful:")
  191.             print(f"Transaction ID: {data['data']['transactionId']}")
  192.             print(f"Date: {data['data']['date']}")
  193.             print(f"Amount: {data['data']['amount']}")
  194.             print(f"Sender Name: {data['data']['sender']['name']}")
  195.             print(f"Receiver Name: {data['data']['receiver']['name']}")
  196.             print(f"Receiver Phone: {data['data']['receiver']['phone']}")
  197.         else:
  198.             print(f"Unexpected status code: {response.status_code}")
  199.     except requests.exceptions.HTTPError as http_err:
  200.         if response.status_code == 400:
  201.             error_message = data.get('message', 'Invalid Request (400)')
  202.             print(f"Error: {error_message}")
  203.         elif response.status_code == 401:
  204.             print("Error: Unauthorized (401) - Please check youraccess token.")
  205.         elif response.status_code == 403:
  206.             error_message = data.get('message', 'Access Denied (403)')
  207.             print(f"Error: {error_message}")
  208.         elif response.status_code == 404:
  209.             error_message = data.get('message', 'Slip Not Found (404)')
  210.             print(f"Error: {error_message}")
  211.         elif response.status_code == 500:
  212.             error_message = data.get('message', 'Server Error (500)')
  213.             print(f"Error: {error_message}")
  214.         else:
  215.             print(f"HTTP error occurred: {http_err}")
  216.     except Exception as err:
  217.         print(f"An error occurred: {err}")
  218.     finally:
  219.         files['file'].close()
Add Comment
Please, Sign In to add comment