Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# vim: set nospell:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v6.0.0
hooks:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
Expand All @@ -13,7 +13,7 @@ repos:
- id: check-added-large-files
- id: debug-statements
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.4
rev: v0.15.8
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
7 changes: 6 additions & 1 deletion payments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ class RedirectNeeded(Exception):


class PaymentError(Exception):
def __init__(self, message, code=None, gateway_message=None):
def __init__(
self,
message: str,
code: str | int | None = None,
gateway_message: str | None = None,
) -> None:
super().__init__(message)
self.code = code
self.gateway_message = gateway_message
Expand Down
2 changes: 1 addition & 1 deletion payments/authorizenet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(
transaction_key,
endpoint="https://test.authorize.net/gateway/transact.dll",
**kwargs,
):
) -> None:
self.login_id = login_id
self.transaction_key = transaction_key
self.endpoint = endpoint
Expand Down
16 changes: 8 additions & 8 deletions payments/authorizenet/test_authorizenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@ class Payment(Mock):
captured_amount = 0
message = ""

def get_process_url(self):
def get_process_url(self) -> str:
return "http://example.com"

def get_failure_url(self):
def get_failure_url(self) -> str:
return "http://cancel.com"

def get_success_url(self):
def get_success_url(self) -> str:
return "http://success.com"

def change_status(self, status, message=""):
def change_status(self, status: str, message: str = "") -> None:
self.status = status
self.message = message


@pytest.fixture
def payment():
def payment() -> Payment:
return Payment()


def test_provider_redirects_to_success_on_payment_success(payment):
def test_provider_redirects_to_success_on_payment_success(payment: Payment) -> None:
provider = AuthorizeNetProvider(login_id=LOGIN_ID, transaction_key=TRANSACTION_KEY)

