-
Notifications
You must be signed in to change notification settings - Fork 4
/
subscription.i_subscription.go
379 lines (305 loc) · 10.7 KB
/
subscription.i_subscription.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package subscription
import (
"time"
"github.com/ottemo/commerce/app/models/cart"
"github.com/ottemo/commerce/app/models/checkout"
"github.com/ottemo/commerce/app/models/subscription"
"github.com/ottemo/commerce/app/models/visitor"
"github.com/ottemo/commerce/env"
"github.com/ottemo/commerce/utils"
)
// GetCustomerEmail returns subscriber e-mail
func (it *DefaultSubscription) GetCustomerEmail() string {
return it.CustomerEmail
}
// GetCustomerName returns name of subscriber
func (it *DefaultSubscription) GetCustomerName() string {
return it.CustomerName
}
// GetVisitorID returns the Subscription's Visitor ID
func (it *DefaultSubscription) GetVisitorID() string {
return it.VisitorID
}
// GetOrderID returns the Subscription's Order ID
func (it *DefaultSubscription) GetOrderID() string {
return it.OrderID
}
// GetStatus returns the Subscription status
func (it *DefaultSubscription) GetStatus() string {
return it.Status
}
// GetActionDate returns the date of next action
func (it *DefaultSubscription) GetActionDate() time.Time {
return it.ActionDate
}
// GetPeriod returns the Subscription action
func (it *DefaultSubscription) GetPeriod() int {
return it.Period
}
// SetStatus set Subscription status
func (it *DefaultSubscription) SetStatus(status string) error {
if status != subscription.ConstSubscriptionStatusSuspended && status != subscription.ConstSubscriptionStatusConfirmed && status != subscription.ConstSubscriptionStatusCanceled {
return env.ErrorNew(ConstErrorModule, env.ConstErrorLevelAPI, "3b7d17c3-c5fa-4369-a039-49bafec2fb9d", "new subscription status should be one of allowed")
}
if it.Status == status {
return nil
}
it.Status = status
return nil
}
// SetActionDate set Subscription action date
func (it *DefaultSubscription) SetActionDate(actionDate time.Time) error {
if err := validateSubscriptionDate(actionDate); err != nil {
return env.ErrorDispatch(err)
}
it.ActionDate = actionDate
return nil
}
// UpdateActionDate set Subscription action date
func (it *DefaultSubscription) UpdateActionDate() error {
actionDate := it.GetActionDate()
periodValue := it.GetPeriod()
if periodValue > 0 {
actionDate = actionDate.Add(ConstTimeDay * time.Duration(periodValue))
} else {
actionDate = actionDate.Add(time.Hour * time.Duration(periodValue*-1))
}
return it.SetActionDate(actionDate)
}
// SetPeriod set Subscription period
func (it *DefaultSubscription) SetPeriod(days int) error {
if err := validateSubscriptionPeriod(days); err != nil {
return env.ErrorDispatch(err)
}
it.Period = days
return nil
}
// GetItems return items of subscription
func (it *DefaultSubscription) GetItems() []subscription.StructSubscriptionItem {
return it.items
}
// SetShippingAddress sets shipping address for subscription
func (it *DefaultSubscription) SetShippingAddress(address visitor.InterfaceVisitorAddress) error {
if address == nil {
it.ShippingAddress = nil
return nil
}
it.ShippingAddress = address.ToHashMap()
return nil
}
// GetShippingAddress returns subscription shipping address
func (it *DefaultSubscription) GetShippingAddress() visitor.InterfaceVisitorAddress {
if it.ShippingAddress == nil {
return nil
}
shippingAddress, err := visitor.GetVisitorAddressModel()
if err != nil {
_ = env.ErrorDispatch(err)
return nil
}
err = shippingAddress.FromHashMap(it.ShippingAddress)
if err != nil {
_ = env.ErrorDispatch(err)
return nil
}
return shippingAddress
}
// SetBillingAddress sets billing address for subscription
func (it *DefaultSubscription) SetBillingAddress(address visitor.InterfaceVisitorAddress) error {
if address == nil {
it.BillingAddress = nil
return nil
}
it.BillingAddress = address.ToHashMap()
return nil
}
// GetBillingAddress returns subscription billing address
func (it *DefaultSubscription) GetBillingAddress() visitor.InterfaceVisitorAddress {
if it.BillingAddress == nil {
return nil
}
billingAddress, err := visitor.GetVisitorAddressModel()
if err != nil {
_ = env.ErrorDispatch(err)
return nil
}
err = billingAddress.FromHashMap(it.BillingAddress)
if err != nil {
_ = env.ErrorDispatch(err)
return nil
}
return billingAddress
}
// SetCreditCard sets credit card for subscription
func (it *DefaultSubscription) SetCreditCard(creditCard visitor.InterfaceVisitorCard) error {
if creditCard == nil {
it.PaymentInstrument = nil
return nil
}
it.PaymentInstrument = creditCard.ToHashMap()
return nil
}
// GetCreditCard sets payment method for subscription
func (it *DefaultSubscription) GetCreditCard() visitor.InterfaceVisitorCard {
visitorCardModel, err := visitor.GetVisitorCardModel()
if err != nil {
_ = env.ErrorDispatch(err)
return nil
}
if it.PaymentInstrument == nil {
return visitorCardModel
}
err = visitorCardModel.FromHashMap(it.PaymentInstrument)
if err != nil {
_ = env.ErrorDispatch(err)
return visitorCardModel
}
return visitorCardModel
}
// GetPaymentMethod returns subscription payment method
func (it *DefaultSubscription) GetPaymentMethod() checkout.InterfacePaymentMethod {
creditCard := it.GetCreditCard()
if creditCard == nil {
return nil
}
return checkout.GetPaymentMethodByCode(creditCard.GetPaymentMethodCode())
}
// SetShippingMethod sets payment method for subscription
func (it *DefaultSubscription) SetShippingMethod(shippingMethod checkout.InterfaceShippingMethod) error {
it.ShippingMethodCode = shippingMethod.GetCode()
return nil
}
// GetShippingMethod returns a subscription shipping method
func (it *DefaultSubscription) GetShippingMethod() checkout.InterfaceShippingMethod {
return checkout.GetShippingMethodByCode(it.ShippingMethodCode)
}
// SetShippingRate sets shipping rate for subscription
func (it *DefaultSubscription) SetShippingRate(shippingRate checkout.StructShippingRate) error {
it.ShippingRate = shippingRate
return nil
}
// GetShippingRate returns a subscription shipping rate
func (it *DefaultSubscription) GetShippingRate() checkout.StructShippingRate {
return it.ShippingRate
}
// GetCheckout return checkout object created from subscription
func (it *DefaultSubscription) GetCheckout() (checkout.InterfaceCheckout, error) {
checkoutInstance, err := checkout.GetCheckoutModel()
if err != nil {
return nil, env.ErrorDispatch(err)
}
// set visitor basic info
visitorID := it.GetVisitorID()
if visitorID != "" {
if err := checkoutInstance.Set("VisitorID", visitorID); err != nil {
return checkoutInstance, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelActor, "205c5663-e5d7-404e-b731-7de78c055fac", err.Error())
}
}
if err := checkoutInstance.SetInfo("customer_email", it.GetCustomerEmail()); err != nil {
return checkoutInstance, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelActor, "12ef8ee8-0f77-4f1f-9a65-cc94648cf981", err.Error())
}
if err := checkoutInstance.SetInfo("customer_name", it.GetCustomerName()); err != nil {
return checkoutInstance, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelActor, "7054a0f4-294c-47a3-8921-96a4406b7ed4", err.Error())
}
// set billing and shipping address
shippingAddress := it.GetShippingAddress()
if shippingAddress != nil {
if err := checkoutInstance.Set("ShippingAddress", shippingAddress.ToHashMap()); err != nil {
return checkoutInstance, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelActor, "37424b34-0406-4361-a882-43514292c5bd", err.Error())
}
}
billingAddress := it.GetBillingAddress()
if billingAddress != nil {
if err := checkoutInstance.Set("BillingAddress", billingAddress.ToHashMap()); err != nil {
return checkoutInstance, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelActor, "f07174df-4eb7-4447-9b3b-338076516369", err.Error())
}
}
// check payment and shipping methods for availability
shippingMethod := it.GetShippingMethod()
if shippingMethod != nil {
if !shippingMethod.IsAllowed(checkoutInstance) {
return checkoutInstance, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelActor, "db2e8933-d0eb-4a16-a28b-78c169fe20c0", "shipping method not allowed")
}
err = checkoutInstance.SetShippingMethod(shippingMethod)
if err != nil {
return checkoutInstance, env.ErrorDispatch(err)
}
err = checkoutInstance.SetShippingRate(it.GetShippingRate())
if err != nil {
return checkoutInstance, env.ErrorDispatch(err)
}
}
paymentMethod := it.GetPaymentMethod()
if paymentMethod != nil {
if !paymentMethod.IsAllowed(checkoutInstance) {
return checkoutInstance, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelActor, "e7cfc56b-97d9-43f5-862e-fb370004c8cf", "payment method not allowed")
}
err = checkoutInstance.SetPaymentMethod(paymentMethod)
if err != nil {
return checkoutInstance, env.ErrorDispatch(err)
}
}
if err := checkoutInstance.SetInfo("cc", it.GetCreditCard()); err != nil {
return checkoutInstance, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelActor, "4b47d1a1-577d-4ef0-a57a-8e7f00679b72", err.Error())
}
// handle cart
currentCart, err := cart.GetCartModel()
if err != nil {
return checkoutInstance, env.ErrorDispatch(err)
}
for _, item := range it.GetItems() {
if _, err = currentCart.AddItem(item.ProductID, item.Qty, item.Options); err != nil {
return checkoutInstance, env.ErrorDispatch(err)
}
}
if err = currentCart.ValidateCart(); err != nil {
return checkoutInstance, env.ErrorDispatch(err)
}
if err = currentCart.Save(); err != nil {
return checkoutInstance, env.ErrorDispatch(err)
}
if err = checkoutInstance.SetCart(currentCart); err != nil {
return checkoutInstance, env.ErrorDispatch(err)
}
checkoutInstance.GetGrandTotal()
return checkoutInstance, nil
}
// Validate allows to validate subscription object for data presence
// TODO: validate ALL values and their existence
func (it *DefaultSubscription) Validate() error {
if err := validateSubscriptionPeriod(it.Period); err != nil {
return env.ErrorDispatch(err)
}
if err := validateSubscriptionDate(it.ActionDate); err != nil {
return env.ErrorDispatch(err)
}
if !utils.ValidEmailAddress(it.CustomerEmail) {
return env.ErrorNew(ConstErrorModule, env.ConstErrorLevelActor, "1c033c36-d63b-4659-95e8-9f348f5e2880", "Subscription invalid: email")
}
if len(it.items) == 0 {
return env.ErrorNew(ConstErrorModule, env.ConstErrorLevelActor, "c9110694-12af-4dd3-9730-986ec03539e5", "no items in subscription")
}
return nil
}
// SetInfo sets additional info for subscription
func (it *DefaultSubscription) SetInfo(key string, value interface{}) {
if value == nil {
if _, present := it.Info[key]; present {
delete(it.Info, key)
}
} else {
it.Info[key] = value
}
return
}
// GetInfo returns additional subscription info value or nil,
// - use "*" as a key to get all keys and values currently set
func (it *DefaultSubscription) GetInfo(key string) interface{} {
if key == "*" {
return it.Info
}
if value, present := it.Info[key]; present {
return value
}
return nil
}