|
| 1 | +import { isIP } from 'net'; |
| 2 | +import * as snakecaseKeys from 'snakecase-keys'; |
| 3 | +import { Tag } from '../constants'; |
| 4 | +import { ArgumentError } from '../errors'; |
| 5 | + |
| 6 | +interface TransactionReportProps { |
| 7 | + /** |
| 8 | + * A string which is provided by your payment processor indicating the |
| 9 | + * reason for the chargeback. |
| 10 | + */ |
| 11 | + chargebackCode?: string; |
| 12 | + /** |
| 13 | + * The IP address of the customer placing the order. |
| 14 | + */ |
| 15 | + ipAddress: string; |
| 16 | + /** |
| 17 | + * A unique eight character string identifying a minFraud Standard or |
| 18 | + * Premium request. These IDs are returned in the maxmindID field of a |
| 19 | + * response for a successful minFraud request. This field is not required, |
| 20 | + * but you are encouraged to provide it, if possible. |
| 21 | + */ |
| 22 | + maxmindId?: string; |
| 23 | + /** |
| 24 | + * A UUID that identifies a minFraud Score, minFraud Insights, or minFraud |
| 25 | + * Factors request. This ID is returned at /id in the response. This field |
| 26 | + * is not required, but you are encouraged to provide it if the request was |
| 27 | + * made to one of these services. |
| 28 | + */ |
| 29 | + minfraudId?: string; |
| 30 | + /** |
| 31 | + * Your notes on the fraud tag associated with the transaction. We manually |
| 32 | + * review many reported transactions to improve our scoring for you so any |
| 33 | + * additional details to help us understand context are helpful. |
| 34 | + */ |
| 35 | + notes?: string; |
| 36 | + /** |
| 37 | + * A Tag enum indicating the likelihood that a transaction may be |
| 38 | + * fraudulent. |
| 39 | + */ |
| 40 | + tag: Tag; |
| 41 | + /** |
| 42 | + * The transaction ID you originally passed to minFraud. This field is not |
| 43 | + * required, but you are encouraged to provide it or the transaction’s |
| 44 | + * maxmindId or minfraudId. |
| 45 | + */ |
| 46 | + transactionId?: string; |
| 47 | +} |
| 48 | + |
| 49 | +export default class TransactionReport { |
| 50 | + /** @inheritDoc TransactionReportProps.chargebackCode */ |
| 51 | + public chargebackCode?: string; |
| 52 | + /** @inheritDoc TransactionReportProps.ipAddress */ |
| 53 | + public ipAddress: string; |
| 54 | + /** @inheritDoc TransactionReportProps.maxmindId */ |
| 55 | + public maxmindId?: string; |
| 56 | + /** @inheritDoc TransactionReportProps.minfraudId */ |
| 57 | + public minfraudId?: string; |
| 58 | + /** @inheritDoc TransactionReportProps.notes */ |
| 59 | + public notes?: string; |
| 60 | + /** @inheritDoc TransactionReportProps.tag */ |
| 61 | + public tag: Tag; |
| 62 | + /** @inheritDoc TransactionReportProps.transactionId */ |
| 63 | + public transactionId?: string; |
| 64 | + |
| 65 | + public constructor(transactionReport: TransactionReportProps) { |
| 66 | + if (isIP(transactionReport.ipAddress) === 0) { |
| 67 | + throw new ArgumentError( |
| 68 | + '`transactionReport.ipAddress` is an invalid IP address' |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + if ( |
| 73 | + !transactionReport.tag || |
| 74 | + !Object.values(Tag).includes(transactionReport.tag) |
| 75 | + ) { |
| 76 | + throw new ArgumentError( |
| 77 | + `"${transactionReport.tag}" is an invalid value for "transactionReport.tag"` |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + this.ipAddress = transactionReport.ipAddress; |
| 82 | + this.tag = transactionReport.tag; |
| 83 | + Object.assign(this, transactionReport); |
| 84 | + } |
| 85 | + |
| 86 | + public toString(): string { |
| 87 | + return JSON.stringify(snakecaseKeys(this)); |
| 88 | + } |
| 89 | +} |
0 commit comments