forked from actions-on-google/actions-on-google-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.js
1439 lines (1357 loc) · 34.1 KB
/
transactions.js
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A collection of Transaction related constants, utility functions, and
* builders.
*/
'use strict';
const Debug = require('debug');
const error = Debug('actions-on-google:error');
const ORDER_LOCATION_LIMIT = 2;
const GENERIC_EXTENSION_TYPE = 'type.googleapis.com/google.actions.v2.orders.GenericExtension';
/**
* Price type.
* @typedef {Object} Price
* @property {string} type - One of Transaction.PriceType.
* @property {Object} amount
* @property {string} amount.currencyCode - Currency code of price.
* @property {number} amount.units - Unit count of price.
* @property {number=} amount.nanos - Partial unit count of price.
*/
/**
* Order rejection info.
* @typedef {Object} RejectionInfo
* @property {string} type - One of Transaction.RejectionType.
* @property {string} reason - Reason for the order rejection.
*/
/**
* Order receipt info.
* @typedef {Object} ReceiptInfo
* @property {string} confirmedActionOrderId - Action provided order ID. Used
* when the order has been received by the integrator.
*/
/**
* Order cancellation info.
* @typedef {Object} CancellationInfo
* @property {string} reason - Reason for the cancellation.
*/
/**
* Order transit info.
* @typedef {Object} TransitInfo
* @property {Object} updatedTime - UTC timestamp of the transit update.
* @property {number} updatedTime.seconds - Seconds since Unix epoch.
* @property {number=} updatedTime.nanos - Partial seconds since Unix epoch.
*/
/**
* Order fulfillment info.
* @typedef {Object} FulfillmentInfo
* @property {Object} deliveryTime - UTC timestamp of the fulfillment update.
* @property {number} deliveryTime.seconds - Seconds since Unix epoch.
* @property {number=} deliveryTime.nanos - Partial seconds since Unix epoch.
*/
/**
* Order return info.
* @typedef {Object} ReturnInfo
* @property {string} reason - Reason for the return.
*/
/**
* Transaction config for transactions not involving a Google provided
* payment instrument.
* @typedef {Object} ActionPaymentTransactionConfig
* @property {boolean} deliveryAddressRequired - True if delivery address is
* required for the transaction.
* @property {boolean} type - One of Transactions.PaymentType.
* @property {string} displayName - The name of the instrument displayed on
* receipt. For example, for card payment, could be "VISA-1234".
* @property {CustomerInfoOptions=} customerInfoOptions
*/
/**
* Transaction config for transactions involving a Google provided payment
* instrument.
* @typedef {Object} GooglePaymentTransactionConfig
* @property {boolean} deliveryAddressRequired - True if delivery address is
* required for the transaction.
* @property {Object} tokenizationParameters - Tokenization parameters provided
* by payment gateway.
* @property {Array<string>} cardNetworks - List of accepted card networks.
* Must be any number of Transactions.CardNetwork.
* @property {boolean} prepaidCardDisallowed - True if prepaid cards are not
* allowed for transaction.
* @property {CustomerInfoOptions=} customerInfoOptions
*/
/**
* Customer information requested as part of the transaction
* @typedef {Object} CustomerInfoOptions
* @property {Array<string>} customerInfoProperties - one of
* Transactions.CustomerInfoProperties
*/
/**
* Generic Location type.
* @typedef {Object} Location
* @property {Object} postalAddress
* @property {string} postalAddress.regionCode
* @property {string} postalAddress.languageCode
* @property {string} postalAddress.postalCode
* @property {string} postalAddress.administrativeArea
* @property {string} postalAddress.locality
* @property {Array<string>} postalAddress.addressLines
* @property {string} postalAddress.recipients
* @property {string} phoneNumber
* @property {string} notes
*/
/**
* Decision and order information returned when calling getTransactionDecision().
* @typedef {Object} TransactionDecision
* @property {string} userDecision - One of Transactions.ConfirmationDecision.
* @property {Object} checkResult
* @property {string} checkResult.resultType - One of Transactions.ResultType.
* @property {Object} order
* @property {Order} order.finalOrder - The proposed order used in the transaction
* decision.
* @property {string} order.googleOrderId - Order ID assigned by Google.
* @property {string} order.actionOrderId - User visible order ID set in proposed
* order.
* @property {Object} order.orderDate
* @property {string} order.orderDate.seconds
* @property {number} order.orderDate.nanos
* @property {Object} order.paymentInfo
* @property {Object} order.customerInfo
* @property {string} order.customerInfo.email - Customer email.
* @property {DeliveryAddress} deliveryAddress - The delivery address if user
* requested. Will appear if userDecision is
* Transactions.DELIVERY_ADDRESS_UPDATED.
*/
/**
* Values related to supporting transactions.
* @readonly
* @type {Object}
*/
const TransactionValues = {
/**
* List of transaction card networks available when paying with Google.
* @readonly
* @enum {string}
*/
CardNetwork: {
/**
* Unspecified.
*/
UNSPECIFIED: 'UNSPECIFIED',
/**
* American Express.
*/
AMEX: 'AMEX',
/**
* Discover.
*/
DISCOVER: 'DISCOVER',
/**
* Master Card.
*/
MASTERCARD: 'MASTERCARD',
/**
* Visa.
*/
VISA: 'VISA',
/**
* JCB.
*/
JCB: 'JCB'
},
/**
* List of possible item types.
* @readonly
* @enum {string}
*/
ItemType: {
/**
* Unspecified.
*/
UNSPECIFIED: 'UNSPECIFIED',
/**
* Regular.
*/
REGULAR: 'REGULAR',
/**
* Tax.
*/
TAX: 'TAX',
/**
* Discount
*/
DISCOUNT: 'DISCOUNT',
/**
* Gratuity
*/
GRATUITY: 'GRATUITY',
/**
* Delivery
*/
DELIVERY: 'DELIVERY',
/**
* Subtotal
*/
SUBTOTAL: 'SUBTOTAL',
/**
* Fee. For everything else, there's fee.
*/
FEE: 'FEE'
},
/**
* List of price types.
* @readonly
* @enum {string}
*/
PriceType: {
/**
* Unknown.
*/
UNKNOWN: 'UNKNOWN',
/**
* Estimate.
*/
ESTIMATE: 'ESTIMATE',
/**
* Actual.
*/
ACTUAL: 'ACTUAL'
},
/**
* List of possible item types.
* @readonly
* @enum {string}
*/
PaymentType: {
/**
* Unspecified.
*/
UNSPECIFIED: 'UNSPECIFIED',
/**
* Payment card.
*/
PAYMENT_CARD: 'PAYMENT_CARD',
/**
* Bank.
*/
BANK: 'BANK',
/**
* Loyalty program.
*/
LOYALTY_PROGRAM: 'LOYALTY_PROGRAM',
/**
* On order fulfillment, such as cash on delivery.
*/
ON_FULFILLMENT: 'ON_FULFILLMENT',
/**
* Gift card.
*/
GIFT_CARD: 'GIFT_CARD'
},
/**
* List of customer information properties that can be requested.
* @readonly
* @enum {string}
*/
CustomerInfoProperties: {
EMAIL: 'EMAIL'
},
/**
* List of possible order confirmation user decisions
* @readonly
* @enum {string}
*/
ConfirmationDecision: {
/**
* Order was approved by user.
*/
ACCEPTED: 'ORDER_ACCEPTED',
/**
* Order was declined by user.
*/
REJECTED: 'ORDER_REJECTED',
/**
* Order was not declined, but the delivery address was updated during
* confirmation.
*/
DELIVERY_ADDRESS_UPDATED: 'DELIVERY_ADDRESS_UPDATED',
/**
* Order was not declined, but the cart was updated during confirmation.
*/
CART_CHANGE_REQUESTED: 'CART_CHANGE_REQUESTED'
},
/**
* List of possible order states.
* @readonly
* @enum {string}
*/
OrderState: {
/**
* Order was rejected.
*/
REJECTED: 'REJECTED',
/**
* Order was confirmed by integrator and is active.
*/
CONFIRMED: 'CONFIRMED',
/**
* User cancelled the order.
*/
CANCELLED: 'CANCELLED',
/**
* Order is being delivered.
*/
IN_TRANSIT: 'IN_TRANSIT',
/**
* User performed a return.
*/
RETURNED: 'RETURNED',
/**
* User received what was ordered.
*/
FULFILLED: 'FULFILLED'
},
/**
* List of possible actions to take on the order.
* @readonly
* @enum {string}
*/
OrderAction: {
/**
* View details.
*/
VIEW_DETAILS: 'VIEW_DETAILS',
/**
* Modify order.
*/
MODIFY: 'MODIFY',
/**
* Cancel order.
*/
CANCEL: 'CANCEL',
/**
* Return order.
*/
RETURN: 'RETURN',
/**
* Exchange order.
*/
EXCHANGE: 'EXCHANGE',
/**
* Email.
*/
EMAIL: 'EMAIL',
/**
* Call.
*/
CALL: 'CALL',
/**
* Reorder.
*/
REORDER: 'REORDER',
/**
* Review.
*/
REVIEW: 'REVIEW'
},
/**
* List of possible types of order rejection.
* @readonly
* @enum {string}
*/
RejectionType: {
/**
* Unknown
*/
UNKNOWN: 'UNKNOWN',
/**
* Payment was declined.
*/
PAYMENT_DECLINED: 'PAYMENT_DECLINED'
},
/**
* List of possible order state objects.
* @readonly
* @enum {string}
*/
OrderStateInfo: {
/**
* Information about order rejection. Used with {@link RejectionInfo}.
*/
REJECTION: 'rejectionInfo',
/**
* Information about order receipt. Used with {@link ReceiptInfo}.
*/
RECEIPT: 'receipt',
/**
* Information about order cancellation. Used with {@link CancellationInfo}.
*/
CANCELLATION: 'cancellationInfo',
/**
* Information about in-transit order. Used with {@link TransitInfo}.
*/
IN_TRANSIT: 'inTransitInfo',
/**
* Information about order fulfillment. Used with {@link FulfillmentInfo}.
*/
FULFILLMENT: 'fulfillmentInfo',
/**
* Information about order return. Used with {@link ReturnInfo}.
*/
RETURN: 'returnInfo'
},
/**
* List of possible order transaction requirements check result types.
* @readonly
* @enum {string}
*/
ResultType: {
/**
* Unspecified.
*/
UNSPECIFIED: 'RESULT_TYPE_UNSPECIFIED',
/**
* OK to continue transaction.
*/
OK: 'OK',
/**
* User is expected to take action, e.g. enable payments, to continue
* transaction.
*/
USER_ACTION_REQUIRED: 'USER_ACTION_REQUIRED',
/**
* Transactions are not supported on current device/surface.
*/
ASSISTANT_SURFACE_NOT_SUPPORTED: 'ASSISTANT_SURFACE_NOT_SUPPORTED',
/**
* Transactions are not supported for current region/country.
*/
REGION_NOT_SUPPORTED: 'REGION_NOT_SUPPORTED'
},
/**
* List of possible user decisions to give delivery address.
* @readonly
* @enum {string}
*/
DeliveryAddressDecision: {
/**
* Unknown.
*/
UNKNOWN: 'UNKNOWN_USER_DECISION',
/**
* User granted delivery address.
*/
ACCEPTED: 'ACCEPTED',
/**
* User denied to give delivery address.
*/
REJECTED: 'REJECTED'
},
/**
* List of possible order location types.
* @readonly
* @enum {string}
*/
LocationType: {
/**
* Unknown.
*/
UNKNOWN: 'UNKNOWN',
/**
* Delivery location for an order.
*/
DELIVERY: 'DELIVERY',
/**
* Business location of order provider.
*/
BUSINESS: 'BUSINESS',
/**
* Origin of the order.
*/
ORIGIN: 'ORIGIN',
/**
* Destination of the order.
*/
DESTINATION: 'DESTINATION'
},
/**
* List of possible order time types.
* @readonly
* @enum {string}
*/
TimeType: {
/**
* Unknown.
*/
UNKNOWN: 'UNKNOWN',
/**
* Date of delivery for the order.
*/
DELIVERY_DATE: 'DELIVERY_DATE',
/**
* Estimated Time of Arrival for order.
*/
ETA: 'ETA',
/**
* Reservation time.
*/
RESERVATION_SLOT: 'RESERVATION_SLOT'
}
};
/**
* Valid keys for the TransactionValues.OrderStateInfo enum.
* @readonly
* @enum {string}
*/
const reverseOrderStateInfo = Object.keys(TransactionValues.OrderStateInfo)
.reduce((reverseValues, infoType) => {
reverseValues[TransactionValues.OrderStateInfo[infoType]] = infoType;
return reverseValues;
}, {});
/**
* Class for initializing and constructing Order with chainable interface.
*/
const Order = class {
/**
* Constructor for Order.
*
* @param {string} orderId Unique identifier for the order.
*/
constructor (orderId) {
/**
* ID for the order. Required.
* @type {string}
*/
this.id = orderId;
/**
* Cart for the order.
* @type {Cart}
*/
this.cart = undefined;
/**
* Items not held in the order cart.
* @type {Array<LineItem>}
*/
this.otherItems = [];
/**
* Image for the order.
* @type {Image}
*/
this.image = undefined;
/**
* TOS for the order.
* @type {String}
*/
this.termsOfServiceUrl = undefined;
/**
* Total price for the order.
* @type {Price}
*/
this.totalPrice = undefined;
/**
* Extensions for this order. Used for vertical-specific order attributes,
* like times and locations.
* @type {Object}
*/
this.extension = undefined;
}
/**
* Set the cart for this order.
*
* @param {Cart} cart Cart for this order.
* @return {Order} Returns current constructed Order.
*/
setCart (cart) {
if (!cart) {
error('Invalid cart');
return this;
}
this.cart = cart;
return this;
}
/**
* Adds a single item or list of items to the non-cart items list.
*
* @param {LineItem|Array<LineItem>} items Line Items to add.
* @return {Order} Returns current constructed Order.
*/
addOtherItems (items) {
if (!items) {
error('items cannot be null');
return this;
}
if (Array.isArray(items)) {
for (let item of items) {
this.otherItems.push(item);
}
} else {
this.otherItems.push(items);
}
return this;
}
/**
* Sets the image for this order.
*
* @param {string} url Image source URL.
* @param {string} accessibilityText Text to replace for image for
* accessibility.
* @param {number=} width Width of the image.
* @param {number=} height Height of the image.
* @return {Order} Returns current constructed Order.
*/
setImage (url, accessibilityText, width, height) {
if (!url) {
error('url cannot be empty');
return this;
}
if (!accessibilityText) {
error('accessibilityText cannot be empty');
return this;
}
this.image = { url, accessibilityText };
if (width) {
this.image.width = width;
}
if (height) {
this.image.height = height;
}
return this;
}
/**
* Set the TOS for this order.
*
* @param {string} url String URL of the TOS.
* @return {Order} Returns current constructed Order.
*/
setTermsOfService (url) {
if (!url) {
error('Invalid TOS url');
return this;
}
this.termsOfServiceUrl = url;
return this;
}
/**
* Sets the total price for this order.
*
* @param {string} priceType One of TransactionValues.PriceType.
* @param {string} currencyCode Currency code of price.
* @param {number} units Unit count of price.
* @param {number=} nanos Partial unit count of price.
* @return {Order} Returns current constructed Order.
*/
setTotalPrice (priceType, currencyCode, units, nanos) {
if (!priceType) {
error('priceType cannot be empty');
return this;
}
if (!currencyCode) {
error('currencyCode cannot be empty');
return this;
}
if (typeof units !== 'number' || Number.isNaN(units)) {
error('Invalid units');
return this;
}
this.totalPrice = {
type: priceType,
amount: {
currencyCode: currencyCode,
units: units,
nanos: nanos || 0
}
};
return this;
}
/**
* Adds an associated location to the order. Up to 2 locations can be added.
*
* @param {string} type One of TransactionValues.LocationType.
* @param {Location} location Location to add.
* @return {Order} Returns current constructed Order.
*/
addLocation (type, location) {
if (!type) {
error('type cannot be empty');
return this;
}
if (!location) {
error('location cannot be null');
return this;
}
if (!this.extension) {
this.extension = {
'@type': GENERIC_EXTENSION_TYPE
};
}
if (!this.extension.locations) {
this.extension.locations = [];
}
if (this.extension.locations.length >= ORDER_LOCATION_LIMIT) {
error('Order can have no more than ' + ORDER_LOCATION_LIMIT +
' associated locations');
return this;
}
this.extension.locations.push({ type, location });
return this;
}
/**
* Sets an associated time to the order.
*
* @param {string} type One of TransactionValues.TimeType.
* @param {string} time Time to add. Time should be ISO 8601 representation
* of time value. Could be date, datetime, or duration.
* @return {Order} Returns current constructed Order.
*/
setTime (type, time) {
if (!type) {
error('type cannot be empty');
return this;
}
if (!time) {
error('time cannot be empty');
return this;
}
if (!this.extension) {
this.extension = {
'@type': GENERIC_EXTENSION_TYPE
};
}
this.extension.time = { type, time_iso8601: time };
return this;
}
};
/**
* Class for initializing and constructing Cart with chainable interface.
*/
const Cart = class {
/**
* Constructor for Cart.
*
* @param {string=} cartId Optional unique identifier for the cart.
*/
constructor (cartId) {
/**
* ID for the cart. Optional.
* @type {string}
*/
this.id = cartId || undefined;
/**
* Merchant providing the cart.
* @type {Object}
*/
this.merchant = undefined;
/**
* Optional notes about the cart.
* @type {string}
*/
this.notes = undefined;
/**
* Items held in the order cart.
* @type {Array<LineItem>}
*/
this.lineItems = [];
/**
* Non-line items.
* @type {Array<LineItem>}
*/
this.otherItems = [];
}
/**
* Set the merchant for this cart.
*
* @param {string} id Merchant ID.
* @param {string} name Name of the merchant.
* @return {Cart} Returns current constructed Cart.
*/
setMerchant (id, name) {
if (!id) {
error('Merchant ID cannot be empty');
return this;
}
if (!name) {
error('Merchant name cannot be empty');
return this;
}
this.merchant = { id, name };
return this;
}
/**
* Set the notes for this cart.
*
* @param {string} notes Notes.
* @return {Cart} Returns current constructed Cart.
*/
setNotes (notes) {
if (!notes) {
error('Notes cannot be empty');
return this;
}
this.notes = notes;
return this;
}
/**
* Adds a single item or list of items to the cart.
*
* @param {LineItem|Array<LineItem>} items Line Items to add.
* @return {Cart} Returns current constructed Cart.
*/
addLineItems (items) {
if (!items) {
error('items cannot be null');
return this;
}
if (Array.isArray(items)) {
for (let item of items) {
this.lineItems.push(item);
}
} else {
this.lineItems.push(items);
}
return this;
}
/**
* Adds a single item or list of items to the non-items list of this cart.
*
* @param {LineItem|Array<LineItem>} items Line Items to add.
* @return {Cart} Returns current constructed Cart.
*/
addOtherItems (items) {
if (!items) {
error('items cannot be null');
return this;
}
if (Array.isArray(items)) {
for (let item of items) {
this.otherItems.push(item);
}
} else {
this.otherItems.push(items);
}
return this;
}
};
/**
* Class for initializing and constructing LineItem with chainable interface.
*/
const LineItem = class {
/**
* Constructor for LineItem.
*
* @param {string} lineItemId Unique identifier for the item.
* @param {string} name Name of the item.
*/
constructor (lineItemId, name) {
/**
* Item ID.
* @type {string}
*/
this.id = lineItemId;
/**
* Name of the item.
* @type {string}
*/
this.name = name;
/**
* Item price.
* @type {Price}
*/
this.price = undefined;
/**
* Sublines for current item. Only valid if item type is REGULAR.
* @type {Array<string|LineItem>}
*/
this.subLines = undefined;
/**
* Image of the item.
* @type {Image}
*/
this.image = undefined;
/**
* Type of the item. One of TransactionValues.ItemType.
* @type {string}
*/
this.type = undefined;
/**
* Quantity of the item.
* @type {number}
*/
this.quantity = undefined;
/**
* Description for the item.
* @type {string}
*/
this.description = undefined;
/**
* Offer ID for the item.
* @type {string}
*/
this.offerId = undefined;
}
/**
* Adds a single item or list of items or notes to the sublines. Only valid
* if item type is REGULAR.
*
* @param {string|LineItem|Array<string|LineItem>} items Sublines to add.
* @return {LineItem} Returns current constructed LineItem.
*/
addSublines (items) {
if (!items) {
error('items cannot be null');
return this;
}
if (!this.subLines) {
this.subLines = [];
}
const list = (Array.isArray(items) ? items : [items]).map(item =>
typeof item === 'string' ? { note: item } : { lineItem: item }
);
this.subLines.push(...list);
return this;
}
/**
* Sets the image for this item.
*
* @param {string} url Image source URL.
* @param {string} accessibilityText Text to replace for image for
* accessibility.
* @param {number=} width Width of the image.
* @param {number=} height Height of the image.
* @return {LineItem} Returns current constructed LineItem.
*/
setImage (url, accessibilityText, width, height) {
if (!url) {
error('url cannot be empty');
return this;
}
if (!accessibilityText) {
error('accessibilityText cannot be empty');
return this;
}
this.image = {url, accessibilityText};
if (width) {
this.image.width = width;
}
if (height) {
this.image.height = height;