kryptheon 0.1.54 → 0.1.55

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.
@@ -128,6 +128,26 @@ function fixPrompt(result, f) {
128
128
  'Keep the signature check first. Do not change anything else. Afterwards I will run: npx kryptheon payments',
129
129
  ]).join('\n');
130
130
  }
131
+ if (f.kind === 'paid-nothing') {
132
+ return [
133
+ 'A customer can pay in my app and get nothing for it.',
134
+ '',
135
+ 'I tested it with a real ' + ADJ(result.provider) + ' test payment: the ' + f.events.join(' / ') + ' webhook reached ' + where +
136
+ ', which answered ' + f.status + (f.said ? ' (' + f.said.slice(0, 120) + ')' : '') +
137
+ (f.logged ? ' and changed nothing in my database but its log.' : ' and changed nothing in my database.'),
138
+ f.before ? 'Nothing before it (checkout, the payment page) gave anything either, so the plan, credits or order this payment buys was never given.'
139
+ : 'So the plan, credits or order this payment buys was not given by the webhook.',
140
+ '',
141
+ 'First tell me where this app is meant to give what a payment buys. Then:',
142
+ '- make ' + where + ' act on the event ' + who + ' really sends for a finished payment (' +
143
+ (result.provider === 'razorpay' ? 'payment.captured / order.paid' : result.provider === 'stripe' ? 'checkout.session.completed / invoice.paid' : f.events.join(' / ')) +
144
+ ') - check the event names my handler listens for against ' + who + '\'s dashboard;',
145
+ '- find the customer from the payment (the notes, order id or customer id my checkout passes) and give the plan, credits or order;',
146
+ '- save the payment id with a UNIQUE rule first, so the same webhook sent again gives nothing twice;',
147
+ '- answer 200 once it is saved.',
148
+ 'Keep the signature check first. Do not change anything else. Afterwards I will run: npx kryptheon payments',
149
+ ].join('\n');
150
+ }
131
151
  if (f.kind === 'refund-nothing') {
132
152
  return [
133
153
  'When ' + who + ' tells my app a payment was refunded, my app changes nothing.',
@@ -220,6 +240,18 @@ function findingLines(result, f) {
220
240
  ' Your database changed again:',
221
241
  ].concat(f.changes.map((c) => ' ' + c), f.noise.length ? [' Also changed, which is normal:'].concat(f.noise.map((c) => ' ' + c)) : []);
222
242
  }
243
+ if (f.kind === 'paid-nothing') {
244
+ return [
245
+ 'HIGH A payment went through, and your app gave nothing - to check.',
246
+ '',
247
+ ' ' + f.events.join(' / ') + ' reached ' + result.path + ', which answered ' + f.status +
248
+ (f.said ? ' (' + f.said.slice(0, 80) + ')' : '') + (f.logged ? ' and wrote nothing but its log.' : ' and wrote nothing.'),
249
+ f.before ? ' Nothing before it gave anything either (no plan, credits or payment saved at checkout).'
250
+ : ' What the app saved before this webhook was not read.',
251
+ ' The customer paid and has nothing to show for it. Fine if your app gives it some',
252
+ ' other way (by hand, from another service); a hole if it is meant to give it here.',
253
+ ];
254
+ }
223
255
  if (f.kind === 'refund-nothing') {
224
256
  return [
225
257
  'MEDIUM A refund took nothing back - to check.',
@@ -385,6 +417,12 @@ async function run(root, options) {
385
417
  // being checked - what that check sends would be in it.
386
418
  let beforeRead = null;
387
419
  let checking = false;
420
+ // The database when nothing is being paid: now, and again after each
421
+ // payment is checked. What changed from it to a payment's first webhook is
422
+ // what checkout gave, so a payment the webhook gives nothing for is not
423
+ // called empty when the page already gave it.
424
+ const readQuiet = () => payments.snapshot(client, schema, tables).catch(() => null);
425
+ let quiet = await readQuiet();
388
426
  const readBefore = () => {
389
427
  if (checking) return Promise.resolve(null);
390
428
  return (beforeRead = beforeRead || (async () => {
@@ -436,6 +474,8 @@ async function run(root, options) {
436
474
  const hooks = capture.captured.slice(next);
437
475
  next = capture.captured.length;
438
476
  const before = beforeRead ? await beforeRead.catch(() => null) : null;
477
+ const since = quiet && before && !before.late ? payments.writesOf(payments.diff(quiet, before.snap)) : null;
478
+ const groupTypes = hooks.map((h) => payments.eventOf(h).type);
439
479
  checking = true;
440
480
  for (const [i, hook] of hooks.entries()) {
441
481
  const mode = payments.modeOf(hook.provider, hook.body, env);
@@ -453,6 +493,7 @@ async function run(root, options) {
453
493
  const options_ = { client: client, schema: schema, target: app, hook: hook, say: (l) => say(' ' + l),
454
494
  quietMs: options.quietMs, minMs: options.minMs, maxMs: options.maxMs,
455
495
  before: i === 0 && before && !before.late ? { snap: before.snap, shared: hooks.length > 1 } : null,
496
+ sinceWrites: i === 0 ? since : null, groupTypes: i === 0 ? groupTypes : null,
456
497
  beforeWhy: i > 0 ? 'what it wrote is counted with the first webhook of this payment, which arrived with it'
457
498
  : before && before.late ? 'reading the database first took too long, so the webhook went on before it finished' : '' };
458
499
  say('');
@@ -476,6 +517,7 @@ async function run(root, options) {
476
517
  }
477
518
  checking = false;
478
519
  beforeRead = null;
520
+ quiet = await readQuiet();
479
521
  save(root, report);
480
522
  say('');
481
523
  say('Ready for the next payment - make it now, or press ' + (options.stopWith ? 'Stop' : 'Ctrl-C') + ' when you are done.');
@@ -422,6 +422,32 @@ const ok = (status) => status !== null && status >= 200 && status < 300;
422
422
  // Events that say a payment was refunded, on either provider.
423
423
  const REFUND_EVENT = /^(refund\.(created|processed)|payment\.refunded|charge\.refunded)$/;
424
424
 
425
+ // Events that say a payment went through: payment.captured, order.paid,
426
+ // subscription.charged, checkout.session.completed, invoice.paid,
427
+ // payment_intent.succeeded - and an app's own "payment.succeeded". Not
428
+ // payment.authorized (not taken yet) or Razorpay's subscription.completed
429
+ // (its last cycle ended).
430
+ const PAID_EVENT = /(^|[._])(paid|captured|succeeded|completed|charged|activated)$/i;
431
+ const NOT_PAID_EVENT = /^subscription\.completed$/;
432
+ const isPaidEvent = (type) => PAID_EVENT.test(String(type || '')) && !NOT_PAID_EVENT.test(String(type || ''));
433
+
434
+ // What a payment gives, by column or by table: written before its webhook
435
+ // (at checkout, from the page), it was given there.
436
+ const GRANT_COLUMN = /plan|tier|premium|(^|_)pro($|_)|credit|balance|quota|access|role|status|paid|active|subscri|expir|period|valid_until|wallet|seats/i;
437
+ const GRANT_TABLE = /subscri|entitle|purchase|payment|credit|licen|wallet|transaction/i;
438
+
439
+ /**
440
+ * Whether what changed between the start of the check and a payment's first
441
+ * webhook gave the customer something: the app may grant at checkout, from
442
+ * the page, and use the webhook only as a back-up. An order row made when
443
+ * checkout began is not that.
444
+ */
445
+ function givenElsewhere(writes) {
446
+ return (writes || []).some((w) => (w.class || 'business') === 'business' && (
447
+ (w.kind === 'changed' && (w.columns || []).some((c) => GRANT_COLUMN.test(c.name))) ||
448
+ ((w.kind === 'added' || w.kind === 'count') && GRANT_TABLE.test(w.table))));
449
+ }
450
+
425
451
  // Columns whose values are never written into a trace, whatever the table.
426
452
  const SECRET_COLUMN = /pass(word|wd)?|secret|token|api_?key|access_?key|private|signature|card|cvv|otp|(^|_)(pan|pin|ssn|aadhaar)(_|$)|(^|_)keys?$/i;
427
453
  const SHORT = 200;
@@ -568,9 +594,19 @@ async function check(options) {
568
594
  !firstDelivery.writes.some((w) => w.class === 'business')) {
569
595
  result.findings.push({ kind: 'refund-nothing', severity: 'MEDIUM', toCheck: true, status: hook.status, said: hook.said || '' });
570
596
  }
597
+ // A payment that gave nothing: the customer paid, and neither its webhooks
598
+ // nor anything before them (checkout, the page) gave a plan, credits or an
599
+ // order. Seen, not proven wrong: an app may give it by hand or elsewhere.
600
+ const types = options.groupTypes && options.groupTypes.length ? options.groupTypes : [event.type];
601
+ const paid = types.filter(isPaidEvent);
602
+ if (paid.length && firstDelivery.writes && !firstDelivery.writes.some((w) => w.class === 'business') &&
603
+ !givenElsewhere(options.sinceWrites)) {
604
+ result.findings.push({ kind: 'paid-nothing', severity: 'HIGH', toCheck: true, status: hook.status, said: hook.said || '',
605
+ events: paid, logged: firstDelivery.writes.length > 0, before: Array.isArray(options.sinceWrites) });
606
+ }
571
607
 
572
608
  // 1. The same webhook again.
573
- say('Sending the same webhook again, exactly as ' + (hook.provider === 'razorpay' ? 'Razorpay' : 'Stripe') + ' would on a retry ...');
609
+ say('Sending the same webhook again, exactly as ' + (hook.provider === 'razorpay' ? 'Razorpay' : hook.provider === 'stripe' ? 'Stripe' : 'your provider') + ' would on a retry ...');
574
610
  const again = await send(target, hook);
575
611
  const second = await settle(client, schema, tables, options);
576
612
  const related = relatedTo(firstDelivery.writes, idsOf(hook));
@@ -678,6 +714,8 @@ function lifecycle(result, earlier) {
678
714
  module.exports = {
679
715
  signatureHeaderOf: signatureHeaderOf,
680
716
  lifecycle: lifecycle,
717
+ isPaidEvent: isPaidEvent,
718
+ givenElsewhere: givenElsewhere,
681
719
  isLocalAddress: isLocalAddress,
682
720
  providerOf: providerOf,
683
721
  modeOf: modeOf,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kryptheon",
3
- "version": "0.1.54",
3
+ "version": "0.1.55",
4
4
  "description": "Record what you do in your app and get plain-language reports when it breaks.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "commonjs",