Skip to content

Commit 2a7ece2

Browse files
🎨 1. part: invoice expiration fix (🗃️) (ITISFoundation#5532)
1 parent 9e9bbb2 commit 2a7ece2

File tree

14 files changed

+52
-57
lines changed

14 files changed

+52
-57
lines changed

packages/models-library/src/models_library/payments.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from decimal import Decimal
2-
from typing import Any, ClassVar
2+
from typing import Any, ClassVar, TypeAlias
33

44
from models_library.emails import LowerCaseEmailStr
55
from pydantic import BaseModel, Field, validator
66

77
from .products import StripePriceID, StripeTaxRateID
88

9+
StripeInvoiceID: TypeAlias = str
10+
911

1012
class UserInvoiceAddress(BaseModel):
1113
line1: str | None = None
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""add invoice id column
2+
3+
Revision ID: 4a0f4efe8c86
4+
Revises: 5b37c3bc99af
5+
Create Date: 2024-03-21 10:27:45.493789+00:00
6+
7+
"""
8+
import sqlalchemy as sa
9+
from alembic import op
10+
11+
# revision identifiers, used by Alembic.
12+
revision = "4a0f4efe8c86"
13+
down_revision = "5b37c3bc99af"
14+
branch_labels = None
15+
depends_on = None
16+
17+
18+
def upgrade():
19+
# ### commands auto generated by Alembic - please adjust! ###
20+
op.add_column(
21+
"payments_transactions",
22+
sa.Column("stripe_invoice_id", sa.String(), nullable=True),
23+
)
24+
# ### end Alembic commands ###
25+
26+
27+
def downgrade():
28+
# ### commands auto generated by Alembic - please adjust! ###
29+
op.drop_column("payments_transactions", "stripe_invoice_id")
30+
# ### end Alembic commands ###

packages/postgres-database/src/simcore_postgres_database/models/payments_transactions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ def is_acknowledged(self) -> bool:
9595
nullable=True,
9696
doc="Link to invoice of this transaction. Available when completed",
9797
),
98+
sa.Column(
99+
"stripe_invoice_id",
100+
sa.String,
101+
nullable=True,
102+
doc="Invoice ID of invoice of this transaction. Available when completed",
103+
),
98104
#
99105
# States
100106
#

services/payments/src/simcore_service_payments/api/rpc/_payments.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from servicelib.logging_utils import get_log_record_extra, log_context
2020
from servicelib.rabbitmq import RPCRouter
2121

22-
from ...core.settings import ApplicationSettings
2322
from ...db.payments_transactions_repo import PaymentsTransactionsRepo
2423
from ...services import payments
2524
from ...services.payments_gateway import PaymentsGatewayApi
@@ -47,9 +46,6 @@ async def init_payment( # pylint: disable=too-many-arguments
4746
stripe_tax_rate_id: StripeTaxRateID,
4847
comment: str | None = None,
4948
) -> WalletPaymentInitiated:
50-
51-
settings: ApplicationSettings = app.state.settings
52-
5349
with log_context(
5450
_logger,
5551
logging.INFO,
@@ -60,7 +56,6 @@ async def init_payment( # pylint: disable=too-many-arguments
6056
return await payments.init_one_time_payment(
6157
gateway=PaymentsGatewayApi.get_from_app_state(app),
6258
repo=PaymentsTransactionsRepo(db_engine=app.state.engine),
63-
settings=settings,
6459
amount_dollars=amount_dollars,
6560
target_credits=target_credits,
6661
product_name=product_name,

services/payments/src/simcore_service_payments/api/rpc/_payments_methods.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from servicelib.logging_utils import get_log_record_extra, log_context
2222
from servicelib.rabbitmq import RPCRouter
2323

24-
from ...core.settings import ApplicationSettings
2524
from ...db.payments_methods_repo import PaymentsMethodsRepo
2625
from ...db.payments_transactions_repo import PaymentsTransactionsRepo
2726
from ...services import payments, payments_methods
@@ -170,8 +169,6 @@ async def pay_with_payment_method( # noqa: PLR0913 # pylint: disable=too-many-a
170169
stripe_tax_rate_id: StripeTaxRateID,
171170
comment: str | None = None,
172171
):
173-
settings: ApplicationSettings = app.state.settings
174-
175172
with log_context(
176173
_logger,
177174
logging.INFO,
@@ -186,7 +183,6 @@ async def pay_with_payment_method( # noqa: PLR0913 # pylint: disable=too-many-a
186183
repo_transactions=PaymentsTransactionsRepo(db_engine=app.state.engine),
187184
repo_methods=PaymentsMethodsRepo(db_engine=app.state.engine),
188185
notifier=NotifierService.get_from_app_state(app),
189-
settings=settings,
190186
payment_method_id=payment_method_id,
191187
amount_dollars=amount_dollars,
192188
target_credits=target_credits,

services/payments/src/simcore_service_payments/db/payments_transactions_repo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
PaymentNotFoundError,
99
)
1010
from models_library.api_schemas_webserver.wallets import PaymentID
11+
from models_library.payments import StripeInvoiceID
1112
from models_library.products import ProductName
1213
from models_library.users import UserID
1314
from models_library.wallets import WalletID
@@ -66,6 +67,7 @@ async def update_ack_payment_transaction(
6667
completion_state: PaymentTransactionState,
6768
state_message: str | None,
6869
invoice_url: HttpUrl | None,
70+
stripe_invoice_id: StripeInvoiceID | None,
6971
) -> PaymentsTransactionsDB:
7072
"""
7173
- ACKs payment by updating state with SUCCESS, CANCEL, etc
@@ -112,6 +114,7 @@ async def update_ack_payment_transaction(
112114
completed_at=sa.func.now(),
113115
state=completion_state,
114116
invoice_url=invoice_url,
117+
stripe_invoice_id=stripe_invoice_id,
115118
**optional,
116119
)
117120
.where(payments_transactions.c.payment_id == f"{payment_id}")

services/payments/src/simcore_service_payments/models/db.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from models_library.api_schemas_webserver.wallets import PaymentID, PaymentMethodID
66
from models_library.emails import LowerCaseEmailStr
7+
from models_library.payments import StripeInvoiceID
78
from models_library.products import ProductName
89
from models_library.users import UserID
910
from models_library.wallets import WalletID
@@ -23,6 +24,7 @@
2324
"wallet_id": 123,
2425
"comment": "This is a test comment.",
2526
"invoice_url": None,
27+
"stripe_invoice_id": None,
2628
"initiated_at": "2023-09-27T10:00:00",
2729
"state": PaymentTransactionState.PENDING,
2830
}
@@ -38,6 +40,7 @@ class PaymentsTransactionsDB(BaseModel):
3840
wallet_id: WalletID
3941
comment: str | None
4042
invoice_url: HttpUrl | None
43+
stripe_invoice_id: StripeInvoiceID | None
4144
initiated_at: datetime.datetime
4245
completed_at: datetime.datetime | None
4346
state: PaymentTransactionState

