-
Notifications
You must be signed in to change notification settings - Fork 50
feat: remittances #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
luigimorel
wants to merge
13
commits into
sparkplug:master
Choose a base branch
from
luigimorel:feat-remittances
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ea22f4e
add the remittances class
luigimorel 7aa75fa
add the remittances class tests
luigimorel 022c104
add auth for the remittance operation
luigimorel f2d2a8b
add tests for auth - remittance operation
luigimorel 7c9ac8d
add validation for the remittance operation
luigimorel 498ef91
add the remit inteface to error class
luigimorel 83febfb
add remittance operation to global config
luigimorel e1b0a50
add tests for remittance op. - global config
luigimorel 1fc148d
mock the remittance endpoint
luigimorel fe5f872
add example for the remittance operation
luigimorel d4f1d19
reject promise with error, remove ngrok url
luigimorel 4440102
fix: remove type coercion
luigimorel 74fc4f5
fix:changes referenceId type to strictly string
luigimorel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| const momo = require("../lib"); | ||
|
|
||
| const { Remittances } = momo.create({ callbackHost: process.env.CALLBACK_HOST }); | ||
|
|
||
| // initialise collections | ||
| const remittances = Remittances({ | ||
| userSecret: process.env.REMITTANCES_USER_SECRET, | ||
| userId: process.env.REMITTANCES_USER_ID, | ||
| primaryKey: process.env.REMITTANCES_PRIMARY_KEY | ||
| }); | ||
|
|
||
| const partyId = "256776564739"; | ||
| const partyIdType = momo.PayerType.MSISDN; | ||
| // Transfer | ||
| remittances | ||
| .isPayerActive(partyId, partyIdType) | ||
| .then((isActive) => { | ||
| console.log("Is Active ? ", isActive); | ||
| if (!isActive) { | ||
| return Promise.reject("Party not active"); | ||
| } | ||
| return remittances.remit({ | ||
| amount: "100", | ||
| currency: "EUR", | ||
| externalId: "947354", | ||
| payee: { | ||
| partyIdType, | ||
| partyId | ||
| }, | ||
| payerMessage: "testing", | ||
| payeeNote: "hello", | ||
| callbackUrl: process.env.CALLBACK_URL ? process.env.CALLBACK_URL : "https://75f59b50.ngrok.io" | ||
ernest-okot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| }) | ||
|
|
||
| .then(transactionId => { | ||
| console.log({ transactionId }); | ||
|
|
||
| // Get transaction status | ||
| return remittances.getTransaction(transactionId); | ||
| }) | ||
| .then(transaction => { | ||
| console.log({ transaction }); | ||
|
|
||
| // Get account balance | ||
| return remittances.getBalance(); | ||
| }) | ||
| .then(accountBalance => console.log({ accountBalance })) | ||
| .catch(error => { | ||
| console.log(error); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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); | ||
ernest-okot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.