Skip to content

Commit 6c60088

Browse files
apphelper: start donation forms on Recurring from ?recurring=1 giving links (#155)
Guest and member donation forms open on Recurring when the giving link has ?recurring=1; the one-time/recurring choice stays visible so the donor can switch before paying.
1 parent 0300efa commit 6c60088

8 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@churchapps/apphelper": minor
3+
---
4+
Donations: a giving link with `?recurring=1` opens the guest and member donation forms with Recurring selected. The donor still sees both options and can switch to a one-time gift before paying. Adds `DonationHelper.isRecurringLink` / `isRecurringLinkValue`.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { readFileSync } from "node:fs";
2+
import { dirname, join } from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import { DonationHelper } from "../helpers/DonationHelper";
6+
7+
const dir = dirname(fileURLToPath(import.meta.url));
8+
const read = (rel: string) => readFileSync(join(dir, rel), "utf8");
9+
10+
const guestForms = [
11+
"../providers/stripe/NonAuthDonationInner.tsx",
12+
"../providers/paypal/PayPalNonAuthDonationInner.tsx",
13+
"../providers/paystack/PaystackNonAuthDonationInner.tsx",
14+
"../providers/kingdomfunding/KingdomFundingNonAuthDonationInner.tsx"
15+
];
16+
17+
describe("giving link ?recurring param", () => {
18+
afterEach(() => { window.history.replaceState(null, "", "/"); });
19+
20+
it("treats 1, true and yes as a recurring link", () => {
21+
for (const v of ["1", "true", "TRUE", " yes "]) expect(DonationHelper.isRecurringLinkValue(v), v).toBe(true);
22+
});
23+
24+
it("defaults to a one-time gift when the param is missing or anything else", () => {
25+
for (const v of [null, undefined, "", "0", "false", "no", "monthly"]) expect(DonationHelper.isRecurringLinkValue(v as any), String(v)).toBe(false);
26+
});
27+
28+
it("reads the param from a query string", () => {
29+
expect(DonationHelper.isRecurringLink("?fundId=abc&amount=25&recurring=1")).toBe(true);
30+
expect(DonationHelper.isRecurringLink("?fundId=abc&amount=25")).toBe(false);
31+
expect(DonationHelper.isRecurringLink("")).toBe(false);
32+
});
33+
34+
it("falls back to the page URL when no query string is passed", () => {
35+
window.history.replaceState(null, "", "/donate?recurring=1");
36+
expect(DonationHelper.isRecurringLink()).toBe(true);
37+
window.history.replaceState(null, "", "/donate");
38+
expect(DonationHelper.isRecurringLink()).toBe(false);
39+
});
40+
41+
it("every guest form starts on Recurring from the link but keeps one-time as the default", () => {
42+
for (const rel of guestForms) {
43+
const src = read(rel);
44+
expect(src, rel).toMatch(/DonationHelper\.isRecurringLinkValue\(getUrlParam\("recurring"\)\)\) setDonationType\("recurring"\)/);
45+
// The default stays one-time unless the church only allows recurring gifts.
46+
expect(src, rel).not.toMatch(/useState<"once" \| "recurring">\(\s*"recurring"\s*\)/);
47+
// The donor can still switch back before paying.
48+
expect(src, rel).toContain('onClick={() => setDonationType("once")}');
49+
}
50+
});
51+
52+
it("only applies the link where the one-time/recurring choice is shown", () => {
53+
expect(read(guestForms[0])).toContain('if (paymentType !== "bank" && allowSingleGift && allowRecurring && DonationHelper.isRecurringLinkValue');
54+
expect(read(guestForms[1])).toContain("if (allowSingleGift && allowRecurring && DonationHelper.isRecurringLinkValue");
55+
expect(read(guestForms[2])).toContain("if (props.allowRecurring !== false && DonationHelper.isRecurringLinkValue");
56+
});
57+
58+
it("the member form starts on Recurring from the link and keeps both buttons", () => {
59+
const src = read("../components/MultiGatewayDonationForm.tsx");
60+
expect(src).toContain('useState<string | undefined>("once")');
61+
expect(src).toMatch(/if \(DonationHelper\.isRecurringLink\(\)\) setDonationType\("recurring"\)/);
62+
expect(src).toContain('id="single-donation-button"');
63+
expect(src).toContain('id="recurring-donation-button"');
64+
});
65+
});

‎apphelper/src/donations/components/MultiGatewayDonationForm.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ const MultiGatewayDonationInner: React.FC<Props> = (props) => {
363363
}, 500);
364364
}, [donation, funds, gateway, selectedGatewayObj?.id, selectedGateway, getTransactionFee]);
365365

