Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Use transactions in example orders (#18354)

Anna Szczęchc62941a63c3e
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 2a3dec1f22..2ed8dc79a6 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -29,6 +29,7 @@ from ...account.search import (
generate_user_fields_search_document_value,
)
from ...account.utils import store_user_address
+from ...app.models import App
from ...attribute.models import (
AssignedProductAttributeValue,
AssignedVariantAttribute,
@@ -49,21 +50,26 @@ from ...discount import DiscountValueType, RewardValueType, VoucherType
from ...discount.models import (
Promotion,
PromotionRule,
+ PromotionType,
+ RewardType,
Voucher,
VoucherChannelListing,
VoucherCode,
)
from ...giftcard import events as gift_card_events
from ...giftcard.models import GiftCard, GiftCardTag
-from ...graphql.discount.enums import RewardTypeEnum
from ...menu.models import Menu, MenuItem
from ...order import OrderStatus
from ...order.models import Fulfillment, Order, OrderLine
from ...order.search import prepare_order_search_vector_value
from ...order.utils import update_order_status
from ...page.models import Page, PageType
-from ...payment import gateway
-from ...payment.utils import create_payment
+from ...payment import TransactionAction, TransactionEventType
+from ...payment.models import TransactionEvent, TransactionItem
+from ...payment.utils import (
+ recalculate_transaction_amounts,
+ update_order_with_transaction_details,
+)
from ...permission.enums import (
AccountPermissions,
CheckoutPermissions,
@@ -562,32 +568,190 @@ def create_fake_user(user_password, save=True, generate_id=False):
# fake customers.
@patch("saleor.plugins.manager.PluginsManager.notify")
def create_fake_payment(mock_notify, order):
- payment = create_payment(
- gateway="mirumee.payments.dummy",
- customer_ip_address=fake.ipv4(),
- email=order.user_email,
- order=order,
- payment_token=str(uuid.uuid4()),
- total=order.total.gross.amount,
- currency=order.total.gross.currency,
+ amount = order.total.gross.amount
+ dummy_app = App.objects.filter(identifier="saleor.io.dummy-payment-app").first()
+ dummy_app_identifier = (
+ dummy_app.identifier if dummy_app else "saleor.io.dummy-payment-app"
)
- manager = get_plugins_manager(allow_replica=False)
+ flow = order.channel.default_transaction_flow_strategy.lower()
+
+ auth_psp = uuid.uuid4()
+ charge_psp = uuid.uuid4()
+ refund_psp = uuid.uuid4()
+ pick = random.random()
+
+ kwargs = {
+ "order": order,
+ "name": "",
+ "app": dummy_app,
+ "app_identifier": dummy_app_identifier,
+ "user": None,
+ "available_actions": [],
+ "authorized_amount": None,
+ "charge_amount": None,
+ "refund_amount": None,
+ "authorization_psp": None,
+ "charge_psp": None,
+ "refund_psp": None,
+ }
+ if flow == "authorization":
+ if pick < 0.15:
+ # Authorized only: actions -> CHARGE, CANCEL
+ kwargs["available_actions"] = [
+ TransactionAction.CHARGE,
+ TransactionAction.CANCEL,
+ ]
+ kwargs["authorized_amount"] = amount
+ kwargs["authorization_psp"] = auth_psp
+ return create_transaction_with_events(**kwargs)
+ if pick < 0.75:
+ # Authorized + Charged: actions -> REFUND
+ kwargs["available_actions"] = [TransactionAction.REFUND]
+ kwargs["authorized_amount"] = amount
+ kwargs["authorization_psp"] = auth_psp
+ kwargs["charge_amount"] = amount
+ kwargs["charge_psp"] = charge_psp
+ return create_transaction_with_events(**kwargs)
+ # Authorized + Charged + Refunded
+ kwargs["authorized_amount"] = amount
+ kwargs["authorization_psp"] = auth_psp
+ kwargs["charge_amount"] = amount
+ kwargs["charge_psp"] = charge_psp
+ kwargs["refund_amount"] = amount
+ kwargs["refund_psp"] = refund_psp
+ return create_transaction_with_events(**kwargs)
+
+ # Charge strategy:
+ if pick < 0.8:
+ # Charged only
+ kwargs["available_actions"] = [TransactionAction.REFUND]
+ kwargs["charge_amount"] = amount
+ kwargs["charge_psp"] = charge_psp
+ return create_transaction_with_events(**kwargs)
+ # Charged + Refunded
+ kwargs["charge_amount"] = amount
+ kwargs["charge_psp"] = charge_psp
+ kwargs["refund_amount"] = amount
+ kwargs["refund_psp"] = refund_psp
+ return create_transaction_with_events(**kwargs)
+
+
+def create_transaction_with_events(
+ *,
+ order: Order,
+ name: str,
+ app: App | None,
+ app_identifier: str | None = None,
+ user: User | None,
+ available_actions: list[str] | None,
+ authorized_amount: Decimal | None,
+ charge_amount: Decimal | None,
+ refund_amount: Decimal | None,
+ authorization_psp: str | None,
+ charge_psp: str | None,
+ refund_psp: str | None,
+) -> TransactionItem:
+ item_psp = authorization_psp or charge_psp or str(uuid.uuid4())
+ transaction_item = TransactionItem.objects.create(
+ name=name,
+ order_id=order.pk,
+ currency=order.currency,
+ app=app,
+ app_identifier=(app.identifier if app else app_identifier),
+ user=user,
+ psp_reference=item_psp,
+ available_actions=available_actions or [],
+ )
+
+ events_to_create: list[TransactionEvent] = []
+
+ event_defaults = {
+ "currency": order.currency,
+ "transaction_id": transaction_item.pk,
+ "app": app,
+ "app_identifier": (app.identifier if app else app_identifier),
+ }
+ if authorized_amount is not None:
+ events_to_create.append(
+ TransactionEvent(
+ type=TransactionEventType.AUTHORIZATION_REQUEST,
+ amount_value=authorized_amount,
+ psp_reference=authorization_psp or item_psp,
+ include_in_calculations=False,
+ created_at=timezone.now() + datetime.timedelta(microseconds=1),
+ user=None,
+ **event_defaults,
+ )
+ )
+ events_to_create.append(
+ TransactionEvent(
+ type=TransactionEventType.AUTHORIZATION_SUCCESS,
+ amount_value=authorized_amount,
+ psp_reference=authorization_psp or item_psp,
+ include_in_calculations=True,
+ created_at=timezone.now() + datetime.timedelta(microseconds=1),
+ user=user,
+ message="Authorization success",
+ **event_defaults,
+ )
+ )
+
+ if charge_amount is not None:
+ events_to_create.append(
+ TransactionEvent(
+ type=TransactionEventType.CHARGE_REQUEST,
+ amount_value=charge_amount,
+ psp_reference=charge_psp or item_psp,
+ include_in_calculations=False,
+ created_at=timezone.now() + datetime.timedelta(microseconds=1),
+ user=user,
+ **event_defaults,
+ )
+ )
+ events_to_create.append(
+ TransactionEvent(
+ type=TransactionEventType.CHARGE_SUCCESS,
+ amount_value=charge_amount,
+ psp_reference=charge_psp or item_psp,
+ include_in_calculations=True,
+ created_at=timezone.now() + datetime.timedelta(microseconds=1),
+ user=user,
+ message="Charge success",
+ **event_defaults,
+ )
+ )
+
+ if refund_amount is not None and refund_amount > 0:
+ events_to_create.append(
+ TransactionEvent(
+ type=TransactionEventType.REFUND_REQUEST,
+ amount_value=refund_amount,
+ psp_reference=refund_psp or item_psp,
+ include_in_calculations=False,
+ created_at=timezone.now() + datetime.timedelta(microseconds=1),
+ user=user,
+ **event_defaults,
+ )
+ )
+ events_to_create.append(
+ TransactionEvent(
+ type=TransactionEventType.REFUND_SUCCESS,
+ amount_value=refund_amount,
+ psp_reference=refund_psp or item_psp,
+ include_in_calculations=True,
+ created_at=timezone.now() + datetime.timedelta(microseconds=1),
+ user=user,
+ message="Refund success",
+ **event_defaults,
+ )
+ )
+
+ if events_to_create:
+ TransactionEvent.objects.bulk_create(events_to_create)
+ recalculate_transaction_amounts(transaction=transaction_item)
+ update_order_with_transaction_details(transaction_item)
- # Create authorization transaction
- gateway.authorize(payment, payment.token, manager, order.channel.slug)
- # 20% chance to void the transaction at this stage
- if random.choice([0, 0, 0, 0, 1]):
- gateway.void(payment, manager, order.channel.slug)
- return payment
- # 25% to end the payment at the authorization stage
- if not random.choice([1, 1, 1, 0]):
- return payment
- # Create capture transaction
- gateway.capture(payment, manager, order.channel.slug)
- # 25% to refund the payment
- if random.choice([0, 0, 0, 1]):
- gateway.refund(payment, manager, order.channel.slug)
- return payment
+ return transaction_item
def create_order_lines(order, how_many=10):
@@ -780,10 +944,8 @@ def create_fake_order(max_order_lines=5, create_preorder_lines=False):
)
customer = random.choice([None, customers.first()])
- # 20% chance to be unconfirmed order.
- will_be_unconfirmed = (
- random.choice([0, 0, 0, 0, 1]) if not create_preorder_lines else True
- )
+ # # 10% chance to be unconfirmed order.
+ will_be_unconfirmed = (random.random() < 0.1) if not create_preorder_lines else True
if customer and customer.default_shipping_address:
address = customer.default_shipping_address
@@ -829,6 +991,10 @@ def create_fake_order(max_order_lines=5, create_preorder_lines=False):
else:
lines = create_order_lines(order, random.randrange(1, max_order_lines))
order.total = sum([line.total_price for line in lines], shipping_price)
+ order.subtotal = sum(
+ [line.total_price for line in lines],
+ TaxedMoney(net=Money(0, order.currency), gross=Money(0, order.currency)),
+ )
weight = Weight(kg=0)
for line in order.lines.all():
if line.variant:
@@ -842,7 +1008,11 @@ def create_fake_order(max_order_lines=5, create_preorder_lines=False):
create_fake_payment(order=order)
- if not will_be_unconfirmed:
+ # Ensure totals (including total_balance) reflect the latest transactions
+ order.refresh_from_db()
+
+ # Fulfill only when order is confirmed and fully balanced (total_balance == 0)
+ if not will_be_unconfirmed and order.total_balance == Money(0, order.currency):
create_fulfillments(order)
return order
@@ -896,6 +1066,8 @@ def create_fake_catalogue_promotion():
def create_fake_order_promotion():
promotion = Promotion.objects.create(
name=f"Happy {fake.word()} day!",
+ type=PromotionType.ORDER,
+ start_date=timezone.now() + datetime.timedelta(days=1),
)
rules = PromotionRule.objects.bulk_create(
[
@@ -903,7 +1075,7 @@ def create_fake_order_promotion():
promotion=promotion,
reward_value_type=RewardValueType.PERCENTAGE,
reward_value=random.choice([10, 20, 30, 40, 50]),
- reward_type=RewardTypeEnum.SUBTOTAL_DISCOUNT.name,
+ reward_type=RewardType.SUBTOTAL_DISCOUNT,
order_predicate={
"discountedObjectPredicate": {
"baseSubtotalPrice": {"range": {"gte": "200"}}
@@ -914,7 +1086,7 @@ def create_fake_order_promotion():
promotion=promotion,
reward_value_type=RewardValueType.FIXED,
reward_value=random.choice([10, 20, 30, 40, 50]),
- reward_type=RewardTypeEnum.SUBTOTAL_DISCOUNT.name,
+ reward_type=RewardType.SUBTOTAL_DISCOUNT,
order_predicate={
"discountedObjectPredicate": {
"baseSubtotalPrice": {"range": {"gte": "100"}}
@@ -1048,19 +1220,29 @@ def create_order_promotions(how_many=5):
yield f"Promotion: {promotion}"
-def create_channel(channel_name, currency_code, slug=None, country=None):
+def create_channel(
+ channel_name,
+ currency_code,
+ slug=None,
+ country=None,
+ mark_as_paid_strategy="transaction_flow",
+ default_transaction_flow_strategy="charge",
+):
if not slug:
slug = slugify(channel_name)
- channel, _ = Channel.objects.get_or_create(
+ defaults = {
+ "name": channel_name,
+ "currency_code": currency_code,
+ "is_active": True,
+ "default_country": country,
+ "order_mark_as_paid_strategy": mark_as_paid_strategy,
+ "default_transaction_flow_strategy": default_transaction_flow_strategy,
+ }
+ channel, _ = Channel.objects.update_or_create(
slug=slug,
- defaults={
- "name": channel_name,
- "currency_code": currency_code,
- "is_active": True,
- "default_country": country,
- },
+ defaults=defaults,
)
- TaxConfiguration.objects.get_or_create(channel=channel)
+ TaxConfiguration.objects.update_or_create(channel=channel)
return f"Channel: {channel}"
@@ -1070,6 +1252,7 @@ def create_channels():
currency_code="USD",
slug=settings.DEFAULT_CHANNEL_SLUG,
country=settings.DEFAULT_COUNTRY,
+ default_transaction_flow_strategy="authorization",
)
yield create_channel(
channel_name="Channel-PLN",
diff --git a/saleor/static/populatedb_data.json b/saleor/static/populatedb_data.json
index e8d9b3d85c..8f82a2e98c 100644
--- a/saleor/static/populatedb_data.json
+++ b/saleor/static/populatedb_data.json
@@ -9586,7 +9586,7 @@
"customer_set_password_url": null,
"automatically_confirm_all_new_orders": true,
"fulfillment_auto_approve": true,
- "fulfillment_allow_unpaid": true,
+ "fulfillment_allow_unpaid": false,
"reserve_stock_duration_anonymous_user": null,
"reserve_stock_duration_authenticated_user": null,
"limit_quantity_per_checkout": 50,