@goweekdays/core 2.4.2 → 2.6.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/dist/index.mjs CHANGED
@@ -906,9 +906,11 @@ var SPACES_BUCKET = process.env.SPACES_BUCKET;
906
906
  var PAYPAL_CLIENT_ID = process.env.PAYPAL_CLIENT_ID ?? "";
907
907
  var PAYPAL_CLIENT_SECRET = process.env.PAYPAL_CLIENT_SECRET ?? "";
908
908
  var PAYPAL_API_URL = process.env.PAYPAL_API_URL ?? "https://api-m.sandbox.paypal.com";
909
+ var PAYPAL_WEBHOOK_ID = process.env.PAYPAL_WEBHOOK_ID ?? "";
909
910
  var XENDIT_SECRET_KEY = process.env.XENDIT_SECRET_KEY ?? "";
910
911
  var XENDIT_BASE_URL = process.env.XENDIT_BASE_URL ?? "https://api.xendit.co";
911
912
  var DOMAIN = process.env.DOMAIN || "localhost";
913
+ var APP_ORG = process.env.APP_ORG || "http://localhost/organizations/create";
912
914
 
913
915
  // src/resources/user/user.service.ts
914
916
  import {
@@ -3502,6 +3504,11 @@ function useAppService() {
3502
3504
  code: "ride",
3503
3505
  name: "Ride",
3504
3506
  description: "Transportation and ride services."
3507
+ },
3508
+ {
3509
+ code: "job",
3510
+ name: "jobs",
3511
+ description: "Job listings and recruitment services."
3505
3512
  }
3506
3513
  ];
3507
3514
  const session = useAtlas9.getClient()?.startSession();
@@ -6340,6 +6347,102 @@ function usePlanController() {
6340
6347
  };
6341
6348
  }
6342
6349
 
6350
+ // src/resources/utils/paypal.service.ts
6351
+ import {
6352
+ Client,
6353
+ Environment,
6354
+ OrdersController,
6355
+ CheckoutPaymentIntent,
6356
+ OrderApplicationContextUserAction
6357
+ } from "@paypal/paypal-server-sdk";
6358
+ import crypto3 from "crypto";
6359
+ import crc32 from "buffer-crc32";
6360
+ var certCache = /* @__PURE__ */ new Map();
6361
+ function usePaypalService() {
6362
+ const paypalClient = new Client({
6363
+ clientCredentialsAuthCredentials: {
6364
+ oAuthClientId: PAYPAL_CLIENT_ID,
6365
+ oAuthClientSecret: PAYPAL_CLIENT_SECRET
6366
+ },
6367
+ timeout: 0,
6368
+ environment: isDev ? Environment.Sandbox : Environment.Production
6369
+ });
6370
+ function addOrder({
6371
+ amount = 0,
6372
+ currency = "PHP",
6373
+ customId = "",
6374
+ returnUrl = "",
6375
+ cancelUrl = "",
6376
+ action = "pay"
6377
+ } = {}) {
6378
+ return new OrdersController(paypalClient).createOrder({
6379
+ body: {
6380
+ intent: CheckoutPaymentIntent.Capture,
6381
+ purchaseUnits: [
6382
+ {
6383
+ amount: {
6384
+ currencyCode: currency,
6385
+ value: amount.toFixed(2)
6386
+ },
6387
+ customId
6388
+ }
6389
+ ],
6390
+ applicationContext: {
6391
+ returnUrl,
6392
+ cancelUrl,
6393
+ userAction: action === "pay" ? OrderApplicationContextUserAction.PayNow : action === "continue" ? OrderApplicationContextUserAction.Continue : void 0
6394
+ }
6395
+ },
6396
+ prefer: "return=minimal"
6397
+ });
6398
+ }
6399
+ function captureOrder(id) {
6400
+ return new OrdersController(paypalClient).captureOrder({ id });
6401
+ }
6402
+ async function downloadAndCacheCert(url) {
6403
+ const cachedCert = certCache.get(url);
6404
+ if (cachedCert) {
6405
+ return cachedCert;
6406
+ }
6407
+ const response = await fetch(url);
6408
+ if (!response.ok) {
6409
+ throw new Error(
6410
+ `Failed to download PayPal certificate: ${response.statusText}`
6411
+ );
6412
+ }
6413
+ const certPem = await response.text();
6414
+ certCache.set(url, certPem);
6415
+ return certPem;
6416
+ }
6417
+ async function verifyWebhook(rawBody, headers, webhookId = PAYPAL_WEBHOOK_ID) {
6418
+ const transmissionId = headers["paypal-transmission-id"];
6419
+ const timeStamp = headers["paypal-transmission-time"];
6420
+ const certUrl = headers["paypal-cert-url"];
6421
+ const transmissionSig = headers["paypal-transmission-sig"];
6422
+ if (!transmissionId || !timeStamp || !certUrl || !transmissionSig) {
6423
+ return false;
6424
+ }
6425
+ const eventBuffer = Buffer.isBuffer(rawBody) ? rawBody : Buffer.from(rawBody);
6426
+ const crcValue = parseInt("0x" + crc32(eventBuffer).toString("hex"));
6427
+ const message = `${transmissionId}|${timeStamp}|${webhookId}|${crcValue}`;
6428
+ try {
6429
+ const certPem = await downloadAndCacheCert(certUrl);
6430
+ const signatureBuffer = Buffer.from(transmissionSig, "base64");
6431
+ const verifier = crypto3.createVerify("SHA256");
6432
+ verifier.update(message);
6433
+ return verifier.verify(certPem, signatureBuffer);
6434
+ } catch (error) {
6435
+ console.error("PayPal webhook verification error:", error);
6436
+ throw new Error("Failed to verify PayPal webhook signature.");
6437
+ }
6438
+ }
6439
+ return {
6440
+ addOrder,
6441
+ captureOrder,
6442
+ verifyWebhook
6443
+ };
6444
+ }
6445
+
6343
6446
  // src/resources/organization/organization.service.ts
