@spreeloop/orange_money 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/package.json +4 -1
  2. package/.eslintrc.js +0 -47
  3. package/.prettierrc +0 -6
  4. package/babel.config.js +0 -3
  5. package/index.spec.ts +0 -9
  6. package/index.ts +0 -1
  7. package/jest.before-test.ts +0 -3
  8. package/jest.config.ts +0 -54
  9. package/src/disbursements/implementations/disbursement_service.ts +0 -133
  10. package/src/disbursements/implementations/src/live.ts +0 -79
  11. package/src/disbursements/implementations/src/sandbox.ts +0 -124
  12. package/src/disbursements/operations/create_access_token.spec.ts +0 -87
  13. package/src/disbursements/operations/create_access_token.ts +0 -83
  14. package/src/disbursements/operations/get_transfer_status.spec.ts +0 -132
  15. package/src/disbursements/operations/get_transfer_status.ts +0 -265
  16. package/src/disbursements/operations/transfer.spec.ts +0 -158
  17. package/src/disbursements/operations/transfer.ts +0 -125
  18. package/src/disbursements/routes/routes.ts +0 -54
  19. package/src/disbursements/utils/constants.ts +0 -4
  20. package/src/disbursements/utils/regex.spec.ts +0 -39
  21. package/src/disbursements/utils/regex.ts +0 -10
  22. package/src/disbursements/utils/status.ts +0 -62
  23. package/src/disbursements/utils/utils.spec.ts +0 -42
  24. package/src/disbursements/utils/utils.ts +0 -24
  25. package/src/index.ts +0 -12
  26. package/src/payments/implementations/fake.ts +0 -133
  27. package/src/payments/implementations/live.ts +0 -87
  28. package/src/payments/operations/get_access_token.spec.ts +0 -60
  29. package/src/payments/operations/get_access_token.ts +0 -77
  30. package/src/payments/operations/get_pay_token.spec.ts +0 -65
  31. package/src/payments/operations/get_pay_token.ts +0 -66
  32. package/src/payments/operations/get_payment_status.spec.ts +0 -82
  33. package/src/payments/operations/get_payment_status.ts +0 -78
  34. package/src/payments/operations/initialize_om_payment.spec.ts +0 -234
  35. package/src/payments/operations/initialize_om_payment.ts +0 -191
  36. package/src/payments/payments.ts +0 -86
  37. package/src/payments/routes/routes.ts +0 -61
  38. package/src/payments/utils/constants.ts +0 -120
  39. package/src/payments/utils/joi_schema.ts +0 -117
  40. package/src/payments/utils/request_model.ts +0 -103
  41. package/src/payments/utils/utils.ts +0 -31
  42. package/src/utils/https.spec.ts +0 -101
  43. package/src/utils/https.ts +0 -266
  44. package/src/utils/logging_interface.ts +0 -25
  45. package/src/utils/operation_response.ts +0 -17
  46. package/src/utils/utils.ts +0 -5
  47. package/tsconfig.build.json +0 -4
  48. package/tsconfig.dev.json +0 -4
  49. package/tsconfig.json +0 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spreeloop/orange_money",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "A orange money api integration package",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,6 +16,9 @@
16
16
  "coverage": "jest --coverage"
17
17
  },
18
18
  "main": "dist/index.js",
19
+ "files": [
20
+ "/dist"
21
+ ],
19
22
  "license": "MIT",
20
23
  "types": "dist/index.d.ts",
21
24
  "type": "commonjs",
