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 <hello@internxt.com>",
"version": "1.16.0",
"version": "1.16.1",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down
118 changes: 15 additions & 103 deletions src/drive/payments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ import AppError from '../../shared/types/errors';
import { Tier } from './types/tiers';
import {
AvailableProducts,
CreateCheckoutSessionPayload,
CreatedSubscriptionData,
CreatePaymentSessionPayload,
CouponCodeData,
CustomerBillingInfo,
DisplayPrice,
FreeTrialAvailable,
Invoice,
InvoicePayload,
PaymentMethod,
ProductData,
RedeemCodePayload,
UpdateSubscriptionPaymentMethod,
UserSubscription,
Expand All @@ -36,83 +32,6 @@ export class Payments {
this.apiSecurity = apiSecurity;
}

public createCustomer(
name: string,
email: string,
country?: string,
companyVatId?: string,
): Promise<{ customerId: string; token: string }> {
return this.client.post('/create-customer', { name, email, country, companyVatId }, this.headers());
}

public createSubscription(
customerId: string,
priceId: string,
token: string,
quantity: number,
currency?: string,
promoCodeId?: string,
): Promise<CreatedSubscriptionData> {
return this.client.post(
'/create-subscription',
{
customerId,
priceId,
token,
quantity,
currency,
promoCodeId,
},
this.headers(),
);
}

public createPaymentIntent(
customerId: string,
amount: number,
planId: string,
token: string,
currency?: string,
promoCodeName?: string,
): Promise<{ clientSecret: string; id: string; invoiceStatus?: string }> {
const query = new URLSearchParams();
query.set('customerId', customerId);
query.set('amount', String(amount));
query.set('planId', planId);
query.set('token', token);
if (currency !== undefined) query.set('currency', currency);
if (promoCodeName !== undefined) query.set('promoCodeName', promoCodeName);
return this.client.get(`/payment-intent?${query.toString()}`, this.headers());
}

/**
* Fetches the existing products and its details
*/
public getProducts(): Promise<ProductData[]> {
return this.client.get('/v3/stripe/products', this.headers());
}

/**
* Creates and returns a new session identifier for the client to go to payment platform
* @param payload
*/
public createSession(payload: CreatePaymentSessionPayload): Promise<{
id: string;
}> {
return this.client.post(
'/v2/stripe/session',
{
test: payload.test,
lifetime_tier: payload.lifetime_tier,
mode: payload.mode,
priceId: payload.priceId,
successUrl: payload.successUrl,
canceledUrl: payload.canceledUrl,
},
this.headers(),
);
}

public getSetupIntent(userType?: UserType): Promise<{ clientSecret: string }> {
const query = new URLSearchParams();
if (userType) query.set('userType', userType);
Expand Down Expand Up @@ -140,15 +59,6 @@ export class Payments {
return this.client.get(`/invoices?${query.toString()}`, this.headers());
}

public isCouponUsedByUser({ couponCode }: { couponCode: string }): Promise<{
couponUsed: boolean;
}> {
const query = new URLSearchParams();
if (couponCode !== undefined) query.set('code', couponCode);

return this.client.get(`/coupon-in-use?${query.toString()}`, this.headers());
}

public getPromoCodesUsedByUser(): Promise<{ usedCoupons: string[] }> {
return this.client.get('/customer/redeemed-promotion-codes', this.headers());
}
Expand All @@ -164,21 +74,27 @@ export class Payments {
});
}

public async fetchPromotionCodeByName(priceId: string, promotionCodeName: string): Promise<CouponCodeData> {
const promotionCode = await this.client.get<{ codeId: string; amountOff: number; percentOff: number }>(
`/promo-code-by-name?priceId=${priceId}&promotionCode=${promotionCodeName}`,
this.headers(),
);

return {
codeId: promotionCode.codeId,
codeName: promotionCodeName,
amountOff: promotionCode.amountOff,
percentOff: promotionCode.percentOff,
};
}

public async getPrices(currency?: string, userType?: UserType): Promise<DisplayPrice[]> {
const query = new URLSearchParams();
if (currency !== undefined) query.set('currency', currency);
if (userType) query.set('userType', userType);
return this.client.get<DisplayPrice[]>(`/prices?${query.toString()}`, this.headers());
}

public requestPreventCancellation(): Promise<FreeTrialAvailable> {
return this.client.get('/request-prevent-cancellation', this.headers());
}

public preventCancellation(): Promise<void> {
return this.client.put('/prevent-cancellation', {}, this.headers());
}

