Skip to content

Commit 1d5ac3f

Browse files
committed
Completed code for settlement, subscription, rates, currency and there testcases
1 parent b094020 commit 1d5ac3f

25 files changed

+2181
-6
lines changed

src/bitpay_sdk/client.py

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@
1212
from .tokens import Tokens
1313
from .models.facade import Facade
1414
from .models.bill.bill import Bill
15+
from .models.Rate.rate import Rate
1516
from .utils.rest_cli import RESTcli
17+
from .models.Rate.rates import Rates
18+
from .models.currency import Currency
1619
from .models.ledger.ledger import Ledger
1720
from .models.wallet.wallet import Wallet
1821
from .models.payout.payout import Payout
1922
from .models.invoice.refund import Refund
2023
from .models.invoice.invoice import Invoice
2124
from .models.payout.payout_batch import PayoutBatch
2225
from .models.ledger.ledger_entry import LedgerEntry
26+
from .models.settlement.settlement import Settlement
2327
from .exceptions.bitpay_exception import BitPayException
28+
from .models.subscription.subscription import Subscription
2429
from .models.payout.payout_recipient import PayoutRecipient
2530
from .models.payout.payout_recipients import PayoutRecipients
2631
from .exceptions.bill_query_exception import BillQueryException
32+
from .exceptions.rate_query_exception import RateQueryException
2733
from .exceptions.bill_update_exception import BillUpdateException
2834
from .exceptions.ledger_query_exception import LedgerQueryException
2935
from .exceptions.wallet_query_exception import WalletQueryException
@@ -34,17 +40,22 @@
3440
from .exceptions.refund_update_exception import RefundUpdateException
3541
from .exceptions.invoice_query_exception import InvoiceQueryException
3642
from .exceptions.invoice_update_exception import InvoiceUpdateException
43+
from .exceptions.currency_query_exception import CurrencyQueryException
3744
from .exceptions.refund_creation_exception import RefundCreationException
3845
from .exceptions.payout_creation_exception import PayoutCreationException
46+
from .exceptions.settlement_query_exception import SettlementQueryException
3947
from .exceptions.invoice_creation_exception import InvoiceCreationException
4048
from .exceptions.payoutbatch_query_exception import PayoutBatchQueryException
49+
from .exceptions.subscription_query_exception import SubscriptionQueryException
50+
from .exceptions.subscription_update_exception import SubscriptionUpdateException
4151
from .exceptions.refund_notification_exception import RefundNotificationException
4252
from .exceptions.refund_cancellation_exception import RefundCancellationException
4353
from .exceptions.payout_cancellation_exception import PayoutCancellationException
4454
from .exceptions.payout_notification_exception import PayoutNotificationException
4555
from .exceptions.invoice_cancellation_exception import InvoiceCancellationException
4656
from .exceptions.invoice_notification_exception import InvoiceNotificationException
4757
from .exceptions.payoutbatch_creation_exception import PayoutBatchCreationException
58+
from .exceptions.subscription_creation_exception import SubscriptionCreationException
4859
from .exceptions.payout_recipient_query_exception import PayoutRecipientQueryException
4960
from .exceptions.payout_recipient_update_exception import PayoutRecipientUpdateException
5061
from .exceptions.payoutbatch_cancellation_exception import PayoutBatchCancellationException
@@ -1062,3 +1073,187 @@ def request_payout_batch_notification(self, payout_batch_id: str):
10621073
raise PayoutBatchNotificationException("failed to deserialize BitPay server response "
10631074
" (PayoutBatch) : %s" % str(exe))
10641075
return payout_batch
1076+
1077+
def get_settlements(self, currency: str, date_start, date_end, status: str,
1078+
limit: int = 100, offset: int = 0) -> [Settlement]:
1079+
try:
1080+
params = {"token": self.get_access_token(Facade.Merchant),
1081+
"startDate": date_start, "endDate": date_end,
1082+
"currency": currency, "status": status, "limit": limit,
1083+
"offset": offset}
1084+
response_json = self.__restcli.get("settlements", params)
1085+
except BitPayException as exe:
1086+
raise SettlementQueryException("failed to serialize Settlement object : %s" % str(exe),
1087+
exe.get_api_code())
1088+
1089+
try:
1090+
settlements = []
1091+
for settlement in response_json:
1092+
settlements.append(Settlement(**settlement))
1093+
except Exception as exe:
1094+
raise SettlementQueryException("failed to deserialize BitPay server response "
1095+
" (Settlement) : %s" % str(exe))
1096+
return settlements
1097+
1098+
def get_settlement(self, settlement_id: str) -> Settlement:
1099+
try:
1100+
params = {"token": self.get_access_token(Facade.Merchant)}
1101+
response_json = self.__restcli.get("settlements/%s" % settlement_id, params)
1102+
except BitPayException as exe:
1103+
raise SettlementQueryException("failed to serialize Settlement object : %s" % str(exe),
1104+
exe.get_api_code())
1105+
1106+
try:
1107+
settlement = Settlement(**response_json)
1108+
except Exception as exe:
1109+
raise SettlementQueryException("failed to deserialize BitPay server response "
1110+
" (Settlement) : %s" % str(exe))
1111+
return settlement
1112+
1113+
def get_settlement_reconciliation_report(self, settlement: Settlement) -> Settlement:
1114+
try:
1115+
params = {"token": self.get_access_token(Facade.Merchant)}
1116+
response_json = self.__restcli.get("settlements/%s" % settlement.get_id() + "/reconciliationReport",
1117+
params)
1118+
except BitPayException as exe:
1119+
raise SettlementQueryException("failed to serialize Settlement object : %s" % str(exe),
1120+
exe.get_api_code())
1121+
1122+
try:
1123+
settlement = Settlement(**response_json)
1124+
except Exception as exe:
1125+
raise SettlementQueryException("failed to deserialize BitPay server response "
1126+
" (Settlement) : %s" % str(exe))
1127+
return settlement
1128+
1129+
def create_subscription(self, subscription: Subscription) -> Subscription:
1130+
try:
1131+
subscription.set_token(self.get_access_token(Facade.Merchant))
1132+
subscription.to_json()
1133+
1134+
response_json = self.__restcli.post("subscriptions", subscription, True)
1135+
except BitPayException as exe:
1136+
raise SubscriptionCreationException("failed to serialize Subscription object : %s" % str(exe),
1137+
exe.get_api_code())
1138+
1139+
try:
1140+
subscription = Subscription(**response_json)
1141+
except Exception as exe:
1142+
raise SubscriptionCreationException("failed to deserialize BitPay server response "
1143+
" (Subscription) : %s" % str(exe))
1144+
return subscription
1145+
1146+
def get_subscription(self, subscription_id: str) -> Subscription:
1147+
try:
1148+
params = {"token": self.get_access_token(Facade.Merchant)}
1149+
response_json = self.__restcli.get("subscriptions/%s" % subscription_id, params)
1150+
except BitPayException as exe:
1151+
raise SubscriptionQueryException("failed to serialize Subscription object : %s" % str(exe),
1152+
exe.get_api_code())
1153+
1154+
try:
1155+
subscription = Subscription(**response_json)
1156+
except Exception as exe:
1157+
raise SubscriptionQueryException("failed to deserialize BitPay server response "
1158+
" (Subscription) : %s" % str(exe))
1159+
return subscription
1160+
1161+
def get_subscriptions(self, status: str = None) -> [Subscription]:
1162+
try:
1163+
params = {"token": self.get_access_token(Facade.Merchant)}
1164+
if status:
1165+
params["status"] = status
1166+
response_json = self.__restcli.get("subscriptions", params)
1167+
except BitPayException as exe:
1168+
raise SubscriptionQueryException("failed to serialize Subscription object : %s" % str(exe),
1169+
exe.get_api_code())
1170+
1171+
try:
1172+
subscriptions = []
1173+
for subscription in response_json:
1174+
subscriptions.append(Subscription(**subscription))
1175+
except Exception as exe:
1176+
raise SubscriptionQueryException("failed to deserialize BitPay server response "
1177+
" (Subscription) : %s" % str(exe))
1178+
return subscriptions
1179+
1180+
def update_subscription(self, subscription: Subscription, subscription_id) -> Subscription:
1181+
try:
1182+
subscription_token = self.get_subscription(subscription.get_id()).get_token()
1183+
subscription.set_token(subscription_token)
1184+
subscription.to_json()
1185+
1186+
response_json = self.__restcli.update("subscriptions/%s" % subscription_id, subscription)
1187+
except BitPayException as exe:
1188+
raise SubscriptionUpdateException("failed to serialize Subscription object : %s" % str(exe),
1189+
exe.get_api_code())
1190+
1191+
try:
1192+
subscription = Subscription(**response_json)
1193+
except Exception as exe:
1194+
raise SubscriptionUpdateException("failed to deserialize BitPay server response "
1195+
" (Subscription) : %s" % str(exe))
1196+
return subscription
1197+
1198+
def get_currencies(self) -> [Currency]:
1199+
try:
1200+
response_json = self.__restcli.get("currencies", None, False)
1201+
except BitPayException as exe:
1202+
raise CurrencyQueryException("failed to serialize Currency object : %s" % str(exe),
1203+
exe.get_api_code())
1204+
1205+
try:
1206+
subscriptions = []
1207+
for subscription in response_json:
1208+
subscriptions.append(Subscription(**subscription))
1209+
except Exception as exe:
1210+
raise CurrencyQueryException("failed to deserialize BitPay server response "
1211+
" (Currency) : %s" % str(exe))
1212+
return subscriptions
1213+
1214+
def get_rates(self) -> [Rates]:
1215+
try:
1216+
response_json = self.__restcli.get("rates", None, False)
1217+
except BitPayException as exe:
1218+
raise RateQueryException("failed to serialize Rates object : %s" % str(exe),
1219+
exe.get_api_code())
1220+
1221+
try:
1222+
rates = []
1223+
for rate in response_json:
1224+
rates.append(Rates(**rate))
1225+
except Exception as exe:
1226+
raise RateQueryException("failed to deserialize BitPay server response "
1227+
" (Rates) : %s" % str(exe))
1228+
return rates
1229+
1230+
def get_currency_rates(self, base_currency: str) -> [Rates]:
1231+
try:
1232+
response_json = self.__restcli.get("rates/%s" % base_currency, None, False)
1233+
except BitPayException as exe:
1234+
raise RateQueryException("failed to serialize Rates object : %s" % str(exe),
1235+
exe.get_api_code())
1236+
1237+
try:
1238+
rates = []
1239+
for rate in response_json:
1240+
rates.append(Rates(**rate))
1241+
except Exception as exe:
1242+
raise RateQueryException("failed to deserialize BitPay server response "
1243+
" (Rates) : %s" % str(exe))
1244+
return rates
1245+
1246+
def get_currency_pair_rate(self, base_currency, currency) -> Rate:
1247+
try:
1248+
response_json = self.__restcli.get("rates/%s" % base_currency + "/%s" % currency,
1249+
None, False)
1250+
except BitPayException as exe:
1251+
raise RateQueryException("failed to serialize Rates object : %s" % str(exe),
1252+
exe.get_api_code())
1253+
1254+
try:
1255+
rate = Rate(**response_json)
1256+
except Exception as exe:
1257+
raise RateQueryException("failed to deserialize BitPay server response "
1258+
" (Rate) : %s" % str(exe))
1259+
return rate
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Currency Exception gets raised when some unexpected error occurs while processing a request
3+
or trying to manage currencies.
4+
"""
5+
from .bitpay_exception import BitPayException
6+
7+
8+
class CurrencyException(BitPayException):
9+
"""
10+
CurrencyException
11+
"""
12+
__bitpay_message = "An unexpected error occurred while trying to manage the currency"
13+
__bitpay_code = "BITPAY-CURRENCY-GENERIC"
14+
__api_code = ""
15+
16+
def __init__(self, message="", code=181, api_code="000000"):
17+
"""
18+
Construct the CurrencyException.
19+
20+
:param message: The Exception message to throw.
21+
:param code: [optional] The Exception code to throw.
22+
:param api_code: [optional] The API Exception code to throw.
23+
"""
24+
message = self.__bitpay_code + ": " + self.__bitpay_message + ":" + message
25+
self.__api_code = api_code
26+
super().__init__(message, code)
27+
28+
# def get_api_code(self):
29+
# return self.__api_code
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Currency Query Exception gets raised when request for currency retrieval gets failed .
3+
"""
4+
from .invoice_exception import InvoiceException
5+
6+
7+
class CurrencyQueryException(InvoiceException):
8+
"""
9+
CurrencyQueryException
10+
"""
11+
__bitpay_message = "Failed to retrieve currencies"
12+
__bitpay_code = "BITPAY-CURRENCY-GET"
13+
__api_code = ""
14+
15+
def __init__(self, message, code=182, api_code="000000"):
16+
"""
17+
Construct the CurrencyQueryException.
18+
19+
:param message: The Exception message to throw.
20+
:param code: [optional] The Exception code to throw.
21+
:param api_code: [optional] The API Exception code to throw.
22+
"""
23+
message = self.__bitpay_code + ": " + self.__bitpay_message + ":" + message
24+
self.__api_code = api_code
25+
super().__init__(message, code)
26+
27+
# def get_api_code(self):
28+
# """
29+
# :return: Error code provided by the BitPay REST API
30+
# """
31+
# return self.__api_code
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Rate Query Exception gets raised when request for rate retrieval gets failed .
3+
"""
4+
from .rates_exception import RateException
5+
6+
7+
class RateQueryException(RateException):
8+
"""
9+
RateQueryException
10+
"""
11+
__bitpay_message = "Failed to retrieve rates"
12+
__bitpay_code = "BITPAY-RATES-GET"
13+
__api_code = ""
14+
15+
def __init__(self, message, code=142, api_code="000000"):
16+
"""
17+
Construct the RateQueryException.
18+
19+
:param message: The Exception message to throw.
20+
:param code: [optional] The Exception code to throw.
21+
:param api_code: [optional] The API Exception code to throw.
22+
"""
23+
message = self.__bitpay_code + ": " + self.__bitpay_message + ":" + message
24+
self.__api_code = api_code
25+
super().__init__(message, code)
26+
27+
# def get_api_code(self):
28+
# """
29+
# :return: Error code provided by the BitPay REST API
30+
# """
31+
# return self.__api_code
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Rate Exception gets raised when some unexpected error occurs while processing a request
3+
or trying to manage rates.
4+
"""
5+
from .bitpay_exception import BitPayException
6+
7+
8+
class RateException(BitPayException):
9+
"""
10+
RateException
11+
"""
12+
__bitpay_message = "An unexpected error occurred while trying to manage the rate"
13+
__bitpay_code = "BITPAY-RATES-GENERIC"
14+
__api_code = ""
15+
16+
def __init__(self, message="", code=141, api_code="000000"):
17+
"""
18+
Construct the RateException.
19+
20+
:param message: The Exception message to throw.
21+
:param code: [optional] The Exception code to throw.
22+
:param api_code: [optional] The API Exception code to throw.
23+
"""
24+
message = self.__bitpay_code + ": " + self.__bitpay_message + ":" + message
25+
self.__api_code = api_code
26+
super().__init__(message, code)
27+
28+
# def get_api_code(self):
29+
# return self.__api_code
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Settlement Exception gets raised when some unexpected error occurs while processing a request
3+
or trying to manage settlement.
4+
"""
5+
from .bitpay_exception import BitPayException
6+
7+
8+
class SettlementException(BitPayException):
9+
"""
10+
SettlementException
11+
"""
12+
__bitpay_message = "An unexpected error occurred while trying to manage the settlement"
13+
__bitpay_code = "BITPAY-SETTLEMENTS-GENERIC"
14+
__api_code = ""
15+
16+
def __init__(self, message="", code=151, api_code="000000"):
17+
"""
18+
Construct the SettlementException.
19+
20+
:param message: The Exception message to throw.
21+
:param code: [optional] The Exception code to throw.
22+
:param api_code: [optional] The API Exception code to throw.
23+
"""
24+
message = self.__bitpay_code + ": " + self.__bitpay_message + ":" + message
25+
self.__api_code = api_code
26+
super().__init__(message, code)
27+
28+
# def get_api_code(self):
29+
# return self.__api_code

0 commit comments

Comments
 (0)