-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathremittances.ts
More file actions
185 lines (163 loc) · 5.48 KB
/
remittances.ts
File metadata and controls
185 lines (163 loc) · 5.48 KB
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
import { AxiosInstance } from "axios";
import {v4 as uuid} from "uuid";
import { getTransactionError } from "./errors";
import { validateRemittance } from "./validate";
import { Balance,
FailureReason,
PartyIdType,
TransactionStatus
} from "./common";
export interface RemittanceRequest {
/**
* Unique Transfer Reference (UUID v4), will be automatically generated if not explicitly supplied
*/
referenceId: string;
/**
* Amount that will be debited from the payer account.
*/
amount: string;
/**
* ISO4217 Currency
*/
currency: string;
/**
* External id is used as a reference to the transaction.
* External id is used for reconciliation.
* The external id will be included in transaction history report.
* External id is not required to be unique.
*/
externalId?: string;
/**
* Party identifies an account holder in the wallet platform.
* Party consists of two parameters, type and partyId.
* Each type have its own validation of the partyId
* MSISDN - Mobile Number validated according to ITU-T E.164. Validated with IsMSISDN
* EMAIL - Validated to be a valid e-mail format. Validated with IsEmail
* PARTY_CODE - UUID of the party. Validated with IsUuid
*/
payee: {
partyIdType: PartyIdType;
partyId: string;
};
/**
* Message that will be written in the payer transaction history message field.
*/
payerMessage?: string;
/**
* Message that will be written in the payee transaction history note field.
*/
payeeNote?: string;
/**
* URL to the server where the callback should be sent.
*/
callbackUrl?: string;
}
export interface Remit {
/**
* Amount that will be debited from the payer account.
*/
amount: string;
/**
* Financial transactionIdd from mobile money manager.
* Used to connect to the specific financial transaction made in the account
*/
financialTransactionId: string;
/**
* ISO4217 Currency
*/
currency: string;
/**
* External id is used as a reference to the transaction.
* External id is used for reconciliation.
* The external id will be included in transaction history report.
* External id is not required to be unique.
*/
externalId: string;
/**
* Party identifies a account holder in the wallet platform.
* Party consists of two parameters, type and partyId.
* Each type have its own validation of the partyId
* MSISDN - Mobile Number validated according to ITU-T E.164. Validated with IsMSISDN
* EMAIL - Validated to be a valid e-mail format. Validated with IsEmail
* PARTY_CODE - UUID of the party. Validated with IsUuid
*/
payee: {
partyIdType: "MSISDN";
partyId: string;
};
status: TransactionStatus;
reason?: FailureReason;
}
export default class Remittances {
private client: AxiosInstance;
constructor(client: AxiosInstance) {
this.client = client;
}
/**
* Remit operation to send funds to local recipients from the diaspora
* from the owner's account to the payee's account
*
* @param remittanceRequest
*/
public remit({
callbackUrl,
referenceId= uuid(),
...remittanceRequest
}: RemittanceRequest): Promise<string> {
return validateRemittance({referenceId, ...remittanceRequest}).then(() => {
return this.client
.post<void>("/remittance/v1_0/transfer", remittanceRequest, {
headers: {
"X-Reference-Id": referenceId,
...(callbackUrl ? { "X-Callback-Url": callbackUrl } : {})
}
})
.then(() => referenceId);
});
}
/**
* This method is used to retrieve the transaction. You can invoke this method
* to at intervals until your transaction fails or succeeds.
*
* If the transaction has failed, it will throw an appropriate error. The error will be a subclass
* of `MtnMoMoError`. Check [`src/error.ts`](https://github.com/sparkplug/momoapi-node/blob/master/src/errors.ts)
* for the various errors that can be thrown
*
* @param referenceId the value returned from `remit`
*/
public getTransaction( referenceId: string): Promise<Remit> {
return this.client.get<Remit>(`/remittance/v1_0/transfer/${referenceId}`)
.then(response => response.data)
.then(transaction => {
if (transaction.status === TransactionStatus.FAILED) {
return Promise.reject(getTransactionError(transaction));
}
return Promise.resolve(transaction);
});
}
/**
* Get the balance of the account.
*/
public getBalance(): Promise<Balance> {
return this.client
.get<Balance>("/remittance/v1_0/account/balance")
.then(response => response.data);
}
/**
* This method is used to check if an account holder is registered and active in the system.
*
* @param id Specifies the type of the party ID. Allowed values [msisdn, email, party_code].
* accountHolderId should explicitly be in small letters.
*
* @param type The party number. Validated according to the party ID type (case Sensitive).
* msisdn - Mobile Number validated according to ITU-T E.164. Validated with IsMSISDN
* email - Validated to be a valid e-mail format. Validated with IsEmail
* party_code - UUID of the party. Validated with IsUuid
*/
public isPayerActive(id: string, type: PartyIdType = PartyIdType.MSISDN): Promise<boolean> {
return this.client
.get<{result: boolean}>(`/remittance/v1_0/accountholder/${String(type).toLowerCase()}/${id}/active`)
.then(response => response.data)
.then(data => data.result ? data.result : false);
}
}