Skip to content

Commit

Permalink
feat: return purchaseStatus in .purchase() (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasstark authored Oct 4, 2024
1 parent 2cd7127 commit 13eae65
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
47 changes: 45 additions & 2 deletions Supertab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { server } from "@/mocks/server";
import EventEmitter from "events";
import Supertab from ".";
import {
ClientConfig,
Currency,
Price,
PurchaseStatus,
SiteOffering,
TabResponsePurchaseEnhanced,
TabStatus,
Expand Down Expand Up @@ -76,16 +78,17 @@ const setup = ({
],
redirectUri: "",
siteName: "",
testMode: false,
offerings: [
{
id: "test-offering-id",
description: "Test Offering Description",
createdAt: new Date("2021-01-01T00:00:00.000Z"),
updatedAt: new Date("2021-01-01T00:00:00.000Z"),
deletedAt: null,
productId: "test-product-id",
salesModel: "time_pass",
paymentModel: "pay_later",
summary: "test-summary",
price: {
amount: 100,
currency: clientConfigProps?.offeringCurrency ?? "USD",
Expand All @@ -106,6 +109,20 @@ const setup = ({
] as Price[],
recurringDetails: null,
timePassDetails: null,
offeringPrices: [
{
id: "test-offering-price",
createdAt: new Date("2021-01-01T00:00:00.000Z"),
updatedAt: new Date("2021-01-01T00:00:00.000Z"),
offeringId: "test-offering-id",
price: {
amount: 100,
currency: clientConfigProps?.offeringCurrency ?? "USD",
},
},
],
isActive: true,
subscriptionOfferingId: null,
connectedSubscriptionOffering: null,
},
] as SiteOffering[],
Expand All @@ -130,7 +147,7 @@ const setup = ({
},
] as Currency[],
suggestedCurrency: clientConfigProps?.suggestedCurrency ?? "USD",
});
} as ClientConfig);

return {
client,
Expand Down Expand Up @@ -168,6 +185,10 @@ describe("Supertab", () => {
const user: UserResponse = {
id: "test-user-id",
firstName: "Test",
email: "[email protected]",
registrationOrigin: "supertab",
isSuperuser: false,
tabCurrency: "USD",
lastName: "User",
createdAt: new Date("2021-01-01T00:00:00.000Z"),
updatedAt: new Date("2021-01-01T00:00:00.000Z"),
Expand Down Expand Up @@ -484,6 +505,20 @@ describe("Supertab", () => {
currency: "BRL",
},
],
offeringPrices: [
{
id: "test-offering-price",
createdAt: new Date("2023-11-03T15:34:44.852Z"),
updatedAt: new Date("2023-11-03T15:34:44.852Z"),
price: {
amount: 100,
currency: "USD",
},
},
],
isActive: true,
connectedSubscriptionOffering: null,
subscriptionOfferingId: null,
} as SiteOffering,
],
currencies: [
Expand Down Expand Up @@ -950,6 +985,7 @@ describe("Supertab", () => {
server.withPurchase({
detail: {
itemAdded: true,
purchaseStatus: PurchaseStatus.Added,
},
tab: tabData,
});
Expand All @@ -961,6 +997,7 @@ describe("Supertab", () => {
}),
).toEqual({
itemAdded: true,
purchaseStatus: PurchaseStatus.Added,
tab: {
currency: "USD",
id: "test-tab-id",
Expand Down Expand Up @@ -1004,6 +1041,7 @@ describe("Supertab", () => {
server.withPurchase({
detail: {
itemAdded: true,
purchaseStatus: PurchaseStatus.Added,
},
tab: euroTabData,
});
Expand All @@ -1015,6 +1053,7 @@ describe("Supertab", () => {
}),
).toEqual({
itemAdded: true,
purchaseStatus: PurchaseStatus.Added,
tab: {
currency: "EUR",
id: "test-tab-id",
Expand Down Expand Up @@ -1058,6 +1097,7 @@ describe("Supertab", () => {
server.withPurchase({
detail: {
itemAdded: true,
purchaseStatus: PurchaseStatus.Added,
},
tab: {
...tabData,
Expand All @@ -1068,6 +1108,7 @@ describe("Supertab", () => {
expect(await client.purchase({ offeringId: "test-offering-id" })).toEqual(
{
itemAdded: true,
purchaseStatus: PurchaseStatus.Added,
tab: {
currency: "BRL",
id: "test-tab-id",
Expand Down Expand Up @@ -1111,6 +1152,7 @@ describe("Supertab", () => {
{
detail: {
itemAdded: false,
purchaseStatus: PurchaseStatus.Added,
},
tab: {
...tabData,
Expand All @@ -1127,6 +1169,7 @@ describe("Supertab", () => {
}),
).toEqual({
itemAdded: false,
purchaseStatus: PurchaseStatus.Added,
tab: {
currency: "USD",
id: "test-tab-id",
Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
"typescript": "^4.0.0 || ^5.0.0"
},
"dependencies": {
"@getsupertab/auth": "^1.1.1",
"@getsupertab/tapper-sdk": "^3.0.0",
"@getsupertab/auth": "^1.1.4",
"@getsupertab/tapper-sdk": "^9.0.0",
"zod": "^3.22.3"
},
"targets": {
Expand Down
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
Price,
SiteOffering,
TabResponse,
PurchaseStatus,
} from "@getsupertab/tapper-sdk";

import { authFlow, getAuthStatus, getAccessToken, AuthStatus } from "./auth";
Expand Down Expand Up @@ -146,10 +147,11 @@ export class Supertab {
const clientConfig = await this.#getClientConfig();
let tab = null;

/* eslint-disable */
try {
tab = await this.getTab();
// eslint-disable-next-line no-empty
} catch (e) {}
/* eslint-enable */

const presentedCurrency =
tab?.currency ??
Expand Down Expand Up @@ -345,7 +347,11 @@ export class Supertab {
}: {
offeringId: string;
preferredCurrencyCode?: string;
}): Promise<{ itemAdded: boolean; tab: FormattedTab }> {
}): Promise<{
itemAdded: boolean;
purchaseStatus: PurchaseStatus | null;
tab: FormattedTab;
}> {
const tab = await this.getTab();
const clientConfig = await this.#getClientConfig();
const currency =
Expand All @@ -367,6 +373,7 @@ export class Supertab {

return {
itemAdded: !!detail?.itemAdded,
purchaseStatus: detail?.purchaseStatus || null,
tab: this.formatTab({ tab, clientConfig }),
};
} catch (e) {
Expand All @@ -376,6 +383,7 @@ export class Supertab {
);
return {
itemAdded: !!detail?.itemAdded,
purchaseStatus: detail?.purchaseStatus || null,
tab: this.formatTab({ tab, clientConfig }),
};
}
Expand Down

0 comments on commit 13eae65

Please sign in to comment.