Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xendit-node

Package Overview
Dependencies
Maintainers
5
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xendit-node - npm Package Compare versions

Comparing version 1.20.1 to 1.21.0

examples/with_async/recurring_payment.js

7

CHANGELOG.md
# CHANGELOG
## 2022-03-11
- Added recurring service
- recurring schedules
- recurring plans
- recurring cycles
## 2021-12-07

@@ -4,0 +11,0 @@

174

examples/with_async/recurring.js

@@ -0,43 +1,155 @@

/* eslint-disable no-console */
const x = require('../xendit');
const RecurringPayment = x.RecurringPayment;
const rp = new RecurringPayment({});
const Recurring = x.Recurring;
const r = new Recurring({});
(async function() {
try {
const payment = await rp.createPayment({
externalID: '123',
payerEmail: 'stanley@xendit.co',
description: 'Payment for something',
amount: 10000,
interval: RecurringPayment.Interval.Month,
intervalCount: 1,
});
// eslint-disable-next-line no-console
console.log('recurring payment created:', payment);
/**
* Before running this example consider the following
* 1. make sure to use a valid exampleCustomerId and examplePaymentMethodId
* 2. After plan has been deactivated - it's scheduled cycles become cancelled
* which makes impossible to be edit or cancel them
*/
const exampleBusinessId = '6066ebf68204c740b61aa3c1';
const exampleReferenceId = 'cf53cdbb-f92d-4220-835d-b907915d9551';
const exampleCustomerId = 'ba0e4584-4fc4-4cb2-a835-6191500540ef';
const examplePaymentMethodId = 'pm-fb182048-7448-4ffe-877a-a29efa3d7195';
const scheduledTimestamp = new Date(Date.now() + 3600 * 1000).toISOString();
const { id } = payment;
const retrievedPayment = await rp.getPayment({ id });
// eslint-disable-next-line no-console
console.log('recurring payment details:', retrievedPayment);
let scheduleId;
let planId;
const editedPayment = await rp.editPayment({ id, amount: 20000 });
// eslint-disable-next-line no-console
console.log('recurring payment updated:', editedPayment.id);
async function runSchedules() {
const createdSchedule = await r.createSchedule({
referenceId: exampleReferenceId,
businessId: exampleBusinessId,
interval: 'DAY',
intervalCount: 1,
totalRecurrence: 3,
anchorDate: '2022-01-01T00:00:00.001Z',
retryInterval: 'DAY',
retryIntervalCount: 1,
totalRetry: 1,
failedAttemptNotifications: [1],
});
scheduleId = createdSchedule.id;
const pausedPayment = await rp.pausePayment({ id });
// eslint-disable-next-line no-console
console.log('recurring payment paused:', pausedPayment.id);
console.log('created recurring schedule:', createdSchedule);
const resumedPayment = await rp.resumePayment({ id });
// eslint-disable-next-line no-console
console.log('recurring payment resumed:', resumedPayment.id);
const schedule = await r.getSchedule({
id: createdSchedule.id,
businessId: exampleBusinessId,
});
const stoppedPayment = await rp.stopPayment({ id });
// eslint-disable-next-line no-console
console.log('recurring payment stopped:', stoppedPayment.id);
console.log('recurring schedule:', schedule);
const editedSchedule = await r.editSchedule({
id: schedule.id,
businessId: exampleBusinessId,
interval: 'MONTH',
intervalCount: 1,
});
console.log('edited recurring schedule:', editedSchedule);
}
async function runPlans() {
const createdPlan = await r.createPlan({
businessId: exampleBusinessId,
referenceId: exampleReferenceId,
customerId: exampleCustomerId,
recurringAction: 'PAYMENT',
currency: 'IDR',
amount: 1000,
paymentMethods: [
{
paymentMethodId: examplePaymentMethodId,
rank: 1,
},
],
scheduleId: scheduleId,
immediateActionType: null,
notification_config: {
recurring_created: ['SMS'],
recurring_succeeded: ['SMS'],
recurring_failed: ['SMS'],
},
failedCycleAction: 'STOP',
metadata: null,
});
planId = createdPlan.id;
console.log('created recurring plan:', createdPlan);
const plan = await r.getPlan({
id: createdPlan.id,
businessId: exampleBusinessId,
});
console.log('recurring plan:', plan);
const editedPlan = await r.editPlan({
id: plan.id,
businessId: exampleBusinessId,
amount: 1000,
});
console.log('edited recurring plan:', editedPlan);
/** This is commented out to allow cycle "edit" and "cancel" actions */
// const deactivatedPlan = await r.deactivatePlan({
// id: plan.id,
// businessId: exampleBusinessId,
// });
// console.log('deactivated recurring plan:', deactivatedPlan);
}
async function runCycles() {
const response = await r.getAllCycles({
planId: planId,
businessId: exampleBusinessId,
limit: 2,
});
console.log('recurring cycles:', response.data);
console.log('has more recurring cycles:', response.has_more);
const cycle = await r.getCycle({
id: response.data[0].id,
planId: planId,
businessId: exampleBusinessId,
});
console.log('recurring cycle:', cycle);
const editedCycle = await r.editCycle({
id: cycle.id,
planId: planId,
businessId: exampleBusinessId,
amount: 1000,
currency: 'IDR',
scheduledTimestamp: scheduledTimestamp,
});
console.log('edited recurring cycle:', editedCycle);
const canceledCycle = await r.cancelCycle({
id: cycle.id,
planId: cycle.plan_id,
businessId: exampleBusinessId,
});
console.log('canceled recurring cycle:', canceledCycle);
}
(async function() {
try {
await runSchedules();
await runPlans();
await runCycles();
} catch (e) {
console.error(e); // eslint-disable-line no-console
console.error(e);
process.exit(1);
}
})();

230

examples/with_promises/recurring.js

@@ -0,52 +1,184 @@

/* eslint-disable no-console */
const x = require('../xendit');
const RecurringPayment = x.RecurringPayment;
const rp = new RecurringPayment({});
const Recurring = x.Recurring;
const r = new Recurring({});
rp.createPayment({
externalID: '123',
payerEmail: 'stanley@xendit.co',
description: 'Payment for something',
amount: 10000,
interval: RecurringPayment.Interval.Month,
intervalCount: 1,
})
.then(r => {
// eslint-disable-next-line no-console
console.log('recurring payment created:', r);
return r;
})
.then(({ id }) => rp.getPayment({ id }))
.then(r => {
// eslint-disable-next-line no-console
console.log('recurring payment details:', r);
return r;
})
.then(({ id }) => rp.editPayment({ id, amount: 20000 }))
.then(r => {
// eslint-disable-next-line no-console
console.log('recurring payment updated:', r.id);
return r;
})
.then(({ id }) => rp.pausePayment({ id }))
.then(r => {
// eslint-disable-next-line no-console
console.log('recurring payment paused:', r.id);
return r;
})
.then(({ id }) => rp.resumePayment({ id }))
.then(r => {
// eslint-disable-next-line no-console
console.log('recurring payment resumed:', r.id);
return r;
})
.then(({ id }) => rp.stopPayment({ id }))
.then(r => {
// eslint-disable-next-line no-console
console.log('recurring payment stopped:', r.id);
return r;
})
.catch(e => {
console.error(e); // eslint-disable-line no-console
process.exit(1);
});
/**
* Before running this example consider the following
* 1. make sure to use a valid exampleCustomerId and examplePaymentMethodId
* 2. After plan has been deactivated - it's scheduled cycles become cancelled
* which makes impossible to be edit or cancel them
*/
const exampleBusinessId = '6066ebf68204c740b61aa3c1';
const exampleReferenceId = 'cf53cdbb-f92d-4220-835d-b907915d9551';
const exampleCustomerId = 'ba0e4584-4fc4-4cb2-a835-6191500540ef';
const examplePaymentMethodId = 'pm-fb182048-7448-4ffe-877a-a29efa3d7195';
const scheduledTimestamp = new Date(Date.now() + 3600 * 1000).toISOString();
let scheduleId;
let planId;
function runSchedules() {
return r
.createSchedule({
referenceId: exampleReferenceId,
businessId: exampleBusinessId,
interval: 'DAY',
intervalCount: 1,
totalRecurrence: 3,
anchorDate: '2022-01-01T00:00:00.001Z',
retryInterval: 'DAY',
retryIntervalCount: 1,
totalRetry: 1,
failedAttemptNotifications: [1],
})
.then(createdSchedule => {
scheduleId = createdSchedule.id;
console.log('created recurring schedule:', createdSchedule);
return createdSchedule;
})
.then(createdSchedule => {
return r.getSchedule({
id: createdSchedule.id,
businessId: exampleBusinessId,
});
})
.then(schedule => {
console.log('recurring schedule:', schedule);
return schedule;
})
.then(schedule => {
return r.editSchedule({
id: schedule.id,
businessId: exampleBusinessId,
interval: 'MONTH',
intervalCount: 1,
});
})
.then(editedSchedule => {
console.log('edited recurring schedule:', editedSchedule);
});
}
function runPlans() {
return r
.createPlan({
businessId: exampleBusinessId,
referenceId: exampleReferenceId,
customerId: exampleCustomerId,
recurringAction: 'PAYMENT',
currency: 'IDR',
amount: 1000,
paymentMethods: [
{
paymentMethodId: examplePaymentMethodId,
rank: 1,
},
],
scheduleId: scheduleId,
immediateActionType: null,
notification_config: {
recurring_created: ['SMS'],
recurring_succeeded: ['SMS'],
recurring_failed: ['SMS'],
},
failedCycleAction: 'STOP',
metadata: null,
})
.then(createdPlan => {
planId = createdPlan.id;
console.log('created recurring plan:', createdPlan);
return createdPlan;
})
.then(createdPlan => {
return r.getPlan({
id: createdPlan.id,
businessId: exampleBusinessId,
});
})
.then(plan => {
console.log('recurring plan:', plan);
return plan;
})
.then(plan => {
return r.editPlan({
id: plan.id,
businessId: exampleBusinessId,
amount: 1000,
});
})
.then(editedPlan => {
console.log('edited recurring plan:', editedPlan);
return editedPlan;
});
/** This is commented out to allow cycle "edit" and "cancel" actions */
// .then(plan => {
// return r.deactivatePlan({
// id: plan.id,
// businessId: exampleBusinessId,
// });
// })
// .then(deactivatedPlan => {
// console.log('deactivated recurring plan:', deactivatedPlan);
// })
}
function runCycles() {
return r
.getAllCycles({
planId: planId,
businessId: exampleBusinessId,
limit: 2,
})
.then(response => {
console.log('recurring cycles:', response.data);
console.log('has more recurring cycles:', response.has_more);
return response;
})
.then(response => {
return r.getCycle({
id: response.data[0].id,
planId: planId,
businessId: exampleBusinessId,
});
})
.then(cycle => {
console.log('recurring cycle:', cycle);
return cycle;
})
.then(cycle => {
return r.editCycle({
id: cycle.id,
planId: planId,
businessId: exampleBusinessId,
amount: 1000,
currency: 'IDR',
scheduledTimestamp: scheduledTimestamp,
});
})
.then(editedCycle => {
console.log('edited recurring cycle:', editedCycle);
return editedCycle;
})
.then(editedCycle => {
return r.cancelCycle({
id: editedCycle.id,
planId: planId,
businessId: exampleBusinessId,
});
})
.then(canceledCycle => {
console.log('canceled recurring cycle:', canceledCycle);
});
}
(function() {
runSchedules()
.then(() => runPlans())
.then(() => runCycles())
.catch(e => {
console.error(e);
process.exit(1);
});
})();
{
"name": "xendit-node",
"version": "1.20.1",
"version": "1.21.0",
"description": "NodeJS client for Xendit API",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -50,2 +50,15 @@ # Xendit API Node.js Client

+ [Resume recurring payment](#resume-recurring-payment)
* [Recurring Services](#recurring-services)
+ [Create recurring schedule](#create-recurring-schedule)
+ [Edit recurring schedule](#edit-recurring-schedule)
+ [Get recurring schedule](#get-recurring-schedule)
+ [Create recurring plan](#create-recurring-plan)
+ [Create recurring plan with schedule](#create-recurring-plan-with-schedule)
+ [Edit recurring plan](#edit-recurring-plan)
+ [Get recurring plan](#get-recurring-plan)
+ [Deactivate recurring plan](#deactivate-recurring-plan)
+ [Edit recurring cycle](#edit-recurring-cycle)
+ [Get recurring cycle](#get-recurring-cycle)
+ [Get all recurring cycles](#get-all-recurring-cycles)
+ [Cancel recurring cycle](#cancel-recurring-cycle)
* [Payout Services](#payout-services)

@@ -627,6 +640,202 @@ + [Create a payout](#create-a-payout)

```
### Recurring Services
Instantiate Recurring service using constructor that has been injected with Xendit keys
```js
const { Recurring } = x;
const rSpecificOptions = {};
const r = new Recurring(rSpecificOptions);
```
Example: Create a recurring plan
```js
r.createPlan({
businessId: '6066ebf68204c740b61aa3c6',
referenceId: 'ref-123',
customerId: 'cus-123',
recurringAction: 'PAYMENT',
currency: 'IDR',
amount: 1000,
paymentMethods: [
{ paymentMethodId: 'pm-123', rank: 1 },
],
scheduleId: 'resc-123',
immediateActionType: 'FULL_AMOUNT',
notificationConfig: {
recurringCreated: ['EMAIL'],
recurringSucceeded: ['SMS'],
recurringFailed: ['WHATSAPP']
},
failedCycleAction: 'RESUME'
})
.then(({ id }) => {
console.log(`Recurring plan created with ID: ${id}`);
})
.catch(e => {
console.error(
`Recurring plan creation failed with message: ${e.message}`,
);
});
```
Refer to [Xendit API Reference](https://developers.xendit.co/api-reference/#recurring-plans) for more info about methods' parameters
#### Create recurring schedule
```ts
r.createSchedule(data: {
referenceId: string;
businessId: string;
interval: string;
intervalCount: number;
totalRecurrence?: number;
anchorDate?: string;
retryInterval?: string;
retryIntervalCount?: number;
totalRetry?: number;
failedAttemptNotifications?: number[];
});
```
#### Edit recurring schedule
```ts
r.editSchedule(data: {
id: string;
businessId: string;
interval: string;
intervalCount: number;
totalRecurrence?: number;
anchorDate?: string;
retryInterval?: string;
retryIntervalCount?: number;
totalRetry?: number;
failedAttemptNotifications?: number[];
});
```
#### Get recurring schedule
```ts
r.getSchedule(data: {
id: string;
businessId: string;
});
```
#### Create recurring plan
```ts
r.createPlan(data: {
businessId: string;
referenceId: string;
customerId: string;
recurringAction: RecurringAction;
currency: Currency;
amount: number;
paymentMethods?: Array<PaymentMethodIdRanked>;
scheduleId: string;
immediateActionType?: ImmediateActionType | null;
notificationConfig?: NotificationConfig | null;
failedCycleAction?: FailingCycleAction;
metadata?: object | null;
})
```
#### Create recurring plan with schedule
```ts
r.createPlan(data: {
businessId: string;
referenceId: string;
customerId: string;
recurringAction: RecurringAction;
currency: Currency;
amount: number;
paymentMethods?: Array<PaymentMethodIdRanked>;
schedule: RecurringSchedule;
immediateActionType?: ImmediateActionType | null;
notificationConfig?: NotificationConfig | null;
failedCycleAction?: FailingCycleAction;
metadata?: object | null;
})
```
#### Edit recurring plan
```ts
r.editPlan(data: {
businessId: string;
customerId?: string;
currency?: Currency;
amount?: number;
paymentMethods?: Array<PaymentMethodIdRanked>;
notificationConfig?: NotificationConfig | null;
metadata?: object | null;
description?: string;
})
```
#### Get recurring plan
```ts
r.getPlan(data: { id: string; businessId: string; })
```
#### Deactivate recurring plan
```ts
r.deactivatePlan(data: { id: string; businessId: string; })
```
#### Edit recurring cycle
```ts
r.editCycle(data: {
id: string;
businessId: string;
planId: string;
scheduledTimestamp: string;
currency: Currency;
amount: number;
metadata?: object | null;
})
```
#### Get recurring cycle
```ts
r.getCycle(data: {
id: string;
planId: string;
businessId: string;
})
```
#### Get all recurring cycles
```ts
r.getAllCycles(data: {
planId: string;
businessId: string;
limit?: number;
beforeId?: string;
afterId?: string;
})
```
#### Cancel recurring cycle
```ts
r.cancelCycle(data: {
id: string;
planId: string;
businessId: string;
})
```
### Payout Services
Instanitiate Payout service using constructor that has been injected with Xendit keys
Instantiate Payout service using constructor that has been injected with Xendit keys

@@ -1199,3 +1408,3 @@ ```js

console.error(`Generate Report Failed with Error: ${e.message}`);
})
});
```

@@ -1202,0 +1411,0 @@

@@ -1,3 +0,3 @@

import RecurringPayment from './recurring';
import Recurring from './recurring';
export { RecurringPayment };
export { Recurring };

@@ -1,3 +0,3 @@

const RecurringPayment = require('./recurring');
const Recurring = require('./recurring');
module.exports = { RecurringPayment };
module.exports = { Recurring };
import { XenditOptions } from '../xendit_opts';
import {
Interval as IntervalEnum,
Action as ActionEnum,
} from './manage_payments';
import { createPayment, getPayment, editPayment } from './manage_payments';
import { stopPayment, pausePayment, resumePayment } from './operate_payments';
ImmediateActionType,
NotificationChannel,
NotificationConfigLocale,
RecurringAction,
FailingCycleAction,
RecurringPlanStatus,
} from './manage_plans';
import { createPlan, editPlan, getPlan, deactivatePlan } from './manage_plans';
import {
getCycle,
editCycle,
getAllCycles,
cancelCycle,
} from './manage_cycles';
import { createSchedule, getSchedule, editSchedule } from './manage_schedules';
export = class RecurringPayment {
export default class Recurring {
constructor({});
static _constructorWithInjectedXenditOpts: (
opts: XenditOptions,
) => typeof RecurringPayment;
static Interval: {
Day: IntervalEnum;
Week: IntervalEnum;
Month: IntervalEnum;
};
static Action: {
Stop: ActionEnum;
Ignore: ActionEnum;
};
createPayment = createPayment;
getPayment = getPayment;
editPayment = editPayment;
stopPayment = stopPayment;
pausePayment = pausePayment;
resumePayment = resumePayment;
};
) => typeof RecurringPlan;
static recurringAction: RecurringAction;
static immediateActionType: ImmediateActionType;
static notificationChannel: NotificationChannel;
static locale: NotificationConfigLocale;
static failedCycleAction: FailingCycleAction;
static status: RecurringPlanStatus;
createPlan: typeof createPlan;
editPlan: typeof editPlan;
getPlan: typeof getPlan;
deactivatePlan: typeof deactivatePlan;
createSchedule: typeof createSchedule;
getSchedule: typeof getSchedule;
editSchedule: typeof editSchedule;
getCycle: typeof getCycle;
editCycle: typeof editCycle;
getAllCycles: typeof getAllCycles;
cancelCycle: typeof cancelCycle;
}

@@ -1,45 +0,92 @@

const { createPayment, getPayment, editPayment } = require('./manage_payments');
const {
stopPayment,
pausePayment,
resumePayment,
} = require('./operate_payments');
createPlan,
editPlan,
getPlan,
deactivatePlan,
} = require('./manage_plans');
const {
createSchedule,
getSchedule,
editSchedule,
} = require('./manage_schedules');
const {
getCycle,
getAllCycles,
cancelCycle,
editCycle,
} = require('./manage_cycles');
const RECURRING_PATH = '/recurring_payments';
const RECURRING_PATH = '/recurring';
function RecurringPayment(options) {
let aggOpts = options;
if (
RecurringPayment._injectedOpts &&
Object.keys(RecurringPayment._injectedOpts).length > 0
) {
aggOpts = Object.assign({}, options, RecurringPayment._injectedOpts);
class Recurring {
constructor(options) {
let aggOpts = Object.assign({}, options);
if (Object.keys(Recurring._injectedOpts || {}).length) {
aggOpts = Object.assign({}, options, Recurring._injectedOpts);
}
this.opts = aggOpts;
this.API_ENDPOINT_PLANS = this.opts.xenditURL + `${RECURRING_PATH}/plans`;
this.API_ENDPOINT_SCHEDULES =
this.opts.xenditURL + `${RECURRING_PATH}/schedules`;
}
this.opts = aggOpts;
this.API_ENDPOINT = this.opts.xenditURL + RECURRING_PATH;
static _constructorWithInjectedXenditOpts(options) {
Recurring._injectedOpts = options;
return Recurring;
}
}
RecurringPayment._injectedOpts = {};
RecurringPayment._constructorWithInjectedXenditOpts = function(options) {
RecurringPayment._injectedOpts = options;
return RecurringPayment;
};
RecurringPayment.Interval = {
Day: 'DAY',
Week: 'WEEK',
Month: 'MONTH',
};
RecurringPayment.Action = {
Stop: 'STOP',
Ignore: 'IGNORE',
};
Object.assign(Recurring, {
_injectedOpts: {},
Interval: {
Day: 'DAY',
Week: 'WEEK',
Month: 'MONTH',
},
Action: {
Stop: 'STOP',
Ignore: 'IGNORE',
},
recurringAction: {
payment: 'PAYMENT',
disbursment: 'DISBURSEMENT',
},
immediateActionType: {
fullAmount: 'FULL_AMOUNT',
},
notificationChannel: {
whatsapp: 'WHATSAPP',
sms: 'SMS',
email: 'EMAIL',
},
locale: {
en: 'en',
id: 'id',
},
failedCycleAction: {
resume: 'RESUME',
stop: 'STOP',
},
status: {
active: 'ACTIVE',
inactive: 'INACTIVE',
pending: 'PENDING',
},
});
RecurringPayment.prototype.createPayment = createPayment;
RecurringPayment.prototype.getPayment = getPayment;
RecurringPayment.prototype.editPayment = editPayment;
RecurringPayment.prototype.stopPayment = stopPayment;
RecurringPayment.prototype.pausePayment = pausePayment;
RecurringPayment.prototype.resumePayment = resumePayment;
Object.assign(Recurring.prototype, {
createPlan,
editPlan,
getPlan,
deactivatePlan,
createSchedule,
getSchedule,
editSchedule,
getCycle,
getAllCycles,
editCycle,
cancelCycle,
});
module.exports = RecurringPayment;
module.exports = Recurring;

@@ -7,3 +7,4 @@ import Errors from './errors';

import { PayoutService } from './payout';
import { RecurringPayment } from './recurring';
import { RecurringPayment } from './recurring_payment';
import { Recurring } from './recurring';
import { XenditOptions } from './xendit_opts';

@@ -29,2 +30,4 @@ import { EWalletService } from './ewallet';

RecurringPayment: typeof RecurringPayment;
Recurring: typeof Recurring;
RecurringPlan: typeof RecurringPlan;
EWallet: typeof EWalletService;

@@ -31,0 +34,0 @@ Balance: typeof BalanceServices;

@@ -6,3 +6,4 @@ const { CardService } = require('./card');

const { PayoutService } = require('./payout');
const { RecurringPayment } = require('./recurring');
const { Recurring } = require('./recurring');
const { RecurringPayment } = require('./recurring_payment');
const { EWalletService } = require('./ewallet');

@@ -37,2 +38,3 @@ const { BalanceServices } = require('./balance');

this.Payout = PayoutService._constructorWithInjectedXenditOpts(this.opts);
this.Recurring = Recurring._constructorWithInjectedXenditOpts(this.opts);
this.RecurringPayment = RecurringPayment._constructorWithInjectedXenditOpts(

@@ -39,0 +41,0 @@ this.opts,

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc