Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payments | Fix #112

Merged
merged 37 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ef0f2bf
CI/CD for payments
lsoulworkerl Mar 28, 2023
4351596
Update payments_action.yml
lsoulworkerl Mar 28, 2023
b1c4472
Update payments_action.yml
lsoulworkerl Mar 28, 2023
7ccb538
Merge branch 'DJWOMS:payments' into payments
lsoulworkerl Mar 30, 2023
0114b83
Fix start.sh
lsoulworkerl Mar 30, 2023
4f7ac0d
fix start.sh with Docerfile
lsoulworkerl Mar 30, 2023
71e4db4
Moun host directory, edit entrypoint to run server
lsoulworkerl Mar 30, 2023
a25580b
Add localtunnel
lsoulworkerl Mar 31, 2023
f7b9ffe
Cosmetic changes for linter
lsoulworkerl Mar 31, 2023
26af0b5
Cosmetic changes for linter #1
lsoulworkerl Mar 31, 2023
1c720cb
Create .env.example for docker ci/cd
lsoulworkerl Mar 31, 2023
3c29408
Update payments_action.yml
lsoulworkerl Mar 31, 2023
8d5373f
Fix docker files
lsoulworkerl Apr 1, 2023
4365f99
Merge branch 'payments' of https://github.com/lsoulworkerl/GSpot into…
lsoulworkerl Apr 1, 2023
d1cbc41
Cosmetic changes
lsoulworkerl Apr 2, 2023
5da0ab4
Create .env.example
lsoulworkerl Apr 2, 2023
6b9605c
Cosmetic changes
lsoulworkerl Apr 2, 2023
50deabe
Merge branch 'payments' of https://github.com/lsoulworkerl/GSpot into…
lsoulworkerl Apr 2, 2023
fd3fbd8
Cosmetic changes
lsoulworkerl Apr 2, 2023
9bd78d1
Add some settings to .env file
lsoulworkerl Apr 3, 2023
8485143
readme
lsoulworkerl Apr 3, 2023
280ff52
readme
lsoulworkerl Apr 3, 2023
60a4119
Edit README file
lsoulworkerl Apr 3, 2023
f01ae67
Update .env.example
lsoulworkerl Apr 3, 2023
b416165
Add migrations
lsoulworkerl Apr 3, 2023
d2f3336
Merge branch 'payments' of https://github.com/lsoulworkerl/GSpot into…
lsoulworkerl Apr 3, 2023
35df12a
Edit migrations for CI/CD
lsoulworkerl Apr 3, 2023
e923bc2
Removed static ignore
lsoulworkerl Apr 3, 2023
e7eb634
Merge branch 'payments' into payments
lsoulworkerl Apr 3, 2023
372734f
Add black and auto-trailing-comma
lsoulworkerl Apr 7, 2023
5650357
Fix
lsoulworkerl Apr 7, 2023
69524a0
Merge branch 'payments' of https://github.com/lsoulworkerl/GSpot into…
lsoulworkerl Apr 9, 2023
a3c18c8
Edit .env.example
lsoulworkerl Apr 9, 2023
02e2946
Add black/isort to pre-commit
lsoulworkerl Apr 9, 2023
0330deb
Delete flake8-isort
lsoulworkerl Apr 9, 2023
5a0e750
hotfix
lsoulworkerl Apr 9, 2023
185ca95
Update migrations and readme
lsoulworkerl Apr 9, 2023
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
2 changes: 1 addition & 1 deletion backend/payments/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SECRET_KEY='django'
DEBUG=True
ALLOWED_HOSTS='*'
JWT_SECRET_KEY='Token'
SHOP_AСCOUNT_ID=111111
SHOP_ACCOUNT_ID=111111
SHOP_SECRET_KEY='test_1111111111'
ROLLBAR_ACCESS_TOKEN='1111111111'
POSTGRES_PASSWORD=postgres
Expand Down
1 change: 1 addition & 0 deletions backend/payments/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/payment_response_example.py
/static
57 changes: 33 additions & 24 deletions backend/payments/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
files: 'backend/payments*'
repos:
- repo: https://github.com/pre-commit/mirrors-isort
rev: "v5.9.3"
hooks:
- id: isort
args: [--profile=black]

