Skip to content

Commit

Permalink
BIL-284: Use Stripe params from the server instead of env variables (#57
Browse files Browse the repository at this point in the history
)

* Use Stripe params from the server instead of env variables

* Update tests
  • Loading branch information
francocorreasosa authored Feb 6, 2024
1 parent 1966e03 commit aab6dfb
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 67 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ jobs:
env:
CI: true
VITE_RC_API_KEY: ${{ secrets.RC_API_KEY }}
VITE_RC_STRIPE_ACCOUNT_ID: ${{ secrets.RC_STRIPE_ACCOUNT_ID }}
VITE_RC_STRIPE_PK_KEY: ${{ secrets.RC_STRIPE_PK_KEY }}

- name: Sync files to S3 bucket
working-directory: ./examples/rcbilling-demo
Expand Down
2 changes: 0 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,4 @@ jobs:
workingDirectory: "examples/rcbilling-demo"
displayName: E2E tests rcbilling-demo
env:
VITE_RC_STRIPE_PK_KEY: $(VITE_RC_STRIPE_PK_KEY)
VITE_RC_STRIPE_ACCOUNT_ID: $(VITE_RC_STRIPE_ACCOUNT_ID)
VITE_RC_API_KEY: $(VITE_RC_API_KEY)
4 changes: 1 addition & 3 deletions examples/rcbilling-demo/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
VITE_RC_API_KEY="rcb_abcdef1234567890abcdef"
VITE_RC_STRIPE_PK_KEY="pk_test_abcdef1234567890abcde"
VITE_RC_STRIPE_ACCOUNT_ID="acct_abcdef1234567890"
VITE_RC_API_KEY="rcb_abcdef1234567890abcdef"
7 changes: 1 addition & 6 deletions examples/rcbilling-demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ import { SuccessPage } from "./pages/success";
import DogServices from "./pages/dogServices";
import WithoutEntitlement from "./components/WithoutEntitlement";

const purchases = new Purchases(import.meta.env.VITE_RC_API_KEY as string, {
stripe: {
publishableKey: import.meta.env.VITE_RC_STRIPE_PK_KEY as string,
accountId: import.meta.env.VITE_RC_STRIPE_ACCOUNT_ID as string,
},
});
const purchases = new Purchases(import.meta.env.VITE_RC_API_KEY as string);
export const catServicesEntitlementId = "catServices";
export const dogServicesEntitlementId = "dogServices";

Expand Down
13 changes: 0 additions & 13 deletions src/entities/types.ts

This file was deleted.

21 changes: 1 addition & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Package as InnerPackage,
toOffering,
} from "./entities/offerings";
import { PaymentProviderSettings } from "./entities/types";
import RCPurchasesUI from "./ui/rcb-ui.svelte";

