/
order.ts
77 lines (71 loc) · 2.1 KB
/
order.ts
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
import { URL } from 'url';
import isURL from 'validator/lib/isURL';
import { ArgumentError } from '../errors';
interface OrderProps {
/**
* The total order amount for the transaction.
*/
amount?: number;
/**
* The ISO 4217 currency code for the currency used in the transaction.
*/
currency?: string;
/**
* The discount code applied to the transaction. If multiple discount codes
* were used, please separate them with a comma.
*/
discountCode?: string;
/**
* The ID of the affiliate where the order is coming from.
*/
affiliateId?: string;
/**
* The ID of the sub-affiliate where the order is coming from.
*/
subaffiliateId?: string;
/**
* The URI of the referring site for this order.
*/
referrerUri?: URL;
/**
* Whether order was marked as a gift by the purchaser.
*/
isGift?: boolean;
/**
* Whether the purchaser included a gift message.
*/
hasGiftMessage?: boolean;
}
const currencyRegex = /^[A-Z]{3}$/;
/**
* The order information for the transaction being sent to the web service.
*/
export default class Order implements OrderProps {
/** @inheritDoc OrderProps.amount */
public amount?: number;
/** @inheritDoc OrderProps.currency */
public currency?: string;
/** @inheritDoc OrderProps.discountCode */
public discountCode?: string;
/** @inheritDoc OrderProps.affiliateId */
public affiliateId?: string;
/** @inheritDoc OrderProps.subaffiliateId */
public subaffiliateId?: string;
/** @inheritDoc OrderProps.referrerUri */
public referrerUri?: URL;
/** @inheritDoc OrderProps.isGift */
public isGift?: boolean;
/** @inheritDoc OrderProps.hasGiftMessage */
public hasGiftMessage?: boolean;
public constructor(order: OrderProps) {
if (order.currency != null && !currencyRegex.test(order.currency)) {
throw new ArgumentError(`The currency code ${order.currency} is invalid`);
}
if (order.referrerUri != null && !isURL(order.referrerUri.toString())) {
throw new ArgumentError(
`The referrer URI ${order.referrerUri.toString()} is invalid`
);
}
Object.assign(this, order);
}
}