@@ -57,7 +57,7 @@ func (s *service) Create(ctx context.Context, namespace string, spec subscriptio
57
57
return def , fmt .Errorf ("spec is invalid: %w" , err )
58
58
}
59
59
60
- // Fetch the customer
60
+ // Fetch the customer & validate the customer
61
61
cust , err := s .CustomerService .GetCustomer (ctx , customer.GetCustomerInput {
62
62
Namespace : namespace ,
63
63
ID : spec .CustomerId ,
@@ -70,9 +70,21 @@ func (s *service) Create(ctx context.Context, namespace string, spec subscriptio
70
70
return def , fmt .Errorf ("customer is nil" )
71
71
}
72
72
73
- if cust .Currency != nil {
74
- if string (* cust .Currency ) != string (spec .Currency ) {
75
- return def , models .NewGenericValidationError (fmt .Errorf ("currency mismatch: customer currency is %s, but subscription currency is %s" , * cust .Currency , spec .Currency ))
73
+ if _ , err := cust .UsageAttribution .GetSubjectKey (); err != nil {
74
+ if spec .HasEntitlements () {
75
+ return def , models .NewGenericValidationError (errors .New ("customer has no subject but subscription has entitlements" ))
76
+ }
77
+
78
+ if spec .HasMeteredBillables () {
79
+ return def , models .NewGenericValidationError (errors .New ("customer has no subject but subscription has metered billables" ))
80
+ }
81
+ }
82
+
83
+ if spec .HasBillables () {
84
+ if cust .Currency != nil {
85
+ if string (* cust .Currency ) != string (spec .Currency ) {
86
+ return def , models .NewGenericValidationError (fmt .Errorf ("currency mismatch: customer currency is %s, but subscription currency is %s" , * cust .Currency , spec .Currency ))
87
+ }
76
88
}
77
89
}
78
90
@@ -136,8 +148,8 @@ func (s *service) Create(ctx context.Context, namespace string, spec subscriptio
136
148
return def , err
137
149
}
138
150
139
- // Let's set the customer's currency to the subscription currency (if not already set)
140
- if cust .Currency == nil {
151
+ // Let's set the customer's currency to the subscription currency for paid subscriptions (if not already set)
152
+ if cust .Currency == nil && spec . HasBillables () {
141
153
if _ , err := s .CustomerService .UpdateCustomer (ctx , customer.UpdateCustomerInput {
142
154
CustomerID : cust .GetID (),
143
155
CustomerMutate : customer.CustomerMutate {
@@ -194,6 +206,37 @@ func (s *service) Update(ctx context.Context, subscriptionID models.NamespacedID
194
206
return def , err
195
207
}
196
208
209
+ // Fetch the customer & validate the customer
210
+ cust , err := s .CustomerService .GetCustomer (ctx , customer.GetCustomerInput {
211
+ Namespace : view .Subscription .Namespace ,
212
+ ID : view .Subscription .CustomerId ,
213
+ })
214
+ if err != nil {
215
+ return def , err
216
+ }
217
+
218
+ if cust == nil {
219
+ return def , fmt .Errorf ("customer is nil" )
220
+ }
221
+
222
+ if _ , err := cust .UsageAttribution .GetSubjectKey (); err != nil {
223
+ if newSpec .HasEntitlements () {
224
+ return def , models .NewGenericValidationError (errors .New ("customer has no subject but subscription has entitlements" ))
225
+ }
226
+
227
+ if newSpec .HasMeteredBillables () {
228
+ return def , models .NewGenericValidationError (errors .New ("customer has no subject but subscription has metered billables" ))
229
+ }
230
+ }
231
+
232
+ if newSpec .HasBillables () {
233
+ if cust .Currency != nil {
234
+ if string (* cust .Currency ) != string (newSpec .Currency ) {
235
+ return def , models .NewGenericValidationError (fmt .Errorf ("currency mismatch: customer currency is %s, but subscription currency is %s" , * cust .Currency , newSpec .Currency ))
236
+ }
237
+ }
238
+ }
239
+
197
240
return transaction .Run (ctx , s .TransactionManager , func (ctx context.Context ) (subscription.Subscription , error ) {
198
241
subs , err := s .sync (ctx , view , newSpec )
199
242
if err != nil {
0 commit comments