Skip to content

Commit

Permalink
SP-1155: use resource token in webhook resend requests
Browse files Browse the repository at this point in the history
  • Loading branch information
swlodarski-sumoheavy committed Jan 24, 2025
1 parent eef8b96 commit 213ff71
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "bitpay"
version = "7.0.0"
version = "7.0.1"
authors = [
{ name="Antonio Buedo", email="[email protected]" },
]
Expand Down
12 changes: 8 additions & 4 deletions src/bitpay/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,18 +353,21 @@ def pay_invoice(self, invoice_id: str, status: str = "complete") -> Invoice:
client = self.create_invoice_client()
return client.pay(invoice_id, status)

def request_invoice_notifications(self, invoice_id: str) -> bool:
def request_invoice_notifications(
self, invoice_id: str, invoice_token: str
) -> bool:
"""
Request a BitPay Invoice Webhook.
:param str invoice_id: A BitPay invoice ID.
:param str invoice_token: The resource token for the invoice_id. This token can be retrieved from the Bitpay's invoice object.
:return: True if the webhook was successfully requested, false otherwise.
:rtype: bool
:raises BitPayException
:raises InvoiceNotificationException
"""
client = self.create_invoice_client()
return client.request_invoice_notifications(invoice_id)
return client.request_invoice_notifications(invoice_id, invoice_token)

def create_refund(
self,
Expand Down Expand Up @@ -501,18 +504,19 @@ def cancel_refund_by_guid(self, guid: str) -> Refund:
client = self.create_refund_client()
return client.cancel_by_guid(guid)

def request_refund_notification(self, refund_id: str) -> bool:
def request_refund_notification(self, refund_id: str, refund_token: str) -> bool:
"""
Send a refund notification.
:param str refund_id: BitPay refund ID to notify.
:param str refund_token: The resource token for the refund_id. This token can be retrieved from the Bitpay's refund object.
:return: True if the webhook was successfully requested, false otherwise.
:rtype: bool
:raises BitPayException
:raises RefundNotificationException
"""
client = self.create_refund_client()
return client.request_notification(refund_id)
return client.request_notification(refund_id, refund_token)

def get_supported_wallets(self) -> List[Wallet]:
"""
Expand Down
7 changes: 5 additions & 2 deletions src/bitpay/clients/invoice_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,17 +335,20 @@ def pay(self, invoice_id: str, status: str) -> Invoice:

return invoice

def request_invoice_notifications(self, invoice_id: str) -> bool:
def request_invoice_notifications(
self, invoice_id: str, invoice_token: str
) -> bool:
"""
Request a BitPay Invoice Webhook.
:param str invoice_id: A BitPay invoice ID.
:param str invoice_token: The resource token for the invoice_id. This token can be retrieved from the Bitpay's invoice object.
:return: True if the webhook was successfully requested, false otherwise.
:rtype: bool
:raises BitPayApiException
:raises BitPayGenericException
"""
params = {"token": self.__token_container.get_access_token(Facade.MERCHANT)}
params = {"token": invoice_token}
response = self.__bitpay_client.post(
"invoices/%s" % invoice_id + "/notifications", params
)
Expand Down
5 changes: 3 additions & 2 deletions src/bitpay/clients/refund_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,18 @@ def cancel_by_guid(self, guid: str) -> Refund:
"Refund", str(exe)
)

def request_notification(self, refund_id: str) -> bool:
def request_notification(self, refund_id: str, refund_token: str) -> bool:
"""
Send a refund notification.
:param str refund_id: BitPay refund ID to notify.
:param str refund_token: The resource token for the refund_id. This token can be retrieved from the Bitpay's refund object.
:return: True if the webhook was successfully requested, false otherwise.
:rtype: bool
:raises BitPayApiException
:raises BitPayGenericException
"""
params = {"token": self.__token_container.get_access_token(Facade.MERCHANT)}
params = {"token": refund_token}
response = self.__bitpay_client.post(
"refunds/%s" % refund_id + "/notifications", params, True
)
Expand Down
2 changes: 1 addition & 1 deletion src/bitpay/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class Config(Enum):
TEST_URL = "https://test.bitpay.com/"
PROD_URL = "https://bitpay.com/"
BITPAY_API_VERSION = "2.0.0"
BITPAY_PLUGIN_INFO = "BitPay_Python_Client_v7.0.0"
BITPAY_PLUGIN_INFO = "BitPay_Python_Client_v7.0.1"
BITPAY_API_FRAME = "std"
BITPAY_API_FRAME_VERSION = "1.0.0"
10 changes: 6 additions & 4 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
payout_token_value = "somePayoutToken"
pos_token_value = "somePosToken"
invoice_id = "UZjwcYkWAKfTMn9J1yyfs4"
invoice_token = "someInvoiceToken"


def get_bitpay_client(mocker):
Expand Down Expand Up @@ -542,14 +543,14 @@ def test_request_invoice_notifications(mocker):
bitpay_client = get_bitpay_client(mocker)
response = Mock()
response.json.return_value = {"data": "success"}
params = {"token": merchant_token_value}
params = {"token": invoice_token}
bitpay_client.post.side_effect = mock_response(
response, "invoices/" + invoice_id + "/notifications", params, True
)
client = init_client(mocker, bitpay_client)

# act
result = client.request_invoice_notifications(invoice_id)
result = client.request_invoice_notifications(invoice_id, invoice_token)

# assert
assert result is True
Expand Down Expand Up @@ -731,17 +732,18 @@ def update_refund_by_guid(mocker):
def test_send_refund_notification(mocker):
# arrange
refund_id = "1234"
refund_token = "someRefundToken"
bitpay_client = get_bitpay_client(mocker)
response = Mock()
response.json.return_value = {"status": "success"}
params = {"token": merchant_token_value}
params = {"token": refund_token}
bitpay_client.post.side_effect = mock_response(
response, "refunds/" + refund_id + "/notifications", params, True
)
client = init_client(mocker, bitpay_client)

# act
result = client.request_refund_notification(refund_id)
result = client.request_refund_notification(refund_id, refund_token)

# assert
assert result is True
Expand Down

0 comments on commit 213ff71

Please sign in to comment.