public applyRedeemCode(payload: RedeemCodePayload): Promise<void> {
return this.client.post('/licenses', { code: payload.code, provider: payload.provider }, this.headers());
}
Expand Down Expand Up @@ -217,10 +133,6 @@ export class Payments {
return this.client.delete(`/subscriptions?${query.toString()}`, this.headers());
}

public createCheckoutSession(payload: CreateCheckoutSessionPayload): Promise<{ sessionId: string }> {
return this.client.post('/checkout-session', { ...payload }, this.headers());
}

public updateCustomerBillingInfo(payload: CustomerBillingInfo): Promise<void> {
return this.client.patch('/billing', { ...payload }, this.headers());
}
Expand Down
7 changes: 7 additions & 0 deletions src/drive/payments/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,10 @@ export type AvailableProducts = {
backups: boolean;
};
};

export interface CouponCodeData {
codeId: string;
codeName: string;
amountOff?: number;
percentOff?: number;
}
2 changes: 0 additions & 2 deletions src/payments/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export class Checkout {
currency,
captchaToken,
promoCodeId,
quantity,
}: CreateSubscriptionPayload): Promise<CreatedSubscriptionData> {
return this.client.post(
'/checkout/subscription',
Expand All @@ -104,7 +103,6 @@ export class Checkout {
currency,
captchaToken,
promoCodeId,
quantity,
},
this.authHeaders(),
);
Expand Down
3 changes: 0 additions & 3 deletions src/payments/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export interface CreateSubscriptionPayload {
captchaToken: string;
currency?: string;
promoCodeId?: string;
quantity?: number;
}

export interface CreatePaymentIntentPayload {
Expand Down Expand Up @@ -64,8 +63,6 @@ export type Price = {
decimalAmount: number;
type: UserType;
product: string;
minimumSeats?: number;
maximumSeats?: number;
};

export type Taxes = {
Expand Down
73 changes: 1 addition & 72 deletions test/drive/payments/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { Payments } from '../../../src/drive';
import { CreatePaymentSessionPayload, StripeSessionMode, UserType } from '../../../src/drive/payments/types/types';
import { UserType } from '../../../src/drive/payments/types/types';
import { ApiSecurity, AppDetails } from '../../../src/shared';
import { headersWithToken } from '../../../src/shared/headers';
import { HttpClient } from '../../../src/shared/http/client';
Expand All @@ -10,26 +10,6 @@ describe('payments service', () => {
vi.restoreAllMocks();
});

describe('get products', () => {
it('should call with right params & return data', async () => {
// Arrange
const callStub = vi.spyOn(HttpClient.prototype, 'get').mockResolvedValue({
content: 'true',
});

const { client, headers } = clientAndHeadersWithToken({});

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

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

describe('getInvoices', () => {
it('should call with right params & return data', async () => {
const callStub = vi.spyOn(HttpClient.prototype, 'get').mockResolvedValue([
Expand Down Expand Up @@ -100,57 +80,6 @@ describe('payments service', () => {
});
});
});

describe('create payment session', () => {
it('should call with right params & return data', async () => {
// Arrange
const callStub = vi.spyOn(HttpClient.prototype, 'post').mockResolvedValue({
id: 'ident',
});
const { client, headers } = clientAndHeadersWithToken({});
const payload: CreatePaymentSessionPayload = {
canceledUrl: '',
lifetime_tier: undefined,
mode: StripeSessionMode.Payment,
priceId: '',
successUrl: '',
test: false,
};

// Act
const body = await client.createSession(payload);

// Assert
expect(callStub).toHaveBeenCalledWith(
'/v2/stripe/session',
{
test: payload.test,
lifetime_tier: payload.lifetime_tier,
mode: payload.mode,
priceId: payload.priceId,
successUrl: payload.successUrl,
canceledUrl: payload.canceledUrl,
},
headers,
);
expect(body).toEqual({
id: 'ident',
});
});
});

describe('Get user used promotional codes', () => {
it('When requesting a user redeemed promo codes, it returns the correct data with the right parameters', async () => {
const response = { usedCoupons: ['PROMO_CODE', 'PROMO_CODE_1'] };
const callStub = vi.spyOn(HttpClient.prototype, 'get').mockResolvedValue(response);
const { client, headers } = clientAndHeadersWithToken({});

const body = await client.getPromoCodesUsedByUser();

expect(callStub).toHaveBeenCalledWith('/customer/redeemed-promotion-codes', headers);
expect(body).toEqual(response);
});
});
});

function clientAndHeadersWithToken({
Expand Down
Loading