Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the same url parameters as on the website #413

Merged
merged 2 commits into from
Aug 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ export const useSubmitDonation = () => {
publicTestimony,
giftCardCode
} = useWidgetContext();
const {minDonationAmount, webhookToken, redeemGiftCardInFlow} =
useConfigContext();
const {
minDonationAmount,
webhookToken,
redeemGiftCardInFlow,
designation,
requireShareInfo
} = useConfigContext();

const submitDonation = useCallback(
(event: JSXInternal.TargetedEvent<HTMLFormElement>) => {
Expand All @@ -48,7 +53,9 @@ export const useSubmitDonation = () => {
utmSource: config.utmSource,
privateNote,
publicTestimony,
webhookToken
webhookToken,
designation,
requireShareInfo
};
switch (selectedPaymentMethod) {
case PaymentMethod.CRYPTO:
Expand Down Expand Up @@ -128,7 +135,9 @@ export const useSubmitDonation = () => {
publicTestimony,
giftCardCode,
webhookToken,
redeemGiftCardInFlow
redeemGiftCardInFlow,
designation,
requireShareInfo
]
);

Expand Down
71 changes: 71 additions & 0 deletions packages/donate-button-v4/src/components/widget/types/UrlParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
export enum DonateUrlParameters {
DONATE_NONPROFIT_ID = 'donateTo',
JOIN_DONATION_ID = 'joinTo',
JOIN_DONATION_USER_ID = 'joinUser',
MATCH_CAMPAIGN = 'match',
DONATION_HEADER_TEXT = 'donation_header_text',

REDIRECT_URL = 'redirect_url',
FUNDRAISER_ID = 'fundraiser_id',
MIN_VALUE = 'min_value',
FREQUENCY_OPTIONS = 'frequency_options',
REQUIRE_SHARE_INFO = 'require_share_info',

METHOD = 'method',

AMOUNT = 'amount',
FREQUENCY = 'frequency',
DONOR_FIRST_NAME = 'first_name',
DONOR_LAST_NAME = 'last_name',
DONOR_EMAIL = 'email',
DESCRIPTION = 'description',
MONTHLY_TITLE = 'monthlyTitle',

PARTNER_DONATION_ID = 'partner_donation_id',
PARTNER_WEBHOOK_TOKEN = 'webhook_token',
PARTNER_METADATA = 'partner_metadata',
TO_NONPROFITS = 'to_nonprofits',
TO_NONPROFIT_WEIGHTS = 'to_nonprofit_weights',
TO_NONPROFIT_MATCHINGS = 'to_nonprofit_matchings',
RECURRING_MATCHES = 'recurring_matches',

SHARE_INFO = 'share_info',
NO_EXIT = 'no_exit',

SUCCESS_URL = 'success_url',

EXIT_URL = 'exit_url',

GIFT_CARD_AMOUNT = 'gift_card_amount',
GIFT_CARD_QUANTITY = 'gift_card_quantity',
GIFT_CARD_NONPROFIT_ID = 'gift_card_nonprofit_ID',
GIFT_CARD_TAG_ID = 'gift_card_tag_ID',

THEME_COLOR = 'theme_color',
THEME_COLOR_HIGHLIGHT = 'theme_color_highlight',

CRYPTO_AMOUNT = 'crypto_amount',
CRYPTO_CURRENCY = 'crypto_currency',

STOCK_SYMBOL = 'stock_symbol',
STOCK_AMOUNT = 'stock_amount',

SUGGESTED_AMOUNTS = 'suggestedAmounts',

DESIGNATION = 'designation',

PRIVATE_NOTE = 'private_note',
PUBLIC_TESTIMONY = 'public_testimony',

SEARCH_META = 'search_meta',

GIFT_CARD_CODE = 'gift_card_code'
}

export enum UTM_QUERY_PARAM {
utm_source = 'utm_source',
utm_medium = 'utm_medium',
utm_campaign = 'utm_campaign',
utm_term = 'utm_term',
utm_content = 'utm_content'
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ export interface WidgetConfig {

redeemGiftCardInFlow?: boolean;

designation?: string;

requireShareInfo?: boolean;

previewMode?: boolean;
}
44 changes: 28 additions & 16 deletions packages/donate-button-v4/src/helpers/constructDonateUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {
AvailablePaymentMethods,
PaymentMethod
} from 'src/components/widget/types/PaymentMethod';
import {
DonateUrlParameters,
UTM_QUERY_PARAM
} from 'src/components/widget/types/UrlParams';
import {BASE_URL, GIFT_CARD_URL} from 'src/constants/url';

const UTM_MEDIUM = 'donate-button-0.4'; // Update this if the major version changes
Expand All @@ -19,6 +23,8 @@ interface BaseUrlParams {
publicTestimony?: string;
utmSource?: string;
webhookToken?: string;
designation?: string;
requireShareInfo?: boolean;
}

interface DonateUrlParams extends BaseUrlParams {
Expand Down Expand Up @@ -70,7 +76,9 @@ function getBaseParams({
privateNote,
publicTestimony,
utmSource,
webhookToken
webhookToken,
designation,
requireShareInfo
}: Pick<
BaseUrlParams,
| 'nonprofitSlug'
Expand All @@ -80,16 +88,20 @@ function getBaseParams({
| 'publicTestimony'
| 'utmSource'
| 'webhookToken'
| 'designation'
| 'requireShareInfo'
>) {
return {
method: methods?.join(','),
utm_campaign: 'donate-button',
utm_source: utmSource ?? nonprofitSlug,
utm_medium: UTM_MEDIUM,
no_exit: noExit ?? 1,
private_note: privateNote,
public_testimony: publicTestimony,
webhook_token: webhookToken
[DonateUrlParameters.METHOD]: methods?.join(','),
[DonateUrlParameters.NO_EXIT]: noExit ?? 1,
[DonateUrlParameters.PRIVATE_NOTE]: privateNote,
[DonateUrlParameters.PUBLIC_TESTIMONY]: publicTestimony,
[DonateUrlParameters.PARTNER_WEBHOOK_TOKEN]: webhookToken,
[DonateUrlParameters.DESIGNATION]: designation,
[DonateUrlParameters.REQUIRE_SHARE_INFO]: requireShareInfo,
[UTM_QUERY_PARAM.utm_campaign]: 'donate-button',
[UTM_QUERY_PARAM.utm_source]: utmSource ?? nonprofitSlug,
[UTM_QUERY_PARAM.utm_medium]: UTM_MEDIUM
};
}

Expand All @@ -114,8 +126,8 @@ export function constructDonateUrl({
const params = getBaseParams(rest);

const parameters = serializeParams({
frequency,
amount,
[DonateUrlParameters.FREQUENCY]: frequency,
[DonateUrlParameters.AMOUNT]: amount,
...params
});

Expand All @@ -131,8 +143,8 @@ export function constructDonateStocksUrl({
const params = getBaseParams(rest);

const parameters = serializeParams({
stock_amount: stockAmount,
stock_symbol: stockSymbol,
[DonateUrlParameters.STOCK_AMOUNT]: stockAmount,
[DonateUrlParameters.STOCK_SYMBOL]: stockSymbol,
...params
});

Expand All @@ -148,8 +160,8 @@ export function constructDonateCryptoUrl({
const params = getBaseParams(rest);

const parameters = serializeParams({
crypto_amount: cryptoAmount,
crypto_currency: cryptoCurrency,
[DonateUrlParameters.CRYPTO_AMOUNT]: cryptoAmount,
[DonateUrlParameters.CRYPTO_CURRENCY]: cryptoCurrency,
...params
});

Expand All @@ -167,7 +179,7 @@ export function constructGiftCardUrl({

const parameters = serializeParams({
...params,
gift_card_code: giftCardCode
[DonateUrlParameters.GIFT_CARD_CODE]: giftCardCode
});

return `${baseUrl}?${parameters}#/${HASH}`;
Expand Down
44 changes: 32 additions & 12 deletions packages/donate-button-v4/src/helpers/parseDonateUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import {
AvailablePaymentMethods,
PaymentMethod
} from 'src/components/widget/types/PaymentMethod';
import {
DonateUrlParameters,
UTM_QUERY_PARAM
} from 'src/components/widget/types/UrlParams';
import {WidgetConfig} from 'src/components/widget/types/WidgetConfig';

const MAX_AMOUNT_SUGGESTIONS = 5;

enum DonateUrlParameters {
METHOD = 'method',
FREQUENCY = 'frequency',
MONTHLY_TITLE = 'monthlyTitle',
SUGGESTED_AMOUNTS = 'suggestedAmounts',
AMOUNT = 'amount',
UTM_SOURCE = 'utm_source',
/**
* Just for backwards compatibility
* @deprecated
*/
enum OLD_DonateUrlParameters {
MIN_AMOUNT = 'minAmount',
THEME_COLOR = 'themeColor'
}
Expand Down Expand Up @@ -52,6 +54,11 @@ function intFromString(string?: string | null) {
return Number.isNaN(number) ? undefined : number;
}

function booleanFromString(string?: string | null) {
if (!string) return;
return string === 'true';
}

function removeEmptyValues<T extends Record<string, unknown>>(object: T): T {
return Object.fromEntries(
Object.entries(object).filter(([, value]) => value !== undefined)
Expand Down Expand Up @@ -83,12 +90,23 @@ export function parseDonateUrl(
searchParameters.get(DonateUrlParameters.SUGGESTED_AMOUNTS)
);
const utmSource =
searchParameters.get(DonateUrlParameters.UTM_SOURCE) ?? undefined;
const minAmount = intFromString(
searchParameters.get(DonateUrlParameters.MIN_AMOUNT)
searchParameters.get(UTM_QUERY_PARAM.utm_source) ?? undefined;

const minAmount =
intFromString(searchParameters.get(DonateUrlParameters.MIN_VALUE)) ??
intFromString(searchParameters.get(OLD_DonateUrlParameters.MIN_AMOUNT));

const designation =
searchParameters.get(DonateUrlParameters.DESIGNATION) ?? undefined;

const requireShareInfo = booleanFromString(
searchParameters.get(DonateUrlParameters.REQUIRE_SHARE_INFO)
);

const primaryColorFromUrl =
searchParameters.get(DonateUrlParameters.THEME_COLOR) ?? undefined;
searchParameters.get(DonateUrlParameters.THEME_COLOR) ??
searchParameters.get(OLD_DonateUrlParameters.THEME_COLOR) ??
undefined;
const primaryColor = primaryColorFromUrl
? primaryColorFromUrl.startsWith('#')
? primaryColorFromUrl
Expand All @@ -111,6 +129,8 @@ export function parseDonateUrl(
amount,
defaultDonationAmount: amount,
minDonationAmount: minAmount,
primaryColor
primaryColor,
designation,
requireShareInfo
});
}
Loading