Skip to content
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@internxt/sdk",
"author": "Internxt <[email protected]>",
"version": "1.9.6",
"version": "1.9.7",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down
9 changes: 9 additions & 0 deletions src/drive/payments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { headersWithToken } from '../../shared/headers';
import { HttpClient } from '../../shared/http/client';
import AppError from '../../shared/types/errors';
import {
AvailableProducts,
CreateCheckoutSessionPayload,
CreatedSubscriptionData,
CreatePaymentSessionPayload,
Expand Down Expand Up @@ -219,6 +220,14 @@ export class Payments {
return this.client.patch('/billing', { ...payload }, this.headers());
}

/**
* Gets the available products from the user
* @returns an object containing available products
*/
public checkUserAvailableProducts(): Promise<AvailableProducts> {
return this.client.get('/products', this.headers());
}

/**
* Returns the needed headers for the module requests
* @private
Expand Down
6 changes: 6 additions & 0 deletions src/drive/payments/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,9 @@ export type CreatedSubscriptionData = {
subscriptionId?: string;
paymentIntentId?: string;
};

export type AvailableProducts = {
featuresPerService: {
antivirus: boolean;
};
};
105 changes: 85 additions & 20 deletions test/drive/payments/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import sinon from 'sinon';
import { ApiSecurity, AppDetails } from '../../../src/shared';
import { headersWithToken } from '../../../src/shared/headers';
import { Payments } from '../../../src/drive';
import { CreatePaymentSessionPayload, StripeSessionMode } from '../../../src/drive/payments/types';
import { CreatePaymentSessionPayload, StripeSessionMode, UserType } from '../../../src/drive/payments/types';
import { HttpClient } from '../../../src/shared/http/client';

const httpClient = HttpClient.create('');

describe('payments service', () => {

beforeEach(() => {
sinon.stub(HttpClient, 'create').returns(httpClient);
});
Expand All @@ -18,35 +17,104 @@ describe('payments service', () => {
});

describe('get products', () => {

it('should call with right params & return data', async () => {
// Arrange
const callStub = sinon.stub(httpClient, 'get').resolves({
content: 'true'
content: 'true',
});
const { client, headers } = clientAndHeadersWithToken();

// Act
const body = await client.getProducts();

// Assert
expect(callStub.firstCall.args).toEqual([
'/v3/stripe/products',
headers
]);
expect(callStub.firstCall.args).toEqual(['/v3/stripe/products', headers]);
expect(body).toEqual({
content: 'true'
content: 'true',
});
});
});

describe('getInvoices', () => {
afterEach(() => {
sinon.restore();
});

it('should call with right params & return data', async () => {
const callStub = sinon.stub(httpClient, 'get').resolves([
{ id: 'invoice_123', amount: 1000 },
{ id: 'invoice_456', amount: 2000 },
]);

const { client, headers } = clientAndHeadersWithToken();

const params = {
subscriptionId: 'sub_789',
startingAfter: 'invoice_123',
userType: UserType.Individual,
limit: 10,
};

const invoices = await client.getInvoices(params);

const expectedQuery = new URLSearchParams({
subscription: 'sub_789',
starting_after: 'invoice_123',
userType: UserType.Individual,
limit: '10',
}).toString();

expect(callStub.firstCall.args).toEqual([`/invoices?${expectedQuery}`, headers]);

expect(invoices).toEqual([
{ id: 'invoice_123', amount: 1000 },
{ id: 'invoice_456', amount: 2000 },
]);
});

it('should handle missing optional parameters correctly', async () => {
const callStub = sinon.stub(httpClient, 'get').resolves([]);

const { client, headers } = clientAndHeadersWithToken();

const params = {};

const expectedQuery = new URLSearchParams({
userType: UserType.Individual,
}).toString();

const invoices = await client.getInvoices(params as any);

expect(callStub.firstCall.args).toEqual([`/invoices?${expectedQuery}`, headers]);
expect(invoices).toEqual([]);
});
});

describe('check if product is available', () => {
it('should call with right params & return data', async () => {
const callStub = sinon.stub(httpClient, 'get').resolves({
featuresPerService: {
antivirus: false,
},
});
const { client, headers } = clientAndHeadersWithToken();

const body = await client.checkUserAvailableProducts();

expect(callStub.firstCall.args).toEqual(['/products', headers]);
expect(body).toEqual({
featuresPerService: {
antivirus: false,
},
});
});
});

describe('create payment session', () => {

it('should call with right params & return data', async () => {
// Arrange
const callStub = sinon.stub(httpClient, 'post').resolves({
id: 'ident'
id: 'ident',
});
const { client, headers } = clientAndHeadersWithToken();
const payload: CreatePaymentSessionPayload = {
Expand All @@ -55,7 +123,7 @@ describe('payments service', () => {
mode: StripeSessionMode.Payment,
priceId: '',
successUrl: '',
test: false
test: false,
};

// Act
Expand All @@ -72,26 +140,23 @@ describe('payments service', () => {
successUrl: payload.successUrl,
canceledUrl: payload.canceledUrl,
},
headers
headers,
]);
expect(body).toEqual({
id: 'ident'
id: 'ident',
});
});

});

});


function clientAndHeadersWithToken(
apiUrl = '',
clientName = 'c-name',
clientVersion = '0.1',
token = 'token'
token = 'token',
): {
client: Payments,
headers: object
client: Payments;
headers: object;
} {
const appDetails: AppDetails = {
clientName: clientName,
Expand Down