-
Notifications
You must be signed in to change notification settings - Fork 28
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
30 changed files
with
270 additions
and
129 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
--- | ||
name: Policies Doc | ||
about: File a bug or suggestion for a policy | ||
title: '' | ||
labels: '' | ||
assignees: '' | ||
|
||
title: "" | ||
labels: "" | ||
assignees: "" | ||
--- | ||
|
||
<!-- Describe the issue or suggestion for the policy --> |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--- | ||
title: Monetization Terms Glossary | ||
title: Monetization Glossary | ||
sidebar_label: Glossary | ||
--- |
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 |
---|---|---|
@@ -1,3 +1,91 @@ | ||
--- | ||
title: Monetization Programmatic Quotas | ||
sidebar_label: Programmatic Quotas | ||
--- | ||
|
||
Typically, when adding monetization to your API, you set the number of meters a | ||
request will consume in the settings of the | ||
[Monetization Policy](https://zuplo.com/docs/policies/monetization-inbound). For | ||
example, the policy below specifies that the request will consume 1 `requests` | ||
meter and 5 `computeUnits` meters. | ||
|
||
```json | ||
{ | ||
"export": "MonetizationInboundPolicy", | ||
"module": "$import(@zuplo/runtime)", | ||
"options": { | ||
"allowRequestsOverQuota": false, | ||
"allowedSubscriptionStatuses": ["active", "incomplete"], | ||
"meterOnStatusCodes": "200-399", | ||
"meters": { | ||
"requests": 1, | ||
"computeUnits": 5 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
However, in some cases, you may not know up front how many units of a particular | ||
meter will be consumed until after the response is sent. For example, maybe your | ||
backend is responsible for computing the `computeUnits` on a request and send | ||
the result in the response in the `compute-units` header. | ||
|
||
In Zuplo, you can support these dynamic meters by writing a little code. To make | ||
the `computeUnits` meter dynamic, first update the policy by setting the | ||
`computeUnits` meter to `0` as shown below. | ||
|
||
```json | ||
{ | ||
"export": "MonetizationInboundPolicy", | ||
"module": "$import(@zuplo/runtime)", | ||
"options": { | ||
"allowRequestsOverQuota": false, | ||
"allowedSubscriptionStatuses": ["active", "incomplete"], | ||
"meterOnStatusCodes": "200-399", | ||
"meters": { | ||
"requests": 1, | ||
"computeUnits": 0 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Next you can create a | ||
[custom code outbound policy](/docs/policies/custom-code-outbound) that reads | ||
data from the Response (in this case the `compute-units` header) and sets the | ||
meter programmatically. | ||
|
||
```ts title="/modules/set-compute-units-outbound.ts" | ||
import { | ||
MonetizationInboundPolicy, | ||
ZuploRequest, | ||
ZuploContext, | ||
} from "@zuplo/runtime"; | ||
|
||
export default async function ( | ||
response: Response, | ||
request: ZuploRequest, | ||
context: ZuploContext, | ||
options: any, | ||
policyName: string, | ||
) { | ||
const headerValue = response.headers.get("compute-units"); | ||
let computeUnits; | ||
if (headerValue && typeof headerValue === "string") { | ||
computeUnits = parseInt(headerValue); | ||
} | ||
|
||
// Throw an error if the server doesn't send compute units | ||
// Alternatively, you could have a default value | ||
if (!computeUnits) { | ||
throw new Error("Invalid response, no compute units sent."); | ||
} | ||
|
||
// Set the compute units for the request | ||
MonetizationInboundPolicy.setMeters(context, { | ||
computeUnits, | ||
}); | ||
|
||
return response; | ||
} | ||
``` |
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,54 @@ | ||
--- | ||
title: Stripe Monetization Plugin | ||
sidebar_label: Stripe Plugin | ||
--- | ||
|
||
The Stripe Monetization Plugin makes makes it easy to register a Stripe Webhook | ||
in your Zuplo API that will handle Stripe subscription events. | ||
|
||
When you register the Stripe Plugin a new route is configured on your API at the | ||
path `/__plugins/stripe/webhook`. This route is used to receive webhooks sent by | ||
stripe for Stripe subscription events. | ||
|
||
The Plugin is registered in the `zuplo.runtime.ts` extension. It requires | ||
setting the `webhooks.signingSecret` value and the `stripeSecretKey` in order to | ||
function. | ||
|
||
There is additional configuration if you wan to customize the path, etc, but for | ||
most cases no additional configuration is required. | ||
|
||
```ts | ||
import { | ||
RuntimeExtensions, | ||
StripeMonetizationPlugin, | ||
environment, | ||
} from "@zuplo/runtime"; | ||
|
||
export function runtimeInit(runtime: RuntimeExtensions) { | ||
// Create the Stripe Plugin | ||
const stripe = new StripeMonetizationPlugin({ | ||
webhooks: { | ||
signingSecret: environment.STRIPE_WEBHOOK_SIGNING_SECRET, | ||
}, | ||
stripeSecretKey: environment.STRIPE_SECRET_KEY, | ||
}); | ||
// Register the plugin | ||
runtime.addPlugin(stripe); | ||
} | ||
``` | ||
|
||
## Debugging & Troubleshooting | ||
|
||
The Runtime Plugin emits logs that show what the Webhook is doing. For example, | ||
when a new subscription is created, the plugin will log information about the | ||
Stripe subscription, user, etc. | ||
|
||
If are having trouble with the Webhooks, reviewing the logs for the Plugin is | ||
the place to start. | ||
|
||
Additionally, you can use the Stripe | ||
[Webhook logs](https://dashboard.stripe.com/test/webhooks) in the Stripe | ||
Dashboard to view the webhooks that were send and their status. You can also | ||
resend a webhook event. | ||
|
||
![Stripe Webhooks](../../public/media/stripe-monetization-plugin/image.png) |
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
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 |
---|---|---|
|
@@ -3,4 +3,4 @@ module.exports = { | |
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
} | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,30 +1,30 @@ | ||
import Link from 'next/link' | ||
import clsx from 'clsx' | ||
import Link from "next/link"; | ||
import clsx from "clsx"; | ||
|
||
const variantStyles = { | ||
primary: | ||
'rounded-full bg-pink py-2 px-4 text-sm font-semibold text-gray-900 hover:bg-pink-HOVER focus:outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-pink/50 active:bg-pink-HOVER', | ||
"rounded-full bg-pink py-2 px-4 text-sm font-semibold text-gray-900 hover:bg-pink-HOVER focus:outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-pink/50 active:bg-pink-HOVER", | ||
secondary: | ||
'rounded-full bg-gray-800 py-2 px-4 text-sm font-medium text-white hover:bg-gray-700 focus:outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white/50 active:text-gray-400', | ||
} | ||
"rounded-full bg-gray-800 py-2 px-4 text-sm font-medium text-white hover:bg-gray-700 focus:outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white/50 active:text-gray-400", | ||
}; | ||
|
||
type ButtonProps = { | ||
variant?: keyof typeof variantStyles | ||
variant?: keyof typeof variantStyles; | ||
} & ( | ||
| React.ComponentPropsWithoutRef<typeof Link> | ||
| (React.ComponentPropsWithoutRef<'button'> & { href?: undefined }) | ||
) | ||
| (React.ComponentPropsWithoutRef<"button"> & { href?: undefined }) | ||
); | ||
|
||
export function Button({ | ||
variant = 'primary', | ||
variant = "primary", | ||
className, | ||
...props | ||
}: ButtonProps) { | ||
className = clsx(variantStyles[variant], className) | ||
className = clsx(variantStyles[variant], className); | ||
|
||
return typeof props.href === 'undefined' ? ( | ||
return typeof props.href === "undefined" ? ( | ||
<button className={className} {...props} /> | ||
) : ( | ||
<Link className={className} {...props} /> | ||
) | ||
); | ||
} |
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
Oops, something went wrong.