- repo: https://github.com/pycqa/flake8.git
rev: 6.0.0
hooks:
- id: flake8
args: [
"--ignore=E501"
]
additional_dependencies: [
"flake8-bugbear",
"flake8-builtins",
"pep8-naming",
"flake8-commas",
"flake8-quotes",
"flake8-todo",
"flake8-django",
"flake8-cognitive-complexity",
"flake8-isort",
]
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
args: [--skip-string-normalization, --line-length=100]
language_version: python3.11
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.4.0
hooks:
- id: add-trailing-comma
- repo: https://github.com/timothycrosley/isort
rev: 5.12.0
hooks:
- id: isort
args: [--profile=black]
- repo: https://github.com/pycqa/flake8.git
rev: 6.0.0
hooks:
- id: flake8
args: [
"--max-line-length=100"
]
additional_dependencies: [
"flake8-bugbear",
"flake8-builtins",
"pep8-naming",
"flake8-commas",
"flake8-quotes",
"flake8-todo",
"flake8-django",
"flake8-cognitive-complexity",
]
1 change: 0 additions & 1 deletion backend/payments/Dockerfiles/scripts/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ done
>&2 echo "Redis is available"

python manage.py collectstatic --noinput
python manage.py makemigrations --noinput
python manage.py migrate
gunicorn config.wsgi:application --bind :8000 -k gevent

Expand Down
2 changes: 1 addition & 1 deletion backend/payments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Subsequently, all indexed files will be checked with `Flake8` before committing.