import {
Expand Down Expand Up @@ -44,37 +43,19 @@ export { ErrorCode, PurchasesError } from "./entities/errors";
export class Purchases {
// @internal
readonly _API_KEY: string;
// @internal
readonly _PAYMENT_PROVIDER_SETTINGS: PaymentProviderSettings | null = null;

private readonly backend: Backend;
private readonly purchaseOperationHelper: PurchaseOperationHelper;

constructor(
apiKey: string,
paymentProviderSettings: PaymentProviderSettings,
) {
constructor(apiKey: string) {
this._API_KEY = apiKey;
this._PAYMENT_PROVIDER_SETTINGS = paymentProviderSettings;

if (RC_ENDPOINT === undefined) {
console.error(
"Project was build without some of the environment variables set",
);
}

// Will need to change this to something more flexible
// if/when we end up supporting more payment gateways
if (
!this._PAYMENT_PROVIDER_SETTINGS.stripe ||
!this._PAYMENT_PROVIDER_SETTINGS.stripe?.accountId ||
!this._PAYMENT_PROVIDER_SETTINGS.stripe?.publishableKey
) {
console.error(
"Project was build without the stripe payment provider settings set",
);
}

this.backend = new Backend(this._API_KEY);
this.purchaseOperationHelper = new PurchaseOperationHelper(this.backend);
}
Expand Down
2 changes: 2 additions & 0 deletions src/networking/responses/subscribe-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ export interface SubscribeResponse {
next_action: string;
data: {
client_secret?: string;
stripe_account_id?: string;
publishable_api_key?: string;
};
}
18 changes: 7 additions & 11 deletions src/tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@ import { getRequestHandlers } from "./test-responses";
import { PackageType } from "../entities/offerings";
import { CustomerInfo } from "../entities/customer-info";

const STRIPE_TEST_DATA = {
stripe: { accountId: "acct_123", publishableKey: "pk_123" },
} as const;

const server = setupServer(...getRequestHandlers());

beforeAll(() => {
server.listen();
});

test("Purchases is defined", () => {
const billing = new Purchases("test_api_key", STRIPE_TEST_DATA);
const billing = new Purchases("test_api_key");
expect(billing).toBeDefined();
});

test("returns true if a user is entitled", async () => {
const billing = new Purchases("test_api_key", STRIPE_TEST_DATA);
const billing = new Purchases("test_api_key");
const isEntitled = await billing.isEntitledTo(
"someAppUserId",
"activeCatServices",
Expand All @@ -30,7 +26,7 @@ test("returns true if a user is entitled", async () => {
});

test("returns false if a user is not entitled", async () => {
const billing = new Purchases("test_api_key", STRIPE_TEST_DATA);
const billing = new Purchases("test_api_key");
const isEntitled = await billing.isEntitledTo(
"someAppUserId",
"expiredEntitlement",
Expand All @@ -56,7 +52,7 @@ describe("getOfferings", () => {
},
};
test("can get offerings", async () => {
const billing = new Purchases("test_api_key", STRIPE_TEST_DATA);
const billing = new Purchases("test_api_key");
const offerings = await billing.getOfferings("someAppUserId");

const currentOffering = {
Expand Down Expand Up @@ -116,7 +112,7 @@ describe("getOfferings", () => {
});

test("can get offerings without current offering id", async () => {
const billing = new Purchases("test_api_key", STRIPE_TEST_DATA);
const billing = new Purchases("test_api_key");
const offerings = await billing.getOfferings(
"appUserIdWithoutCurrentOfferingId",
);
Expand Down Expand Up @@ -176,7 +172,7 @@ describe("getOfferings", () => {
});

test("can get offerings with missing products", async () => {
const billing = new Purchases("test_api_key", STRIPE_TEST_DATA);
const billing = new Purchases("test_api_key");
const offerings = await billing.getOfferings(
"appUserIdWithMissingProducts",
);
Expand Down Expand Up @@ -207,7 +203,7 @@ describe("getOfferings", () => {
});

test("can get customer info", async () => {
const billing = new Purchases("test_api_key", STRIPE_TEST_DATA);
const billing = new Purchases("test_api_key");
const customerInfo = await billing.getCustomerInfo("someAppUserId");
const activeCatServicesEntitlementInfo: EntitlementInfo = {
identifier: "activeCatServices",
Expand Down
1 change: 0 additions & 1 deletion src/ui/modal-header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import { BrandingInfoResponse } from "../networking/responses/branding-response";
import { buildAssetURL } from "../networking/assets";
export let title = "";
export let brandingInfo: BrandingInfoResponse | null = null;
</script>

Expand Down
4 changes: 4 additions & 0 deletions src/ui/rcb-ui.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
paymentInfoCollectionMetadata = result;
return;
}
if (result.next_action === 'completed') {
state = 'success';
return;
}
})
.catch((e: PurchasesError) => {
handleError(new PurchaseFlowError(
Expand Down
1 change: 0 additions & 1 deletion src/ui/shell.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import Modal from "./modal.svelte";
import { BrandingInfoResponse } from "../networking/responses/branding-response";
export let title = "";
export let dark = false;
export let showHeader = false;
export let brandingInfo: BrandingInfoResponse | null = null;
Expand Down
10 changes: 2 additions & 8 deletions src/ui/states/state-needs-payment-info.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,8 @@
}
onMount(async () => {
const stripeSettings = purchases?._PAYMENT_PROVIDER_SETTINGS?.stripe;
if (!stripeSettings) {
throw new Error("Stripe settings not found");
}
const stripePk = stripeSettings.publishableKey;
const stripeAcctId = stripeSettings.accountId;
const stripePk = paymentInfoCollectionMetadata.data.publishable_api_key;
const stripeAcctId = paymentInfoCollectionMetadata.data.stripe_account_id;
if (!stripePk || !stripeAcctId) {
throw new Error("Stripe publishable key or account ID not found");
Expand Down

0 comments on commit aab6dfb

Please sign in to comment.