better-auth-paystack 3.4.2 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -1
- package/dist/client.d.mts +3 -1
- package/dist/client.d.mts.map +1 -1
- package/dist/client.mjs +1 -1
- package/dist/client.mjs.map +1 -1
- package/dist/{index-C9c7-9sB.d.mts → index-Bt1AmLyW.d.mts} +14 -2
- package/dist/index-Bt1AmLyW.d.mts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +260 -101
- package/dist/index.mjs.map +1 -1
- package/dist/{version-BJvYyq-a.mjs → version-BEzR5d9x.mjs} +2 -2
- package/dist/{version-BJvYyq-a.mjs.map → version-BEzR5d9x.mjs.map} +1 -1
- package/package.json +2 -2
- package/skills/better-auth-paystack-setup/SKILL.md +1 -1
- package/skills/paystack-billing-flows/SKILL.md +1 -1
- package/skills/paystack-catalog-limits/SKILL.md +1 -1
- package/skills/paystack-client-api/SKILL.md +1 -1
- package/skills/paystack-local-subscriptions/SKILL.md +1 -1
- package/skills/paystack-organization-billing/SKILL.md +1 -1
- package/skills/paystack-schema-migrations/SKILL.md +1 -1
- package/skills/paystack-tanstack-start/SKILL.md +1 -1
- package/skills/paystack-testing-fixtures/SKILL.md +1 -1
- package/skills/paystack-webhooks-events/SKILL.md +1 -1
- package/dist/index-C9c7-9sB.d.mts.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as getMetadataBoolean, c as hasPaystackMetadata, i as createRenewalMetadata, l as parsePaystackMetadata, n as createCheckoutMetadata, o as getMetadataNumber, r as createProrationMetadata, s as getMetadataString, t as PACKAGE_VERSION, u as stringifyPaystackMetadata } from "./version-
|
|
1
|
+
import { a as getMetadataBoolean, c as hasPaystackMetadata, i as createRenewalMetadata, l as parsePaystackMetadata, n as createCheckoutMetadata, o as getMetadataNumber, r as createProrationMetadata, s as getMetadataString, t as PACKAGE_VERSION, u as stringifyPaystackMetadata } from "./version-BEzR5d9x.mjs";
|
|
2
2
|
import { HIDE_METADATA, defineErrorCodes } from "better-auth";
|
|
3
3
|
import { APIError, createAuthEndpoint, createAuthMiddleware, getSessionFromCtx, originCheckMiddleware, sessionMiddleware } from "better-auth/api";
|
|
4
4
|
import { defu } from "defu";
|
|
@@ -662,75 +662,6 @@ const checkTeamLimit = async (ctx, organizationId, maxTeams) => {
|
|
|
662
662
|
return true;
|
|
663
663
|
};
|
|
664
664
|
//#endregion
|
|
665
|
-
//#region src/reference-access.ts
|
|
666
|
-
const DEFAULT_BILLING_ORG_ROLES = ["owner", "admin"];
|
|
667
|
-
function normalizeBillingRoles(roles) {
|
|
668
|
-
return new Set(roles.map((value) => value.trim()).filter((value) => value !== ""));
|
|
669
|
-
}
|
|
670
|
-
function getBillingRoles(options) {
|
|
671
|
-
return options.organization?.billingRoles ?? DEFAULT_BILLING_ORG_ROLES;
|
|
672
|
-
}
|
|
673
|
-
function hasBillingRole(role, billingRoles = DEFAULT_BILLING_ORG_ROLES) {
|
|
674
|
-
const allowedRoles = normalizeBillingRoles(billingRoles);
|
|
675
|
-
if (Array.isArray(role)) return role.some((value) => hasBillingRole(value, billingRoles));
|
|
676
|
-
if (typeof role !== "string") return false;
|
|
677
|
-
return role.split(",").map((value) => value.trim()).some((value) => allowedRoles.has(value));
|
|
678
|
-
}
|
|
679
|
-
function resolveBillingReferenceId(input) {
|
|
680
|
-
const body = input.body ?? {};
|
|
681
|
-
const query = input.query ?? {};
|
|
682
|
-
const requestQueryReferenceId = typeof input.requestUrl === "string" ? new URL(input.requestUrl).searchParams.get("referenceId") ?? void 0 : void 0;
|
|
683
|
-
return body.referenceId ?? query.referenceId ?? requestQueryReferenceId ?? input.fallbackUserId;
|
|
684
|
-
}
|
|
685
|
-
async function authorizeBillingReference(ctx, options, data) {
|
|
686
|
-
if (data.referenceId === data.user.id) return;
|
|
687
|
-
if (options.subscription?.enabled === true && typeof options.subscription.authorizeReference === "function") {
|
|
688
|
-
if (await options.subscription.authorizeReference({
|
|
689
|
-
user: data.user,
|
|
690
|
-
session: data.session,
|
|
691
|
-
referenceId: data.referenceId,
|
|
692
|
-
action: data.action
|
|
693
|
-
}, ctx) === true) return;
|
|
694
|
-
throw new APIError("UNAUTHORIZED");
|
|
695
|
-
}
|
|
696
|
-
if (options.organization?.enabled === true) {
|
|
697
|
-
const member = await ctx.context.adapter.findOne({
|
|
698
|
-
model: "member",
|
|
699
|
-
where: [{
|
|
700
|
-
field: "userId",
|
|
701
|
-
value: data.user.id
|
|
702
|
-
}, {
|
|
703
|
-
field: "organizationId",
|
|
704
|
-
value: data.referenceId
|
|
705
|
-
}]
|
|
706
|
-
});
|
|
707
|
-
if (member !== null && member !== void 0 && hasBillingRole(member.role, getBillingRoles(options))) return;
|
|
708
|
-
}
|
|
709
|
-
throw new APIError("UNAUTHORIZED");
|
|
710
|
-
}
|
|
711
|
-
//#endregion
|
|
712
|
-
//#region src/middleware.ts
|
|
713
|
-
const referenceMiddleware = (options, action) => createAuthMiddleware(async (ctx) => {
|
|
714
|
-
const session = ctx.context.session;
|
|
715
|
-
if (session === null || session === void 0) throw new APIError("UNAUTHORIZED");
|
|
716
|
-
const referenceId = resolveBillingReferenceId({
|
|
717
|
-
body: ctx.body,
|
|
718
|
-
query: ctx.query,
|
|
719
|
-
requestUrl: ctx.request?.url,
|
|
720
|
-
fallbackUserId: session.user.id
|
|
721
|
-
});
|
|
722
|
-
await authorizeBillingReference(ctx, options, {
|
|
723
|
-
user: session.user,
|
|
724
|
-
session: session.session,
|
|
725
|
-
referenceId,
|
|
726
|
-
action
|
|
727
|
-
});
|
|
728
|
-
return { context: {
|
|
729
|
-
...ctx.context,
|
|
730
|
-
referenceId
|
|
731
|
-
} };
|
|
732
|
-
});
|
|
733
|
-
//#endregion
|
|
734
665
|
//#region src/credential-crypto.ts
|
|
735
666
|
const ALGORITHM = "aes-256-gcm";
|
|
736
667
|
const VERSION = "v1";
|
|
@@ -835,6 +766,53 @@ async function savePaystackPaymentCredentials(adapter, options, subscriptionId,
|
|
|
835
766
|
});
|
|
836
767
|
}
|
|
837
768
|
//#endregion
|
|
769
|
+
//#region src/reference-access.ts
|
|
770
|
+
const DEFAULT_BILLING_ORG_ROLES = ["owner", "admin"];
|
|
771
|
+
function normalizeBillingRoles(roles) {
|
|
772
|
+
return new Set(roles.map((value) => value.trim()).filter((value) => value !== ""));
|
|
773
|
+
}
|
|
774
|
+
function getBillingRoles(options) {
|
|
775
|
+
return options.organization?.billingRoles ?? DEFAULT_BILLING_ORG_ROLES;
|
|
776
|
+
}
|
|
777
|
+
function hasBillingRole(role, billingRoles = DEFAULT_BILLING_ORG_ROLES) {
|
|
778
|
+
const allowedRoles = normalizeBillingRoles(billingRoles);
|
|
779
|
+
if (Array.isArray(role)) return role.some((value) => hasBillingRole(value, billingRoles));
|
|
780
|
+
if (typeof role !== "string") return false;
|
|
781
|
+
return role.split(",").map((value) => value.trim()).some((value) => allowedRoles.has(value));
|
|
782
|
+
}
|
|
783
|
+
function resolveBillingReferenceId(input) {
|
|
784
|
+
const body = input.body ?? {};
|
|
785
|
+
const query = input.query ?? {};
|
|
786
|
+
const requestQueryReferenceId = typeof input.requestUrl === "string" ? new URL(input.requestUrl).searchParams.get("referenceId") ?? void 0 : void 0;
|
|
787
|
+
return body.referenceId ?? query.referenceId ?? requestQueryReferenceId ?? input.fallbackUserId;
|
|
788
|
+
}
|
|
789
|
+
async function authorizeBillingReference(ctx, options, data) {
|
|
790
|
+
if (data.referenceId === data.user.id) return;
|
|
791
|
+
if (options.subscription?.enabled === true && typeof options.subscription.authorizeReference === "function") {
|
|
792
|
+
if (await options.subscription.authorizeReference({
|
|
793
|
+
user: data.user,
|
|
794
|
+
session: data.session,
|
|
795
|
+
referenceId: data.referenceId,
|
|
796
|
+
action: data.action
|
|
797
|
+
}, ctx) === true) return;
|
|
798
|
+
throw new APIError("UNAUTHORIZED");
|
|
799
|
+
}
|
|
800
|
+
if (options.organization?.enabled === true) {
|
|
801
|
+
const member = await ctx.context.adapter.findOne({
|
|
802
|
+
model: "member",
|
|
803
|
+
where: [{
|
|
804
|
+
field: "userId",
|
|
805
|
+
value: data.user.id
|
|
806
|
+
}, {
|
|
807
|
+
field: "organizationId",
|
|
808
|
+
value: data.referenceId
|
|
809
|
+
}]
|
|
810
|
+
});
|
|
811
|
+
if (member !== null && member !== void 0 && hasBillingRole(member.role, getBillingRoles(options))) return;
|
|
812
|
+
}
|
|
813
|
+
throw new APIError("UNAUTHORIZED");
|
|
814
|
+
}
|
|
815
|
+
//#endregion
|
|
838
816
|
//#region src/reconciliation.ts
|
|
839
817
|
function getAllowedSubscriptionChannels$1(options) {
|
|
840
818
|
const channels = options.subscription?.allowedPaymentChannels;
|
|
@@ -1162,6 +1140,49 @@ async function reconcilePaystackTransaction(ctx, options, input) {
|
|
|
1162
1140
|
};
|
|
1163
1141
|
}
|
|
1164
1142
|
//#endregion
|
|
1143
|
+
//#region src/reference-middleware.ts
|
|
1144
|
+
const referenceMiddleware = (options, action) => createAuthMiddleware(async (ctx) => {
|
|
1145
|
+
const session = ctx.context.session;
|
|
1146
|
+
if (session === null || session === void 0) throw new APIError("UNAUTHORIZED");
|
|
1147
|
+
const referenceId = resolveBillingReferenceId({
|
|
1148
|
+
body: ctx.body,
|
|
1149
|
+
query: ctx.query,
|
|
1150
|
+
requestUrl: ctx.request?.url,
|
|
1151
|
+
fallbackUserId: session.user.id
|
|
1152
|
+
});
|
|
1153
|
+
await authorizeBillingReference(ctx, options, {
|
|
1154
|
+
user: session.user,
|
|
1155
|
+
session: session.session,
|
|
1156
|
+
referenceId,
|
|
1157
|
+
action
|
|
1158
|
+
});
|
|
1159
|
+
return { context: {
|
|
1160
|
+
...ctx.context,
|
|
1161
|
+
referenceId
|
|
1162
|
+
} };
|
|
1163
|
+
});
|
|
1164
|
+
//#endregion
|
|
1165
|
+
//#region src/route-modules/callback-url.ts
|
|
1166
|
+
/** Accept only absolute HTTP(S) URLs or root-relative paths on the Better Auth origin. */
|
|
1167
|
+
function hasControlCharacters(value) {
|
|
1168
|
+
for (const character of value) {
|
|
1169
|
+
const codePoint = character.codePointAt(0);
|
|
1170
|
+
if (codePoint !== void 0 && (codePoint <= 31 || codePoint >= 127 && codePoint <= 159)) return true;
|
|
1171
|
+
}
|
|
1172
|
+
return false;
|
|
1173
|
+
}
|
|
1174
|
+
function isTrustedCallbackURL(callbackURL, baseURL) {
|
|
1175
|
+
if (callbackURL.startsWith("//") || callbackURL.includes("\\") || hasControlCharacters(callbackURL)) return false;
|
|
1176
|
+
try {
|
|
1177
|
+
const base = new URL(baseURL);
|
|
1178
|
+
if (!/^https?:\/\//i.test(callbackURL) && !callbackURL.startsWith("/")) return false;
|
|
1179
|
+
const callback = new URL(callbackURL, base);
|
|
1180
|
+
return (callback.protocol === "http:" || callback.protocol === "https:") && callback.origin === base.origin && callback.username === "" && callback.password === "";
|
|
1181
|
+
} catch {
|
|
1182
|
+
return false;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
//#endregion
|
|
1165
1186
|
//#region src/route-modules/catalog.ts
|
|
1166
1187
|
async function listStoredProducts(ctx) {
|
|
1167
1188
|
return createBillingStore(ctx).listProducts();
|
|
@@ -1190,7 +1211,8 @@ const initializeTransactionBodySchema = z.object({
|
|
|
1190
1211
|
quantity: z.number().int().positive().optional(),
|
|
1191
1212
|
scheduleAtPeriodEnd: z.boolean().optional(),
|
|
1192
1213
|
cancelAtPeriodEnd: z.boolean().optional(),
|
|
1193
|
-
prorateAndCharge: z.boolean().optional()
|
|
1214
|
+
prorateAndCharge: z.boolean().optional(),
|
|
1215
|
+
confirmTrialTokenizationCharge: z.boolean().optional()
|
|
1194
1216
|
});
|
|
1195
1217
|
//#endregion
|
|
1196
1218
|
//#region src/route-modules/shared.ts
|
|
@@ -1683,6 +1705,34 @@ const paystackWebhook = (options, path = "/webhook") => {
|
|
|
1683
1705
|
ctx.context.logger.warn("Failed to update transaction status for charge.success", e);
|
|
1684
1706
|
throw e;
|
|
1685
1707
|
}
|
|
1708
|
+
if (options.subscription?.enabled === true) {
|
|
1709
|
+
const pendingTrial = await ctx.context.adapter.findOne({
|
|
1710
|
+
model: PAYSTACK_MODELS.subscription,
|
|
1711
|
+
where: [{
|
|
1712
|
+
field: "transactionReference",
|
|
1713
|
+
value: reference
|
|
1714
|
+
}]
|
|
1715
|
+
});
|
|
1716
|
+
if (pendingTrial?.status === "incomplete" && pendingTrial.trialStart !== void 0 && pendingTrial.trialStart !== null) {
|
|
1717
|
+
const startedTrial = {
|
|
1718
|
+
...pendingTrial,
|
|
1719
|
+
status: "trialing",
|
|
1720
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
1721
|
+
};
|
|
1722
|
+
await ctx.context.adapter.update({
|
|
1723
|
+
model: PAYSTACK_MODELS.subscription,
|
|
1724
|
+
where: [{
|
|
1725
|
+
field: "id",
|
|
1726
|
+
value: pendingTrial.id
|
|
1727
|
+
}],
|
|
1728
|
+
update: {
|
|
1729
|
+
status: "trialing",
|
|
1730
|
+
updatedAt: startedTrial.updatedAt
|
|
1731
|
+
}
|
|
1732
|
+
});
|
|
1733
|
+
await (await getPlanByName(options, pendingTrial.plan))?.freeTrial?.onTrialStart?.(startedTrial);
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1686
1736
|
try {
|
|
1687
1737
|
const transaction = await ctx.context.adapter.findOne({
|
|
1688
1738
|
model: "paystackTransaction",
|
|
@@ -1702,21 +1752,84 @@ const paystackWebhook = (options, path = "/webhook") => {
|
|
|
1702
1752
|
}
|
|
1703
1753
|
if (eventName === "charge.failure") {
|
|
1704
1754
|
const reference = data?.reference;
|
|
1705
|
-
if (reference !== void 0 && reference !== null && reference !== "")
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1755
|
+
if (reference !== void 0 && reference !== null && reference !== "") {
|
|
1756
|
+
try {
|
|
1757
|
+
await ctx.context.adapter.update({
|
|
1758
|
+
model: "paystackTransaction",
|
|
1759
|
+
update: {
|
|
1760
|
+
status: "failed",
|
|
1761
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
1762
|
+
},
|
|
1763
|
+
where: [{
|
|
1764
|
+
field: "reference",
|
|
1765
|
+
value: reference
|
|
1766
|
+
}]
|
|
1767
|
+
});
|
|
1768
|
+
} catch (e) {
|
|
1769
|
+
ctx.context.logger.warn("Failed to update transaction status for charge.failure", e);
|
|
1770
|
+
throw e;
|
|
1771
|
+
}
|
|
1772
|
+
if (options.subscription?.enabled === true) {
|
|
1773
|
+
const pendingTrial = await ctx.context.adapter.findOne({
|
|
1774
|
+
model: PAYSTACK_MODELS.subscription,
|
|
1775
|
+
where: [{
|
|
1776
|
+
field: "transactionReference",
|
|
1777
|
+
value: reference
|
|
1778
|
+
}]
|
|
1779
|
+
});
|
|
1780
|
+
if (pendingTrial?.status === "incomplete" && pendingTrial.trialStart !== void 0 && pendingTrial.trialStart !== null) {
|
|
1781
|
+
const endedAt = /* @__PURE__ */ new Date();
|
|
1782
|
+
await ctx.context.adapter.update({
|
|
1783
|
+
model: PAYSTACK_MODELS.subscription,
|
|
1784
|
+
where: [{
|
|
1785
|
+
field: "id",
|
|
1786
|
+
value: pendingTrial.id
|
|
1787
|
+
}],
|
|
1788
|
+
update: {
|
|
1789
|
+
status: "canceled",
|
|
1790
|
+
endedAt,
|
|
1791
|
+
updatedAt: endedAt
|
|
1792
|
+
}
|
|
1793
|
+
});
|
|
1794
|
+
}
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
if (eventName === "invoice.payment_failed") {
|
|
1799
|
+
const invoice = data;
|
|
1800
|
+
const subscriptionCode = invoice.subscription?.subscription_code ?? invoice.subscription_code;
|
|
1801
|
+
if (subscriptionCode !== null && subscriptionCode !== void 0 && subscriptionCode !== "") {
|
|
1802
|
+
const existing = await ctx.context.adapter.findOne({
|
|
1803
|
+
model: PAYSTACK_MODELS.subscription,
|
|
1712
1804
|
where: [{
|
|
1713
|
-
field: "
|
|
1714
|
-
value:
|
|
1805
|
+
field: "subscriptionCode",
|
|
1806
|
+
value: subscriptionCode
|
|
1715
1807
|
}]
|
|
1716
1808
|
});
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1809
|
+
if (existing && existing.status !== "canceled" && existing.status !== "past_due") {
|
|
1810
|
+
const pastDue = {
|
|
1811
|
+
...existing,
|
|
1812
|
+
status: "past_due",
|
|
1813
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
1814
|
+
};
|
|
1815
|
+
await ctx.context.adapter.update({
|
|
1816
|
+
model: PAYSTACK_MODELS.subscription,
|
|
1817
|
+
where: [{
|
|
1818
|
+
field: "id",
|
|
1819
|
+
value: existing.id
|
|
1820
|
+
}],
|
|
1821
|
+
update: {
|
|
1822
|
+
status: pastDue.status,
|
|
1823
|
+
updatedAt: pastDue.updatedAt
|
|
1824
|
+
}
|
|
1825
|
+
});
|
|
1826
|
+
const plan = await getPlanByName(options, existing.plan);
|
|
1827
|
+
if (plan) await options.subscription?.onSubscriptionUpdate?.({
|
|
1828
|
+
event,
|
|
1829
|
+
subscription: pastDue,
|
|
1830
|
+
plan
|
|
1831
|
+
}, ctx);
|
|
1832
|
+
}
|
|
1720
1833
|
}
|
|
1721
1834
|
}
|
|
1722
1835
|
if (options.subscription?.enabled === true) try {
|
|
@@ -1888,6 +2001,30 @@ const paystackWebhook = (options, path = "/webhook") => {
|
|
|
1888
2001
|
value: subscriptionCode
|
|
1889
2002
|
}]
|
|
1890
2003
|
});
|
|
2004
|
+
if (existingSub?.status === "past_due") {
|
|
2005
|
+
const recovered = {
|
|
2006
|
+
...existingSub,
|
|
2007
|
+
status: "active",
|
|
2008
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
2009
|
+
};
|
|
2010
|
+
await ctx.context.adapter.update({
|
|
2011
|
+
model: PAYSTACK_MODELS.subscription,
|
|
2012
|
+
update: {
|
|
2013
|
+
status: recovered.status,
|
|
2014
|
+
updatedAt: recovered.updatedAt
|
|
2015
|
+
},
|
|
2016
|
+
where: [{
|
|
2017
|
+
field: "id",
|
|
2018
|
+
value: existingSub.id
|
|
2019
|
+
}]
|
|
2020
|
+
});
|
|
2021
|
+
const plan = await getPlanByName(options, existingSub.plan);
|
|
2022
|
+
if (plan) await options.subscription.onSubscriptionUpdate?.({
|
|
2023
|
+
event,
|
|
2024
|
+
subscription: recovered,
|
|
2025
|
+
plan
|
|
2026
|
+
}, ctx);
|
|
2027
|
+
}
|
|
1891
2028
|
if (existingSub !== void 0 && existingSub !== null && existingSub.pendingPlan !== void 0 && existingSub.pendingPlan !== null && existingSub.pendingPlan !== "") await ctx.context.adapter.update({
|
|
1892
2029
|
model: PAYSTACK_MODELS.subscription,
|
|
1893
2030
|
update: {
|
|
@@ -1956,20 +2093,9 @@ const initializeTransaction = (options, path = "/initialize-transaction") => {
|
|
|
1956
2093
|
use: useMiddlewares
|
|
1957
2094
|
}, async (ctx) => {
|
|
1958
2095
|
const paystack = getPaystackOps(options.paystackClient);
|
|
1959
|
-
const { plan: planName, product: productName, amount: bodyAmount, currency, email, metadata: extraMetadata, callbackURL, quantity, scheduleAtPeriodEnd, cancelAtPeriodEnd, prorateAndCharge, subscriptionId } = ctx.body;
|
|
2096
|
+
const { plan: planName, product: productName, amount: bodyAmount, currency, email, metadata: extraMetadata, callbackURL, quantity, scheduleAtPeriodEnd, cancelAtPeriodEnd, prorateAndCharge, subscriptionId, confirmTrialTokenizationCharge } = ctx.body;
|
|
1960
2097
|
if (callbackURL !== void 0 && callbackURL !== null && callbackURL !== "") {
|
|
1961
|
-
|
|
1962
|
-
try {
|
|
1963
|
-
if (callbackURL?.startsWith("/") === true) return true;
|
|
1964
|
-
const baseUrl = ctx.context?.baseURL ?? ctx.request?.url ?? "";
|
|
1965
|
-
if (baseUrl === "") return false;
|
|
1966
|
-
const baseOrigin = new URL(baseUrl).origin;
|
|
1967
|
-
return new URL(callbackURL).origin === baseOrigin;
|
|
1968
|
-
} catch {
|
|
1969
|
-
return false;
|
|
1970
|
-
}
|
|
1971
|
-
};
|
|
1972
|
-
if (checkTrusted() === false) throw new APIError("FORBIDDEN", {
|
|
2098
|
+
if (!isTrustedCallbackURL(callbackURL, ctx.context?.baseURL ?? ctx.request?.url ?? "")) throw new APIError("FORBIDDEN", {
|
|
1973
2099
|
message: "callbackURL is not a trusted origin.",
|
|
1974
2100
|
status: 403
|
|
1975
2101
|
});
|
|
@@ -2075,6 +2201,11 @@ const initializeTransaction = (options, path = "/initialize-transaction") => {
|
|
|
2075
2201
|
plan
|
|
2076
2202
|
});
|
|
2077
2203
|
const { trialStart, trialEnd } = trial;
|
|
2204
|
+
const trialTokenizationCharge = trial.granted ? getTrialTokenizationCharge(plan, finalCurrency) : void 0;
|
|
2205
|
+
if (trial.granted && confirmTrialTokenizationCharge !== true) throw new APIError("BAD_REQUEST", {
|
|
2206
|
+
code: "TRIAL_TOKENIZATION_CHARGE_CONFIRMATION_REQUIRED",
|
|
2207
|
+
message: `This Paystack trial requires a one-time tokenization charge of ${trialTokenizationCharge} minor units in ${finalCurrency}. Tell the customer the exact charge and retry with confirmTrialTokenizationCharge: true after they accept it.`
|
|
2208
|
+
});
|
|
2078
2209
|
const normalizeInitializationError = (error) => {
|
|
2079
2210
|
ctx.context.logger.error("Failed to initialize Paystack transaction", error);
|
|
2080
2211
|
return new APIError("BAD_REQUEST", {
|
|
@@ -2139,7 +2270,7 @@ const initializeTransaction = (options, path = "/initialize-transaction") => {
|
|
|
2139
2270
|
} };
|
|
2140
2271
|
}
|
|
2141
2272
|
if (plan !== void 0) {
|
|
2142
|
-
if (trialStart !== void 0) initBody.amount =
|
|
2273
|
+
if (trialStart !== void 0) initBody.amount = trialTokenizationCharge;
|
|
2143
2274
|
else {
|
|
2144
2275
|
initBody.plan = plan.planCode;
|
|
2145
2276
|
initBody.invoice_limit = plan.invoiceLimit;
|
|
@@ -2220,7 +2351,7 @@ const initializeTransaction = (options, path = "/initialize-transaction") => {
|
|
|
2220
2351
|
}))?.paystackCustomerCode ?? void 0;
|
|
2221
2352
|
if (customerCode !== void 0 && customerCode !== null && customerCode !== "") await store.saveCustomer(referenceId === user.id ? "user" : "organization", referenceId, customerCode);
|
|
2222
2353
|
} catch {}
|
|
2223
|
-
|
|
2354
|
+
await ctx.context.adapter.create({
|
|
2224
2355
|
model: PAYSTACK_MODELS.subscription,
|
|
2225
2356
|
data: {
|
|
2226
2357
|
plan: plan.name.toLowerCase(),
|
|
@@ -2231,7 +2362,7 @@ const initializeTransaction = (options, path = "/initialize-transaction") => {
|
|
|
2231
2362
|
subscriptionCode: void 0,
|
|
2232
2363
|
planCode: plan.planCode,
|
|
2233
2364
|
transactionReference: reference ?? "",
|
|
2234
|
-
status:
|
|
2365
|
+
status: "incomplete",
|
|
2235
2366
|
billingInterval: plan.interval ?? null,
|
|
2236
2367
|
seats: quantity ?? 1,
|
|
2237
2368
|
periodStart: /* @__PURE__ */ new Date(),
|
|
@@ -2243,19 +2374,47 @@ const initializeTransaction = (options, path = "/initialize-transaction") => {
|
|
|
2243
2374
|
updatedAt: /* @__PURE__ */ new Date()
|
|
2244
2375
|
}
|
|
2245
2376
|
});
|
|
2246
|
-
if (trialStart !== void 0 && newSubscription !== void 0 && newSubscription !== null && plan.freeTrial?.onTrialStart !== void 0) await plan.freeTrial.onTrialStart(newSubscription);
|
|
2247
2377
|
}
|
|
2248
2378
|
return ctx.json({
|
|
2249
2379
|
kind: "checkout",
|
|
2250
2380
|
url: url ?? "",
|
|
2251
2381
|
reference: reference ?? "",
|
|
2252
2382
|
accessCode: accessCode ?? "",
|
|
2253
|
-
redirect: true
|
|
2383
|
+
redirect: true,
|
|
2384
|
+
...trial.granted && trialTokenizationCharge !== void 0 ? { trial: {
|
|
2385
|
+
days: trial.requestedDays,
|
|
2386
|
+
tokenizationCharge: {
|
|
2387
|
+
amount: trialTokenizationCharge,
|
|
2388
|
+
currency: finalCurrency
|
|
2389
|
+
}
|
|
2390
|
+
} } : {}
|
|
2254
2391
|
});
|
|
2255
2392
|
};
|
|
2256
2393
|
return operation !== void 0 ? runSubscriptionOperation(ctx, options, operation, execute) : execute(void 0, () => void 0);
|
|
2257
2394
|
});
|
|
2258
2395
|
};
|
|
2396
|
+
function getTrialTokenizationCharge(plan, currency) {
|
|
2397
|
+
const configured = plan?.freeTrial?.tokenizationCharge;
|
|
2398
|
+
if (configured !== void 0) {
|
|
2399
|
+
if (!Number.isSafeInteger(configured) || configured <= 0) throw new APIError("BAD_REQUEST", {
|
|
2400
|
+
code: "INVALID_TRIAL_TOKENIZATION_CHARGE",
|
|
2401
|
+
message: "freeTrial.tokenizationCharge must be a positive safe integer in minor units."
|
|
2402
|
+
});
|
|
2403
|
+
return configured;
|
|
2404
|
+
}
|
|
2405
|
+
const amount = {
|
|
2406
|
+
NGN: 5e3,
|
|
2407
|
+
GHS: 10,
|
|
2408
|
+
ZAR: 100,
|
|
2409
|
+
KES: 300,
|
|
2410
|
+
USD: 200
|
|
2411
|
+
}[currency.toUpperCase()];
|
|
2412
|
+
if (amount === void 0) throw new APIError("BAD_REQUEST", {
|
|
2413
|
+
code: "TRIAL_TOKENIZATION_CHARGE_REQUIRED",
|
|
2414
|
+
message: `Configure freeTrial.tokenizationCharge in minor units for ${currency}.`
|
|
2415
|
+
});
|
|
2416
|
+
return amount;
|
|
2417
|
+
}
|
|
2259
2418
|
const createSubscription = (options, path = "/create-subscription") => initializeTransaction(options, path);
|
|
2260
2419
|
const upgradeSubscription = (options, path = "/upgrade-subscription") => initializeTransaction(options, path);
|
|
2261
2420
|
const cancelSubscription = (options, path = "/cancel-subscription") => disablePaystackSubscription(options, path);
|