-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathget-paypal-mapping.js
More file actions
259 lines (219 loc) · 6.69 KB
/
Copy pathget-paypal-mapping.js
File metadata and controls
259 lines (219 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: BUSL-1.1
*/
// eslint-disable-next-line import/no-unassigned-import
require('#config/env');
const process = require('node:process');
const Graceful = require('@ladjs/graceful');
const mongoose = require('mongoose');
const logger = require('#helpers/logger');
const { paypalAgent } = require('#helpers/paypal');
const setupMongoose = require('#helpers/setup-mongoose');
const graceful = new Graceful({
mongooses: [mongoose],
logger
});
graceful.listen();
// Pricing structure from PAYPAL_MAPPING
const PAYPAL_PRICING = {
enhanced_protection: {
'30d': 3,
'60d': 6,
'90d': 9,
'180d': 18,
'1y': 36
},
team: {
'30d': 9,
'60d': 18,
'90d': 27,
'180d': 54,
'1y': 108
}
};
// Duration mapping for PayPal billing cycles
const DURATION_MAPPING = {
'30d': { interval_unit: 'MONTH', interval_count: 1 },
'60d': { interval_unit: 'MONTH', interval_count: 2 },
'90d': { interval_unit: 'MONTH', interval_count: 3 },
'180d': { interval_unit: 'MONTH', interval_count: 6 },
'1y': { interval_unit: 'YEAR', interval_count: 1 }
};
async function findOrCreateProduct(agent, productName, description) {
try {
// List all products to find existing one
console.log(`Looking for existing product: ${productName}`);
const { body: productsResponse } = await agent.get(
'/v1/catalogs/products?page_size=20'
);
const existingProduct = productsResponse.products?.find(
(product) => product.name === productName
);
if (existingProduct) {
console.log(
`Found existing product: ${productName} (ID: ${existingProduct.id})`
);
return existingProduct.id;
}
// Create new product
console.log(`Creating new product: ${productName}`);
const productData = {
name: productName,
description,
type: 'SERVICE',
category: 'SOFTWARE'
};
const { body: newProduct } = await agent
.post('/v1/catalogs/products')
.send(productData);
console.log(`Created product: ${productName} (ID: ${newProduct.id})`);
return newProduct.id;
} catch (err) {
console.error(`Error with product ${productName}:`, err.message);
throw err;
}
}
// eslint-disable-next-line max-params
async function findOrCreatePlan(
agent,
productId,
planName,
description,
pricing,
duration
) {
try {
// List all plans to find existing one
console.log(`Looking for existing plan: ${planName}`);
const { body: plansResponse } = await agent.get(
`/v1/billing/plans?product_id=${productId}&page_size=20`
);
const existingPlan = plansResponse.plans?.find(
(plan) => plan.name === planName
);
if (existingPlan) {
console.log(`Found existing plan: ${planName} (ID: ${existingPlan.id})`);
return existingPlan.id;
}
// Create new plan
console.log(`Creating new plan: ${planName}`);
const billingCycle = DURATION_MAPPING[duration];
const planData = {
product_id: productId,
name: planName,
description,
status: 'ACTIVE',
billing_cycles: [
{
frequency: {
interval_unit: billingCycle.interval_unit,
interval_count: billingCycle.interval_count
},
tenure_type: 'REGULAR',
sequence: 1,
total_cycles: 0, // Infinite cycles
pricing_scheme: {
fixed_price: {
value: pricing.toString(),
currency_code: 'USD'
}
}
}
],
payment_preferences: {
auto_bill_outstanding: true,
setup_fee: {
value: '0',
currency_code: 'USD'
},
setup_fee_failure_action: 'CONTINUE',
payment_failure_threshold: 3
}
};
const { body: newPlan } = await agent
.post('/v1/billing/plans')
.send(planData);
console.log(`Created plan: ${planName} (ID: ${newPlan.id})`);
return newPlan.id;
} catch (err) {
console.error(`Error with plan ${planName}:`, err.message);
throw err;
}
}
async function generatePayPalMapping() {
try {
console.log('Starting PayPal mapping generation...');
const agent = await paypalAgent();
const mapping = {};
// Process each product type
for (const [productType, durations] of Object.entries(PAYPAL_PRICING)) {
console.log(`\nProcessing product type: ${productType}`);
// Create or find product
const productName =
productType === 'enhanced_protection'
? 'Forward Email Enhanced Protection'
: 'Forward Email Team Plan';
const productDescription =
productType === 'enhanced_protection'
? 'Enhanced email protection and privacy features'
: 'Team collaboration and advanced email management';
const productId = await findOrCreateProduct(
agent,
productName,
productDescription
);
// Initialize mapping for this product type
mapping[productType] = {};
// Process each duration
for (const [duration, pricing] of Object.entries(durations)) {
console.log(`\nProcessing duration: ${duration} for ${productType}`);
const planName = `${productName} - ${duration.toUpperCase()}`;
const planDescription = `${productDescription} - ${duration} billing cycle`;
const planId = await findOrCreatePlan(
agent,
productId,
planName,
planDescription,
pricing,
duration
);
mapping[productType][duration] = planId;
}
}
// Generate the output
console.log('\n' + '='.repeat(80));
console.log('PAYPAL_PLAN_MAPPING GENERATED SUCCESSFULLY!');
console.log('='.repeat(80));
console.log('\nAdd these to your .env file:');
console.log('');
// Enhanced Protection Plans
console.log('# Enhanced Protection Plans');
for (const [duration, planId] of Object.entries(
mapping.enhanced_protection
)) {
const envVar = `PAYPAL_ENHANCED_PLAN_${duration.toUpperCase()}`;
console.log(`${envVar}=${planId}`);
}
console.log('');
// Team Plans
console.log('# Team Plans');
for (const [duration, planId] of Object.entries(mapping.team)) {
const envVar = `PAYPAL_TEAM_PLAN_${duration.toUpperCase()}`;
console.log(`${envVar}=${planId}`);
}
console.log('');
console.log('='.repeat(80));
console.log('Copy the above environment variables to your .env file');
console.log('='.repeat(80));
return mapping;
} catch (err) {
console.error('Failed to generate PayPal mapping:', err);
throw err;
}
}
(async () => {
await setupMongoose(logger);
await generatePayPalMapping();
process.exit(0);
})();