response_data = [
Expand All @@ -82,7 +82,7 @@ def test_provider_redirects_to_success_on_payment_success(payment):


@pytest.mark.skip
def test_provider_shows_validation_error_message(payment):
def test_provider_shows_validation_error_message(payment: Payment) -> None:
provider = AuthorizeNetProvider(login_id=LOGIN_ID, transaction_key=TRANSACTION_KEY)

error_msg = "The merchant does not accept this type of credit card."
Expand All @@ -102,7 +102,7 @@ def test_provider_shows_validation_error_message(payment):


@pytest.mark.skip
def test_provider_shows_rejection_error_message(payment):
def test_provider_shows_rejection_error_message(payment: Payment) -> None:
provider = AuthorizeNetProvider(login_id=LOGIN_ID, transaction_key=TRANSACTION_KEY)

error_msg = " This transaction has been declined."
Expand Down
9 changes: 8 additions & 1 deletion payments/braintree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ class BraintreeProvider(BasicProvider):
:param sandbox: Whether to use a sandbox environment for testing
"""

def __init__(self, merchant_id, public_key, private_key, sandbox=True, **kwargs):
def __init__(
self,
merchant_id,
public_key,
private_key,
sandbox=True,
**kwargs,
) -> None:
self.merchant_id = merchant_id
self.public_key = public_key
self.private_key = private_key
Expand Down
2 changes: 1 addition & 1 deletion payments/braintree/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_customer_data(self):
"last_name": self.payment.billing_last_name,
}

def save(self):
def save(self) -> None:
braintree.Transaction.submit_for_settlement(self.transaction_id)
self.payment.transaction_id = self.transaction_id
self.payment.captured_amount = self.payment.total
Expand Down
14 changes: 7 additions & 7 deletions payments/braintree/test_braintree.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ class Payment(Mock):
transaction_id = None
captured_amount = 0

def get_process_url(self):
def get_process_url(self) -> str:
return "http://example.com"

def get_failure_url(self):
def get_failure_url(self) -> str:
return "http://cancel.com"

def get_success_url(self):
def get_success_url(self) -> str:
return "http://success.com"

def change_status(self, status):
def change_status(self, status: str) -> None:
self.status = status


@pytest.fixture
def payment():
def payment() -> Payment:
return Payment()


def test_provider_redirects_to_success_on_payment_success(payment):
def test_provider_redirects_to_success_on_payment_success(payment: Payment) -> None:
provider = BraintreeProvider(MERCHANT_ID, PUBLIC_KEY, PRIVATE_KEY)
transaction_id = "12345"
with (
Expand All @@ -72,7 +72,7 @@ def test_provider_redirects_to_success_on_payment_success(payment):
assert payment.transaction_id == transaction_id


def test_provider_shows_validation_error_message(payment):
def test_provider_shows_validation_error_message(payment: Payment) -> None:
provider = BraintreeProvider(MERCHANT_ID, PUBLIC_KEY, PRIVATE_KEY)
error_msg = "error message"
with (
Expand Down
4 changes: 2 additions & 2 deletions payments/coinbase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CoinbaseProvider(BasicProvider):
api_url = "https://api.%(endpoint)s/v1/buttons"
checkout_url = "https://%(endpoint)s/checkouts"

def __init__(self, key, secret, endpoint="sandbox.coinbase.com", **kwargs):
def __init__(self, key, secret, endpoint="sandbox.coinbase.com", **kwargs) -> None:
self.endpoint = endpoint
self.key = key
self.secret = secret
Expand Down Expand Up @@ -74,7 +74,7 @@ def get_checkout_code(self, payment):
results = response.json()
return results["button"]["code"]

def get_action(self, payment):
def get_action(self, payment) -> str:
checkout_url = self.checkout_url % {"endpoint": self.endpoint}
return f"{checkout_url}/{self.get_checkout_code(payment)}"

Expand Down
31 changes: 20 additions & 11 deletions payments/coinbase/test_coinbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.http import HttpResponseForbidden

from payments import PaymentStatus
from payments import PurchasedItem

from . import CoinbaseProvider

Expand All @@ -36,32 +37,32 @@ class Payment:
token = PAYMENT_TOKEN
variant = VARIANT

def change_status(self, status):
def change_status(self, status: str) -> None:
self.status = status

def get_failure_url(self):
def get_failure_url(self) -> str:
return "http://cancel.com"

def get_process_url(self):
def get_process_url(self) -> str:
return "http://example.com"

def get_purchased_items(self):
def get_purchased_items(self) -> list[PurchasedItem]:
return []

def save(self):
def save(self) -> Payment:
return self

def get_success_url(self):
def get_success_url(self) -> str:
return "http://success.com"


@pytest.fixture
def provider():
def provider() -> tuple[Payment, CoinbaseProvider]:
payment = Payment()
return payment, CoinbaseProvider(key=KEY, secret=SECRET)


def test_process_data(provider):
def test_process_data(provider: tuple[Payment, CoinbaseProvider]) -> None:
payment, prov = provider
request = MagicMock()
request.body = json.dumps(COINBASE_REQUEST)
Expand All @@ -70,7 +71,9 @@ def test_process_data(provider):
assert payment.status == PaymentStatus.CONFIRMED


def test_incorrect_custom_token_process_data(provider):
def test_incorrect_custom_token_process_data(
provider: tuple[Payment, CoinbaseProvider],
) -> None:
payment, prov = provider
data = dict(COINBASE_REQUEST)
data.update({"order": {"custom": "fake"}})
Expand All @@ -80,7 +83,9 @@ def test_incorrect_custom_token_process_data(provider):
assert isinstance(response, HttpResponseForbidden)


def test_incorrect_data_process_data(provider):
def test_incorrect_data_process_data(
provider: tuple[Payment, CoinbaseProvider],
) -> None:
payment, prov = provider
request = MagicMock()
request.POST = {"id": "1234"}
Expand All @@ -90,7 +95,11 @@ def test_incorrect_data_process_data(provider):

@patch("time.time")
@patch("requests.post")
def test_provider_returns_checkout_url(mocked_post, mocked_time, provider):
def test_provider_returns_checkout_url(
mocked_post: MagicMock,
mocked_time: MagicMock,
provider: tuple[Payment, CoinbaseProvider],
) -> None:
payment, prov = provider
code = "123abc"
signature = "21d476eff7b2e6cccdfe6deb0c097ba638d5de7e775b303e4fdb2f8bfeff72e2"
Expand Down
2 changes: 1 addition & 1 deletion payments/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_action(self, payment):
"""The ``action`` for the HTML form element."""
return self.get_return_url(payment)

def __init__(self, capture=True):
def __init__(self, capture=True) -> None:
"""Create a new provider instance.

This method should not be called directly; use :func:`provider_factory`
Expand Down
16 changes: 8 additions & 8 deletions payments/cybersource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(
fingerprint_url="https://h.online-metrix.net/fp/",
sandbox=True,
capture=True,
):
) -> None:
self.merchant_id = merchant_id
self.password = password
local_path = os.path.dirname(__file__)
Expand Down Expand Up @@ -108,14 +108,14 @@ def get_form(self, payment, data=None):
return e.args[0]
return form

