Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/dist/donate-button/embed.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 39 additions & 1 deletion packages/donate-embed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,48 @@ Options for `everyWidget.create`:
to, or the element itself. Pass an element when the container is not
reachable from `document`, for example inside a web component's shadow root.
A malformed selector string throws, just like `document.querySelector`.
- `partnerSlug` (required): the partner slug every.org assigned to you.
- `partnerSlug`: the partner slug every.org assigned to you. One of
`partnerSlug` or `partnerId` is required.
- `partnerId`: the partner id every.org assigned to you. An alternative to
`partnerSlug`; if both are given, `partnerId` takes precedence.
- `nonprofitSlug` (required): the every.org slug of the nonprofit to donate to.
- `onSuccess` (optional): called when a donation completes.

The form also takes the customizations of an every.org
[donate link](https://docs.every.org/docs/donate-link). All are optional:

| Option | Donate link parameter | Description |
| ------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `amount` | `amount` | Donation amount the form starts with. |
| `suggestedAmounts` | `suggestedAmounts` | Array of up to five suggested amounts, shown as buttons below the amount field. |
| `frequency` | `frequency` | Donation frequency the form starts with: `'ONCE'` or `'MONTHLY'`. |
| `email` | `email` | Donor's email. |
| `firstName` | `first_name` | Donor's first name. |
| `lastName` | `last_name` | Donor's last name. |
| `description` | `description` | Replaces the text on the nonprofit's card in the form. |
| `partnerDonationId` | `partner_donation_id` | A unique id of yours to associate with the donation. |
| `partnerMetadata` | `partner_metadata` | A base64 encoded JSON object that is forwarded to you in the partner webhook notification. |
| `requireShareInfo` | `require_share_info` | `true` requires the donor to share their contact info with the nonprofit. |
| `shareInfo` | `share_info` | Whether sharing contact info is checked by default. |
| `designation` | `designation` | Designation for the donation (a recommendation to the nonprofit, not a legal restriction). |
| `webhookToken` | `webhook_token` | Token of your partner webhook; it is notified of every donation completed through the form. |
| `themeColor` | `theme_color` | Primary theme color of the form as a hex color, with or without the leading `#`: for example `'#009BF8'`. |
| `method` | `method` | Allowed donation methods, as an array or a comma separated string: `card`, `bank`, `paypal`, `venmo`, `pay`, `crypto`, `stocks`, `daf` and `ira`. |
| `code` | `code` | Code of a custom donate link created by the nonprofit; the form uses that link's settings. Options given here override them. |

```js
everyWidget.create({
selector: '#donate-widget',
partnerSlug: 'your-partner-slug',
nonprofitSlug: 'nonprofit-slug',
amount: 25,
suggestedAmounts: [10, 25, 50, 100],
frequency: 'MONTHLY',
method: ['card', 'bank', 'paypal'],
themeColor: '#009BF8'
});
```

`create` returns the `<iframe>` element, or `null` if the options are invalid.

Inside a web component:
Expand Down
144 changes: 137 additions & 7 deletions packages/donate-embed/src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@
*
* selector CSS selector of the element the donate form is added to, or
* the element itself (for example inside a shadow root).
* partnerSlug The partner slug every.org assigned to you.
* partnerSlug The partner slug every.org assigned to you. One of
* partnerSlug or partnerId is required.
* partnerId The partner id every.org assigned to you; an alternative to
* partnerSlug (takes precedence if both are given).
* nonprofitSlug The every.org slug of the nonprofit to donate to.
* onSuccess Called when a donation completes.
*
* The form also takes the same customizations as an every.org donate link
* (https://docs.every.org/docs/donate-link), as camelCase options: amount,
* suggestedAmounts, frequency, email, firstName, lastName,
* description, partnerDonationId, partnerMetadata, requireShareInfo,
* shareInfo, designation, webhookToken, themeColor, method and code. See
* EveryWidgetOptions below.
*
* The donate form renders in an iframe that fills the width of its
* container and resizes itself to fit its content.
*/
Expand All @@ -27,12 +37,72 @@ interface EveryWidgetOptions {
* `document` (for example inside a web component's shadow root).
*/
selector: string | Element;
/** The partner slug every.org assigned to you. */
partnerSlug: string;
/**
* The partner slug every.org assigned to you. One of partnerSlug or
* partnerId is required.
*/
partnerSlug?: string;
/**
* The partner id every.org assigned to you; an alternative to partnerSlug
* and takes precedence over it.
*/
partnerId?: string;
/** The every.org slug of the nonprofit to donate to. */
nonprofitSlug: string;
/** Called when a donation completes. */
onSuccess?: () => void;

/** Donation amount the form starts with. */
amount?: number;
/** Up to five suggested amounts shown as buttons below the amount field. */
suggestedAmounts?: number[];
/** Donation frequency the form starts with. */
frequency?: 'ONCE' | 'MONTHLY';
/** Donor's email. */
email?: string;
/** Donor's first name. */
firstName?: string;
/** Donor's last name. */
lastName?: string;
/** Replaces the text on the nonprofit's card in the form. */
description?: string;
/** A unique id of yours to associate with the donation. */
partnerDonationId?: string;
/**
* A base64 encoded JSON object that is forwarded to you in the partner
* webhook notification.
*/
partnerMetadata?: string;
/** Require the donor to share their contact info with the nonprofit. */
requireShareInfo?: boolean;
/** Whether sharing contact info is checked by default. */
shareInfo?: boolean;
/**
* Designation for the donation. Every.org grants are unrestricted, so this
* is a recommendation to the nonprofit, not a legal restriction.
*/
designation?: string;
/**
* Token of your partner webhook; a notification is sent to it for every
* donation completed through the form.
*/
webhookToken?: string;
/**
* Primary theme color of the form as a 3 or 6 digit hex color, with or
* without the leading '#': for example '#009BF8'.
*/
themeColor?: string;
/**
* Allowed donation methods: card, bank, paypal, venmo, pay (mobile
* payments), crypto, stocks, daf and ira. Either an array or a comma
* separated string.
*/
method?: string | string[];
/**
* Code of a custom donate link created by the nonprofit; the form uses
* that link's settings. Options given here override them.
*/
code?: string;
}

interface EveryWidget {
Expand Down Expand Up @@ -69,6 +139,63 @@ interface Window {
return selector instanceof Element ? selector : null;
}

/**
* Options that are passed to the form as donate link URL parameters
* (https://docs.every.org/docs/donate-link), keyed by option name. The
* donate link's redirect parameters (no_exit, success_url, exit_url) are
* left out on purpose: the form lives in the page, so there is nowhere to
* exit or redirect to. Use onSuccess instead.
*/
var URL_PARAMS: {[K in keyof EveryWidgetOptions]?: string} = {
partnerId: 'partnerId',
partnerSlug: 'partnerSlug',
amount: 'amount',
suggestedAmounts: 'suggestedAmounts',
frequency: 'frequency',
email: 'email',
firstName: 'first_name',
lastName: 'last_name',
description: 'description',
partnerDonationId: 'partner_donation_id',
partnerMetadata: 'partner_metadata',
requireShareInfo: 'require_share_info',
shareInfo: 'share_info',
designation: 'designation',
webhookToken: 'webhook_token',
themeColor: 'theme_color',
method: 'method',
code: 'code'
};

function buildQuery(options: EveryWidgetOptions): string {
var params: string[] = [];
var name: keyof EveryWidgetOptions;
for (name in URL_PARAMS) {
var value: unknown = options[name];
// Skip unset options; an empty value would override the form's default.
if (value === undefined || value === null || value === '') {
continue;
}

if (Array.isArray(value)) {
if (value.length === 0) {
continue;
}

value = value.join(',');
}

// The form expects the color as bare hex and ignores it otherwise.
if (name === 'themeColor') {
value = String(value).replace(/^#/, '');
}

params.push(URL_PARAMS[name] + '=' + encodeURIComponent(String(value)));
}

return params.join('&');
}

function isEmbedMessage(data: unknown): data is EmbedMessage {
return (
typeof data === 'object' &&
Expand All @@ -84,9 +211,12 @@ interface Window {
return null;
}

if (!options.partnerSlug || !options.nonprofitSlug) {
if (
(!options.partnerSlug && !options.partnerId) ||
!options.nonprofitSlug
) {
console.error(
'everyWidget: partnerSlug and nonprofitSlug are required',
'everyWidget: nonprofitSlug and one of partnerId or partnerSlug are required',
options
);
return null;
Expand All @@ -97,8 +227,8 @@ interface Window {
EMBED_ORIGIN +
'/embed/' +
encodeURIComponent(options.nonprofitSlug) +
'/donate?partnerSlug=' +
encodeURIComponent(options.partnerSlug);
'/donate?' +
buildQuery(options);
iframe.title = 'Donate';
// The form resizes itself to fit its content, so it never scrolls.
iframe.setAttribute('scrolling', 'no');
Expand Down
Loading