Skip to content

Commit

Permalink
Adding 'uri' attribute to 'create_payment_by_invoice'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomazmm committed Nov 6, 2023
1 parent 1bd6b0a commit 96e271a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/nowpayments_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class NOWPaymentsAPI:
BASE_URI = "https://api.nowpayments.io/v1/"
BASE_URI_SANDBOX = "https://api-sandbox.nowpayments.io/v1/"

WEB_APP_PAYMENT_URI = "https://nowpayments.io/payment/"
WEB_APP_PAYMENT_URI_SANDBOX = "https://sandbox.nowpayments.io/payment/"

def __init__(
self, api_key: str, email: str = "", password: str = "", sandbox=False
) -> None:
Expand All @@ -29,6 +32,12 @@ def __init__(
:param str api_key: API key
"""
self.api_uri = self.BASE_URI if not sandbox else self.BASE_URI_SANDBOX
self.web_payment_uri = (
self.WEB_APP_PAYMENT_URI
if not sandbox
else self.WEB_APP_PAYMENT_URI_SANDBOX
)

self._api_key = api_key
self._email = email
self._password = password
Expand Down Expand Up @@ -255,7 +264,12 @@ def create_payment_by_invoice(
if pay_currency not in self.currencies()["currencies"]:
raise NowPaymentsException("Unsupported cryptocurrency")
data = InvoicePaymentData(iid=invoice_id, pay_currency=pay_currency, **kwargs)
return self._post_requests("invoice-payment", data=data.clean_data_to_dict())
response = self._post_requests(
"invoice-payment", data=data.clean_data_to_dict()
)
uri = f"{self.web_payment_uri}?iid={invoice_id}&paymentId={response['payment_id']}"
response["uri"] = uri
return response

def minimum_payment_amount(
self, currency_from: str, currency_to: str, **kwargs
Expand Down
10 changes: 10 additions & 0 deletions tests/test_nowpayments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Testing Module"""
import datetime

import dotenv
import pytest
Expand Down Expand Up @@ -37,6 +38,7 @@ def test_initialization() -> None:
now_payments = NOWPaymentsAPI(api_key=config["API_KEY"])
assert now_payments.sandbox is False
assert now_payments.api_uri == "https://api.nowpayments.io/v1/"
assert now_payments.web_payment_uri == "https://nowpayments.io/payment/"
assert now_payments._api_key == config["API_KEY"]
assert now_payments._email == ""
assert now_payments._password == ""
Expand All @@ -46,13 +48,15 @@ def test_initialization() -> None:
)
assert now_payments.sandbox is False
assert now_payments.api_uri == "https://api.nowpayments.io/v1/"
assert now_payments.web_payment_uri == "https://nowpayments.io/payment/"
assert now_payments._api_key == config["API_KEY"]
assert now_payments._email == config["EMAIL"]
assert now_payments._password == config["PASSWORD"]
# Create a sandbox instance with API key only
now_payments = NOWPaymentsAPI(api_key=config["API_KEY"], sandbox=True)
assert now_payments.sandbox is True
assert now_payments.api_uri == "https://api-sandbox.nowpayments.io/v1/"
assert now_payments.web_payment_uri == "https://sandbox.nowpayments.io/payment/"
assert now_payments._api_key == config["API_KEY"]
assert now_payments._email == ""
assert now_payments._password == ""
Expand All @@ -65,6 +69,7 @@ def test_initialization() -> None:
)
assert now_payments.sandbox is True
assert now_payments.api_uri == "https://api-sandbox.nowpayments.io/v1/"
assert now_payments.web_payment_uri == "https://sandbox.nowpayments.io/payment/"
assert now_payments._api_key == config["API_KEY"]
assert now_payments._email == config["EMAIL"]
assert now_payments._password == config["PASSWORD"]
Expand Down Expand Up @@ -286,6 +291,11 @@ def test_create_payment_by_invoice(now_payments_api_key: NOWPaymentsAPI) -> None
response = now_payments_api_key.create_payment_by_invoice(invoice["id"], "btc")
assert "payment_id" in response
assert response["payment_status"] == "waiting"
assert "uri" in response
assert (
response["uri"]
== f"{now_payments_api_key.web_payment_uri}?iid={invoice['id']}&paymentId={response['payment_id']}"
)
assert "pay_address" in response
assert response["price_amount"] == 100
assert response["price_currency"] == "usd"
Expand Down

0 comments on commit 96e271a

Please sign in to comment.