This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
931 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Billing for your app | ||
|
||
Some partners might wish to charge merchants for using their app. | ||
Shopify provides API endpoints that enable apps to trigger purchases in the Shopify platform, so that apps don't need to implement their own billing features. | ||
|
||
This library provides support for billing apps by: | ||
|
||
1. Checking if the store has already paid for the app | ||
1. Triggering a payment if not | ||
|
||
Learn more about billing in [our documentation](https://shopify.dev/apps/billing). | ||
|
||
## Setting up for billing | ||
|
||
To trigger the billing behaviour, you should set the `BILLING` value when calling `Shopify.Context.initialize`. | ||
This setting is an object containing the following values: | ||
|
||
| Parameter | Type | Required? | Default Value | Notes | | ||
| -------------- | ----------------- | :-------: | :-----------: | ------------------------------------------------------------------ | | ||
| `chargeName` | `string` | Yes | - | The charge name to display to the merchant | | ||
| `amount` | `number` | Yes | - | The amount to charge | | ||
| `currencyCode` | `string` | Yes | - | The currency to charge, currently only `"USD"` is supported | | ||
| `interval` | `BillingInterval` | Yes | - | The interval for purchases, one of the BillingInterval enum values | | ||
|
||
`BillingInterval` values: | ||
|
||
- `OneTime` | ||
- `Every30Days` | ||
- `Annual` | ||
|
||
## Checking for payment | ||
|
||
The main method for billing is `Shopify.Billing.check`. | ||
This method will take in the following parameters: | ||
|
||
| Parameter | Type | Required? | Default Value | Notes | | ||
| --------- | --------- | :-------: | :-----------: | ------------------------------------- | | ||
| `session` | `Session` | Yes | - | The `Session` for the current request | | ||
| `isTest` | `boolean` | Yes | - | Whether this is a test purchase | | ||
|
||
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 | | ||
|
||
Here's a typical example of how to use `check`: | ||
|
||
```ts | ||
const {hasPayment, confirmationUrl} = await Shopify.Billing.check({ | ||
session, | ||
isTest: true, | ||
}); | ||
|
||
if (!hasPayment) { | ||
return redirect(confirmationUrl); | ||
} | ||
|
||
// Proceed with app logic | ||
``` | ||
|
||
## When should the app check for payment | ||
|
||
It's important to note that billing requires a session to access the API, which means that the app must actually be installed before it can request payment. | ||
|
||
With the `check` method, your app can block access to specific endpoints, or to the app as a whole. | ||
If you're gating access to the entire app, you should check for billing: | ||
|
||
1. After OAuth completes, once you get the session back from `Shopify.Auth.validateAuthCallback`. This allows you to ensure billing takes place immediately after the app is installed. | ||
- Note that the merchant may refuse the payment at this point, but the app will already be installed. If your app is using offline tokens, sessions will be unique to a shop, so you can also use the latest session to check for access in full page loads. | ||
1. When validating requests from the frontend. Since the check requires API access, you can only run it in requests that work with `Shopify.Utils.loadCurrentSession`. | ||
|
||
[Back to guide index](../README.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
import {Session} from '../../auth/session/session'; | ||
import {Context} from '../../context'; | ||
import {BillingError} from '../../error'; | ||
import {check} from '../check'; | ||
import {BillingInterval} from '../types'; | ||
|
||
import { | ||
TEST_CHARGE_NAME, | ||
CONFIRMATION_URL, | ||
EMPTY_SUBSCRIPTIONS, | ||
EXISTING_ONE_TIME_PAYMENT, | ||
EXISTING_ONE_TIME_PAYMENT_WITH_PAGINATION, | ||
EXISTING_INACTIVE_ONE_TIME_PAYMENT, | ||
EXISTING_SUBSCRIPTION, | ||
PURCHASE_ONE_TIME_RESPONSE, | ||
PURCHASE_SUBSCRIPTION_RESPONSE, | ||
PURCHASE_ONE_TIME_RESPONSE_WITH_USER_ERRORS, | ||
PURCHASE_SUBSCRIPTION_RESPONSE_WITH_USER_ERRORS, | ||
} from './check_responses'; | ||
|
||
describe('check', () => { | ||
const session = new Session('1234', 'test-shop.myshopify.io', '1234', true); | ||
session.accessToken = 'access-token'; | ||
session.scope = Context.SCOPES.toString(); | ||
|
||
describe('with non-recurring config', () => { | ||
beforeEach(() => { | ||
Context.BILLING = { | ||
amount: 5, | ||
chargeName: TEST_CHARGE_NAME, | ||
currencyCode: 'USD', | ||
interval: BillingInterval.OneTime, | ||
}; | ||
}); | ||
|
||
[true, false].forEach((isTest) => | ||
test(`requests a single payment if there is none (isTest: ${isTest})`, async () => { | ||
fetchMock.mockResponses( | ||
EMPTY_SUBSCRIPTIONS, | ||
PURCHASE_ONE_TIME_RESPONSE, | ||
); | ||
|
||
const response = await check({ | ||
session, | ||
isTest, | ||
}); | ||
|
||
expect(response).toEqual({ | ||
hasPayment: false, | ||
confirmationUrl: CONFIRMATION_URL, | ||
}); | ||
expect(fetchMock).toHaveBeenCalledTimes(2); | ||
expect(fetchMock).toHaveBeenNthCalledWith( | ||
1, | ||
expect.anything(), | ||
expect.objectContaining({ | ||
body: expect.stringContaining('oneTimePurchases'), | ||
headers: expect.objectContaining({ | ||
'X-Shopify-Access-Token': session.accessToken, | ||
}), | ||
}), | ||
); | ||
|
||
const parsedBody = JSON.parse( | ||
fetchMock.mock.calls[1][1]!.body!.toString(), | ||
); | ||
expect(parsedBody).toMatchObject({ | ||
query: expect.stringContaining('appPurchaseOneTimeCreate'), | ||
variables: expect.objectContaining({test: isTest}), | ||
}); | ||
}), | ||
); | ||
|
||
test('does not request payment if there is one', async () => { | ||
fetchMock.mockResponses(EXISTING_ONE_TIME_PAYMENT); | ||
|
||
const response = await check({ | ||
session, | ||
isTest: true, | ||
}); | ||
|
||
expect(response).toEqual({ | ||
hasPayment: true, | ||
confirmationUrl: undefined, | ||
}); | ||
expect(fetchMock).toHaveBeenCalledTimes(1); | ||
expect(fetchMock).toHaveBeenNthCalledWith( | ||
1, | ||
expect.anything(), | ||
expect.objectContaining({ | ||
body: expect.stringContaining('oneTimePurchases'), | ||
}), | ||
); | ||
}); | ||
|
||
test('ignores non-active payments', async () => { | ||
fetchMock.mockResponses( | ||
EXISTING_INACTIVE_ONE_TIME_PAYMENT, | ||
PURCHASE_ONE_TIME_RESPONSE, | ||
); | ||
|
||
const response = await check({ | ||
session, | ||
isTest: true, | ||
}); | ||
|
||
expect(response).toEqual({ | ||
hasPayment: false, | ||
confirmationUrl: CONFIRMATION_URL, | ||
}); | ||
expect(fetchMock).toHaveBeenCalledTimes(2); | ||
expect(fetchMock).toHaveBeenNthCalledWith( | ||
1, | ||
expect.anything(), | ||
expect.objectContaining({ | ||
body: expect.stringContaining('oneTimePurchases'), | ||
}), | ||
); | ||
expect(fetchMock).toHaveBeenNthCalledWith( | ||
2, | ||
expect.anything(), | ||
expect.objectContaining({ | ||
body: expect.stringContaining('appPurchaseOneTimeCreate'), | ||
}), | ||
); | ||
}); | ||
|
||
test('paginates until a payment is found', async () => { | ||
fetchMock.mockResponses(...EXISTING_ONE_TIME_PAYMENT_WITH_PAGINATION); | ||
|
||
const response = await check({ | ||
session, | ||
isTest: true, | ||
}); | ||
|
||
expect(response).toEqual({ | ||
hasPayment: true, | ||
confirmationUrl: undefined, | ||
}); | ||
expect(fetchMock).toHaveBeenCalledTimes(2); | ||
|
||
let parsedBody = JSON.parse(fetchMock.mock.calls[0][1]!.body!.toString()); | ||
expect(parsedBody).toMatchObject({ | ||
query: expect.stringContaining('oneTimePurchases'), | ||
variables: expect.objectContaining({endCursor: null}), | ||
}); | ||
|
||
parsedBody = JSON.parse(fetchMock.mock.calls[1][1]!.body!.toString()); | ||
expect(parsedBody).toMatchObject({ | ||
query: expect.stringContaining('oneTimePurchases'), | ||
variables: expect.objectContaining({endCursor: 'end_cursor'}), | ||
}); | ||
}); | ||
|
||
test('throws on userErrors', async () => { | ||
fetchMock.mockResponses( | ||
EMPTY_SUBSCRIPTIONS, | ||
PURCHASE_ONE_TIME_RESPONSE_WITH_USER_ERRORS, | ||
); | ||
|
||
await expect(() => | ||
check({ | ||
session, | ||
isTest: true, | ||
}), | ||
).rejects.toThrow(BillingError); | ||
|
||
expect(fetchMock).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
|
||
describe('with recurring config', () => { | ||
beforeEach(() => { | ||
Context.BILLING = { | ||
amount: 5, | ||
chargeName: TEST_CHARGE_NAME, | ||
currencyCode: 'USD', | ||
interval: BillingInterval.Every30Days, | ||
}; | ||
}); | ||
|
||
[true, false].forEach((isTest) => | ||
test(`requests a subscription if there is none (isTest: ${isTest})`, async () => { | ||
fetchMock.mockResponses( | ||
EMPTY_SUBSCRIPTIONS, | ||
PURCHASE_SUBSCRIPTION_RESPONSE, | ||
); | ||
|
||
const response = await check({ | ||
session, | ||
isTest, | ||
}); | ||
|
||
expect(response).toEqual({ | ||
hasPayment: false, | ||
confirmationUrl: CONFIRMATION_URL, | ||
}); | ||
expect(fetchMock).toHaveBeenCalledTimes(2); | ||
expect(fetchMock).toHaveBeenNthCalledWith( | ||
1, | ||
expect.anything(), | ||
expect.objectContaining({ | ||
body: expect.stringContaining('activeSubscriptions'), | ||
headers: expect.objectContaining({ | ||
'X-Shopify-Access-Token': session.accessToken, | ||
}), | ||
}), | ||
); | ||
|
||
const parsedBody = JSON.parse( | ||
fetchMock.mock.calls[1][1]!.body!.toString(), | ||
); | ||
expect(parsedBody).toMatchObject({ | ||
query: expect.stringContaining('appSubscriptionCreate'), | ||
variables: expect.objectContaining({test: isTest}), | ||
}); | ||
}), | ||
); | ||
|
||
test('does not request subscription if there is one', async () => { | ||
fetchMock.mockResponses(EXISTING_SUBSCRIPTION); | ||
|
||
const response = await check({ | ||
session, | ||
isTest: true, | ||
}); | ||
|
||
expect(response).toEqual({ | ||
hasPayment: true, | ||
confirmationUrl: undefined, | ||
}); | ||
|
||
expect(fetchMock).toHaveBeenCalledTimes(1); | ||
expect(fetchMock).toHaveBeenNthCalledWith( | ||
1, | ||
expect.anything(), | ||
expect.objectContaining({ | ||
body: expect.stringContaining('activeSubscriptions'), | ||
}), | ||
); | ||
}); | ||
|
||
test('throws on userErrors', async () => { | ||
fetchMock.mockResponses( | ||
EMPTY_SUBSCRIPTIONS, | ||
PURCHASE_SUBSCRIPTION_RESPONSE_WITH_USER_ERRORS, | ||
); | ||
|
||
await expect(() => | ||
check({ | ||
session, | ||
isTest: true, | ||
}), | ||
).rejects.toThrow(BillingError); | ||
|
||
expect(fetchMock).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.