services/payments/src/simcore_service_payments/services/auto_recharge_process_message.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ async def _perform_auto_recharge(
138138
payment_method_db: PaymentsMethodsDB,
139139
wallet_auto_recharge: GetWalletAutoRecharge,
140140
):
141-
settings: ApplicationSettings = app.state.settings
142-
143141
rabbitmq_rpc_client = get_rabbitmq_rpc_client(app)
144142

145143
result = await rabbitmq_rpc_client.request(
@@ -157,7 +155,6 @@ async def _perform_auto_recharge(
157155
repo_transactions=PaymentsTransactionsRepo(db_engine=app.state.engine),
158156
repo_methods=PaymentsMethodsRepo(db_engine=app.state.engine),
159157
notifier=NotifierService.get_from_app_state(app),
160-
settings=settings,
161158
#
162159
payment_method_id=cast(PaymentMethodID, wallet_auto_recharge.payment_method_id),
163160
amount_dollars=wallet_auto_recharge.top_up_amount_in_usd,

services/payments/src/simcore_service_payments/services/payments.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from tenacity.stop import stop_after_attempt
3737

3838
from .._constants import RUT
39-
from ..core.settings import ApplicationSettings
4039
from ..db.payments_transactions_repo import PaymentsTransactionsRepo
4140
from ..models.db import PaymentsTransactionsDB
4241
from ..models.db_to_api import to_payments_api_model
@@ -58,7 +57,6 @@
5857
async def init_one_time_payment(
5958
gateway: PaymentsGatewayApi,
6059
repo: PaymentsTransactionsRepo,
61-
settings: ApplicationSettings,
6260
*,
6361
amount_dollars: Decimal,
6462
target_credits: Decimal,
@@ -91,7 +89,6 @@ async def init_one_time_payment(
9189
else StripeTaxExempt.reverse
9290
),
9391
),
94-
payment_gateway_tax_feature_enabled=settings.PAYMENTS_GATEWAY_TAX_FEATURE_ENABLED,
9592
)
9693

9794
submission_link = gateway.get_form_payment_url(init.payment_id)
@@ -146,6 +143,7 @@ async def cancel_one_time_payment(
146143
completion_state=PaymentTransactionState.CANCELED,
147144
state_message=payment_cancelled.message,
148145
invoice_url=None,
146+
stripe_invoice_id=None,
149147
)
150148

151149

@@ -164,6 +162,7 @@ async def acknowledge_one_time_payment(
164162
),
165163
state_message=ack.message,
166164
invoice_url=ack.invoice_url,
165+
stripe_invoice_id=ack.stripe_invoice_id,
167166
)
168167

