Skip to content

Commit

Permalink
server: Set renewal order metadata & fields from subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
birkjernstrom committed Dec 27, 2024
1 parent fa6f853 commit 98020d4
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions server/polar/order/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ async def create_order_from_stripe(
tax = invoice.tax or 0
amount = invoice.total - tax

user_metadata = {}
custom_field_data = {}

# Get subscription if applicable
subscription: Subscription | None = None
if invoice.subscription is not None:
Expand All @@ -382,12 +385,20 @@ async def create_order_from_stripe(
)
billing_reason = OrderBillingReason.subscription_cycle

# Ensure renewals inherit original metadata and custom fields
user_metadata = subscription.user_metadata
custom_field_data = subscription.custom_field_data

# Set the subscription amount to the latest order amount
# Helps to reflect discount that may have ended
if subscription.amount != amount:
subscription.amount = amount
session.add(subscription)

if checkout is not None:
user_metadata = checkout.user_metadata
custom_field_data = checkout.custom_field_data

# Create Order
order = Order(
amount=amount,
Expand All @@ -401,10 +412,8 @@ async def create_order_from_stripe(
discount=discount,
subscription=subscription,
checkout=checkout,
user_metadata=checkout.user_metadata if checkout is not None else {},
custom_field_data=checkout.custom_field_data
if checkout is not None
else {},
user_metadata=user_metadata,
custom_field_data=custom_field_data,
created_at=datetime.fromtimestamp(invoice.created, tz=UTC),
)

Expand Down

0 comments on commit 98020d4

Please sign in to comment.