366+
// ?recurring=1 on a giving link starts on Recurring; the donor still sees both buttons and can switch.
367+
useEffect(() => {
368+
if (DonationHelper.isRecurringLink()) setDonationType("recurring");
369+
}, []);
370+
366371
useEffect(() => {
367372
loadFunds();
368373
}, [loadFunds]);

‎apphelper/src/donations/helpers/DonationHelper.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,16 @@ export class DonationHelper {
3535
return gateways.find(g => this.isProvider(g.provider, provider));
3636
}
3737

38+
// Giving links can carry ?recurring=1 so the form opens with Recurring selected; the donor can still switch to one-time.
39+
static isRecurringLinkValue(value: string | null | undefined): boolean {
40+
const v = (value || "").trim().toLowerCase();
41+
return v === "1" || v === "true" || v === "yes";
42+
}
43+
44+
static isRecurringLink(search?: string): boolean {
45+
const query = search ?? (typeof window !== "undefined" ? window.location.search : "");
46+
if (!query) return false;
47+
return this.isRecurringLinkValue(new URLSearchParams(query).get("recurring"));
48+
}
49+
3850
}

‎apphelper/src/donations/providers/kingdomfunding/KingdomFundingNonAuthDonationInner.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export const KingdomFundingNonAuthDonationInner: React.FC<Props> = ({ mainContai
6565
const fundId = getUrlParam("fundId");
6666
const amount = getUrlParam("amount");
6767
setSearchParams({ fundId, amount });
68+
// ?recurring=1 on a giving link starts on Recurring; the toggle stays visible so the donor can switch back.
69+
if (DonationHelper.isRecurringLinkValue(getUrlParam("recurring"))) setDonationType("recurring");
6870

6971
ApiHelper.get("/funds/churchId/" + props.churchId, "GivingApi").then((data: any) => {
7072
setFunds(data);

‎apphelper/src/donations/providers/paypal/PayPalNonAuthDonationInner.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export const PayPalNonAuthDonationInner: React.FC<Props> = ({ mainContainerCssPr
7373
const fundId = getUrlParam("fundId");
7474
const amount = getUrlParam("amount");
7575
setSearchParams({ fundId, amount });
76+
// ?recurring=1 on a giving link starts on Recurring; the toggle stays visible so the donor can switch back.
77+
if (allowSingleGift && allowRecurring && DonationHelper.isRecurringLinkValue(getUrlParam("recurring"))) setDonationType("recurring");
7678

7779
ApiHelper.get("/funds/churchId/" + props.churchId, "GivingApi").then((data: any) => {
7880
const filteredFunds: FundInterface[] = allowedFundIds.length > 0

‎apphelper/src/donations/providers/paystack/PaystackNonAuthDonationInner.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export const PaystackNonAuthDonationInner: React.FC<Props> = ({ mainContainerCss
6363
const fundId = props.defaultFundId || getUrlParam("fundId");
6464
const amount = getUrlParam("amount");
6565
setSearchParams({ fundId, amount });
66+
// ?recurring=1 on a giving link starts on Recurring; the toggle stays visible so the donor can switch back.
67+
if (props.allowRecurring !== false && DonationHelper.isRecurringLinkValue(getUrlParam("recurring"))) setDonationType("recurring");
6668

6769
ApiHelper.get("/funds/churchId/" + props.churchId, "GivingApi").then((data: any) => {
6870
const list: FundInterface[] = props.allowedFundIds?.length ? data.filter((f: FundInterface) => props.allowedFundIds!.includes(f.id!)) : data;

‎apphelper/src/donations/providers/stripe/NonAuthDonationInner.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export const NonAuthDonationInner: React.FC<Props> = ({ mainContainerCssProps, s
7878
const fundId = getUrlParam("fundId");
7979
const amount = getUrlParam("amount");
8080
setSearchParams({ fundId, amount });
81+
// ?recurring=1 on a giving link starts on Recurring; the toggle stays visible so the donor can switch back.
82+
if (paymentType !== "bank" && allowSingleGift && allowRecurring && DonationHelper.isRecurringLinkValue(getUrlParam("recurring"))) setDonationType("recurring");
8183

8284
ApiHelper.get("/funds/churchId/" + props.churchId, "GivingApi").then((data: any) => {
8385
const filteredFunds: FundInterface[] = allowedFundIds.length > 0

0 commit comments

Comments
 (0)