@chaicodes-com/stripe-helpers 1.0.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 +162 -0
- package/dist/checkout/createCheckoutSession.d.ts +26 -0
- package/dist/checkout/createCheckoutSession.d.ts.map +1 -0
- package/dist/checkout/createCheckoutSession.js +55 -0
- package/dist/checkout/createCheckoutSession.js.map +1 -0
- package/dist/checkout/handlePriceSwitching.d.ts +20 -0
- package/dist/checkout/handlePriceSwitching.d.ts.map +1 -0
- package/dist/checkout/handlePriceSwitching.js +59 -0
- package/dist/checkout/handlePriceSwitching.js.map +1 -0
- package/dist/config/defaultConfig.d.ts +45 -0
- package/dist/config/defaultConfig.d.ts.map +1 -0
- package/dist/config/defaultConfig.js +71 -0
- package/dist/config/defaultConfig.js.map +1 -0
- package/dist/database/databaseUtilities.d.ts +45 -0
- package/dist/database/databaseUtilities.d.ts.map +1 -0
- package/dist/database/databaseUtilities.js +224 -0
- package/dist/database/databaseUtilities.js.map +1 -0
- package/dist/email/emailTemplates.d.ts +18 -0
- package/dist/email/emailTemplates.d.ts.map +1 -0
- package/dist/email/emailTemplates.js +130 -0
- package/dist/email/emailTemplates.js.map +1 -0
- package/dist/email/sendAccessCodeEmail.d.ts +30 -0
- package/dist/email/sendAccessCodeEmail.d.ts.map +1 -0
- package/dist/email/sendAccessCodeEmail.js +61 -0
- package/dist/email/sendAccessCodeEmail.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +50 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/utilityFunctions.d.ts +20 -0
- package/dist/utils/utilityFunctions.d.ts.map +1 -0
- package/dist/utils/utilityFunctions.js +68 -0
- package/dist/utils/utilityFunctions.js.map +1 -0
- package/dist/webhooks/handleCheckoutCompleted.d.ts +26 -0
- package/dist/webhooks/handleCheckoutCompleted.d.ts.map +1 -0
- package/dist/webhooks/handleCheckoutCompleted.js +75 -0
- package/dist/webhooks/handleCheckoutCompleted.js.map +1 -0
- package/dist/webhooks/handleStripeWebhook.d.ts +21 -0
- package/dist/webhooks/handleStripeWebhook.d.ts.map +1 -0
- package/dist/webhooks/handleStripeWebhook.js +68 -0
- package/dist/webhooks/handleStripeWebhook.js.map +1 -0
- package/dist/webhooks/subscriptionHandlers.d.ts +25 -0
- package/dist/webhooks/subscriptionHandlers.d.ts.map +1 -0
- package/dist/webhooks/subscriptionHandlers.js +72 -0
- package/dist/webhooks/subscriptionHandlers.js.map +1 -0
- package/package.json +35 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* databaseUtilities.ts
|
|
4
|
+
* Supabase database functions for access codes and sessions
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.createAccessCode = createAccessCode;
|
|
8
|
+
exports.getAccessCodeByCode = getAccessCodeByCode;
|
|
9
|
+
exports.updateAccessCodeStatus = updateAccessCodeStatus;
|
|
10
|
+
exports.incrementAccessCodeUses = incrementAccessCodeUses;
|
|
11
|
+
exports.createSession = createSession;
|
|
12
|
+
exports.validateSession = validateSession;
|
|
13
|
+
exports.deleteSession = deleteSession;
|
|
14
|
+
exports.cleanupExpiredSessions = cleanupExpiredSessions;
|
|
15
|
+
// Access Code Functions
|
|
16
|
+
async function createAccessCode(supabaseClient, codeData) {
|
|
17
|
+
try {
|
|
18
|
+
const { data, error } = await supabaseClient
|
|
19
|
+
.from('access_codes')
|
|
20
|
+
.insert([codeData])
|
|
21
|
+
.select();
|
|
22
|
+
if (error)
|
|
23
|
+
throw error;
|
|
24
|
+
return {
|
|
25
|
+
success: true,
|
|
26
|
+
data: data?.[0],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
31
|
+
console.error('Error creating access code:', error);
|
|
32
|
+
return {
|
|
33
|
+
success: false,
|
|
34
|
+
error: message,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async function getAccessCodeByCode(supabaseClient, code) {
|
|
39
|
+
try {
|
|
40
|
+
const { data, error } = await supabaseClient
|
|
41
|
+
.from('access_codes')
|
|
42
|
+
.select('*')
|
|
43
|
+
.eq('code', code)
|
|
44
|
+
.single();
|
|
45
|
+
if (error)
|
|
46
|
+
throw error;
|
|
47
|
+
return {
|
|
48
|
+
success: true,
|
|
49
|
+
data: data,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
54
|
+
console.error('Error getting access code:', error);
|
|
55
|
+
return {
|
|
56
|
+
success: false,
|
|
57
|
+
error: message,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function updateAccessCodeStatus(supabaseClient, updates) {
|
|
62
|
+
try {
|
|
63
|
+
const { data, error } = await supabaseClient
|
|
64
|
+
.from('access_codes')
|
|
65
|
+
.update(updates)
|
|
66
|
+
.eq('subscription_id', updates.subscription_id)
|
|
67
|
+
.select();
|
|
68
|
+
if (error)
|
|
69
|
+
throw error;
|
|
70
|
+
return {
|
|
71
|
+
success: true,
|
|
72
|
+
data: data,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
77
|
+
console.error('Error updating access code:', error);
|
|
78
|
+
return {
|
|
79
|
+
success: false,
|
|
80
|
+
error: message,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async function incrementAccessCodeUses(supabaseClient, code) {
|
|
85
|
+
try {
|
|
86
|
+
const { data: current, error: fetchError } = await supabaseClient
|
|
87
|
+
.from('access_codes')
|
|
88
|
+
.select('current_uses')
|
|
89
|
+
.eq('code', code)
|
|
90
|
+
.single();
|
|
91
|
+
if (fetchError)
|
|
92
|
+
throw fetchError;
|
|
93
|
+
const { data, error } = await supabaseClient
|
|
94
|
+
.from('access_codes')
|
|
95
|
+
.update({ current_uses: (current.current_uses || 0) + 1 })
|
|
96
|
+
.eq('code', code)
|
|
97
|
+
.select();
|
|
98
|
+
if (error)
|
|
99
|
+
throw error;
|
|
100
|
+
return {
|
|
101
|
+
success: true,
|
|
102
|
+
data: data?.[0],
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
107
|
+
console.error('Error incrementing access code uses:', error);
|
|
108
|
+
return {
|
|
109
|
+
success: false,
|
|
110
|
+
error: message,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// Session Functions
|
|
115
|
+
async function createSession(supabaseClient, { access_code, session_id }) {
|
|
116
|
+
try {
|
|
117
|
+
const expiresAt = new Date(Date.now() + 90 * 24 * 60 * 60 * 1000).toISOString();
|
|
118
|
+
const { data, error } = await supabaseClient
|
|
119
|
+
.from('sessions')
|
|
120
|
+
.insert([
|
|
121
|
+
{
|
|
122
|
+
access_code: access_code,
|
|
123
|
+
session_id: session_id,
|
|
124
|
+
expires_at: expiresAt,
|
|
125
|
+
},
|
|
126
|
+
])
|
|
127
|
+
.select();
|
|
128
|
+
if (error)
|
|
129
|
+
throw error;
|
|
130
|
+
return {
|
|
131
|
+
success: true,
|
|
132
|
+
data: data?.[0],
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
137
|
+
console.error('Error creating session:', error);
|
|
138
|
+
return {
|
|
139
|
+
success: false,
|
|
140
|
+
error: message,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async function validateSession(supabaseClient, accessCode, sessionId) {
|
|
145
|
+
try {
|
|
146
|
+
const { data, error } = await supabaseClient
|
|
147
|
+
.from('sessions')
|
|
148
|
+
.select('*')
|
|
149
|
+
.eq('access_code', accessCode)
|
|
150
|
+
.eq('session_id', sessionId)
|
|
151
|
+
.gt('expires_at', new Date().toISOString())
|
|
152
|
+
.single();
|
|
153
|
+
if (error)
|
|
154
|
+
throw error;
|
|
155
|
+
return {
|
|
156
|
+
success: true,
|
|
157
|
+
valid: !!data,
|
|
158
|
+
data: data,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
163
|
+
console.error('Error validating session:', error);
|
|
164
|
+
return {
|
|
165
|
+
success: false,
|
|
166
|
+
valid: false,
|
|
167
|
+
error: message,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async function deleteSession(supabaseClient, sessionId) {
|
|
172
|
+
try {
|
|
173
|
+
const { error } = await supabaseClient
|
|
174
|
+
.from('sessions')
|
|
175
|
+
.delete()
|
|
176
|
+
.eq('session_id', sessionId);
|
|
177
|
+
if (error)
|
|
178
|
+
throw error;
|
|
179
|
+
return {
|
|
180
|
+
success: true,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
185
|
+
console.error('Error deleting session:', error);
|
|
186
|
+
return {
|
|
187
|
+
success: false,
|
|
188
|
+
error: message,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async function cleanupExpiredSessions(supabaseClient, accessCode) {
|
|
193
|
+
try {
|
|
194
|
+
const { error } = await supabaseClient
|
|
195
|
+
.from('sessions')
|
|
196
|
+
.delete()
|
|
197
|
+
.eq('access_code', accessCode)
|
|
198
|
+
.lt('expires_at', new Date().toISOString());
|
|
199
|
+
if (error)
|
|
200
|
+
throw error;
|
|
201
|
+
return {
|
|
202
|
+
success: true,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
207
|
+
console.error('Error cleaning up sessions:', error);
|
|
208
|
+
return {
|
|
209
|
+
success: false,
|
|
210
|
+
error: message,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
exports.default = {
|
|
215
|
+
createAccessCode,
|
|
216
|
+
getAccessCodeByCode,
|
|
217
|
+
updateAccessCodeStatus,
|
|
218
|
+
incrementAccessCodeUses,
|
|
219
|
+
createSession,
|
|
220
|
+
validateSession,
|
|
221
|
+
deleteSession,
|
|
222
|
+
cleanupExpiredSessions,
|
|
223
|
+
};
|
|
224
|
+
//# sourceMappingURL=databaseUtilities.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"databaseUtilities.js","sourceRoot":"","sources":["../../src/database/databaseUtilities.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AA+BH,4CAwBC;AAED,kDAyBC;AAED,wDAyBC;AAED,0DAiCC;AAID,sCAgCC;AAED,0CA8BC;AAED,sCAuBC;AAED,wDAwBC;AA1OD,wBAAwB;AAEjB,KAAK,UAAU,gBAAgB,CACpC,cAAmB,EACnB,QAAwB;IAExB,IAAI,CAAC;QACH,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,cAAc;aACzC,IAAI,CAAC,cAAc,CAAC;aACpB,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC;aAClB,MAAM,EAAE,CAAC;QAEZ,IAAI,KAAK;YAAE,MAAM,KAAK,CAAC;QAEvB,OAAO;YACL,OAAO,EAAE,IAAI;YACb,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;SAChB,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvE,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE,KAAK,CAA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E,KAAK;YACd,KAAK,EAAE,OAAO;SACf,CAAC;IACJ,CAAC;AACH,CAAC;AAEM,KAAK,UAAU,sBAAsB,CAC1C,cAAmB,EACnB,UAAkB;IAElB,IAAI,CAAC;QACH,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,cAAc;aACnC,IAAI,CAAC,UAAU,CAAC;aAChB,MAAM,EAAE;aACR,EAAE,CAAC,aAAa,EAAE,UAAU,CAAC;aAC7B,EAAE,CAAC,YAAY,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC;QAE9C,IAAI,KAAK;YAAE,MAAM,KAAK,CAAC;QAEvB,OAAO;YACL,OAAO,EAAE,IAAI;SACd,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvE,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE,KAAK,CAAC,CAAC;QACpD,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,OAAO;SACf,CAAC;IACJ,CAAC;AACH,CAAC;AAED,kBAAe;IACb,gBAAgB;IAChB,mBAAmB;IACnB,sBAAsB;IACtB,uBAAuB;IACvB,aAAa;IACb,eAAe;IACf,aAAa;IACb,sBAAsB;CACvB,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* emailTemplates.ts
|
|
3
|
+
* Default and custom email templates for access codes
|
|
4
|
+
*/
|
|
5
|
+
interface EmailTemplateParams {
|
|
6
|
+
appName?: string;
|
|
7
|
+
supportEmail?: string;
|
|
8
|
+
appUrl?: string;
|
|
9
|
+
brandColor?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function getDefaultEmailTemplate({ appName, supportEmail, appUrl, brandColor, }?: EmailTemplateParams): string;
|
|
12
|
+
export declare function buildCustomEmailTemplate(templateFn: (() => string) | any): any;
|
|
13
|
+
declare const _default: {
|
|
14
|
+
getDefaultEmailTemplate: typeof getDefaultEmailTemplate;
|
|
15
|
+
buildCustomEmailTemplate: typeof buildCustomEmailTemplate;
|
|
16
|
+
};
|
|
17
|
+
export default _default;
|
|
18
|
+
//# sourceMappingURL=emailTemplates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emailTemplates.d.ts","sourceRoot":"","sources":["../../src/email/emailTemplates.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,UAAU,mBAAmB;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,wBAAgB,uBAAuB,CAAC,EACtC,OAAqB,EACrB,YAAsC,EACtC,MAAgC,EAChC,UAAsB,GACvB,GAAE,mBAAwB,GAAG,MAAM,CAyGnC;AAED,wBAAgB,wBAAwB,CAAC,UAAU,EAAE,CAAC,MAAM,MAAM,CAAC,GAAG,GAAG,GAAG,GAAG,CAY9E;;;;;AAED,wBAGE"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* emailTemplates.ts
|
|
4
|
+
* Default and custom email templates for access codes
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.getDefaultEmailTemplate = getDefaultEmailTemplate;
|
|
8
|
+
exports.buildCustomEmailTemplate = buildCustomEmailTemplate;
|
|
9
|
+
function getDefaultEmailTemplate({ appName = 'ChAICodes', supportEmail = 'support@chaicodes.com', appUrl = 'https://chaicodes.com', brandColor = '#C8102E', } = {}) {
|
|
10
|
+
return `
|
|
11
|
+
<!DOCTYPE html>
|
|
12
|
+
<html>
|
|
13
|
+
<head>
|
|
14
|
+
<style>
|
|
15
|
+
body {
|
|
16
|
+
font-family: 'Lora', Georgia, serif;
|
|
17
|
+
background-color: #f5f5f5;
|
|
18
|
+
color: #333;
|
|
19
|
+
line-height: 1.6;
|
|
20
|
+
}
|
|
21
|
+
.container {
|
|
22
|
+
max-width: 600px;
|
|
23
|
+
margin: 0 auto;
|
|
24
|
+
background-color: white;
|
|
25
|
+
padding: 40px;
|
|
26
|
+
border-radius: 4px;
|
|
27
|
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
|
28
|
+
}
|
|
29
|
+
.header {
|
|
30
|
+
border-bottom: 2px solid ${brandColor};
|
|
31
|
+
padding-bottom: 20px;
|
|
32
|
+
margin-bottom: 30px;
|
|
33
|
+
}
|
|
34
|
+
.header h1 {
|
|
35
|
+
color: ${brandColor};
|
|
36
|
+
margin: 0;
|
|
37
|
+
font-size: 24px;
|
|
38
|
+
}
|
|
39
|
+
.content {
|
|
40
|
+
margin: 20px 0;
|
|
41
|
+
}
|
|
42
|
+
.code-box {
|
|
43
|
+
background-color: #f0f0f0;
|
|
44
|
+
border: 2px solid ${brandColor};
|
|
45
|
+
border-radius: 4px;
|
|
46
|
+
padding: 20px;
|
|
47
|
+
text-align: center;
|
|
48
|
+
margin: 20px 0;
|
|
49
|
+
}
|
|
50
|
+
.code-box p {
|
|
51
|
+
margin: 0;
|
|
52
|
+
font-size: 14px;
|
|
53
|
+
color: #666;
|
|
54
|
+
}
|
|
55
|
+
.code-box .code {
|
|
56
|
+
font-family: monospace;
|
|
57
|
+
font-size: 18px;
|
|
58
|
+
font-weight: bold;
|
|
59
|
+
color: ${brandColor};
|
|
60
|
+
letter-spacing: 2px;
|
|
61
|
+
margin: 10px 0;
|
|
62
|
+
}
|
|
63
|
+
.footer {
|
|
64
|
+
margin-top: 30px;
|
|
65
|
+
padding-top: 20px;
|
|
66
|
+
border-top: 1px solid #eee;
|
|
67
|
+
font-size: 12px;
|
|
68
|
+
color: #666;
|
|
69
|
+
text-align: center;
|
|
70
|
+
}
|
|
71
|
+
.footer a {
|
|
72
|
+
color: ${brandColor};
|
|
73
|
+
text-decoration: none;
|
|
74
|
+
}
|
|
75
|
+
</style>
|
|
76
|
+
</head>
|
|
77
|
+
<body>
|
|
78
|
+
<div class="container">
|
|
79
|
+
<div class="header">
|
|
80
|
+
<h1>Welcome to ${appName}! 🎉</h1>
|
|
81
|
+
</div>
|
|
82
|
+
|
|
83
|
+
<div class="content">
|
|
84
|
+
<p>Hi {email},</p>
|
|
85
|
+
|
|
86
|
+
<p>Thank you for subscribing to ${appName}! We're excited to have you on board.</p>
|
|
87
|
+
|
|
88
|
+
<p>Your access code is:</p>
|
|
89
|
+
|
|
90
|
+
<div class="code-box">
|
|
91
|
+
<p>Your Access Code</p>
|
|
92
|
+
<div class="code">{accessCode}</div>
|
|
93
|
+
<p>Use this to log in to your account</p>
|
|
94
|
+
</div>
|
|
95
|
+
|
|
96
|
+
<p>
|
|
97
|
+
Questions or need help? Contact us at
|
|
98
|
+
<a href="mailto:${supportEmail}">${supportEmail}</a>
|
|
99
|
+
</p>
|
|
100
|
+
|
|
101
|
+
<p>
|
|
102
|
+
Visit us at <a href="${appUrl}">${appName}</a>
|
|
103
|
+
</p>
|
|
104
|
+
</div>
|
|
105
|
+
|
|
106
|
+
<div class="footer">
|
|
107
|
+
<p>© ${new Date().getFullYear()} ${appName}. All rights reserved.</p>
|
|
108
|
+
<p>Built by <a href="https://chaicodes.com">ChAI Codes</a></p>
|
|
109
|
+
</div>
|
|
110
|
+
</div>
|
|
111
|
+
</body>
|
|
112
|
+
</html>
|
|
113
|
+
`;
|
|
114
|
+
}
|
|
115
|
+
function buildCustomEmailTemplate(templateFn) {
|
|
116
|
+
if (typeof templateFn !== 'function') {
|
|
117
|
+
throw new Error('Template must be a function');
|
|
118
|
+
}
|
|
119
|
+
// Validate that the function returns a string
|
|
120
|
+
const result = templateFn();
|
|
121
|
+
if (typeof result !== 'string') {
|
|
122
|
+
throw new Error('Template function must return a string');
|
|
123
|
+
}
|
|
124
|
+
return templateFn;
|
|
125
|
+
}
|
|
126
|
+
exports.default = {
|
|
127
|
+
getDefaultEmailTemplate,
|
|
128
|
+
buildCustomEmailTemplate,
|
|
129
|
+
};
|
|
130
|
+
//# sourceMappingURL=emailTemplates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emailTemplates.js","sourceRoot":"","sources":["../../src/email/emailTemplates.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AASH,0DA8GC;AAED,4DAYC;AA5HD,SAAgB,uBAAuB,CAAC,EACtC,OAAO,GAAG,WAAW,EACrB,YAAY,GAAG,uBAAuB,EACtC,MAAM,GAAG,uBAAuB,EAChC,UAAU,GAAG,SAAS,MACC,EAAE;IACzB,OAAO;;;;;;;;;;;;;;;;;;;;qCAoB4B,UAAU;;;;;mBAK5B,UAAU;;;;;;;;;8BASC,UAAU;;;;;;;;;;;;;;;mBAerB,UAAU;;;;;;;;;;;;;mBAaV,UAAU;;;;;;;;2BAQF,OAAO;;;;;;4CAMU,OAAO;;;;;;;;;;;;8BAYrB,YAAY,KAAK,YAAY;;;;mCAIxB,MAAM,KAAK,OAAO;;;;;iBAKpC,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,IAAI,OAAO;;;;;;GAMjD,CAAC;AACJ,CAAC;AAED,SAAgB,wBAAwB,CAAC,UAAgC;IACvE,IAAI,OAAO,UAAU,KAAK,UAAU,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACjD,CAAC;IAED,8CAA8C;IAC9C,MAAM,MAAM,GAAG,UAAU,EAAE,CAAC;IAC5B,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC5D,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,kBAAe;IACb,uBAAuB;IACvB,wBAAwB;CACzB,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sendAccessCodeEmail.ts
|
|
3
|
+
* Sends access code email via SMTP
|
|
4
|
+
*/
|
|
5
|
+
interface SmtpConfig {
|
|
6
|
+
host?: string;
|
|
7
|
+
port?: number;
|
|
8
|
+
secure?: boolean;
|
|
9
|
+
user?: string;
|
|
10
|
+
password?: string;
|
|
11
|
+
from?: string;
|
|
12
|
+
}
|
|
13
|
+
interface EmailParams {
|
|
14
|
+
email: string;
|
|
15
|
+
accessCode: string;
|
|
16
|
+
appName?: string;
|
|
17
|
+
emailTemplate: string;
|
|
18
|
+
smtpConfig?: SmtpConfig | null;
|
|
19
|
+
expiresAt?: string | null;
|
|
20
|
+
}
|
|
21
|
+
interface EmailResponse {
|
|
22
|
+
success: boolean;
|
|
23
|
+
message?: string;
|
|
24
|
+
messageId?: string;
|
|
25
|
+
recipient?: string;
|
|
26
|
+
error?: string;
|
|
27
|
+
}
|
|
28
|
+
export declare function sendAccessCodeEmail({ email, accessCode, appName, emailTemplate, smtpConfig, expiresAt, }: EmailParams): Promise<EmailResponse>;
|
|
29
|
+
export default sendAccessCodeEmail;
|
|
30
|
+
//# sourceMappingURL=sendAccessCodeEmail.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sendAccessCodeEmail.d.ts","sourceRoot":"","sources":["../../src/email/sendAccessCodeEmail.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,UAAU,UAAU;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,UAAU,WAAW;IACnB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IAC/B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B;AAED,UAAU,aAAa;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,wBAAsB,mBAAmB,CAAC,EACxC,KAAK,EACL,UAAU,EACV,OAAqB,EACrB,aAAa,EACb,UAAU,EACV,SAAgB,GACjB,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,CAAC,CAyDtC;AAED,eAAe,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* sendAccessCodeEmail.ts
|
|
4
|
+
* Sends access code email via SMTP
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.sendAccessCodeEmail = sendAccessCodeEmail;
|
|
8
|
+
async function sendAccessCodeEmail({ email, accessCode, appName = 'ChAICodes', emailTemplate, smtpConfig, expiresAt = null, }) {
|
|
9
|
+
if (!email || !accessCode) {
|
|
10
|
+
throw new Error('Email and access code are required');
|
|
11
|
+
}
|
|
12
|
+
if (!smtpConfig) {
|
|
13
|
+
console.warn('SMTP config not provided, skipping email send');
|
|
14
|
+
return {
|
|
15
|
+
success: true,
|
|
16
|
+
message: 'Email sending skipped (no SMTP config)',
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
const nodemailer = require('nodemailer');
|
|
21
|
+
// Create transporter
|
|
22
|
+
const transporter = nodemailer.createTransport({
|
|
23
|
+
host: smtpConfig.host,
|
|
24
|
+
port: smtpConfig.port,
|
|
25
|
+
secure: smtpConfig.secure || true,
|
|
26
|
+
auth: {
|
|
27
|
+
user: smtpConfig.user,
|
|
28
|
+
pass: smtpConfig.password,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
// Replace placeholders in template
|
|
32
|
+
const html = emailTemplate
|
|
33
|
+
.replace('{accessCode}', accessCode)
|
|
34
|
+
.replace('{appName}', appName)
|
|
35
|
+
.replace('{email}', email)
|
|
36
|
+
.replace('{expiresAt}', expiresAt || 'N/A');
|
|
37
|
+
// Send email
|
|
38
|
+
const info = await transporter.sendMail({
|
|
39
|
+
from: smtpConfig.from || `noreply@${appName.toLowerCase()}.com`,
|
|
40
|
+
to: email,
|
|
41
|
+
subject: `Your ${appName} Access Code`,
|
|
42
|
+
html: html,
|
|
43
|
+
});
|
|
44
|
+
console.log(`Email sent to ${email}: ${info.messageId}`);
|
|
45
|
+
return {
|
|
46
|
+
success: true,
|
|
47
|
+
messageId: info.messageId,
|
|
48
|
+
recipient: email,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
53
|
+
console.error('Error sending email:', error);
|
|
54
|
+
return {
|
|
55
|
+
success: false,
|
|
56
|
+
error: message,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.default = sendAccessCodeEmail;
|
|
61
|
+
//# sourceMappingURL=sendAccessCodeEmail.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sendAccessCodeEmail.js","sourceRoot":"","sources":["../../src/email/sendAccessCodeEmail.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AA4BH,kDAgEC;AAhEM,KAAK,UAAU,mBAAmB,CAAC,EACxC,KAAK,EACL,UAAU,EACV,OAAO,GAAG,WAAW,EACrB,aAAa,EACb,UAAU,EACV,SAAS,GAAG,IAAI,GACJ;IACZ,IAAI,CAAC,KAAK,IAAI,CAAC,UAAU,EAAE,CAAC;QAC1B,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SAAS;YACzB,SAAS,EAAE,KAAK;SACjB,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvE,OAAO,CAAC,KAAK,CAAC,sBAAsB,EAAE,KAAK,CAAC,CAAC;QAC7C,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,OAAO;SACf,CAAC;IACJ,CAAC;AACH,CAAC;AAED,kBAAe,mBAAmB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @chaicodes/stripe-helpers
|
|
3
|
+
* Stripe integration utilities for ChAICodes applications
|
|
4
|
+
*/
|
|
5
|
+
export { createCheckoutSession } from './checkout/createCheckoutSession';
|
|
6
|
+
export { handlePriceSwitching } from './checkout/handlePriceSwitching';
|
|
7
|
+
export { createAccessCode, getAccessCodeByCode, updateAccessCodeStatus, incrementAccessCodeUses, createSession, validateSession, deleteSession, cleanupExpiredSessions, } from './database/databaseUtilities';
|
|
8
|
+
export { getDefaultEmailTemplate, buildCustomEmailTemplate } from './email/emailTemplates';
|
|
9
|
+
export { sendAccessCodeEmail } from './email/sendAccessCodeEmail';
|
|
10
|
+
export { generateAccessCode, calculateExpiryDate, getDaysRemaining, formatPrice, validateEmail, validateAccessCode, } from './utils/utilityFunctions';
|
|
11
|
+
export { handleCheckoutCompleted } from './webhooks/handleCheckoutCompleted';
|
|
12
|
+
export { handleStripeWebhook } from './webhooks/handleStripeWebhook';
|
|
13
|
+
export { handleSubscriptionUpdated, handleSubscriptionDeleted } from './webhooks/subscriptionHandlers';
|
|
14
|
+
export { DEFAULT_CONFIG, validateConfig, mergeConfig } from './config/defaultConfig';
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAGvE,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,sBAAsB,EACtB,uBAAuB,EACvB,aAAa,EACb,eAAe,EACf,aAAa,EACb,sBAAsB,GACvB,MAAM,8BAA8B,CAAC;AAGtC,OAAO,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAC3F,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAGlE,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,gBAAgB,EAChB,WAAW,EACX,aAAa,EACb,kBAAkB,GACnB,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EAAE,uBAAuB,EAAE,MAAM,oCAAoC,CAAC;AAC7E,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,yBAAyB,EAAE,yBAAyB,EAAE,MAAM,iCAAiC,CAAC;AAGvG,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @chaicodes/stripe-helpers
|
|
4
|
+
* Stripe integration utilities for ChAICodes applications
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.mergeConfig = exports.validateConfig = exports.DEFAULT_CONFIG = exports.handleSubscriptionDeleted = exports.handleSubscriptionUpdated = exports.handleStripeWebhook = exports.handleCheckoutCompleted = exports.validateAccessCode = exports.validateEmail = exports.formatPrice = exports.getDaysRemaining = exports.calculateExpiryDate = exports.generateAccessCode = exports.sendAccessCodeEmail = exports.buildCustomEmailTemplate = exports.getDefaultEmailTemplate = exports.cleanupExpiredSessions = exports.deleteSession = exports.validateSession = exports.createSession = exports.incrementAccessCodeUses = exports.updateAccessCodeStatus = exports.getAccessCodeByCode = exports.createAccessCode = exports.handlePriceSwitching = exports.createCheckoutSession = void 0;
|
|
8
|
+
// Checkout
|
|
9
|
+
var createCheckoutSession_1 = require("./checkout/createCheckoutSession");
|
|
10
|
+
Object.defineProperty(exports, "createCheckoutSession", { enumerable: true, get: function () { return createCheckoutSession_1.createCheckoutSession; } });
|
|
11
|
+
var handlePriceSwitching_1 = require("./checkout/handlePriceSwitching");
|
|
12
|
+
Object.defineProperty(exports, "handlePriceSwitching", { enumerable: true, get: function () { return handlePriceSwitching_1.handlePriceSwitching; } });
|
|
13
|
+
// Database
|
|
14
|
+
var databaseUtilities_1 = require("./database/databaseUtilities");
|
|
15
|
+
Object.defineProperty(exports, "createAccessCode", { enumerable: true, get: function () { return databaseUtilities_1.createAccessCode; } });
|
|
16
|
+
Object.defineProperty(exports, "getAccessCodeByCode", { enumerable: true, get: function () { return databaseUtilities_1.getAccessCodeByCode; } });
|
|
17
|
+
Object.defineProperty(exports, "updateAccessCodeStatus", { enumerable: true, get: function () { return databaseUtilities_1.updateAccessCodeStatus; } });
|
|
18
|
+
Object.defineProperty(exports, "incrementAccessCodeUses", { enumerable: true, get: function () { return databaseUtilities_1.incrementAccessCodeUses; } });
|
|
19
|
+
Object.defineProperty(exports, "createSession", { enumerable: true, get: function () { return databaseUtilities_1.createSession; } });
|
|
20
|
+
Object.defineProperty(exports, "validateSession", { enumerable: true, get: function () { return databaseUtilities_1.validateSession; } });
|
|
21
|
+
Object.defineProperty(exports, "deleteSession", { enumerable: true, get: function () { return databaseUtilities_1.deleteSession; } });
|
|
22
|
+
Object.defineProperty(exports, "cleanupExpiredSessions", { enumerable: true, get: function () { return databaseUtilities_1.cleanupExpiredSessions; } });
|
|
23
|
+
// Email
|
|
24
|
+
var emailTemplates_1 = require("./email/emailTemplates");
|
|
25
|
+
Object.defineProperty(exports, "getDefaultEmailTemplate", { enumerable: true, get: function () { return emailTemplates_1.getDefaultEmailTemplate; } });
|
|
26
|
+
Object.defineProperty(exports, "buildCustomEmailTemplate", { enumerable: true, get: function () { return emailTemplates_1.buildCustomEmailTemplate; } });
|
|
27
|
+
var sendAccessCodeEmail_1 = require("./email/sendAccessCodeEmail");
|
|
28
|
+
Object.defineProperty(exports, "sendAccessCodeEmail", { enumerable: true, get: function () { return sendAccessCodeEmail_1.sendAccessCodeEmail; } });
|
|
29
|
+
// Utilities
|
|
30
|
+
var utilityFunctions_1 = require("./utils/utilityFunctions");
|
|
31
|
+
Object.defineProperty(exports, "generateAccessCode", { enumerable: true, get: function () { return utilityFunctions_1.generateAccessCode; } });
|
|
32
|
+
Object.defineProperty(exports, "calculateExpiryDate", { enumerable: true, get: function () { return utilityFunctions_1.calculateExpiryDate; } });
|
|
33
|
+
Object.defineProperty(exports, "getDaysRemaining", { enumerable: true, get: function () { return utilityFunctions_1.getDaysRemaining; } });
|
|
34
|
+
Object.defineProperty(exports, "formatPrice", { enumerable: true, get: function () { return utilityFunctions_1.formatPrice; } });
|
|
35
|
+
Object.defineProperty(exports, "validateEmail", { enumerable: true, get: function () { return utilityFunctions_1.validateEmail; } });
|
|
36
|
+
Object.defineProperty(exports, "validateAccessCode", { enumerable: true, get: function () { return utilityFunctions_1.validateAccessCode; } });
|
|
37
|
+
// Webhooks
|
|
38
|
+
var handleCheckoutCompleted_1 = require("./webhooks/handleCheckoutCompleted");
|
|
39
|
+
Object.defineProperty(exports, "handleCheckoutCompleted", { enumerable: true, get: function () { return handleCheckoutCompleted_1.handleCheckoutCompleted; } });
|
|
40
|
+
var handleStripeWebhook_1 = require("./webhooks/handleStripeWebhook");
|
|
41
|
+
Object.defineProperty(exports, "handleStripeWebhook", { enumerable: true, get: function () { return handleStripeWebhook_1.handleStripeWebhook; } });
|
|
42
|
+
var subscriptionHandlers_1 = require("./webhooks/subscriptionHandlers");
|
|
43
|
+
Object.defineProperty(exports, "handleSubscriptionUpdated", { enumerable: true, get: function () { return subscriptionHandlers_1.handleSubscriptionUpdated; } });
|
|
44
|
+
Object.defineProperty(exports, "handleSubscriptionDeleted", { enumerable: true, get: function () { return subscriptionHandlers_1.handleSubscriptionDeleted; } });
|
|
45
|
+
// Config
|
|
46
|
+
var defaultConfig_1 = require("./config/defaultConfig");
|
|
47
|
+
Object.defineProperty(exports, "DEFAULT_CONFIG", { enumerable: true, get: function () { return defaultConfig_1.DEFAULT_CONFIG; } });
|
|
48
|
+
Object.defineProperty(exports, "validateConfig", { enumerable: true, get: function () { return defaultConfig_1.validateConfig; } });
|
|
49
|
+
Object.defineProperty(exports, "mergeConfig", { enumerable: true, get: function () { return defaultConfig_1.mergeConfig; } });
|
|
50
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,WAAW;AACX,0EAAyE;AAAhE,8HAAA,qBAAqB,OAAA;AAC9B,wEAAuE;AAA9D,4HAAA,oBAAoB,OAAA;AAE7B,WAAW;AACX,kEASsC;AARpC,qHAAA,gBAAgB,OAAA;AAChB,wHAAA,mBAAmB,OAAA;AACnB,2HAAA,sBAAsB,OAAA;AACtB,4HAAA,uBAAuB,OAAA;AACvB,kHAAA,aAAa,OAAA;AACb,oHAAA,eAAe,OAAA;AACf,kHAAA,aAAa,OAAA;AACb,2HAAA,sBAAsB,OAAA;AAGxB,QAAQ;AACR,yDAA2F;AAAlF,yHAAA,uBAAuB,OAAA;AAAE,0HAAA,wBAAwB,OAAA;AAC1D,mEAAkE;AAAzD,0HAAA,mBAAmB,OAAA;AAE5B,YAAY;AACZ,6DAOkC;AANhC,sHAAA,kBAAkB,OAAA;AAClB,uHAAA,mBAAmB,OAAA;AACnB,oHAAA,gBAAgB,OAAA;AAChB,+GAAA,WAAW,OAAA;AACX,iHAAA,aAAa,OAAA;AACb,sHAAA,kBAAkB,OAAA;AAGpB,WAAW;AACX,8EAA6E;AAApE,kIAAA,uBAAuB,OAAA;AAChC,sEAAqE;AAA5D,0HAAA,mBAAmB,OAAA;AAC5B,wEAAuG;AAA9F,iIAAA,yBAAyB,OAAA;AAAE,iIAAA,yBAAyB,OAAA;AAE7D,SAAS;AACT,wDAAqF;AAA5E,+GAAA,cAAc,OAAA;AAAE,+GAAA,cAAc,OAAA;AAAE,4GAAA,WAAW,OAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* utilityFunctions.ts
|
|
3
|
+
* Helper utility functions for stripe-helpers
|
|
4
|
+
*/
|
|
5
|
+
export declare function generateAccessCode(email: string, prefix?: string): string;
|
|
6
|
+
export declare function calculateExpiryDate(billingPeriod?: string): string;
|
|
7
|
+
export declare function getDaysRemaining(expiresAt: string | null): number | null;
|
|
8
|
+
export declare function formatPrice(cents: number): string;
|
|
9
|
+
export declare function validateEmail(email: string): boolean;
|
|
10
|
+
export declare function validateAccessCode(code: string): boolean;
|
|
11
|
+
declare const _default: {
|
|
12
|
+
generateAccessCode: typeof generateAccessCode;
|
|
13
|
+
calculateExpiryDate: typeof calculateExpiryDate;
|
|
14
|
+
getDaysRemaining: typeof getDaysRemaining;
|
|
15
|
+
formatPrice: typeof formatPrice;
|
|
16
|
+
validateEmail: typeof validateEmail;
|
|
17
|
+
validateAccessCode: typeof validateAccessCode;
|
|
18
|
+
};
|
|
19
|
+
export default _default;
|
|
20
|
+
//# sourceMappingURL=utilityFunctions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utilityFunctions.d.ts","sourceRoot":"","sources":["../../src/utils/utilityFunctions.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,MAAc,GAAG,MAAM,CAchF;AAED,wBAAgB,mBAAmB,CAAC,aAAa,GAAE,MAAiB,GAAG,MAAM,CAa5E;AAED,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CAWxE;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAGpD;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAGxD;;;;;;;;;AAED,wBAOE"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* utilityFunctions.ts
|
|
4
|
+
* Helper utility functions for stripe-helpers
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.generateAccessCode = generateAccessCode;
|
|
8
|
+
exports.calculateExpiryDate = calculateExpiryDate;
|
|
9
|
+
exports.getDaysRemaining = getDaysRemaining;
|
|
10
|
+
exports.formatPrice = formatPrice;
|
|
11
|
+
exports.validateEmail = validateEmail;
|
|
12
|
+
exports.validateAccessCode = validateAccessCode;
|
|
13
|
+
function generateAccessCode(email, prefix = 'SUB') {
|
|
14
|
+
if (!email) {
|
|
15
|
+
throw new Error('Email is required to generate access code');
|
|
16
|
+
}
|
|
17
|
+
// Extract domain from email
|
|
18
|
+
const emailParts = email.split('@');
|
|
19
|
+
const namePart = emailParts[0].split('.')[0].toUpperCase();
|
|
20
|
+
// Generate random suffix
|
|
21
|
+
const randomPart = Math.random().toString(36).substring(2, 8).toUpperCase();
|
|
22
|
+
// Format: PREFIX-NAME-RANDOM
|
|
23
|
+
return `${prefix}-${namePart}-${randomPart}`;
|
|
24
|
+
}
|
|
25
|
+
function calculateExpiryDate(billingPeriod = 'annual') {
|
|
26
|
+
const now = new Date();
|
|
27
|
+
let expiresAt;
|
|
28
|
+
if (billingPeriod === 'monthly') {
|
|
29
|
+
expiresAt = new Date(now.getTime() + 30 * 24 * 60 * 60 * 1000);
|
|
30
|
+
}
|
|
31
|
+
else if (billingPeriod === 'annual') {
|
|
32
|
+
expiresAt = new Date(now.getTime() + 365 * 24 * 60 * 60 * 1000);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
throw new Error('Invalid billing period. Use "monthly" or "annual"');
|
|
36
|
+
}
|
|
37
|
+
return expiresAt.toISOString();
|
|
38
|
+
}
|
|
39
|
+
function getDaysRemaining(expiresAt) {
|
|
40
|
+
if (!expiresAt) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const now = new Date();
|
|
44
|
+
const expiry = new Date(expiresAt);
|
|
45
|
+
const diffMs = expiry.getTime() - now.getTime();
|
|
46
|
+
const diffDays = Math.ceil(diffMs / (1000 * 60 * 60 * 24));
|
|
47
|
+
return Math.max(0, diffDays);
|
|
48
|
+
}
|
|
49
|
+
function formatPrice(cents) {
|
|
50
|
+
return (cents / 100).toFixed(2);
|
|
51
|
+
}
|
|
52
|
+
function validateEmail(email) {
|
|
53
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
54
|
+
return emailRegex.test(email);
|
|
55
|
+
}
|
|
56
|
+
function validateAccessCode(code) {
|
|
57
|
+
// Access codes should be in format: PREFIX-NAME-RANDOM
|
|
58
|
+
return /^[A-Z]+-[A-Z0-9]+-[A-Z0-9]+$/.test(code);
|
|
59
|
+
}
|
|
60
|
+
exports.default = {
|
|
61
|
+
generateAccessCode,
|
|
62
|
+
calculateExpiryDate,
|
|
63
|
+
getDaysRemaining,
|
|
64
|
+
formatPrice,
|
|
65
|
+
validateEmail,
|
|
66
|
+
validateAccessCode,
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=utilityFunctions.js.map
|