To check with `Flake8` the files indexed for the commit run:
```shell
pre-commit run
pre-commit run --all-files
```
If it's necessary to skip using `pre-commit hook`, the commit should run with the `-n` or `--no-verify` flag:
```shell
Expand Down
21 changes: 14 additions & 7 deletions backend/payments/apps/base/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
class MoneyField(DecimalField):
def __init__(
self,
verbose_name=None,
name=None,
max_digits=settings.MAX_BALANCE_DIGITS,
decimal_places=2,
**kwargs,
verbose_name=None,
name=None,
max_digits=settings.MAX_BALANCE_DIGITS,
decimal_places=2,
**kwargs,
):
super().__init__(
verbose_name, name, max_digits, decimal_places, **kwargs,
verbose_name,
name,
max_digits,
decimal_places,
**kwargs,
)


Expand All @@ -26,5 +30,8 @@ def __init__(
**kwargs,
):
super().__init__(
max_digits, decimal_places, min_value, **kwargs,
max_digits,
decimal_places,
min_value,
**kwargs,
)
35 changes: 18 additions & 17 deletions backend/payments/apps/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import rollbar
from _decimal import Decimal
from apps.payment_accounts.models import Account, BalanceChange
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from django.db.models import Model

from apps.payment_accounts.models import Account, BalanceChange

DjangoModel = TypeVar('DjangoModel', bound=Model)


def parse_model_instance(
*,
django_model: type[DjangoModel],
error_message: str,
pk: int,
*,
django_model: type[DjangoModel],
error_message: str,
pk: int,
) -> DjangoModel | None:
try:
django_model_instance = django_model.objects.get(pk=pk)
Expand All @@ -28,9 +27,9 @@ def parse_model_instance(


def increase_user_balance(
*,
balance_change_object: BalanceChange,
amount: Decimal,
*,
balance_change_object: BalanceChange,
amount: Decimal,
) -> None:
with transaction.atomic():
balance_change_object.is_accepted = True
Expand All @@ -41,10 +40,11 @@ def increase_user_balance(
pk=balance_change_object.account_id.pk,
amount=Decimal(amount),
)
rollbar.report_message((
f'Deposit {balance_change_object.amount} {settings.DEFAULT_CURRENCY} '
f'to user account {balance_change_object.account_id}'
),
rollbar.report_message(
(
f'Deposit {balance_change_object.amount} {settings.DEFAULT_CURRENCY} '
f'to user account {balance_change_object.account_id}'
),
'info',
)

Expand All @@ -62,9 +62,10 @@ def decrease_user_balance(*, account: Account, amount: Decimal):
pk=account.pk,
amount=Decimal(amount),
)
rollbar.report_message((
f'Withdraw {balance_change_object.amount} {settings.DEFAULT_CURRENCY} '
f'from user account {balance_change_object.account_id}'
),
rollbar.report_message(
(
f'Withdraw {balance_change_object.amount} {settings.DEFAULT_CURRENCY} '
f'from user account {balance_change_object.account_id}'
),
'info',
)
3 changes: 1 addition & 2 deletions backend/payments/apps/external_payments/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from decimal import Decimal
from uuid import UUID

from apps.base.schemas import URL
from dataclasses_json import config, dataclass_json
from django.conf import settings

from apps.base.schemas import URL


class PaymentResponseStatuses(enum.Enum):
succeeded = 'payment.succeeded'
Expand Down
4 changes: 2 additions & 2 deletions backend/payments/apps/external_payments/serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from apps.base.fields import MoneySerializerField
from rest_enumfield import EnumField
from rest_framework import serializers

from apps.base.fields import MoneySerializerField

from .schemas import PaymentResponseStatuses, PaymentTypes


Expand All @@ -18,6 +17,7 @@ def rename_builtin_names(self, old_data: dict, postfix: str = '_'):
given postfix.
e.g. field name is `id` and postfix is `_yookassa`: id -> id_yookassa
"""

def generate_new_key():
builtin_names = ('id', 'type', 'object')
if key in builtin_names:
Expand Down
27 changes: 13 additions & 14 deletions backend/payments/apps/external_payments/services/accept_payment.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from decimal import Decimal

import rollbar

from apps.base import utils
from apps.external_payments.schemas import (PaymentResponseStatuses,
YookassaPaymentResponse)
from apps.external_payments.schemas import (
PaymentResponseStatuses,
YookassaPaymentResponse,
)
from apps.payment_accounts.models import Account, BalanceChange
from apps.payment_accounts.services.payment_commission import \
calculate_payment_without_commission
from apps.payment_accounts.services.payment_commission import (
calculate_payment_without_commission,
)
from apps.transactions.models import Invoice

from . import invoice_execution as pay_proc
Expand Down Expand Up @@ -64,9 +66,7 @@ def _run_payment_acceptance(self):
def parse_user_account(self):
return utils.parse_model_instance(
django_model=Account,
error_message=(
f"Can't get user account instance for user id {self.account_id}"
),
error_message=(f"Can't get user account instance for user id {self.account_id}"),
pk=self.account_id,
)

Expand All @@ -79,9 +79,7 @@ def __init__(self, yookassa_response: YookassaPaymentResponse):
def _parse_balance_object(self) -> BalanceChange | None:
return utils.parse_model_instance(
django_model=BalanceChange,
error_message=(
f"Can't get payment instance for payment id {self.payment_body.id_}"
),
error_message=(f"Can't get payment instance for payment id {self.payment_body.id_}"),
pk=int(self.payment_body.metadata['balance_change_id']),
)

Expand Down Expand Up @@ -154,9 +152,10 @@ def _is_commission_correct(self):


def execute_invoice_operations(
*, invoice_instance: Invoice,
payer_account: Account,
decrease_amount: Decimal,
*,
invoice_instance: Invoice,
payer_account: Account,
decrease_amount: Decimal,
):
invoice_executioner = pay_proc.InvoiceExecution(invoice_instance)
invoice_executioner.process_invoice_transactions()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from yookassa import Payment

from apps.base.schemas import URL
from yookassa import Payment

from .. import schemas


def get_yookassa_payment_url(
payment_data: schemas.PaymentCreateDataClass,
metadata: dict,
payment_data: schemas.PaymentCreateDataClass,
metadata: dict,
) -> URL:

yookassa_payment_info = schemas.YookassaPaymentCreate(
amount=schemas.AmountDataClass(
value=payment_data.payment_amount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from datetime import datetime

from django.conf import settings

from apps.transactions.models import Invoice, Transaction, TransactionHistory
from django.conf import settings

from ..exceptions import ExtraTransactionHistoriesError
from ..tasks import get_item_for_self_user, gift_item_to_other_user
Expand Down Expand Up @@ -33,7 +32,7 @@ def process_transaction(self, invoice_transaction: Transaction) -> None:

@staticmethod
def get_transaction_execution_date_time(
invoice_transaction: Transaction,
invoice_transaction: Transaction,
) -> datetime:
transactions_history = invoice_transaction.transactions_history.all()
if len(transactions_history) > 1:
Expand Down
Loading