6344
6447
  function useOrgService() {
6345
6448
  const { add: addOrg } = useOrgRepo();
@@ -6350,6 +6453,7 @@ function useOrgService() {
6350
6453
  const { getDefault } = usePlanRepo();
6351
6454
  const { add: addSubscription } = useSubscriptionRepo();
6352
6455
  const { add: addSubscriptionTransaction } = useSubscriptionTransactionRepo();
6456
+ const { addOrder: addPaypalOrder } = usePaypalService();
6353
6457
  async function add(value) {
6354
6458
  const { error } = schemaOrgAdd.validate(value);
6355
6459
  if (error) {
@@ -6444,8 +6548,22 @@ function useOrgService() {
6444
6548
  },
6445
6549
  session
6446
6550
  );
6551
+ const order = await addPaypalOrder({
6552
+ amount,
6553
+ currency: plan.currency,
6554
+ customId: subscriptionId,
6555
+ returnUrl: `${APP_ORG}/organizations/success`,
6556
+ cancelUrl: `${APP_ORG}/organizations/cancel`,
6557
+ action: "pay"
6558
+ });
6447
6559
  await session?.commitTransaction();
6448
- return String(org);
6560
+ const paypalOrderLink = JSON.parse(order.body.toString()).links.find(
6561
+ (link) => link.rel === "approve"
6562
+ );
6563
+ return {
6564
+ org: String(org),
6565
+ paypalOrderLink: paypalOrderLink ? paypalOrderLink.href : ""
6566
+ };
6449
6567
  } catch (error2) {
6450
6568
  await session?.abortTransaction();
6451
6569
  throw error2;
@@ -6478,10 +6596,10 @@ function useOrgController() {
6478
6596
  return;
6479
6597
  }
6480
6598
  try {
6481
- const org = await _add(value);
6599
+ const data = await _add(value);
6482
6600
  res.json({
6483
6601
  message: "Organization created successfully.",
6484
- data: { org }
6602
+ data
6485
6603
  });
6486
6604
  return;
6487
6605
  } catch (error2) {
@@ -9739,9 +9857,26 @@ function useUtilController() {
9739
9857
  }
9740
9858
  }
9741
9859
  }
9860
+ const { verifyWebhook } = usePaypalService();
9861
+ async function paypalWebhook(req, res, next) {
9862
+ console.log(req);
9863
+ try {
9864
+ await verifyWebhook(req.body, req.headers, PAYPAL_WEBHOOK_ID);
9865
+ res.status(200).send("OK");
9866
+ return;
9867
+ } catch (error) {
9868
+ logger28.log({
9869
+ level: "error",
9870
+ message: "PayPal webhook verification failed"
9871
+ });
9872
+ res.status(400).send("Invalid webhook");
9873
+ return;
9874
+ }
9875
+ }
9742
9876
  return {
9743
9877
  healthCheck,
9744
- setGitHubVariables
9878
+ setGitHubVariables,
9879
+ paypalWebhook
9745
9880
  };
9746
9881
  }
9747
9882
 
@@ -10007,6 +10142,7 @@ export {
10007
10142
  ACCESS_TOKEN_SECRET,
10008
10143
  APP_ACCOUNT,
10009
10144
  APP_MAIN,
10145
+ APP_ORG,
10010
10146
  DEFAULT_USER_EMAIL,
10011
10147
  DEFAULT_USER_FIRST_NAME,
10012
10148
  DEFAULT_USER_LAST_NAME,
@@ -10025,6 +10161,7 @@ export {
10025
10161
  PAYPAL_API_URL,
10026
10162
  PAYPAL_CLIENT_ID,
10027
10163
  PAYPAL_CLIENT_SECRET,
10164
+ PAYPAL_WEBHOOK_ID,
10028
10165
  PORT,
10029
10166
  REDIS_HOST,
10030
10167
  REDIS_PASSWORD,