File size: 6,774 Bytes
ccc21f3 | 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 | import { logger } from "./logger";
import { db, appConfigTable } from "@workspace/db";
import { eq } from "drizzle-orm";
const PAYPAL_MODE = process.env["PAYPAL_MODE"] ?? "sandbox";
const PAYPAL_BASE =
PAYPAL_MODE === "live"
? "https://api-m.paypal.com"
: "https://api-m.sandbox.paypal.com";
const CLIENT_ID = process.env["PAYPAL_CLIENT_ID"];
const CLIENT_SECRET = process.env["PAYPAL_CLIENT_SECRET"];
if (!CLIENT_ID || !CLIENT_SECRET) {
logger.warn("PayPal credentials missing — subscription endpoints will fail");
}
interface CachedToken {
token: string;
expiresAt: number;
}
let cachedToken: CachedToken | null = null;
export async function getAccessToken(): Promise<string> {
if (cachedToken && cachedToken.expiresAt > Date.now() + 60_000) {
return cachedToken.token;
}
const credentials = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString(
"base64",
);
const res = await fetch(`${PAYPAL_BASE}/v1/oauth2/token`, {
method: "POST",
headers: {
Authorization: `Basic ${credentials}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: "grant_type=client_credentials",
});
if (!res.ok) {
const text = await res.text();
throw new Error(`PayPal auth failed: ${res.status} ${text}`);
}
const data = (await res.json()) as { access_token: string; expires_in: number };
cachedToken = {
token: data.access_token,
expiresAt: Date.now() + data.expires_in * 1000,
};
return cachedToken.token;
}
async function paypalRequest(
method: string,
path: string,
body?: unknown,
extraHeaders?: Record<string, string>,
): Promise<unknown> {
const token = await getAccessToken();
const res = await fetch(`${PAYPAL_BASE}${path}`, {
method,
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
Accept: "application/json",
...(extraHeaders ?? {}),
},
body: body !== undefined ? JSON.stringify(body) : undefined,
});
const text = await res.text();
let parsed: unknown = null;
if (text) {
try {
parsed = JSON.parse(text);
} catch {
parsed = text;
}
}
if (!res.ok) {
logger.error({ status: res.status, path, body, response: parsed }, "PayPal request failed");
throw new Error(`PayPal ${method} ${path} failed: ${res.status}`);
}
return parsed;
}
async function deleteCachedConfig(key: string): Promise<void> {
await db.delete(appConfigTable).where(eq(appConfigTable.key, key));
}
async function checkPlanExists(planId: string): Promise<boolean> {
try {
const res = (await paypalRequest("GET", `/v1/billing/plans/${planId}`)) as { status?: string };
return !!res?.status;
} catch {
return false;
}
}
async function checkProductExists(productId: string): Promise<boolean> {
try {
const res = (await paypalRequest("GET", `/v1/catalogs/products/${productId}`)) as { id?: string };
return !!res?.id;
} catch {
return false;
}
}
async function getOrCreateProductId(): Promise<string> {
const [row] = await db
.select()
.from(appConfigTable)
.where(eq(appConfigTable.key, "paypal_product_id"));
if (row) {
const valid = await checkProductExists(row.value);
if (valid) return row.value;
logger.warn("Cached PayPal product not found in this environment — recreating");
await deleteCachedConfig("paypal_product_id");
}
const product = (await paypalRequest("POST", "/v1/catalogs/products", {
name: "FB Group Publisher PRO",
description: "Subscription access to FB Group Publisher PRO",
type: "SERVICE",
category: "SOFTWARE",
})) as { id: string };
await db
.insert(appConfigTable)
.values({ key: "paypal_product_id", value: product.id });
logger.info({ productId: product.id }, "Created new PayPal product");
return product.id;
}
export async function getOrCreatePlanId(): Promise<string> {
const [row] = await db
.select()
.from(appConfigTable)
.where(eq(appConfigTable.key, "paypal_plan_id_v2"));
if (row) {
const valid = await checkPlanExists(row.value);
if (valid) return row.value;
logger.warn({ planId: row.value }, "Cached PayPal plan not found in this environment — recreating");
await deleteCachedConfig("paypal_plan_id_v2");
}
const productId = await getOrCreateProductId();
const plan = (await paypalRequest("POST", "/v1/billing/plans", {
product_id: productId,
name: "FB Group Publisher PRO – $2.50/2months",
description: "$2.50 every 2 months (trial handled in-app)",
status: "ACTIVE",
billing_cycles: [
{
frequency: { interval_unit: "MONTH", interval_count: 2 },
tenure_type: "REGULAR",
sequence: 1,
total_cycles: 0,
pricing_scheme: {
fixed_price: { value: "2.50", currency_code: "USD" },
},
},
],
payment_preferences: {
auto_bill_outstanding: true,
setup_fee: { value: "0", currency_code: "USD" },
setup_fee_failure_action: "CONTINUE",
payment_failure_threshold: 2,
},
})) as { id: string };
await db
.insert(appConfigTable)
.values({ key: "paypal_plan_id_v2", value: plan.id });
logger.info({ planId: plan.id }, "Created new PayPal plan");
return plan.id;
}
export interface CreateSubResult {
id: string;
approvalUrl: string;
}
export async function createSubscription(
returnUrl: string,
cancelUrl: string,
customId: string,
): Promise<CreateSubResult> {
const planId = await getOrCreatePlanId();
const sub = (await paypalRequest("POST", "/v1/billing/subscriptions", {
plan_id: planId,
custom_id: customId,
application_context: {
brand_name: "FB Group Publisher PRO",
locale: "ar-SA",
shipping_preference: "NO_SHIPPING",
user_action: "SUBSCRIBE_NOW",
return_url: returnUrl,
cancel_url: cancelUrl,
},
})) as { id: string; links: Array<{ rel: string; href: string }> };
const approval = sub.links.find((l) => l.rel === "approve");
if (!approval) throw new Error("PayPal did not return approval link");
return { id: sub.id, approvalUrl: approval.href };
}
export interface PaypalSubscription {
id: string;
status: string;
custom_id?: string;
billing_info?: {
next_billing_time?: string;
};
start_time?: string;
}
export async function getSubscription(
subscriptionId: string,
): Promise<PaypalSubscription> {
return (await paypalRequest(
"GET",
`/v1/billing/subscriptions/${subscriptionId}`,
)) as PaypalSubscription;
}
export async function cancelSubscriptionApi(
subscriptionId: string,
reason: string,
): Promise<void> {
await paypalRequest(
"POST",
`/v1/billing/subscriptions/${subscriptionId}/cancel`,
{ reason },
);
}
|