169168

@@ -217,7 +216,6 @@ async def pay_with_payment_method( # noqa: PLR0913
217216
repo_transactions: PaymentsTransactionsRepo,
218217
repo_methods: PaymentsMethodsRepo,
219218
notifier: NotifierService,
220-
settings: ApplicationSettings,
221219
*,
222220
payment_method_id: PaymentMethodID,
223221
amount_dollars: Decimal,
@@ -256,7 +254,6 @@ async def pay_with_payment_method( # noqa: PLR0913
256254
else StripeTaxExempt.reverse
257255
),
258256
),
259-
payment_gateway_tax_feature_enabled=settings.PAYMENTS_GATEWAY_TAX_FEATURE_ENABLED,
260257
)
261258

262259
payment_id = ack.payment_id
@@ -290,6 +287,7 @@ async def pay_with_payment_method( # noqa: PLR0913
290287
),
291288
state_message=ack.message,
292289
invoice_url=ack.invoice_url,
290+
stripe_invoice_id=ack.stripe_invoice_id,
293291
)
294292

295293
# NOTE: notifications here are done as background-task after responding `POST /wallets/{wallet_id}/payments-methods/{payment_method_id}:pay`

services/payments/src/simcore_service_payments/services/payments_gateway.py

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,10 @@ class PaymentsGatewayApi(
121121
#
122122

123123
@_handle_status_errors
124-
async def init_payment(
125-
self, payment: InitPayment, payment_gateway_tax_feature_enabled: bool
126-
) -> PaymentInitiated:
127-
_payment_json = payment.dict(
128-
exclude_none=True,
129-
by_alias=True,
130-
exclude={
131-
"user_address",
132-
"stripe_price_id",
133-
"stripe_tax_rate_id",
134-
"stripe_tax_exempt_value",
135-
},
136-
)
137-
if payment_gateway_tax_feature_enabled:
138-
_payment_json = payment.dict(exclude_none=True, by_alias=True)
139-
124+
async def init_payment(self, payment: InitPayment) -> PaymentInitiated:
140125
response = await self.client.post(
141126
"/init",
142-
json=jsonable_encoder(_payment_json),
127+
json=jsonable_encoder(payment.dict(exclude_none=True, by_alias=True)),
143128
)
144129
response.raise_for_status()
145130
return PaymentInitiated.parse_obj(response.json())
@@ -210,24 +195,10 @@ async def pay_with_payment_method(
210195
self,
211196
id_: PaymentMethodID,
212197
payment: InitPayment,
213-
payment_gateway_tax_feature_enabled: bool, # noqa: FBT001
214198
) -> AckPaymentWithPaymentMethod:
215-
_payment_json = payment.dict(
216-
exclude_none=True,
217-
by_alias=True,
218-
exclude={
219-
"user_address",
220-
"stripe_price_id",
221-
"stripe_tax_rate_id",
222-
"stripe_tax_exempt_value",
223-
},
224-
)
225-
if payment_gateway_tax_feature_enabled:
226-
_payment_json = payment.dict(exclude_none=True, by_alias=True)
227-
228199
response = await self.client.post(
229200
f"/payment-methods/{id_}:pay",
230-
json=jsonable_encoder(_payment_json),
201+
json=jsonable_encoder(payment.dict(exclude_none=True, by_alias=True)),
231202
)
232203
response.raise_for_status()
233204
return AckPaymentWithPaymentMethod.parse_obj(response.json())

0 commit comments

Comments
 (0)