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
15 changes: 6 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CI

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
lint:
Expand All @@ -15,7 +15,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: "3.13"
cache: "pip"
cache-dependency-path: "setup.py"
- name: lint
Expand All @@ -28,20 +28,17 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: "3.13"
cache: "pip"
cache-dependency-path: "setup.py"
- name: fmtcheck
run: make fmtcheck

build:
# Specific ubuntu version to support python 3.6 testing
# see https://github.com/actions/setup-python/issues/544#issuecomment-1332535877 for details
# move to ubuntu-latest when we drop 3.6
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
strategy:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9, '3.10']
python-version: [3.9, "3.10", "3.11", "3.12", "3.13"]

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

## 0.0.9 - 2025-10-01
* [#16](https://github.com/Dintero/Dintero.Python.SDK/issues/16) Fix bug in session validation

## 0.0.8 - 2023-07-13
* Change link to documentation for creating Checkout client
Expand Down
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
VENV_NAME?=venv
PIP?=pip
PYTHON?=python
PYTHON?=python3

venv: $(VENV_NAME)/bin/activate

$(VENV_NAME)/bin/activate: setup.py
$(PIP) install --upgrade pip virtualenv
@test -d $(VENV_NAME) || $(PYTHON) -m virtualenv --clear $(VENV_NAME)
@test -d $(VENV_NAME) || $(PYTHON) -m venv $(VENV_NAME)
${VENV_NAME}/bin/python -m pip install -U pip tox
${VENV_NAME}/bin/python -m pip install -e .
${VENV_NAME}/bin/python -m pip install -e '.[dev]'
@touch $(VENV_NAME)/bin/activate

test: venv
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pip install --upgrade dintero

### Requirements

* Python 3.6+
* Python 3.9+

## Using the SDK

Expand Down
15 changes: 11 additions & 4 deletions dintero/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@


def validate_session(session):
if session["order"]["amount"] != sum(
[item["amount"] for item in session["order"]["items"]]
):
"""
Validates that the session's total order amount matches the sum of the
item amounts plus the shipping amount.
"""
order = session.get("order", {})
items_total = sum(item.get("amount", 0) for item in order.get("items", []))
shipping_amount = order.get("shipping_option", {}).get("amount", 0)

if order.get("amount") != items_total + shipping_amount:
raise InvalidFieldError(
"order.amount doesn't match sum of order.items", "order.amount"
"order.amount does not match the sum of order.items and shipping_option.amount",
"order.amount",
)
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"pytest",
]
},
python_requires=">=3.6",
python_requires=">=3.9",
project_urls={
"Bug Tracker": "https://github.com/dintero/Dintero.Python.SDK/issues",
"Documentation": "https://docs.dintero.com",
Expand All @@ -52,6 +52,9 @@
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Software Development :: Libraries :: Python Modules",
],
Expand Down
67 changes: 67 additions & 0 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pytest

from dintero.error import InvalidFieldError
from dintero.validator import validate_session


def test_validate_session_valid():
"""
Should not raise an exception when the order amount matches the sum of
the item amounts.
"""
session = {
"order": {
"amount": 60000,
"items": [
{"amount": 10000},
{"amount": 20000},
{"amount": 30000},
],
}
}
validate_session(session)


def test_validate_session_shipping_option_valid():
"""
Should not raise an exception when the order amount matches the sum of
the item amounts and shipping option.
"""
session = {
"order": {
"amount": 70000,
"items": [
{"amount": 10000},
{"amount": 20000},
{"amount": 30000},
],
"shipping_option": {
"amount": 10000,
},
},
}
validate_session(session)


def test_validate_session_invalid_raises_error():
"""
Should raise InvalidFieldError when the order amount does not match the
sum of the item amounts.
"""
session = {
"order": {
"amount": 50000,
"items": [
{"amount": 10000},
{"amount": 20000},
{"amount": 30000},
],
}
}
with pytest.raises(InvalidFieldError) as excinfo:
validate_session(session)
assert (
"order.amount does not match the sum of order.items and shipping_option.amount"
in str(excinfo.value)
)
assert excinfo.value.field == "order.amount"
8 changes: 4 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
envlist =
fmt
lint
py{310,39,38,37,36,py3}
py{313,312,311,310,39}
skip_missing_interpreters = true

[tool:pytest]
Expand All @@ -19,7 +19,7 @@ deps =
pytest >= 6.0.0
pytest-cov >= 2.12.1
depends =
report: py310
report: py313
commands = pytest --cov --cov-append

[testenv:report]
Expand All @@ -31,14 +31,14 @@ commands =

[testenv:fmt]
description = run code formatting using black
basepython = python3.10
basepython = python3.13
deps = black==23.7.0
commands = black . {posargs}
skip_install = true

[testenv:lint]
description = run static analysis and style check using flake8
basepython = python3.10
basepython = python3.13
deps = flake8
commands = python -m flake8 --show-source dintero tests setup.py
skip_install = true