def _change_status_to_confirmed(self, payment):
def _change_status_to_confirmed(self, payment) -> None:
if self._capture:
payment.captured_amount = payment.total
payment.change_status(PaymentStatus.CONFIRMED)
else:
payment.change_status(PaymentStatus.PREAUTH)

def _set_proper_payment_status_from_reason_code(self, payment, reason_code):
def _set_proper_payment_status_from_reason_code(self, payment, reason_code) -> None:
if reason_code == ACCEPTED:
payment.change_fraud_status(FraudStatus.ACCEPT, commit=False)
self._change_status_to_confirmed(payment)
Expand Down Expand Up @@ -164,7 +164,7 @@ def _set_proper_payment_status_from_reason_code(self, payment, reason_code):
payment.change_status(PaymentStatus.ERROR, message=error)
raise PaymentError(error)

def charge(self, payment, data):
def charge(self, payment, data) -> None:
if self._capture:
params = self._prepare_sale(payment, data)
else:
Expand All @@ -182,10 +182,10 @@ def charge(self, payment, data):
cc_data = dict(data)
expiration = cc_data.pop("expiration")
cc_data["expiration"] = {"month": expiration.month, "year": expiration.year}
cc_data = signing.dumps(cc_data)
cc_data_signed = signing.dumps(cc_data)
payload = {
"PaReq": response.payerAuthEnrollReply.paReq,
"TermUrl": self.get_return_url(payment, {"token": cc_data}),
"TermUrl": self.get_return_url(payment, {"token": cc_data_signed}),
"MD": xid,
}
form = BaseForm(data=payload, action=action, autosubmit=True)
Expand All @@ -208,7 +208,7 @@ def capture(self, payment, amount=None):
raise PaymentError(error)
return amount

def release(self, payment):
def release(self, payment) -> None:
params = self._prepare_release(payment)
response = self._make_request(payment, params)
if response.reasonCode == ACCEPTED:
Expand Down Expand Up @@ -384,7 +384,7 @@ def _prepare_refund(self, payment, amount=None):
"purchaseTotals": self._prepare_totals(payment, amount=amount),
}

def _prepare_card_type(self, card_number):
def _prepare_card_type(self, card_number) -> str | None:
card_type, _card_name = get_credit_card_issuer(card_number)
if card_type == "visa":
return "001"
Expand Down
4 changes: 2 additions & 2 deletions payments/cybersource/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class FingerprintInput(forms.CharField):
widget = FingerprintWidget
hidden_widget = FingerprintWidget

def __init__(self, org_id, merchant_id, fingerprint_url, *args, **kwargs):
def __init__(self, org_id, merchant_id, fingerprint_url, *args, **kwargs) -> None:
self.org_id = org_id
self.merchant_id = merchant_id
self.fingerprint_url = fingerprint_url
Expand All @@ -37,7 +37,7 @@ def widget_attrs(self, widget):


class PaymentForm(CreditCardPaymentFormWithName):
def __init__(self, *args, **kwargs):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
if self.provider.org_id:
try:
Expand Down
Loading
Loading