Skip to main content
ThunderLang
← All examples

SubscriptionUpgrade

examples/SubscriptionUpgrade.thunder

SubscriptionUpgrade.thunder
1mission SubscriptionUpgrade
2use product
3use delivery
4
5title "Upgrade a customer to a higher subscription tier"
6for BillingCustomer
7
8goal
9 Let a customer move to a higher tier, charged correctly and reversibly.
10
11guarantee the customer is charged at most once per upgrade
12 because a double charge on an upgrade is a trust-breaking billing error
13 verify idempotent charge test
14
15never
16 charge a customer whose payment method is invalid
17 downgrade entitlements before the new charge settles
18
19never charge a customer whose payment method is invalid
20 because a charge that cannot settle becomes a failed payment and a refund cycle
21 verify test CanUpgrade
22
23outcome FasterUpgrades
24 "customers complete an upgrade in one step, without contacting support"
25
26metric upgrade_completion_rate
27 baseline 71%
28 target 85%
29 window 30 days after release
30
31# An executable decision: run it with `thunder run`, assert it with `thunder test`.
32decision CanUpgrade
33 inputs
34 currentTier
35 targetTier
36 paymentValid
37 outstandingBalance
38 rule noPayment
39 when paymentValid == false
40 return Blocked
41 rule owesMoney
42 when outstandingBalance > 0
43 return Blocked
44 rule higherTier
45 when targetTier > currentTier
46 return Allowed
47 default
48 return NoChange
49
50# A lifecycle: walk it with `thunder simulate`.
51lifecycle UpgradeFlow
52 state Requested
53 state Charged
54 state Active
55 state Failed
56 transition charge
57 from Requested
58 to Charged
59 transition activate
60 from Charged
61 to Active
62 transition chargeFailed
63 from Requested
64 to Failed
65 terminal Active, Failed
66
67command ChargeUpgrade
68 idempotency_key upgradeId
69 timeout 20 seconds
70on ChargeUpgradeFailed
71 compensate release entitlement hold
72
73# An outcome contract + the delivery result that measures it: `thunder outcomes`.
74outcome_contract UpgradeConversion
75 outcome FasterUpgrades
76 metric upgrade_completion_rate
77 baseline 71%
78 target 85%
79 window 30 days after release
80 owner GrowthPM
81
82result Q3Upgrades
83 measures FasterUpgrades
84 metric upgrade_completion_rate
85 value 88%
86 baseline 71%
87
88# Tests live in the file , `thunder test` runs them through the deterministic runtime.
89test CanUpgrade
90 case valid upgrade
91 given currentTier 1, targetTier 2, paymentValid true, outstandingBalance 0
92 expect Allowed
93 case invalid payment
94 given currentTier 1, targetTier 2, paymentValid false, outstandingBalance 0
95 expect Blocked
96 case owes money
97 given currentTier 1, targetTier 2, paymentValid true, outstandingBalance 25
98 expect Blocked
99 case same tier
100 given currentTier 2, targetTier 2, paymentValid true, outstandingBalance 0
101 expect NoChange
102
103test UpgradeFlow
104 scenario happy path
105 events charge, activate
106 expect Active
107 valid
108 scenario cannot activate first
109 events activate
110 invalid
Draft syntax. This file is illustrative and does not run yet.