|
| 1 | +import os |
| 2 | +import json |
| 3 | +from .config import Config |
| 4 | +from .tokens import Tokens |
| 5 | +from .models.facade import Facade |
| 6 | +from .utils.rest_cli import RESTcli |
| 7 | +from .models.invoice.invoice import Invoice |
| 8 | +from .exceptions.bitpay_exception import BitPayException |
| 9 | + |
| 10 | + |
| 11 | +class Client: |
| 12 | + __configuration = "" |
| 13 | + __env = "" |
| 14 | + __eckey = "" |
| 15 | + __token_cache = "" |
| 16 | + __restcli = None |
| 17 | + |
| 18 | + def __init__(self, config_file_path): |
| 19 | + self.build_config_from_file(config_file_path) |
| 20 | + self.init_keys() |
| 21 | + self.init() |
| 22 | + |
| 23 | + def build_config_from_file(self, config_file_path): |
| 24 | + self.__configuration = Config() |
| 25 | + |
| 26 | + if os.path.exists(config_file_path): |
| 27 | + |
| 28 | + try: |
| 29 | + read_file = open(config_file_path, 'r') |
| 30 | + content = read_file.read() |
| 31 | + json_data = json.loads(content) |
| 32 | + self.__configuration.set_environment(json_data['BitPayConfiguration']['Environment']) |
| 33 | + self.__env = self.__configuration.get_environment() |
| 34 | + tokens = json_data["BitPayConfiguration"]["EnvConfig"][self.__env]["ApiTokens"] |
| 35 | + private_key_path = json_data["BitPayConfiguration"]["EnvConfig"][self.__env]["PrivateKeyPath"] |
| 36 | + private_key_secret = json_data["BitPayConfiguration"]["EnvConfig"][self.__env]["PrivateKeySecret"] |
| 37 | + proxy = json_data["BitPayConfiguration"]["EnvConfig"][self.__env]["proxy"] |
| 38 | + |
| 39 | + env_config = {self.__env: { |
| 40 | + "PrivateKeyPath": private_key_path, |
| 41 | + "PrivateKeySecret": private_key_secret, |
| 42 | + "ApiTokens": tokens, |
| 43 | + "Proxy": proxy |
| 44 | + }} |
| 45 | + self.__configuration.set_envconfig(env_config) |
| 46 | + |
| 47 | + except BitPayException as e: |
| 48 | + print(e) |
| 49 | + |
| 50 | + else: |
| 51 | + raise BitPayException("Configuration file not found") |
| 52 | + |
| 53 | + def init_keys(self): |
| 54 | + private_key_path = self.__configuration.get_envconfig()[self.__env]["PrivateKeyPath"] |
| 55 | + try: |
| 56 | + if not self.__eckey: |
| 57 | + with open(private_key_path) as f: |
| 58 | + self.__eckey = f.read() |
| 59 | + |
| 60 | + except BitPayException as e: |
| 61 | + raise BitPayException("failed to build configuration : ") |
| 62 | + |
| 63 | + def init(self): |
| 64 | + try: |
| 65 | + proxy = self.__configuration.get_envconfig()[self.__env]["Proxy"] |
| 66 | + self.__restcli = RESTcli(self.__env, self.__eckey, proxy) |
| 67 | + self.load_access_tokens() |
| 68 | + # TODO :load currencies |
| 69 | + except BitPayException as e: |
| 70 | + print(e) |
| 71 | + |
| 72 | + def load_access_tokens(self): |
| 73 | + try: |
| 74 | + self.clear_access_token_cache() |
| 75 | + self.__token_cache = self.__configuration.get_envconfig()[self.__env]["ApiTokens"] |
| 76 | + except BitPayException as e: |
| 77 | + print(e) |
| 78 | + |
| 79 | + def clear_access_token_cache(self): |
| 80 | + self.__token_cache = Tokens() |
| 81 | + |
| 82 | + def create_invoice(self, invoice: Invoice, facade, sign_request=True): |
| 83 | + try: |
| 84 | + invoice.set_token(self.get_access_token(facade)) |
| 85 | + print(invoice) |
| 86 | + invoice_json = invoice.to_json() |
| 87 | + |
| 88 | + response = self.__restcli.post("invoices", invoice_json, sign_request) |
| 89 | + print(response) |
| 90 | + |
| 91 | + except BitPayException as e: |
| 92 | + print(e) |
| 93 | + |
| 94 | + def get_invoice(self, invoice_id, facade, sign_request=True): |
| 95 | + try: |
| 96 | + params = {"token": self.get_access_token(facade)} |
| 97 | + response_json = self.__restcli.get("invoices/%s" % invoice_id, params, sign_request) |
| 98 | + except BitPayException as e: |
| 99 | + print(e) |
| 100 | + |
| 101 | + def get_invoices(self, date_start, date_end, status, order_id, limit, offset): |
| 102 | + try: |
| 103 | + params = {"token": self.get_access_token(Facade.Merchant), "dateStart": date_start, "date_end": date_end} |
| 104 | + response_json = self.__restcli.get("invoices/", parameters=params) |
| 105 | + except BitPayException as e: |
| 106 | + print(e) |
| 107 | + |
| 108 | + def update_invoice(self, invoice_id, buyer_sms, sms_code, buyer_email): |
| 109 | + try: |
| 110 | + params = {'token': self.get_access_token(Facade.Merchant)} |
| 111 | + |
| 112 | + if buyer_sms & sms_code: |
| 113 | + pass |
| 114 | + |
| 115 | + if buyer_sms is not None: |
| 116 | + params['buyer_sms'] = buyer_sms |
| 117 | + |
| 118 | + if sms_code is not None: |
| 119 | + params['sms_code'] = sms_code |
| 120 | + |
| 121 | + if buyer_email is not None: |
| 122 | + params['buyer_email'] = buyer_email |
| 123 | + |
| 124 | + response_json = self.__restcli.update("invoices/%s" % invoice_id, json.dumps(params)) |
| 125 | + |
| 126 | + except BitPayException as e: |
| 127 | + print(e) |
| 128 | + |
| 129 | + def cancel_invoice(self, invoice_id): |
| 130 | + try: |
| 131 | + params = {'token': self.get_access_token(Facade.Merchant)} |
| 132 | + response_json = self.__restcli.delete("invoices/%s" % invoice_id, params) |
| 133 | + except BitPayException as e: |
| 134 | + print(e) |
| 135 | + |
| 136 | + def get_invoice_webhook(self, invoice_id): |
| 137 | + try: |
| 138 | + self.get_invoice(invoice_id) |
| 139 | + params = {} |
| 140 | + # Conditions missing look from node |
| 141 | + response_json = self.__restcli.post("invoices/%s" % invoice_id + "/notifications", params) |
| 142 | + except BitPayException as e: |
| 143 | + print(e) |
| 144 | + |
| 145 | + def get_access_token(self, key: str): |
| 146 | + try: |
| 147 | + return self.__token_cache[key] |
| 148 | + except BitPayException as e: |
| 149 | + print(e) |
0 commit comments