Skip to content

Commit

Permalink
Add helper for decimal quantization, bump CyberSource REST client, qu…
Browse files Browse the repository at this point in the history
…antize totals to 2 decimals
  • Loading branch information
jkachel committed Nov 8, 2024
1 parent a97420c commit fa227d4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
43 changes: 43 additions & 0 deletions src/payment_gateway/changelog.d/20241107_212223_jkachel_add_tax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Removed
- A bullet item for the Removed category.
-->

### Added

- Adds support for tax collection.
- Bumps CyberSource REST Client package to at least 0.0.54.
- Adds a helper for quantizing decimals for currency amounts.

<!--
### Changed
- A bullet item for the Changed category.
-->
<!--
### Deprecated
- A bullet item for the Deprecated category.
-->
<!--
### Fixed
- A bullet item for the Fixed category.
-->
<!--
### Security
- A bullet item for the Security category.
-->
6 changes: 3 additions & 3 deletions src/payment_gateway/mitol/payment_gateway/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
InvalidTransactionException,
RefundDuplicateException,
)
from mitol.payment_gateway.payment_utils import clean_request_data, strip_nones
from mitol.payment_gateway.payment_utils import clean_request_data, strip_nones, quantize_decimal


@dataclass
Expand Down Expand Up @@ -461,8 +461,8 @@ def prepare_checkout(

payload = {
"access_key": settings.MITOL_PAYMENT_GATEWAY_CYBERSOURCE_ACCESS_KEY,
"amount": str(total + tax_total),
"tax_amount": str(tax_total),
"amount": str(quantize_decimal((total + tax_total))),
"tax_amount": str(quantize_decimal((tax_total))),
"consumer_id": consumer_id,
"currency": "USD",
"locale": "en-us",
Expand Down
7 changes: 7 additions & 0 deletions src/payment_gateway/mitol/payment_gateway/payment_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Utilities for the Payment Gateway"""

from decimal import Decimal


# To delete None values in Input Request Json body
def clean_request_data(request_data):
Expand All @@ -21,3 +23,8 @@ def strip_nones(datasource):
retval[key] = datasource[key]

return retval


def quantize_decimal(value, precision=2):
"""Quantize a decimal value to the specified precision"""
return Decimal(value).quantize(Decimal("0.{}".format("0" * precision)))
2 changes: 1 addition & 1 deletion src/payment_gateway/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "mitol-django-payment-gateway"
version = "2023.12.19"
description = "Django application to handle payment processing"
dependencies = [
"cybersource-rest-client-python>=0.0.36",
"cybersource-rest-client-python>=0.0.59",
"django-stubs>=1.13.1",
"django>=3.0",
"mitol-django-common"
Expand Down
5 changes: 3 additions & 2 deletions tests/mitol/payment_gateway/api/test_cybersource.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import namedtuple
from dataclasses import dataclass
from datetime import datetime
from decimal import Decimal
from typing import Dict

import pytest
Expand Down Expand Up @@ -98,8 +99,8 @@ def generate_test_cybersource_payload(order, cartitems, transaction_uuid):

test_payload = {
"access_key": settings.MITOL_PAYMENT_GATEWAY_CYBERSOURCE_ACCESS_KEY,
"amount": str(test_total + tax_total),
"tax_amount": str(tax_total),
"amount": str(Decimal(test_total + tax_total).quantize(Decimal("0.01"))),
"tax_amount": str(Decimal(tax_total).quantize(Decimal("0.01"))),
"consumer_id": consumer_id,
"currency": "USD",
"locale": "en-us",
Expand Down
15 changes: 14 additions & 1 deletion tests/mitol/payment_gateway/utils/test_payment_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Tests for payment_gateway application utils""" # noqa: INP001

from decimal import Decimal

import pytest
from mitol.payment_gateway.payment_utils import clean_request_data, strip_nones
from mitol.payment_gateway.payment_utils import clean_request_data, strip_nones, quantize_decimal


@pytest.mark.parametrize(
Expand Down Expand Up @@ -44,3 +46,14 @@ def test_strip_nones():
test_ds2 = strip_nones(ds2)

assert test_ds2 == ds2


def test_quantize_decimal():
"""Tests quantize_decimal to make sure that the decimal is quantized to the correct precision"""

test_decimal = 1.23456789
test_precision = 2

quantized_decimal = quantize_decimal(test_decimal, test_precision)

assert quantized_decimal == Decimal("1.23")

0 comments on commit fa227d4

Please sign in to comment.