@@ -62,7 +62,7 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate {
62
62
63
63
func checkoutDidComplete( event: CheckoutCompletedEvent ) {
64
64
if hasListeners {
65
- sendEvent ( withName: " completed " , body: encodeToJSON ( from : event) )
65
+ sendEvent ( withName: " completed " , body: ShopifyEventSerialization . serialize ( checkoutCompletedEvent : event) )
66
66
}
67
67
}
68
68
@@ -73,66 +73,12 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate {
73
73
func checkoutDidFail( error: ShopifyCheckoutSheetKit . CheckoutError ) {
74
74
guard hasListeners else { return }
75
75
76
- if case let . checkoutExpired( message, code, recoverable) = error {
77
- sendEvent ( withName: " error " , body: [
78
- " __typename " : " CheckoutExpiredError " ,
79
- " message " : message,
80
- " code " : code. rawValue,
81
- " recoverable " : recoverable
82
- ] )
83
- } else if case let . checkoutUnavailable( message, code, recoverable) = error {
84
- switch code {
85
- case let . clientError( clientErrorCode) :
86
- sendEvent ( withName: " error " , body: [
87
- " __typename " : " CheckoutClientError " ,
88
- " message " : message,
89
- " code " : clientErrorCode. rawValue,
90
- " recoverable " : recoverable
91
- ] )
92
- case let . httpError( statusCode) :
93
- sendEvent ( withName: " error " , body: [
94
- " __typename " : " CheckoutHTTPError " ,
95
- " message " : message,
96
- " code " : " http_error " ,
97
- " statusCode " : statusCode,
98
- " recoverable " : recoverable
99
- ] )
100
- }
101
- } else if case let . configurationError( message, code, recoverable) = error {
102
- sendEvent ( withName: " error " , body: [
103
- " __typename " : " ConfigurationError " ,
104
- " message " : message,
105
- " code " : code. rawValue,
106
- " recoverable " : recoverable
107
- ] )
108
- } else if case let . sdkError( underlying, recoverable) = error {
109
- var errorMessage = " \( underlying. localizedDescription) "
110
- sendEvent ( withName: " error " , body: [
111
- " __typename " : " InternalError " ,
112
- " code " : " unknown " ,
113
- " message " : errorMessage,
114
- " recoverable " : recoverable
115
- ] )
116
- } else {
117
- sendEvent ( withName: " error " , body: [
118
- " __typename " : " UnknownError " ,
119
- " code " : " unknown " ,
120
- " message " : error. localizedDescription,
121
- " recoverable " : error. isRecoverable
122
- ] )
123
- }
76
+ sendEvent ( withName: " error " , body: ShopifyEventSerialization . serialize ( checkoutError: error) )
124
77
}
125
78
126
79
func checkoutDidEmitWebPixelEvent( event: ShopifyCheckoutSheetKit . PixelEvent ) {
127
80
if hasListeners {
128
- var genericEvent : [ String : Any ]
129
- switch event {
130
- case let . standardEvent( standardEvent) :
131
- genericEvent = mapToGenericEvent ( standardEvent: standardEvent)
132
- case let . customEvent( customEvent) :
133
- genericEvent = mapToGenericEvent ( customEvent: customEvent)
134
- }
135
- sendEvent ( withName: " pixel " , body: genericEvent)
81
+ sendEvent ( withName: " pixel " , body: ShopifyEventSerialization . serialize ( pixelEvent: event) )
136
82
}
137
83
}
138
84
@@ -255,94 +201,9 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate {
255
201
" colorScheme " : ShopifyCheckoutSheetKit . configuration. colorScheme. rawValue,
256
202
" tintColor " : ShopifyCheckoutSheetKit . configuration. tintColor,
257
203
" backgroundColor " : ShopifyCheckoutSheetKit . configuration. backgroundColor,
258
- " closeButtonColor " : ShopifyCheckoutSheetKit . configuration. closeButtonTintColor
204
+ " closeButtonColor " : ShopifyCheckoutSheetKit . configuration. closeButtonTintColor
259
205
]
260
206
261
207
resolve ( config)
262
208
}
263
-
264
- // MARK: - Private
265
-
266
- private func stringToJSON( from value: String ? ) -> [ String : Any ] ? {
267
- guard let data = value? . data ( using: . utf8, allowLossyConversion: false ) else { return [ : ] }
268
- do {
269
- return try ? JSONSerialization . jsonObject ( with: data, options: . mutableContainers) as? [ String : Any ]
270
- } catch {
271
- print ( " Failed to convert string to JSON: \( error) " , value)
272
- return [ : ]
273
- }
274
- }
275
-
276
- private func encodeToJSON( from value: Codable ) -> [ String : Any ] {
277
- let encoder = JSONEncoder ( )
278
-
279
- do {
280
- let jsonData = try encoder. encode ( value)
281
- if let jsonObject = try JSONSerialization . jsonObject ( with: jsonData, options: [ ] ) as? [ String : Any ] {
282
- return jsonObject
283
- }
284
- } catch {
285
- print ( " Error encoding to JSON object: \( error) " )
286
- }
287
- return [ : ]
288
- }
289
-
290
- private func mapToGenericEvent( standardEvent: StandardEvent ) -> [ String : Any ] {
291
- let encoded = encodeToJSON ( from: standardEvent)
292
- return [
293
- " context " : encoded [ " context " ] ,
294
- " data " : encoded [ " data " ] ,
295
- " id " : encoded [ " id " ] ,
296
- " name " : encoded [ " name " ] ,
297
- " timestamp " : encoded [ " timestamp " ] ,
298
- " type " : " STANDARD "
299
- ] as [ String : Any ]
300
- }
301
-
302
- private func mapToGenericEvent( customEvent: CustomEvent ) -> [ String : Any ] {
303
- do {
304
- return try decodeAndMap ( event: customEvent)
305
- } catch {
306
- print ( " [debug] Failed to map custom event: \( error) " )
307
- }
308
-
309
- return [ : ]
310
- }
311
-
312
- private func decodeAndMap( event: CustomEvent , decoder _: JSONDecoder = JSONDecoder ( ) ) throws -> [ String : Any ] {
313
- return [
314
- " context " : encodeToJSON ( from: event. context) ,
315
- " customData " : stringToJSON ( from: event. customData) ,
316
- " id " : event. id,
317
- " name " : event. name,
318
- " timestamp " : event. timestamp,
319
- " type " : " CUSTOM "
320
- ] as [ String : Any ]
321
- }
322
- }
323
-
324
- extension UIColor {
325
- convenience init ( hex: String ) {
326
- let hexString : String = hex. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines)
327
- let start = hexString. index ( hexString. startIndex, offsetBy: hexString. hasPrefix ( " # " ) ? 1 : 0 )
328
- let hexColor = String ( hexString [ start... ] )
329
-
330
- let scanner = Scanner ( string: hexColor)
331
- var hexNumber : UInt64 = 0
332
-
333
- if scanner. scanHexInt64 ( & hexNumber) {
334
- let red = ( hexNumber & 0xFF0000 ) >> 16
335
- let green = ( hexNumber & 0x00FF00 ) >> 8
336
- let blue = hexNumber & 0x0000FF
337
-
338
- self . init (
339
- red: CGFloat ( red) / 0xFF ,
340
- green: CGFloat ( green) / 0xFF ,
341
- blue: CGFloat ( blue) / 0xFF ,
342
- alpha: 1
343
- )
344
- } else {
345
- self . init ( red: 0 , green: 0 , blue: 0 , alpha: 1 )
346
- }
347
- }
348
209
}
0 commit comments