Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Applying comments from review
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Aug 15, 2022
1 parent fa37ae5 commit 54b6057
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
12 changes: 6 additions & 6 deletions docs/usage/billing.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ This method will take in the following parameters:

And return the following:

| Parameter | Type | Notes |
| ----------------- | ---------------------- | --------------------------------------------------- |
| `hasPayment` | `boolean` | Whether the store has already paid for the app |
| `confirmationUrl` | `string` / `undefined` | The URL to redirect to if payment is still required |
| Parameter | Type | Notes |
| ------------------- | ---------------------- | --------------------------------------------------- |
| `hasPayment` | `boolean` | Whether the store has already paid for the app |
| `confirmBillingUrl` | `string` / `undefined` | The URL to redirect to if payment is still required |

Here's a typical example of how to use `check`:

```ts
const {hasPayment, confirmationUrl} = await Shopify.Billing.check({
const {hasPayment, confirmBillingUrl} = await Shopify.Billing.check({
session,
isTest: true,
});

if (!hasPayment) {
return redirect(confirmationUrl);
return redirect(confirmBillingUrl);
}

// Proceed with app logic
Expand Down
55 changes: 49 additions & 6 deletions src/billing/__tests__/check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('check', () => {

expect(response).toEqual({
hasPayment: false,
confirmationUrl: CONFIRMATION_URL,
confirmBillingUrl: CONFIRMATION_URL,
});
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(fetchMock).toHaveBeenNthCalledWith(
Expand All @@ -71,6 +71,26 @@ describe('check', () => {
}),
);

test('defaults to test purchases', async () => {
fetchMock.mockResponses(EMPTY_SUBSCRIPTIONS, PURCHASE_ONE_TIME_RESPONSE);

const response = await check({session});

expect(response).toEqual({
hasPayment: false,
confirmBillingUrl: CONFIRMATION_URL,
});
expect(fetchMock).toHaveBeenCalledTimes(2);

const parsedBody = JSON.parse(
fetchMock.mock.calls[1][1]!.body!.toString(),
);
expect(parsedBody).toMatchObject({
query: expect.stringContaining('appPurchaseOneTimeCreate'),
variables: expect.objectContaining({test: true}),
});
});

test('does not request payment if there is one', async () => {
fetchMock.mockResponses(EXISTING_ONE_TIME_PAYMENT);

Expand All @@ -81,7 +101,7 @@ describe('check', () => {

expect(response).toEqual({
hasPayment: true,
confirmationUrl: undefined,
confirmBillingUrl: undefined,
});
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenNthCalledWith(
Expand All @@ -106,7 +126,7 @@ describe('check', () => {

expect(response).toEqual({
hasPayment: false,
confirmationUrl: CONFIRMATION_URL,
confirmBillingUrl: CONFIRMATION_URL,
});
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(fetchMock).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -135,7 +155,7 @@ describe('check', () => {

expect(response).toEqual({
hasPayment: true,
confirmationUrl: undefined,
confirmBillingUrl: undefined,
});
expect(fetchMock).toHaveBeenCalledTimes(2);

Expand Down Expand Up @@ -193,7 +213,7 @@ describe('check', () => {

expect(response).toEqual({
hasPayment: false,
confirmationUrl: CONFIRMATION_URL,
confirmBillingUrl: CONFIRMATION_URL,
});
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(fetchMock).toHaveBeenNthCalledWith(
Expand All @@ -217,6 +237,29 @@ describe('check', () => {
}),
);

test('defaults to test purchases', async () => {
fetchMock.mockResponses(
EMPTY_SUBSCRIPTIONS,
PURCHASE_SUBSCRIPTION_RESPONSE,
);

const response = await check({session});

expect(response).toEqual({
hasPayment: false,
confirmBillingUrl: CONFIRMATION_URL,
});
expect(fetchMock).toHaveBeenCalledTimes(2);

const parsedBody = JSON.parse(
fetchMock.mock.calls[1][1]!.body!.toString(),
);
expect(parsedBody).toMatchObject({
query: expect.stringContaining('appSubscriptionCreate'),
variables: expect.objectContaining({test: true}),
});
});

test('does not request subscription if there is one', async () => {
fetchMock.mockResponses(EXISTING_SUBSCRIPTION);

Expand All @@ -227,7 +270,7 @@ describe('check', () => {

expect(response).toEqual({
hasPayment: true,
confirmationUrl: undefined,
confirmBillingUrl: undefined,
});

expect(fetchMock).toHaveBeenCalledTimes(1);
Expand Down
16 changes: 8 additions & 8 deletions src/billing/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ import {requestPayment} from './request_payment';

interface CheckInterface {
session: SessionInterface;
isTest: boolean;
isTest?: boolean;
}

interface CheckReturn {
hasPayment: boolean;
confirmationUrl?: string;
confirmBillingUrl?: string;
}

export async function check({
session,
isTest,
isTest = true,
}: CheckInterface): Promise<CheckReturn> {
if (!Context.BILLING) {
return {hasPayment: true, confirmationUrl: undefined};
return {hasPayment: true, confirmBillingUrl: undefined};
}

let hasPayment;
let confirmationUrl;
let hasPayment: boolean;
let confirmBillingUrl: string | undefined;

if (await hasActivePayment(session, isTest)) {
hasPayment = true;
} else {
hasPayment = false;
confirmationUrl = await requestPayment(session, isTest);
confirmBillingUrl = await requestPayment(session, isTest);
}

return {hasPayment, confirmationUrl};
return {hasPayment, confirmBillingUrl};
}

0 comments on commit 54b6057

Please sign in to comment.