JOHN SILLVESTOR commited on
Commit
04ed9c3
·
verified ·
1 Parent(s): d0225fe

Show Apple Pay and Google Pay on checkout

Browse files
Files changed (1) hide show
  1. server.js +46 -1
server.js CHANGED
@@ -2469,6 +2469,47 @@ function payUrls(ret) {
2469
  };
2470
  }
2471
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2472
  function payGate(req, res) {
2473
  if (ROLE !== "indexer") {
2474
  json(res, 409, { error: "use_s1" });
@@ -2534,6 +2575,7 @@ async function handleVipStart(req, res) {
2534
  if (!plan || !email) return json(res, 400, { error: "plan" });
2535
  const auto = planKey === "month" && body.auto === true;
2536
  const urls = payUrls(body.returnUrl);
 
2537
  const reused = await openPay(idp.sub, planKey, email, auto);
2538
  if (reused) return json(res, 200, reused);
2539
  const nonce = crypto.randomBytes(8).toString("hex");
@@ -2541,6 +2583,7 @@ async function handleVipStart(req, res) {
2541
  const homeMsg = "After pay you return to [DUB LK 2000 Home](" + HOME_URL + ")";
2542
  const fields = {
2543
  mode: "payment",
 
2544
  success_url: urls.success,
2545
  cancel_url: urls.cancel,
2546
  client_reference_id: idp.sub,
@@ -3248,7 +3291,9 @@ async function main() {
3248
  if (sock.writable) sock.end("HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n");
3249
  } catch { }
3250
  });
3251
- srv.listen(PORT, "0.0.0.0");
 
 
3252
  }
3253
 
3254
  process.on("unhandledRejection", (e) => {
 
2469
  };
2470
  }
2471
 
2472
+ let walletReady = false;
2473
+ let walletNext = 0;
2474
+ let walletJob = null;
2475
+
2476
+ function payPref(node) {
2477
+ const d = node && node.display_preference;
2478
+ if (!d) return "";
2479
+ return String(d.value || d.preference || "");
2480
+ }
2481
+
2482
+ async function ensureFreeWallets() {
2483
+ if (walletReady || ROLE !== "indexer" || !STRIPE_KEY || Date.now() < walletNext) return;
2484
+ if (walletJob) return walletJob;
2485
+ walletJob = (async () => {
2486
+ const list = await stripeGet("payment_method_configurations?limit=10");
2487
+ const rows = list && Array.isArray(list.data) ? list.data : [];
2488
+ const cfg = rows.find((x) => x && x.is_default && x.active) || rows[0];
2489
+ if (!cfg || !/^pmc_/.test(String(cfg.id || ""))) {
2490
+ walletNext = Date.now() + 600000;
2491
+ return;
2492
+ }
2493
+ const want = { card: "on", apple_pay: "on", google_pay: "on", link: "on", paypal: "off" };
2494
+ let failed = false;
2495
+ for (const key of Object.keys(want)) {
2496
+ if (!cfg[key]) continue;
2497
+ if (payPref(cfg[key]) === want[key]) continue;
2498
+ const saved = await stripeForm("payment_method_configurations/" + cfg.id, {
2499
+ [key + "[display_preference][preference]"]: want[key]
2500
+ });
2501
+ if (!saved) failed = true;
2502
+ }
2503
+ if (failed) walletNext = Date.now() + 600000;
2504
+ else walletReady = true;
2505
+ })().catch(() => {
2506
+ walletNext = Date.now() + 600000;
2507
+ }).finally(() => {
2508
+ walletJob = null;
2509
+ });
2510
+ return walletJob;
2511
+ }
2512
+
2513
  function payGate(req, res) {
2514
  if (ROLE !== "indexer") {
2515
  json(res, 409, { error: "use_s1" });
 
2575
  if (!plan || !email) return json(res, 400, { error: "plan" });
2576
  const auto = planKey === "month" && body.auto === true;
2577
  const urls = payUrls(body.returnUrl);
2578
+ await ensureFreeWallets();
2579
  const reused = await openPay(idp.sub, planKey, email, auto);
2580
  if (reused) return json(res, 200, reused);
2581
  const nonce = crypto.randomBytes(8).toString("hex");
 
2583
  const homeMsg = "After pay you return to [DUB LK 2000 Home](" + HOME_URL + ")";
2584
  const fields = {
2585
  mode: "payment",
2586
+ "payment_method_types[0]": "card",
2587
  success_url: urls.success,
2588
  cancel_url: urls.cancel,
2589
  client_reference_id: idp.sub,
 
3291
  if (sock.writable) sock.end("HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n");
3292
  } catch { }
3293
  });
3294
+ srv.listen(PORT, "0.0.0.0", () => {
3295
+ if (ROLE === "indexer" && STRIPE_KEY) ensureFreeWallets().catch(() => { });
3296
+ });
3297
  }
3298
 
3299
  process.on("unhandledRejection", (e) => {