Skip to content

Commit

Permalink
feat (payment-url): add support for invoice payment url (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
lovrocolic authored Feb 12, 2024
1 parent ddae5df commit 48f5055
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lago_python_client/invoices/clients.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import ClassVar, Optional, Type, Union
from typing import ClassVar, Optional, Type, Union, Mapping

from ..base_client import BaseClient
from ..mixins import FindAllCommandMixin, FindCommandMixin, UpdateCommandMixin, CreateCommandMixin
Expand Down Expand Up @@ -81,3 +81,15 @@ def finalize(self, resource_id: str) -> InvoiceResponse:
response_model=self.RESPONSE_MODEL,
data=get_response_data(response=api_response, key=self.ROOT_NAME),
)

def payment_url(self, resource_id: str) -> str:
api_response: Response = send_post_request(
url=make_url(
origin=self.base_url,
path_parts=(self.API_RESOURCE, resource_id, 'payment_url'),
),
headers=make_headers(api_key=self.api_key),
)

response_data = get_response_data(response=api_response, key='invoice_payment_details')
return response_data.get('payment_url', '') if isinstance(response_data, Mapping) else ''
9 changes: 9 additions & 0 deletions tests/fixtures/payment_url.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"invoice_payment_details": {
"lago_customer_id": "631edc95-f26a-4eda-b364-3fa8d9db0bec",
"external_customer_id": "Test55-7",
"payment_provider": "stripe",
"lago_invoice_id": "dc970e9b-270e-44d4-a984-93c598f8abb7",
"payment_url": "https://checkout.stripe.com/c/pay/cs_test_a1cuqFkXvH"
}
}
21 changes: 21 additions & 0 deletions tests/test_invoice_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def mock_response(mock='invoice'):
return invoice_response.read()


def mock_payment_url_response():
this_dir = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(this_dir, 'fixtures/payment_url.json')

with open(data_path, 'rb') as payment_url_response:
return payment_url_response.read()


def test_valid_update_invoice_request(httpx_mock: HTTPXMock):
client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d')

Expand Down Expand Up @@ -163,3 +171,16 @@ def test_valid_retry_payment_invoice_request(httpx_mock: HTTPXMock):
response = client.invoices.retry_payment('5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba')

assert response.lago_id == '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba'


def test_valid_payment_url_request(httpx_mock: HTTPXMock):
client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d')

httpx_mock.add_response(
method='POST',
url='https://api.getlago.com/api/v1/invoices/5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba/payment_url',
content=mock_payment_url_response(),
)
response = client.invoices.payment_url('5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba')

assert response == 'https://checkout.stripe.com/c/pay/cs_test_a1cuqFkXvH'

0 comments on commit 48f5055

Please sign in to comment.