wiki-plugin-shoppe 0.0.15 → 0.0.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-plugin-shoppe",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "Multi-tenant digital goods shoppe for federated wiki, powered by Sanora",
5
5
  "keywords": [
6
6
  "wiki",
package/server/server.js CHANGED
@@ -25,6 +25,7 @@ function fillTemplate(tmpl, vars) {
25
25
 
26
26
  const DATA_DIR = path.join(process.env.HOME || '/root', '.shoppe');
27
27
  const TENANTS_FILE = path.join(DATA_DIR, 'tenants.json');
28
+ const BUYERS_FILE = path.join(DATA_DIR, 'buyers.json');
28
29
  const CONFIG_FILE = path.join(DATA_DIR, 'config.json');
29
30
  const TMP_DIR = '/tmp/shoppe-uploads';
30
31
 
@@ -47,6 +48,47 @@ function getSanoraUrl() {
47
48
  return `http://localhost:${process.env.SANORA_PORT || 7243}`;
48
49
  }
49
50
 
51
+ function getAddieUrl() {
52
+ const config = loadConfig();
53
+ if (config.addieUrl) return config.addieUrl.replace(/\/$/, '');
54
+ return `http://localhost:${process.env.ADDIE_PORT || 3005}`;
55
+ }
56
+
57
+ function loadBuyers() {
58
+ if (!fs.existsSync(BUYERS_FILE)) return {};
59
+ try { return JSON.parse(fs.readFileSync(BUYERS_FILE, 'utf8')); } catch { return {}; }
60
+ }
61
+
62
+ function saveBuyers(buyers) {
63
+ fs.writeFileSync(BUYERS_FILE, JSON.stringify(buyers, null, 2));
64
+ }
65
+
66
+ async function getOrCreateBuyerAddieUser(recoveryKey, productId) {
67
+ const buyerKey = recoveryKey + productId;
68
+ const buyers = loadBuyers();
69
+ if (buyers[buyerKey]) return buyers[buyerKey];
70
+
71
+ const addieKeys = await sessionless.generateKeys(() => {}, () => null);
72
+ sessionless.getKeys = () => addieKeys;
73
+ const timestamp = Date.now().toString();
74
+ const message = timestamp + addieKeys.pubKey;
75
+ const signature = await sessionless.sign(message);
76
+
77
+ const resp = await fetch(`${getAddieUrl()}/user/create`, {
78
+ method: 'PUT',
79
+ headers: { 'Content-Type': 'application/json' },
80
+ body: JSON.stringify({ timestamp, pubKey: addieKeys.pubKey, signature })
81
+ });
82
+
83
+ const addieUser = await resp.json();
84
+ if (addieUser.error) throw new Error(`Addie: ${addieUser.error}`);
85
+
86
+ const buyer = { uuid: addieUser.uuid, pubKey: addieKeys.pubKey, privateKey: addieKeys.privateKey };
87
+ buyers[buyerKey] = buyer;
88
+ saveBuyers(buyers);
89
+ return buyer;
90
+ }
91
+
50
92
  // Same diverse palette as BDO emojicoding
51
93
  const EMOJI_PALETTE = [
52
94
  '🌟', '🌙', '🌍', '🌊', '🔥', '💎', '🎨', '🎭', '🎪', '🎯',
@@ -152,6 +194,25 @@ function generateEmojicode(tenants) {
152
194
  throw new Error('Failed to generate unique emojicode after 100 attempts');
153
195
  }
154
196
 
197
+ async function addieCreateUser() {
198
+ const addieKeys = await sessionless.generateKeys(() => {}, () => null);
199
+ sessionless.getKeys = () => addieKeys;
200
+ const timestamp = Date.now().toString();
201
+ const message = timestamp + addieKeys.pubKey;
202
+ const signature = await sessionless.sign(message);
203
+
204
+ const resp = await fetch(`${getAllyabaseOrigin()}/plugin/allyabase/addie/user/create`, {
205
+ method: 'PUT',
206
+ headers: { 'Content-Type': 'application/json' },
207
+ body: JSON.stringify({ timestamp, pubKey: addieKeys.pubKey, signature })
208
+ });
209
+
210
+ const addieUser = await resp.json();
211
+ if (addieUser.error) throw new Error(`Addie: ${addieUser.error}`);
212
+
213
+ return { uuid: addieUser.uuid, pubKey: addieKeys.pubKey, privateKey: addieKeys.privateKey };
214
+ }
215
+
155
216
  async function registerTenant(name) {
156
217
  const tenants = loadTenants();
157
218
 
@@ -173,12 +234,21 @@ async function registerTenant(name) {
173
234
 
174
235
  const emojicode = generateEmojicode(tenants);
175
236
 
237
+ // Create a dedicated Addie user for payee splits
238
+ let addieKeys = null;
239
+ try {
240
+ addieKeys = await addieCreateUser();
241
+ } catch (err) {
242
+ console.warn('[shoppe] Could not create addie user (payouts unavailable):', err.message);
243
+ }
244
+
176
245
  const tenant = {
177
246
  uuid: sanoraUser.uuid,
178
247
  emojicode,
179
248
  name: name || 'Unnamed Shoppe',
180
249
  keys,
181
250
  sanoraUser,
251
+ addieKeys,
182
252
  createdAt: Date.now()
183
253
  };
184
254
 
@@ -937,10 +1007,11 @@ async function startServer(params) {
937
1007
 
938
1008
  // Save config (owner only)
939
1009
  app.post('/plugin/shoppe/config', owner, (req, res) => {
940
- const { sanoraUrl } = req.body;
1010
+ const { sanoraUrl, addieUrl } = req.body;
941
1011
  if (!sanoraUrl) return res.status(400).json({ success: false, error: 'sanoraUrl required' });
942
1012
  const config = loadConfig();
943
1013
  config.sanoraUrl = sanoraUrl;
1014
+ if (addieUrl) config.addieUrl = addieUrl;
944
1015
  saveConfig(config);
945
1016
  console.log('[shoppe] Sanora URL set to:', sanoraUrl);
946
1017
  res.json({ success: true });
@@ -953,15 +1024,20 @@ async function startServer(params) {
953
1024
  if (!tenant) return res.status(404).send('<h1>Shoppe not found</h1>');
954
1025
 
955
1026
  const title = decodeURIComponent(req.params.title);
956
- const sanoraUrl = getSanoraUrl();
957
- const productsResp = await fetch(`${sanoraUrl}/products/${tenant.uuid}`);
1027
+ const sanoraUrlInternal = getSanoraUrl();
1028
+ const wikiOrigin = `${req.protocol}://${req.get('host')}`;
1029
+ const sanoraUrl = `${wikiOrigin}/plugin/allyabase/sanora`;
1030
+ const productsResp = await fetch(`${sanoraUrlInternal}/products/${tenant.uuid}`);
958
1031
  const products = await productsResp.json();
959
1032
  const product = products[title] || Object.values(products).find(p => p.title === title);
960
1033
  if (!product) return res.status(404).send('<h1>Product not found</h1>');
961
1034
 
962
- const imageUrl = product.image ? `${sanoraUrl}/images/${product.image}` : '';
963
- const ebookUrl = `${req.protocol}://${req.get('host')}/plugin/shoppe/${tenant.uuid}/download/${encodeURIComponent(title)}`;
964
- const shoppeUrl = `${req.protocol}://${req.get('host')}/plugin/shoppe/${tenant.uuid}`;
1035
+ const imageUrl = product.image ? `${sanoraUrlInternal}/images/${product.image}` : '';
1036
+ const ebookUrl = `${wikiOrigin}/plugin/shoppe/${tenant.uuid}/download/${encodeURIComponent(title)}`;
1037
+ const shoppeUrl = `${wikiOrigin}/plugin/shoppe/${tenant.uuid}`;
1038
+ const payees = tenant.addieKeys
1039
+ ? JSON.stringify([{ pubKey: tenant.addieKeys.pubKey, amount: product.price || 0 }])
1040
+ : '[]';
965
1041
 
966
1042
  const html = fillTemplate(templateHtml, {
967
1043
  title: product.title || title,
@@ -973,9 +1049,11 @@ async function startServer(params) {
973
1049
  pubKey: '',
974
1050
  signature: '',
975
1051
  sanoraUrl,
976
- allyabaseOrigin: getAllyabaseOrigin(),
1052
+ allyabaseOrigin: wikiOrigin,
977
1053
  ebookUrl,
978
- shoppeUrl
1054
+ shoppeUrl,
1055
+ payees,
1056
+ tenantUuid: tenant.uuid
979
1057
  });
980
1058
 
981
1059
  res.set('Content-Type', 'text/html');
@@ -994,6 +1072,79 @@ async function startServer(params) {
994
1072
  app.get('/plugin/shoppe/:identifier/buy/:title/address', (req, res) =>
995
1073
  renderPurchasePage(req, res, ADDRESS_STRIPE_TMPL));
996
1074
 
1075
+ // Purchase intent — creates buyer Addie user, checks recovery hash, returns Stripe client secret
1076
+ app.post('/plugin/shoppe/:identifier/purchase/intent', async (req, res) => {
1077
+ try {
1078
+ const tenant = getTenantByIdentifier(req.params.identifier);
1079
+ if (!tenant) return res.status(404).json({ error: 'Shoppe not found' });
1080
+
1081
+ const { recoveryKey, productId, title } = req.body;
1082
+ if (!recoveryKey || !productId) return res.status(400).json({ error: 'recoveryKey and productId required' });
1083
+
1084
+ const sanoraUrlInternal = getSanoraUrl();
1085
+ const recoveryHash = recoveryKey + productId;
1086
+
1087
+ // Check if already purchased
1088
+ const checkResp = await fetch(`${sanoraUrlInternal}/user/check-hash/${encodeURIComponent(recoveryHash)}/product/${encodeURIComponent(productId)}`);
1089
+ const checkJson = await checkResp.json();
1090
+ if (checkJson.success) return res.json({ purchased: true });
1091
+
1092
+ // Get product price
1093
+ const productsResp = await fetch(`${sanoraUrlInternal}/products/${tenant.uuid}`);
1094
+ const products = await productsResp.json();
1095
+ const product = (title && products[title]) || Object.values(products).find(p => p.productId === productId);
1096
+ const amount = product?.price || 0;
1097
+
1098
+ // Create/retrieve buyer Addie user
1099
+ const buyer = await getOrCreateBuyerAddieUser(recoveryKey, productId);
1100
+
1101
+ // Create Stripe intent via Addie
1102
+ const payees = tenant.addieKeys ? [{ pubKey: tenant.addieKeys.pubKey, amount }] : [];
1103
+ const intentResp = await fetch(`${getAddieUrl()}/user/${buyer.uuid}/processor/stripe/intent`, {
1104
+ method: 'PUT',
1105
+ headers: { 'Content-Type': 'application/json' },
1106
+ body: JSON.stringify({ timestamp: Date.now().toString(), amount, currency: 'USD', payees })
1107
+ });
1108
+
1109
+ const intentJson = await intentResp.json();
1110
+ if (intentJson.error) return res.status(500).json({ error: intentJson.error });
1111
+
1112
+ res.json({ purchased: false, clientSecret: intentJson.paymentIntent, publishableKey: intentJson.publishableKey });
1113
+ } catch (err) {
1114
+ console.error('[shoppe] purchase intent error:', err);
1115
+ res.status(500).json({ error: err.message });
1116
+ }
1117
+ });
1118
+
1119
+ // Purchase complete — creates recovery hash in Sanora after successful payment
1120
+ app.post('/plugin/shoppe/:identifier/purchase/complete', async (req, res) => {
1121
+ try {
1122
+ const tenant = getTenantByIdentifier(req.params.identifier);
1123
+ if (!tenant) return res.status(404).json({ error: 'Shoppe not found' });
1124
+
1125
+ const { recoveryKey, productId, order } = req.body;
1126
+ if (!recoveryKey || !productId) return res.status(400).json({ error: 'recoveryKey and productId required' });
1127
+
1128
+ const sanoraUrlInternal = getSanoraUrl();
1129
+ const recoveryHash = recoveryKey + productId;
1130
+
1131
+ if (order) {
1132
+ await fetch(`${sanoraUrlInternal}/user/orders`, {
1133
+ method: 'PUT',
1134
+ headers: { 'Content-Type': 'application/json' },
1135
+ body: JSON.stringify({ timestamp: Date.now().toString(), order })
1136
+ });
1137
+ }
1138
+
1139
+ const createResp = await fetch(`${sanoraUrlInternal}/user/create-hash/${encodeURIComponent(recoveryHash)}/product/${encodeURIComponent(productId)}`);
1140
+ const createJson = await createResp.json();
1141
+ res.json({ success: createJson.success });
1142
+ } catch (err) {
1143
+ console.error('[shoppe] purchase complete error:', err);
1144
+ res.status(500).json({ error: err.message });
1145
+ }
1146
+ });
1147
+
997
1148
  // Ebook download page (reached after successful payment + hash creation)
998
1149
  app.get('/plugin/shoppe/:identifier/download/:title', async (req, res) => {
999
1150
  try {
@@ -481,7 +481,7 @@
481
481
  timestamp: new Date().getTime() + '',
482
482
  amount: {{amount}},
483
483
  currency: 'USD',
484
- payees: []
484
+ payees: {{payees}}
485
485
  };
486
486
 
487
487
  const res = await fetch(`{{allyabaseOrigin}}/plugin/allyabase/addie/processor/stripe/intent`, {
@@ -83,7 +83,7 @@
83
83
  </div>
84
84
 
85
85
  <!-- Payment Form -->
86
- <div>
86
+ <div id="payment-section" style="display:none">
87
87
  <h3 style="margin-bottom: 20px; color: #10b981;">Payment Details</h3>
88
88
  <form id="payment-form" class="payment-form" style="
89
89
  background: #2a2a2e;
@@ -137,29 +137,18 @@
137
137
  </style>
138
138
 
139
139
  <script type="text/javascript">
140
- // Buy button functionality
140
+ // Buy button — just reveal the forms section (payment init happens after recovery key submitted)
141
141
  document.getElementById('buy-button').addEventListener('click', function() {
142
142
  const formsSection = document.getElementById('forms-section');
143
143
  formsSection.style.display = 'block';
144
-
145
- // Smooth scroll to forms section
146
- formsSection.scrollIntoView({
147
- behavior: 'smooth',
148
- block: 'start'
149
- });
150
-
151
- // Initialize payment form
152
- window.addPaymentForm();
144
+ formsSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
153
145
  });
154
-
155
- // Form validation for both forms
146
+
147
+ // Enable submit once Stripe payment element is ready
156
148
  function validateAllForms() {
157
- const recoveryValid = validateForm(formConfig);
158
- const paymentValid = stripe && elements; // Basic check that payment is initialized
159
-
149
+ const paymentValid = stripe && elements;
160
150
  const submitButton = document.getElementById('submit-button');
161
-
162
- if (recoveryValid && paymentValid) {
151
+ if (paymentValid) {
163
152
  submitButton.style.background = 'linear-gradient(90deg, #10b981, #8b5cf6)';
164
153
  submitButton.style.color = 'white';
165
154
  submitButton.style.cursor = 'pointer';
@@ -170,7 +159,6 @@
170
159
  submitButton.style.color = '#999999';
171
160
  submitButton.style.cursor = 'not-allowed';
172
161
  submitButton.disabled = true;
173
- submitButton.textContent = 'Complete Purchase';
174
162
  }
175
163
  }
176
164
  </script>
@@ -582,23 +570,43 @@
582
570
  };
583
571
 
584
572
  async function handleSubmit(formData) {
585
- console.log('Form submitted:', formData);
586
- // Do whatever you want with the form data
587
- const recoveryHash = formData.Recovery + "{{productId}}";
588
- const checkHashURL = `{{sanoraUrl}}/user/check-hash/${recoveryHash}/product/{{productId}}`
573
+ window.formData = formData;
574
+ const recoveryKey = formData.Recovery;
575
+
576
+ const submitBtn = document.querySelector('[type=submit]');
577
+ if (submitBtn) { submitBtn.disabled = true; submitBtn.textContent = 'Checking…'; }
578
+
579
+ try {
580
+ const resp = await fetch('/plugin/shoppe/{{tenantUuid}}/purchase/intent', {
581
+ method: 'POST',
582
+ headers: { 'Content-Type': 'application/json' },
583
+ body: JSON.stringify({ recoveryKey, productId: '{{productId}}', title: '{{title}}' })
584
+ });
585
+ const json = await resp.json();
586
+
587
+ if (json.purchased) {
588
+ window.location.href = '{{ebookUrl}}';
589
+ return;
590
+ }
589
591
 
590
- const resp = await fetch(checkHashURL);
591
- const json = await resp.json();
592
+ if (json.error) {
593
+ alert('Error: ' + json.error);
594
+ return;
595
+ }
592
596
 
593
- if(json.success) {
594
- window.location.href = '{{ebookUrl}}';
595
- return;
597
+ // Show payment element
598
+ document.getElementById('payment-section').style.display = 'block';
599
+ stripe = Stripe(json.publishableKey);
600
+ elements = stripe.elements({ clientSecret: json.clientSecret });
601
+ const paymentElement = elements.create('payment');
602
+ paymentElement.mount('#payment-element');
603
+ paymentElement.on('ready', () => setTimeout(validateAllForms, 500));
604
+ paymentElement.on('change', () => setTimeout(validateAllForms, 100));
605
+ } catch (err) {
606
+ alert('Unexpected error: ' + err.message);
607
+ } finally {
608
+ if (submitBtn) { submitBtn.disabled = false; submitBtn.textContent = 'Check'; }
596
609
  }
597
-
598
- window.formData = formData;
599
- // Don't show payment form here anymore, it's already visible
600
- // Just trigger validation
601
- validateAllForms();
602
610
  }
603
611
 
604
612
  const form = getForm(formConfig, handleSubmit);
@@ -608,130 +616,59 @@
608
616
  <script type="text/javascript">
609
617
  let stripe;
610
618
  let elements;
611
- let response;
612
619
 
613
620
  const paymentForm = document.getElementById('payment-form');
614
621
  const submitButton = document.getElementById('submit-button');
615
622
  const errorMessage = document.getElementById('error-message');
616
623
  const loadingMessage = document.getElementById('loading');
617
624
 
618
- async function getPaymentIntentWithoutSplits(amount, currency) {
619
- try {
620
- const payload = {
621
- timestamp: new Date().getTime() + '',
622
- amount: {{amount}},
623
- currency: 'USD',
624
- payees: []
625
- };
626
-
627
- const res = await fetch(`{{allyabaseOrigin}}/plugin/allyabase/addie/processor/stripe/intent`, {
628
- method: 'put',
629
- body: JSON.stringify(payload),
630
- headers: {'Content-Type': 'application/json'}
631
- });
632
-
633
- const response = await res.json();
634
- console.log('got intent response', response);
635
-
636
- stripe = Stripe(response.publishableKey);
637
- elements = stripe.elements({
638
- clientSecret: response.paymentIntent
639
- });
640
-
641
- const paymentElement = elements.create('payment');
642
- paymentElement.mount('#payment-element');
643
-
644
- // Trigger validation when payment element is ready
645
- paymentElement.on('ready', () => {
646
- setTimeout(() => validateAllForms(), 500);
647
- });
648
-
649
- paymentElement.on('change', () => {
650
- setTimeout(() => validateAllForms(), 100);
651
- });
652
- } catch(err) {
653
- console.warn(err);
654
- }
655
- };
656
-
657
625
  window.confirmPayment = async () => {
658
- const order = {
659
- title: "{{title}}",
660
- productId: "{{productId}}",
661
- formData
662
- };
663
-
664
626
  try {
665
627
  const { error } = await stripe.confirmPayment({
666
- elements,
667
- confirmParams: {
668
- return_url: 'http://wiki.planetnineisaspaceship.com'
669
- },
670
- redirect: 'if_required'
628
+ elements,
629
+ confirmParams: { return_url: '{{shoppeUrl}}' },
630
+ redirect: 'if_required'
671
631
  });
672
632
 
673
- if(error) {
674
- return showError(error.message);
675
- }
633
+ if (error) return showError(error.message);
676
634
 
677
- await fetch('{{sanoraUrl}}/user/orders', {
678
- method: 'PUT',
679
- headers: {'Content-Type': 'application/json'},
680
- body: JSON.stringify({timestamp: new Date().getTime() + '', order})
635
+ const order = { title: '{{title}}', productId: '{{productId}}', formData: window.formData };
636
+ const resp = await fetch('/plugin/shoppe/{{tenantUuid}}/purchase/complete', {
637
+ method: 'POST',
638
+ headers: { 'Content-Type': 'application/json' },
639
+ body: JSON.stringify({ recoveryKey: window.formData.Recovery, productId: '{{productId}}', order })
681
640
  });
682
- const recoveryHash = formData.Recovery + "{{productId}}";
683
- const createHashURL = `{{sanoraUrl}}/user/create-hash/${recoveryHash}/product/{{productId}}`
684
-
685
- const resp = await fetch(createHashURL);
686
641
  const json = await resp.json();
687
642
 
688
- if(json.success) {
689
- window.location.href = '{{ebookUrl}}';
690
- return;
643
+ if (json.success) {
644
+ window.location.href = '{{ebookUrl}}';
645
+ } else {
646
+ window.alert('Payment successful, but error recording purchase. Contact greetings@planetnine.app');
691
647
  }
692
-
693
- window.alert('Your payment was successful, but there was an error creating this recovery hash. Please contact greetings@planetnine.app for support.');
694
-
695
- } catch(err) {
648
+ } catch (err) {
696
649
  showError('An unexpected error occurred.');
697
- console.warn('payment error: ', err);
650
+ console.warn('payment error:', err);
698
651
  }
699
652
  };
700
653
 
701
654
  paymentForm.addEventListener('submit', async (event) => {
702
655
  event.preventDefault();
703
-
704
- if (!stripe || !elements) {
705
- return;
706
- }
707
-
708
- // Disable payment form submission while processing
656
+ if (!stripe || !elements) return;
709
657
  setLoading(true);
710
-
711
658
  await window.confirmPayment();
712
-
713
659
  setLoading(false);
714
660
  });
715
661
 
716
662
  const showError = (message) => {
717
663
  errorMessage.textContent = message;
718
664
  errorMessage.style.display = 'block';
719
- setTimeout(() => {
720
- errorMessage.style.display = 'none';
721
- errorMessage.textContent = '';
722
- }, 5000);
665
+ setTimeout(() => { errorMessage.style.display = 'none'; errorMessage.textContent = ''; }, 5000);
723
666
  };
724
667
 
725
668
  const setLoading = (isLoading) => {
726
669
  submitButton.disabled = isLoading;
727
670
  loadingMessage.style.display = isLoading ? 'block' : 'none';
728
671
  };
729
-
730
- const start = () => {
731
- getPaymentIntentWithoutSplits({{amount}}, 'USD');
732
- };
733
-
734
- window.addPaymentForm = start;
735
672
  </script>
736
673
  </body>
737
674
  </html>