Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/example.js → examples/example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const IntaSend = require('./intasend');
import IntaSend from '../src/intasend';

// IMPORTANT:
// =================================
Expand All @@ -8,14 +8,14 @@ const IntaSend = require('./intasend');
// In your code, do not place the keys and commit to code repositories.
// We recommend using environment variables and 12Factor web approach. Please check it out here: https://12factor.net/

let intasend = new IntaSend(
const intasend = new IntaSend(
'ISPubKey_test_91ffc81a-8ac4-419e-8008-7091caa8d73f',
'ISSecretKey_test_15515fe9-fb5d-4362-970e-625532df8181',
true
);

// Create checkout page i.e payment collection link
let collection = intasend.collection();
const collection = intasend.collection();
collection
.charge({
first_name: 'FELIX',
Expand Down Expand Up @@ -50,7 +50,7 @@ collection
});

// How to create and interact with Wallets
let wallets = intasend.wallets();
const wallets = intasend.wallets();
wallets
.create({
label: 'NodeJS-SDK-TEST',
Expand All @@ -66,11 +66,13 @@ wallets

// How to send money M-PESA (B2C, B2B, BANK, INTASEND P2P)
// Learn more from our API reference on provider types and fields here - https://developers.intasend.com/reference/send-money_initiate_create
let payouts = intasend.payouts();
const payouts = intasend.payouts();
const requiresApproval = 'YES';
payouts
.initiate({
provider: 'MPESA-B2B',
currency: 'KES',
requires_approval: requiresApproval, // Set to 'NO' if you want the transaction to go through without calling the approve method
transactions: [
{
name: 'ABC',
Expand All @@ -84,21 +86,23 @@ payouts
.then((resp) => {
console.log(`Payouts response: ${JSON.stringify(resp)}`);
// Approve payouts
payouts
.approve(resp)
.then((resp) => {
console.log(`Payouts approve: ${JSON.stringify(resp)}`);
})
.catch((err) => {
console.error(`Payouts approve error: ${err}`);
});
if (requiresApproval === 'YES') {
payouts
.approve(resp)
.then((resp) => {
console.log(`Payouts approve: ${JSON.stringify(resp)}`);
})
.catch((err) => {
console.error(`Payouts approve error: ${err}`);
});
}
})
.catch((err) => {
console.error(`Payouts error: ${err}`);
});

// How to handle refunds
let refunds = intasend.refunds();
const refunds = intasend.refunds();
refunds
.create({
invoice: 'INVOICE-NUMBER',
Expand Down
10 changes: 10 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Config } from 'jest';

const config: Config = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/tests'],
testMatch: ['**/*.test.ts'],
};

export default config;
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
"dist"
],
"scripts": {
"build": "tsc"
"build": "tsc -p tsconfig.build.json",
"test": "jest"
},
"license": "GNU Affero General Public License v3.0",
"repository": "https://github.com/IntaSend/intasend-node.git",
"devDependencies": {
"@types/jest": "^30.0.0",
"@types/node": "^18.7.18",
"jest": "^30.2.0",
"ts-jest": "^29.4.6",
"typescript": "^5.4.5"
},
"author": {
Expand Down
19 changes: 12 additions & 7 deletions src/collection.js → src/collection.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
const RequestClient = require('./requests');
import RequestClient from './requests';
import { ChargePayload, MpesaStkPushPayload } from './types';

class Collection extends RequestClient {
charge(payload) {
charge(payload: ChargePayload): Promise<any> {
this.secret_key = '';
return this.send(payload, '/api/v1/checkout/', 'POST');
}

mpesaStkPush(payload) {
mpesaStkPush(payload: MpesaStkPushPayload): Promise<any> {
payload['method'] = 'M-PESA';
payload['currency'] = 'KES';
return this.send(payload, '/api/v1/payment/mpesa-stk-push/', 'POST');
}

status(invoiceID, checkoutID = '', signature = '') {
status(
invoiceID: string,
checkoutID: string = '',
signature: string = ''
): Promise<any> {
this.secret_key = '';
let payload = {
invoice_id: invoiceID
const payload: Record<string, string> = {
invoice_id: invoiceID,
};
if (checkoutID && signature) {
payload['signature'] = signature;
Expand All @@ -25,4 +30,4 @@ class Collection extends RequestClient {
}
}

module.exports = Collection;
export default Collection;
29 changes: 0 additions & 29 deletions src/intasend.js

This file was deleted.

53 changes: 53 additions & 0 deletions src/intasend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import RequestClient from './requests';
import Wallet from './wallets';
import Collection from './collection';
import Payouts from './payouts';
import Refunds from './refunds';

export {
RequestClient,
Wallet,
Collection,
Payouts,
Refunds,
};

export * from './types';

class IntaSend extends RequestClient {
constructor(publishable_key: string, secret_key: string, test_mode: boolean) {
super(publishable_key, secret_key, test_mode);
}

wallets(): Wallet {
return new Wallet(this.publishable_key, this.secret_key, this.test_mode);
}

collection(): Collection {
return new Collection(
this.publishable_key,
this.secret_key,
this.test_mode
);
}

payouts(): Payouts {
return new Payouts(this.publishable_key, this.secret_key, this.test_mode);
}

refunds(): Refunds {
return new Refunds(this.publishable_key, this.secret_key, this.test_mode);
}
}

export default IntaSend;

// CommonJS backward compatibility: allows `const IntaSend = require('intasend-node')`
// without needing `.default`. Named exports are attached as properties.
module.exports = IntaSend;
module.exports.default = IntaSend;
module.exports.RequestClient = RequestClient;
module.exports.Wallet = Wallet;
module.exports.Collection = Collection;
module.exports.Payouts = Payouts;
module.exports.Refunds = Refunds;
42 changes: 0 additions & 42 deletions src/payouts.js

This file was deleted.

43 changes: 43 additions & 0 deletions src/payouts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import RequestClient from './requests';
import { PayoutPayload } from './types';

class Payouts extends RequestClient {
initiate(payload: PayoutPayload): Promise<any> {
return this.send(payload, '/api/v1/send-money/initiate/', 'POST');
}

mpesa(payload: PayoutPayload): Promise<any> {
payload.provider = 'MPESA-B2C';
return this.initiate(payload);
}

mpesaB2B(payload: PayoutPayload): Promise<any> {
payload.provider = 'MPESA-B2B';
return this.initiate(payload);
}

bank(payload: PayoutPayload): Promise<any> {
payload.provider = 'PESALINK';
return this.initiate(payload);
}

intasend(payload: PayoutPayload): Promise<any> {
payload.provider = 'INTASEND';
return this.initiate(payload);
}

airtime(payload: PayoutPayload): Promise<any> {
payload.provider = 'AIRTIME';
return this.initiate(payload);
}

approve(payload: Record<string, any>): Promise<any> {
return this.send(payload, '/api/v1/send-money/approve/', 'POST');
}

status(payload: Record<string, any>): Promise<any> {
return this.send(payload, '/api/v1/send-money/status/', 'POST');
}
}

export default Payouts;
11 changes: 6 additions & 5 deletions src/refunds.js → src/refunds.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const RequestClient = require('./requests');
import RequestClient from './requests';
import { CreateRefundPayload } from './types';

class Refunds extends RequestClient {
list() {
list(): Promise<any> {
return this.send({}, '/api/v1/chargebacks/', 'GET');
}

create(payload) {
create(payload: CreateRefundPayload): Promise<any> {
return this.send(payload, '/api/v1/chargebacks/', 'POST');
}

get(chargebackID) {
get(chargebackID: string): Promise<any> {
return this.send({}, `/api/v1/chargebacks/${chargebackID}/`, 'GET');
}
}

module.exports = Refunds;
export default Refunds;
Loading