package/.eslintrc.js DELETED
@@ -1,47 +0,0 @@
1
- module.exports = {
2
- root: true,
3
- env: {
4
- es6: true,
5
- node: true,
6
- },
7
- extends: [
8
- 'eslint:recommended',
9
- 'plugin:import/errors',
10
- 'plugin:import/warnings',
11
- 'plugin:import/typescript',
12
- 'google',
13
- 'plugin:@typescript-eslint/recommended',
14
- 'prettier',
15
- ],
16
- parser: '@typescript-eslint/parser',
17
- parserOptions: {
18
- project: ['tsconfig.json', 'tsconfig.dev.json'],
19
- tsconfigRootDir: __dirname,
20
- sourceType: 'module',
21
- },
22
- ignorePatterns: [
23
- '/dist/**/*', // Ignore built files.
24
- '/coverage/**/*', // Ignore built files.
25
- ],
26
- plugins: ['@typescript-eslint', 'import', 'prettier'],
27
- rules: {
28
- quotes: [
29
- 'error',
30
- 'single',
31
- {
32
- avoidEscape: true,
33
- },
34
- ],
35
- 'prettier/prettier': 2,
36
- },
37
- settings: {
38
- 'import/resolver': {
39
- 'custom-alias': {
40
- extensions: ['.ts'],
41
- alias: {
42
- '@spreeloop-core/logging': '../logging',
43
- },
44
- },
45
- },
46
- },
47
- };
package/.prettierrc DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "semi": true,
3
- "singleQuote": true,
4
- "trailingComma": "es5",
5
- "printWidth": 80
6
- }
package/babel.config.js DELETED
@@ -1,3 +0,0 @@
1
- module.exports = {
2
- presets: ['@babel/preset-env', '@babel/preset-typescript'],
3
- };
package/index.spec.ts DELETED
@@ -1,9 +0,0 @@
1
- import * as index from './index';
2
-
3
- // Ensure that we import all exported functions,
4
- // in order to have more accurate code coverage stats.
5
- describe('Import all functions', () => {
6
- it('ensures that the index is defined', () => {
7
- expect(index).toBeDefined();
8
- });
9
- });
package/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './src/index';
@@ -1,3 +0,0 @@
1
- afterEach(() => {
2
- jest.restoreAllMocks();
3
- });
package/jest.config.ts DELETED
@@ -1,54 +0,0 @@
1
- import type { Config } from 'jest';
2
-
3
- /*
4
- * For a detailed explanation regarding each configuration property and type check, visit:
5
- * https://jestjs.io/docs/configuration
6
- */
7
- const jestConfig: Config = {
8
- // An array of glob patterns indicating a set of files for which coverage information should be collected
9
- collectCoverageFrom: ['src/**/*.{js,ts}', '!src/**/*.d.ts'],
10
-
11
- // The directory where Jest should output its coverage files
12
- coverageDirectory: 'coverage',
13
-
14
- // An array of directory names to be searched recursively up from the requiring module's location
15
- moduleDirectories: ['<rootDir>', 'node_modules'],
16
-
17
- // An alternative API to setting the NODE_PATH env variable,
18
- // modulePaths is an array of absolute paths to additional locations to search when resolving modules.
19
- modulePaths: ['<rootDir>/node_modules'],
20
-
21
- // An array of file extensions your modules use
22
- // Module file extensions for importing
23
- moduleFileExtensions: ['ts', 'js'],
24
-
25
- // Automatically reset mock state before every test
26
- resetMocks: true,
27
-
28
- // The test environment that will be used for testing
29
- testEnvironment: 'node',
30
-
31
- moduleNameMapper: {
32
- '@spreeloop-core/logging': '<rootDir>../logging',
33
- },
34
-
35
- // A list of paths to modules that run some code to configure or set up the testing framework
36
- // before each test file in the suite is executed.
37
- setupFilesAfterEnv: ['<rootDir>/jest.before-test.ts'],
38
-
39
- // The glob patterns Jest uses to detect test files
40
- testMatch: ['<rootDir>/**/*.spec.{js,ts}'],
41
-
42
- // A transformer is a module that provides a synchronous function for transforming source files.
43
- transform: {
44
- '^.+\\.ts$': 'babel-jest',
45
- },
46
-
47
- // Those file will not be transformed
48
- transformIgnorePatterns: [],
49
-
50
- // Indicates whether each individual test should be reported during the run
51
- verbose: true,
52
- };
53
-
54
- export default jestConfig;
@@ -1,133 +0,0 @@
1
- import Joi from 'joi';
2
- import { DisbursementXTargetEnvironmentType } from '../utils/constants';
3
- import { LoggerInterface } from '../../utils/logging_interface';
4
- import { merchantPhoneNumberRegex } from '../utils/regex';
5
- import { CreateAccessTokenResponse } from '../operations/create_access_token';
6
- import { TransferRequest, TransferResponse } from '../operations/transfer';
7
- import {
8
- GetTransferStatusRequest,
9
- GetTransferStatusResponse,
10
- } from '../operations/get_transfer_status';
11
- import { DisbursementLive } from './src/live';
12
- import { DisbursementFake } from './src/sandbox';
13
-
14
- /**
15
- * Interface for DisbursementServiceConfig.
16
- */
17
- export interface DisbursementServiceConfig {
18
- clientId: string;
19
- clientSecret: string;
20
- customerKey: string;
21
- customerSecret: string;
22
- channelUserMsisdn: string;
23
- pin: string;
24
- environment: DisbursementXTargetEnvironmentType;
25
- logger: LoggerInterface;
26
- }
27
-
28
- /**
29
- * Joi schema for DisbursementServiceConfigSchema.
30
- */
31
- export const DisbursementServiceConfigSchema =
32
- Joi.object<DisbursementServiceConfig>({
33
- clientId: Joi.string().required().messages({
34
- 'string.base': '"clientId" should be a type of \'text\'',
35
- 'string.empty': '"clientId" cannot be an empty field',
36
- 'any.required': '"clientId" is a required field',
37
- }),
38
- clientSecret: Joi.string().required().messages({
39
- 'string.base': '"clientSecret" should be a type of \'text\'',
40
- 'string.empty': '"clientSecret" cannot be an empty field',
41
- 'any.required': '"clientSecret" is a required field',
42
- }),
43
- customerKey: Joi.string().required().messages({
44
- 'string.base': '"customerKey" should be a type of \'text\'',
45
- 'string.empty': '"customerKey" cannot be an empty field',
46
- 'any.required': '"customerKey" is a required field',
47
- }),
48
- customerSecret: Joi.string().required().messages({
49
- 'string.base': '"customerSecret" should be a type of \'text\'',
50
- 'string.empty': '"customerSecret" cannot be an empty field',
51
- 'any.required': '"customerSecret" is a required field',
52
- }),
53
- channelUserMsisdn: Joi.string()
54
- .pattern(merchantPhoneNumberRegex)
55
- .required()
56
- .messages({
57
- 'string.base': '"channelUserMsisdn" should be a type of \'text\'',
58
- 'string.empty': '"channelUserMsisdn" cannot be an empty field',
59
- 'any.required': '"channelUserMsisdn" is a required field',
60
- }),
61
- pin: Joi.string().required().messages({
62
- 'string.base': '"pin" should be a type of \'text\'',
63
- 'string.empty': '"pin" cannot be an empty field',
64
- 'any.required': '"pin" is a required field',
65
- }),
66
- environment: Joi.string()
67
- .valid(...Object.values(DisbursementXTargetEnvironmentType))
68
- .required()
69
- .messages({
70
- 'string.base': '"environment" should be a type of \'text\'',
71
- 'string.empty': '"environment" cannot be an empty field',
72
- 'any.required': '"environment" is a required field',
73
- }),
74
- logger: Joi.object().required().messages({
75
- 'string.base': '"logger" should be a type of \'object\'',
76
- 'string.empty': '"logger" cannot be an empty field',
77
- 'any.required': '"logger" is a required field',
78
- }),
79
- });
80
-
81
- /**
82
- * Orange Money API Disbursement.
83
- * @class
84
- */
85
- export interface DisbursementInterface {
86
- /**
87
- * Creates an access token.
88
- *
89
- * @return {Promise<CreateAccessTokenResponse>} The method response containing a string and a token.
90
- */
91
- createAccessToken(): Promise<CreateAccessTokenResponse>;
92
-
93
- /**
94
- * Transfers the specified amount of money from one account to another.
95
- *
96
- * @param {TransferRequest} params - The parameters for the transfer.
97
- * @return {Promise<TransferResponse>} - The response from the transfer method.
98
- */
99
- transfer(params: TransferRequest): Promise<TransferResponse>;
100
-
101
- /**
102
- * Retrieves the status of a transfer.
103
- *
104
- * @param {GetTransferStatusRequest} params - The parameters for retrieving the transfer status.
105
- * @return {Promise<GetTransferStatusResponse>} - The method response containing the status and transfer status response.
106
- */
107
- getTransferStatus(
108
- params: GetTransferStatusRequest
109
- ): Promise<GetTransferStatusResponse>;
110
- }
111
-
112
- /**
113
- * Orange Money API Disbursement.
114
- * @class
115
- */
116
- export class Disbursement {
117
- /**
118
- * Creates a new Disbursement object based on the provided configuration.
119
- *
120
- * @param {DisbursementServiceConfig} config - The configuration for the Disbursement object.
121
- * @return {Disbursement} A new Disbursement object based on the provided configuration.
122
- */
123
- static createDisbursement(
124
- config: DisbursementServiceConfig
125
- ): DisbursementInterface {
126
- switch (config.environment) {
127
- case DisbursementXTargetEnvironmentType.prod:
128
- return new DisbursementLive(config);
129
- case DisbursementXTargetEnvironmentType.sandbox:
130
- return new DisbursementFake(config);
131
- }
132
- }
133
- }
@@ -1,79 +0,0 @@
1
- import {
2
- TransferRequest,
3
- TransferResponse,
4
- transfer,
5
- } from '../../operations/transfer';
6
- import {
7
- CreateAccessTokenResponse,
8
- createAccessToken,
9
- } from '../../operations/create_access_token';
10
- import { Routes } from '../../routes/routes';
11
- import {
12
- DisbursementInterface,
13
- DisbursementServiceConfig,
14
- } from '../disbursement_service';
15
- import {
16
- GetTransferStatusRequest,
17
- GetTransferStatusResponse,
18
- getTransferStatus,
19
- } from '../../operations/get_transfer_status';
20
-
21
- /**
22
- * Orange Money API Live Disbursement.
23
- * @class
24
- */
25
- export class DisbursementLive implements DisbursementInterface {
26
- protected readonly config: DisbursementServiceConfig;
27
- protected readonly routes: Routes;
28
- /**
29
- * Constructs a new {Disbursement}.
30
- * @constructor
31
- * @param {DisbursementServiceConfig} config - The required global route configuration.
32
- */
33
- constructor(config: DisbursementServiceConfig) {
34
- this.routes = new Routes();
35
- this.config = config;
36
- }
37
-
38
- /**
39
- * Creates an access token.
40
- *
41
- * @return {Promise<CreateAccessTokenResponse>} The method response containing a string and a token.
42
- */
43
- async createAccessToken(): Promise<CreateAccessTokenResponse> {
44
- return await createAccessToken({
45
- configs: this.config,
46
- endPoint: this.routes.createAccessToken(),
47
- });
48
- }
49
-
50
- /**
51
- * Transfers the specified amount of money from one account to another.
52
- *
53
- * @param {TransferRequest} params - The parameters for the transfer.
54
- * @return {Promise<TransferResponse>} - The response from the transfer method.
55
- */
56
- async transfer(params: TransferRequest): Promise<TransferResponse> {
57
- return await transfer({
58
- configs: this.config,
59
- params: params,
60
- endPoint: this.routes.transfer(),
61
- });
62
- }
63
-
64
- /**
65
- * Retrieves the status of a transfer.
66
- *
67
- * @param {GetTransferStatusRequest} params - The parameters for retrieving the transfer status.
68
- * @return {Promise<GetTransferStatusResponse>} - The method response containing the status and transfer status response.
69
- */
70
- async getTransferStatus(
71
- params: GetTransferStatusRequest
72
- ): Promise<GetTransferStatusResponse> {
73
- return await getTransferStatus({
74
- configs: this.config,
75
- params: params,
76
- endPoint: this.routes.transferStatus(params.messageId),
77
- });
78
- }
79
- }
@@ -1,124 +0,0 @@
1
- import { TransferResponse } from '../../operations/transfer';
2
- import { CreateAccessTokenResponse } from '../../operations/create_access_token';
3
- import { Routes } from '../../routes/routes';
4
- import {
5
- DisbursementInterface,
6
- DisbursementServiceConfig,
7
- } from '../disbursement_service';
8
- import {
9
- DisbursementStep,
10
- GetTransferStatusResponse,
11
- } from '../../operations/get_transfer_status';
12
- import {
13
- DisbursementApiRawStatus,
14
- DisbursementStatus,
15
- } from '../../utils/status';
16
-
17
- /**
18
- * Orange Money API Fake Disbursement.
19
- * @class
20
- */
21
- export class DisbursementFake implements DisbursementInterface {
22
- protected readonly config: DisbursementServiceConfig;
23
- protected readonly routes: Routes;
24
- /**
25
- * Constructs a new {Disbursement}.
26
- * @constructor
27
- * @param {DisbursementServiceConfig} config - The required global route configuration.
28
- */
29
- constructor(config: DisbursementServiceConfig) {
30
- this.routes = new Routes();
31
- this.config = config;
32
- }
33
-
34
- /**
35
- * Creates an access token.
36
- *
37
- * @return {Promise<CreateAccessTokenResponse>} The method response containing a string and a token.
38
- */
39
- async createAccessToken(): Promise<CreateAccessTokenResponse> {
40
- return {
41
- data: 'accessToken',
42
- raw: {
43
- access_token: 'accessToken',
44
- token_type: 'Bearer',
45
- expires_in: '300',
46
- },
47
- };
48
- }
49
-
50
- /**
51
- * Transfers the specified amount of money from one account to another.
52
- *
53
- * @param {TransferRequest} params - The parameters for the transfer.
54
- * @return {Promise<TransferResponse>} - The response from the transfer method.
55
- */
56
- async transfer(): Promise<TransferResponse> {
57
- return {
58
- data: 'f7ec5a39-b1ad-4fa5-a734-60ab4217ba75',
59
- raw: {
60
- MD5OfMessageBody: '4b55cf6629b5f0ee3c8ac91435a2eb35',
61
- MD5OfMessageAttributes: '50e3084aee2bf840dd63b6bbf0a62fa9',
62
- MessageId: 'f7ec5a39-b1ad-4fa5-a734-60ab4217ba75',
63
- ResponseMetadata: {
64
- RequestId: 'bbb43d42-95d1-5a6b-92a7-60c0d420e103',
65
- HTTPStatusCode: 200,
66
- HTTPHeaders: {
67
- 'x-amzn-requestid': 'bbb43d42-95d1-5a6b-92a7-60c0d420e103',
68
- 'x-amzn-trace-id':
69
- 'Root=1-65083fb1-37fbf881380338ed3b5fcd2b;Parent=6bfc3c24556c9b27;Sampled=0;Lineage=79e6faf7:0',
70
- date: 'Mon, 18 Sep 2023 12:16:49 GMT',
71
- 'content-type': 'text/xml',
72
- 'content-length': '459',
73
- },
74
- RetryAttempts: 0,
75
- },
76
- },
77
- };
78
- }
79
-
80
- /**
81
- * Retrieves the status of a transfer.
82
- *
83
- * @param {GetTransferStatusRequest} params - The parameters for retrieving the transfer status.
84
- * @return {Promise<GetTransferStatusResponse>} - The method response containing the status and transfer status response.
85
- */
86
- async getTransferStatus(): Promise<GetTransferStatusResponse> {
87
- return {
88
- data: {
89
- status: DisbursementStatus.succeeded,
90
- refundStep: DisbursementStep.TransferSent,
91
- },
92
- raw: {
93
- result: {
94
- message: 'Cash in performed successfully',
95
- data: {
96
- subscriberMsisdn: 'subscriberMsisdn',
97
- channelUserMsisdn: 'channelUserMsisdn',
98
- createtime: '1695039280',
99
- amount: 100,
100
- payToken: 'C2C2309183CB702A1CBB39ECE32C',
101
- txnid: 'PP230918.1314.B57973',
102
- txnmode: 'refundYNote',
103
- txnstatus: '00068',
104
- orderId: 'refundYNote',
105
- status: DisbursementApiRawStatus.succeeded,
106
- description: 'Remboursement Fond',
107
- },
108
- },
109
- parameters: {
110
- amount: '100',
111
- xauth: 'WU5PVEVIRUFEOllOT1RFSEVBRDIwMjA=',
112
- channel_user_msisdn: 'channel_user_msisdn',
113
- customer_key: 'customer_key',
114
- customer_secret: 'customer_secret',
115
- final_customer_name: 'Steve',
116
- final_customer_phone: '692232239',
117
- },
118
- CreateAt: '09-18-2023 12:14:43',
119
- MessageId: 'caee8310-02fa-45da-9b22-a1effcada25c',
120
- RefundStep: DisbursementStep.TransferSent,
121
- },
122
- };
123
- }
124
- }
@@ -1,87 +0,0 @@
1
- import { AxiosError, AxiosResponse } from 'axios';
2
- import { Logger } from '../../../../logging/src/logging';
3
- import * as requests from '../../utils/https';
4
- import { DisbursementXTargetEnvironmentType } from '../utils/constants';
5
- import { Token, createAccessToken } from './create_access_token';
6
-
7
- describe('createAccessToken', () => {
8
- it('Should fail on invalid parameter', async () => {
9
- const result = await createAccessToken({
10
- endPoint: '',
11
- configs: {
12
- channelUserMsisdn: '',
13
- clientId: '',
14
- clientSecret: '',
15
- customerKey: '',
16
- customerSecret: '',
17
- pin: '',
18
- environment: DisbursementXTargetEnvironmentType.sandbox,
19
- logger: new Logger(),
20
- },
21
- });
22
- expect(result.data).toBeUndefined();
23
- });
24
-
25
- it('Should fail on request rejection', async () => {
26
- const errorMessage = 'mock reject the request';
27
- const postSpy = jest
28
- .spyOn(requests, 'postRequest')
29
- .mockImplementation()
30
- .mockResolvedValue({ error: new AxiosError(errorMessage) });
31
-
32
- const result = await createAccessToken({
33
- endPoint: 'https://route.com/link',
34
- configs: {
35
- channelUserMsisdn: '237699947943',
36
- clientId: 'clientId',
37
- clientSecret: 'clientSecret',
38
- customerKey: 'customerKey',
39
- customerSecret: 'customerSecret',
40
- pin: 'pin',
41
- environment: DisbursementXTargetEnvironmentType.sandbox,
42
- logger: new Logger(),
43
- },
44
- });
45
-
46
- expect(postSpy).toHaveBeenCalledTimes(1);
47
- expect(result.data).toBeUndefined();
48
- expect(result.error).toBeDefined();
49
- postSpy.mockRestore();
50
- });
51
-
52
- it('Should successfully retrieve token', async () => {
53
- const accessToken = 'THEaCCESStOKEN';
54
- const postSpy = jest
55
- .spyOn(requests, 'postRequest')
56
- .mockImplementation()
57
- .mockResolvedValue({
58
- response: <AxiosResponse<Token>>{
59
- data: {
60
- access_token: accessToken,
61
- },
62
- },
63
- });
64
-
65
- const result = await createAccessToken({
66
- endPoint: 'https://route.com/link',
67
- configs: {
68
- channelUserMsisdn: '237699947943',
69
- clientId: 'clientId',
70
- clientSecret: 'clientSecret',
71
- customerKey: 'customerKey',
72
- customerSecret: 'customerSecret',
73
- pin: 'pin',
74
- environment: DisbursementXTargetEnvironmentType.sandbox,
75
- logger: new Logger(),
76
- },
77
- });
78
-
79
- expect(postSpy).toHaveBeenCalledTimes(1);
80
- expect(result.error).toBeUndefined();
81
- expect(result.raw).toEqual({
82
- access_token: accessToken,
83
- });
84
- expect(result.data).toBe(accessToken);
85
- postSpy.mockRestore();
86
- });
87
- });
@@ -1,83 +0,0 @@
1
- import {
2
- RequestResponse,
3
- encodeDataToXFormUrl,
4
- hash,
5
- postRequest,
6
- } from '../../utils/https';
7
- import { OperationResponse } from '../../utils/operation_response';
8
- import {
9
- DisbursementServiceConfig,
10
- DisbursementServiceConfigSchema,
11
- } from '../implementations/disbursement_service';
12
-
13
- export type Token = {
14
- /**
15
- * The requested token.
16
- */
17
- access_token: string;
18
-
19
- scope?: string;
20
-
21
- /**
22
- * The type of the requested token.
23
- */
24
- token_type: string;
25
-
26
- /** The time to live in seconds. */
27
- expires_in: string;
28
- };
29
-
30
- export type CreateAccessTokenResponse = OperationResponse<string, Token>;
31
-
32
- /**
33
- * Creates an access token.
34
- *
35
- * @param {CreateAccessTokenRequest} configs - The disbursement service config.
36
- * @param {string} endPoint - The end point.
37
- * @return {CreateAccessTokenResponse} The method response containing the access token and raw response.
38
- */
39
- export async function createAccessToken({
40
- configs,
41
- endPoint,
42
- }: {
43
- configs: DisbursementServiceConfig;
44
- endPoint: string;
45
- }): Promise<CreateAccessTokenResponse> {
46
- const logger = configs.logger;
47
- logger.info('createAccessToken is running ...');
48
- const { error: configsValidationError, value: configValues } =
49
- DisbursementServiceConfigSchema.validate(configs);
50
-
51
- if (configsValidationError) {
52
- logger.error(
53
- `Invalid DisbursementServiceConfig request parameters. params : ${JSON.stringify(
54
- configsValidationError.details
55
- )}`
56
- );
57
- return { error: configsValidationError.details };
58
- }
59
- const authorization = hash(configValues.clientId, configValues.clientSecret);
60
- const headers = {
61
- 'Content-Type': 'application/x-www-form-urlencoded',
62
- Authorization: `Basic ${authorization}`,
63
- };
64
- const body = encodeDataToXFormUrl({
65
- grant_type: 'client_credentials',
66
- });
67
- const response: RequestResponse<Token> = await postRequest<Token>({
68
- logger: logger,
69
- route: endPoint,
70
- data: body,
71
- headers: headers,
72
- rejectUnauthorized: false,
73
- });
74
-
75
- if (!response.response) {
76
- return { error: response.error };
77
- }
78
-
79
- return {
80
- data: response.response.data.access_token,
81
- raw: response.response.data,
82
- };
83
- }