emdash-niaga-commerce 0.0.0-stage → 0.1.1

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.
Files changed (304) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/LICENSE +21 -0
  3. package/README.id.md +268 -0
  4. package/README.md +267 -2
  5. package/bin/niaga.mjs +47 -0
  6. package/dist/admin/fees.d.ts +7 -0
  7. package/dist/admin/fees.d.ts.map +1 -0
  8. package/dist/admin/fees.js +87 -0
  9. package/dist/admin/fees.js.map +1 -0
  10. package/dist/admin/index.d.ts +11 -0
  11. package/dist/admin/index.d.ts.map +1 -0
  12. package/dist/admin/index.js +32 -0
  13. package/dist/admin/index.js.map +1 -0
  14. package/dist/admin/orders.d.ts +7 -0
  15. package/dist/admin/orders.d.ts.map +1 -0
  16. package/dist/admin/orders.js +123 -0
  17. package/dist/admin/orders.js.map +1 -0
  18. package/dist/admin/products.d.ts +4 -0
  19. package/dist/admin/products.d.ts.map +1 -0
  20. package/dist/admin/products.js +187 -0
  21. package/dist/admin/products.js.map +1 -0
  22. package/dist/admin/reports.d.ts +8 -0
  23. package/dist/admin/reports.d.ts.map +1 -0
  24. package/dist/admin/reports.js +41 -0
  25. package/dist/admin/reports.js.map +1 -0
  26. package/dist/admin/returns.d.ts +4 -0
  27. package/dist/admin/returns.d.ts.map +1 -0
  28. package/dist/admin/returns.js +32 -0
  29. package/dist/admin/returns.js.map +1 -0
  30. package/dist/admin/settings.d.ts +7 -0
  31. package/dist/admin/settings.d.ts.map +1 -0
  32. package/dist/admin/settings.js +82 -0
  33. package/dist/admin/settings.js.map +1 -0
  34. package/dist/admin/ui.d.ts +138 -0
  35. package/dist/admin/ui.d.ts.map +1 -0
  36. package/dist/admin/ui.js +368 -0
  37. package/dist/admin/ui.js.map +1 -0
  38. package/dist/admin/vouchers.d.ts +4 -0
  39. package/dist/admin/vouchers.d.ts.map +1 -0
  40. package/dist/admin/vouchers.js +51 -0
  41. package/dist/admin/vouchers.js.map +1 -0
  42. package/dist/descriptor.d.ts +27 -0
  43. package/dist/descriptor.d.ts.map +1 -0
  44. package/dist/descriptor.js +45 -0
  45. package/dist/descriptor.js.map +1 -0
  46. package/dist/i18n/admin-en.d.ts +331 -0
  47. package/dist/i18n/admin-en.d.ts.map +1 -0
  48. package/dist/i18n/admin-en.js +330 -0
  49. package/dist/i18n/admin-en.js.map +1 -0
  50. package/dist/i18n/admin-id.d.ts +4 -0
  51. package/dist/i18n/admin-id.d.ts.map +1 -0
  52. package/dist/i18n/admin-id.js +329 -0
  53. package/dist/i18n/admin-id.js.map +1 -0
  54. package/dist/i18n/en.d.ts +387 -0
  55. package/dist/i18n/en.d.ts.map +1 -0
  56. package/dist/i18n/en.js +387 -0
  57. package/dist/i18n/en.js.map +1 -0
  58. package/dist/i18n/id.d.ts +4 -0
  59. package/dist/i18n/id.d.ts.map +1 -0
  60. package/dist/i18n/id.js +386 -0
  61. package/dist/i18n/id.js.map +1 -0
  62. package/dist/index.d.ts +13 -0
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +30 -0
  65. package/dist/index.js.map +1 -0
  66. package/dist/mock.d.ts +5 -0
  67. package/dist/mock.d.ts.map +1 -0
  68. package/dist/mock.js +34 -0
  69. package/dist/mock.js.map +1 -0
  70. package/dist/plugin.d.ts +8 -0
  71. package/dist/plugin.d.ts.map +1 -0
  72. package/dist/plugin.js +399 -0
  73. package/dist/plugin.js.map +1 -0
  74. package/dist/routes/storefront.d.ts +21 -0
  75. package/dist/routes/storefront.d.ts.map +1 -0
  76. package/dist/routes/storefront.js +233 -0
  77. package/dist/routes/storefront.js.map +1 -0
  78. package/dist/routes/webhook.d.ts +8 -0
  79. package/dist/routes/webhook.d.ts.map +1 -0
  80. package/dist/routes/webhook.js +14 -0
  81. package/dist/routes/webhook.js.map +1 -0
  82. package/dist/runtime/host.d.ts +68 -0
  83. package/dist/runtime/host.d.ts.map +1 -0
  84. package/dist/runtime/host.js +165 -0
  85. package/dist/runtime/host.js.map +1 -0
  86. package/dist/runtime/maintenance.d.ts +37 -0
  87. package/dist/runtime/maintenance.d.ts.map +1 -0
  88. package/dist/runtime/maintenance.js +44 -0
  89. package/dist/runtime/maintenance.js.map +1 -0
  90. package/dist/server/accounts.d.ts +135 -0
  91. package/dist/server/accounts.d.ts.map +1 -0
  92. package/dist/server/accounts.js +225 -0
  93. package/dist/server/accounts.js.map +1 -0
  94. package/dist/server/admin-queue.d.ts +180 -0
  95. package/dist/server/admin-queue.d.ts.map +1 -0
  96. package/dist/server/admin-queue.js +210 -0
  97. package/dist/server/admin-queue.js.map +1 -0
  98. package/dist/server/auth.d.ts +22 -0
  99. package/dist/server/auth.d.ts.map +1 -0
  100. package/dist/server/auth.js +130 -0
  101. package/dist/server/auth.js.map +1 -0
  102. package/dist/server/cart.d.ts +70 -0
  103. package/dist/server/cart.d.ts.map +1 -0
  104. package/dist/server/cart.js +91 -0
  105. package/dist/server/cart.js.map +1 -0
  106. package/dist/server/catalog.d.ts +259 -0
  107. package/dist/server/catalog.d.ts.map +1 -0
  108. package/dist/server/catalog.js +328 -0
  109. package/dist/server/catalog.js.map +1 -0
  110. package/dist/server/checkout.d.ts +137 -0
  111. package/dist/server/checkout.d.ts.map +1 -0
  112. package/dist/server/checkout.js +319 -0
  113. package/dist/server/checkout.js.map +1 -0
  114. package/dist/server/db.d.ts +35 -0
  115. package/dist/server/db.d.ts.map +1 -0
  116. package/dist/server/db.js +25 -0
  117. package/dist/server/db.js.map +1 -0
  118. package/dist/server/documents.d.ts +26 -0
  119. package/dist/server/documents.d.ts.map +1 -0
  120. package/dist/server/documents.js +110 -0
  121. package/dist/server/documents.js.map +1 -0
  122. package/dist/server/events.d.ts +73 -0
  123. package/dist/server/events.d.ts.map +1 -0
  124. package/dist/server/events.js +109 -0
  125. package/dist/server/events.js.map +1 -0
  126. package/dist/server/fees/index.d.ts +7 -0
  127. package/dist/server/fees/index.d.ts.map +1 -0
  128. package/dist/server/fees/index.js +7 -0
  129. package/dist/server/fees/index.js.map +1 -0
  130. package/dist/server/fees/invoices.d.ts +110 -0
  131. package/dist/server/fees/invoices.d.ts.map +1 -0
  132. package/dist/server/fees/invoices.js +141 -0
  133. package/dist/server/fees/invoices.js.map +1 -0
  134. package/dist/server/fees/math.d.ts +142 -0
  135. package/dist/server/fees/math.d.ts.map +1 -0
  136. package/dist/server/fees/math.js +167 -0
  137. package/dist/server/fees/math.js.map +1 -0
  138. package/dist/server/fees/pdf.d.ts +8 -0
  139. package/dist/server/fees/pdf.d.ts.map +1 -0
  140. package/dist/server/fees/pdf.js +215 -0
  141. package/dist/server/fees/pdf.js.map +1 -0
  142. package/dist/server/fees/store.d.ts +128 -0
  143. package/dist/server/fees/store.d.ts.map +1 -0
  144. package/dist/server/fees/store.js +308 -0
  145. package/dist/server/fees/store.js.map +1 -0
  146. package/dist/server/fees/sync.d.ts +24 -0
  147. package/dist/server/fees/sync.d.ts.map +1 -0
  148. package/dist/server/fees/sync.js +45 -0
  149. package/dist/server/fees/sync.js.map +1 -0
  150. package/dist/server/fulfillment.d.ts +190 -0
  151. package/dist/server/fulfillment.d.ts.map +1 -0
  152. package/dist/server/fulfillment.js +494 -0
  153. package/dist/server/fulfillment.js.map +1 -0
  154. package/dist/server/google.d.ts +35 -0
  155. package/dist/server/google.d.ts.map +1 -0
  156. package/dist/server/google.js +96 -0
  157. package/dist/server/google.js.map +1 -0
  158. package/dist/server/http.d.ts +26 -0
  159. package/dist/server/http.d.ts.map +1 -0
  160. package/dist/server/http.js +73 -0
  161. package/dist/server/http.js.map +1 -0
  162. package/dist/server/i18n.d.ts +44 -0
  163. package/dist/server/i18n.d.ts.map +1 -0
  164. package/dist/server/i18n.js +79 -0
  165. package/dist/server/i18n.js.map +1 -0
  166. package/dist/server/index.d.ts +35 -0
  167. package/dist/server/index.d.ts.map +1 -0
  168. package/dist/server/index.js +35 -0
  169. package/dist/server/index.js.map +1 -0
  170. package/dist/server/migrate.d.ts +15 -0
  171. package/dist/server/migrate.d.ts.map +1 -0
  172. package/dist/server/migrate.js +49 -0
  173. package/dist/server/migrate.js.map +1 -0
  174. package/dist/server/migrations.generated.d.ts +7 -0
  175. package/dist/server/migrations.generated.d.ts.map +1 -0
  176. package/dist/server/migrations.generated.js +95 -0
  177. package/dist/server/migrations.generated.js.map +1 -0
  178. package/dist/server/notifications.d.ts +52 -0
  179. package/dist/server/notifications.d.ts.map +1 -0
  180. package/dist/server/notifications.js +168 -0
  181. package/dist/server/notifications.js.map +1 -0
  182. package/dist/server/orders.d.ts +209 -0
  183. package/dist/server/orders.d.ts.map +1 -0
  184. package/dist/server/orders.js +269 -0
  185. package/dist/server/orders.js.map +1 -0
  186. package/dist/server/program.d.ts +81 -0
  187. package/dist/server/program.d.ts.map +1 -0
  188. package/dist/server/program.js +112 -0
  189. package/dist/server/program.js.map +1 -0
  190. package/dist/server/providers.d.ts +55 -0
  191. package/dist/server/providers.d.ts.map +1 -0
  192. package/dist/server/providers.js +84 -0
  193. package/dist/server/providers.js.map +1 -0
  194. package/dist/server/reports.d.ts +195 -0
  195. package/dist/server/reports.d.ts.map +1 -0
  196. package/dist/server/reports.js +248 -0
  197. package/dist/server/reports.js.map +1 -0
  198. package/dist/server/roles.d.ts +46 -0
  199. package/dist/server/roles.d.ts.map +1 -0
  200. package/dist/server/roles.js +75 -0
  201. package/dist/server/roles.js.map +1 -0
  202. package/dist/server/settings.d.ts +97 -0
  203. package/dist/server/settings.d.ts.map +1 -0
  204. package/dist/server/settings.js +116 -0
  205. package/dist/server/settings.js.map +1 -0
  206. package/dist/server/stock.d.ts +137 -0
  207. package/dist/server/stock.d.ts.map +1 -0
  208. package/dist/server/stock.js +292 -0
  209. package/dist/server/stock.js.map +1 -0
  210. package/dist/server/storefront.d.ts +147 -0
  211. package/dist/server/storefront.d.ts.map +1 -0
  212. package/dist/server/storefront.js +127 -0
  213. package/dist/server/storefront.js.map +1 -0
  214. package/dist/server/text.d.ts +9 -0
  215. package/dist/server/text.d.ts.map +1 -0
  216. package/dist/server/text.js +32 -0
  217. package/dist/server/text.js.map +1 -0
  218. package/dist/server/time.d.ts +35 -0
  219. package/dist/server/time.d.ts.map +1 -0
  220. package/dist/server/time.js +93 -0
  221. package/dist/server/time.js.map +1 -0
  222. package/dist/server/util.d.ts +37 -0
  223. package/dist/server/util.d.ts.map +1 -0
  224. package/dist/server/util.js +115 -0
  225. package/dist/server/util.js.map +1 -0
  226. package/dist/server/vouchers.d.ts +159 -0
  227. package/dist/server/vouchers.d.ts.map +1 -0
  228. package/dist/server/vouchers.js +220 -0
  229. package/dist/server/vouchers.js.map +1 -0
  230. package/dist/server/webhooks.d.ts +45 -0
  231. package/dist/server/webhooks.d.ts.map +1 -0
  232. package/dist/server/webhooks.js +164 -0
  233. package/dist/server/webhooks.js.map +1 -0
  234. package/dist/testing/d1.d.ts +14 -0
  235. package/dist/testing/d1.d.ts.map +1 -0
  236. package/dist/testing/d1.js +70 -0
  237. package/dist/testing/d1.js.map +1 -0
  238. package/dist/testing/index.d.ts +6 -0
  239. package/dist/testing/index.d.ts.map +1 -0
  240. package/dist/testing/index.js +6 -0
  241. package/dist/testing/index.js.map +1 -0
  242. package/migrations/0001_core.sql +604 -0
  243. package/migrations/0002_fees.sql +87 -0
  244. package/package.json +111 -3
  245. package/src/admin/fees.tsx +394 -0
  246. package/src/admin/index.tsx +40 -0
  247. package/src/admin/orders.tsx +625 -0
  248. package/src/admin/products.tsx +567 -0
  249. package/src/admin/reports.tsx +245 -0
  250. package/src/admin/returns.tsx +138 -0
  251. package/src/admin/settings.tsx +390 -0
  252. package/src/admin/ui.tsx +496 -0
  253. package/src/admin/vouchers.tsx +214 -0
  254. package/src/descriptor.ts +60 -0
  255. package/src/i18n/admin-en.ts +342 -0
  256. package/src/i18n/admin-id.ts +342 -0
  257. package/src/i18n/en.ts +406 -0
  258. package/src/i18n/id.ts +408 -0
  259. package/src/index.ts +42 -0
  260. package/src/mock.ts +36 -0
  261. package/src/plugin.ts +450 -0
  262. package/src/routes/storefront.ts +249 -0
  263. package/src/routes/webhook.ts +23 -0
  264. package/src/runtime/cloudflare-workers.d.ts +5 -0
  265. package/src/runtime/host.ts +205 -0
  266. package/src/runtime/maintenance.ts +47 -0
  267. package/src/server/accounts.ts +367 -0
  268. package/src/server/admin-queue.ts +280 -0
  269. package/src/server/auth.ts +143 -0
  270. package/src/server/cart.ts +119 -0
  271. package/src/server/catalog.ts +661 -0
  272. package/src/server/checkout.ts +523 -0
  273. package/src/server/db.ts +66 -0
  274. package/src/server/documents.ts +113 -0
  275. package/src/server/events.ts +173 -0
  276. package/src/server/fees/index.ts +6 -0
  277. package/src/server/fees/invoices.ts +247 -0
  278. package/src/server/fees/math.ts +305 -0
  279. package/src/server/fees/pdf.ts +215 -0
  280. package/src/server/fees/store.ts +471 -0
  281. package/src/server/fees/sync.ts +69 -0
  282. package/src/server/fulfillment.ts +709 -0
  283. package/src/server/google.ts +101 -0
  284. package/src/server/http.ts +68 -0
  285. package/src/server/i18n.ts +88 -0
  286. package/src/server/index.ts +34 -0
  287. package/src/server/migrate.ts +55 -0
  288. package/src/server/migrations.generated.ts +100 -0
  289. package/src/server/notifications.ts +207 -0
  290. package/src/server/orders.ts +469 -0
  291. package/src/server/program.ts +139 -0
  292. package/src/server/providers.ts +115 -0
  293. package/src/server/reports.ts +413 -0
  294. package/src/server/roles.ts +94 -0
  295. package/src/server/settings.ts +168 -0
  296. package/src/server/stock.ts +440 -0
  297. package/src/server/storefront.ts +143 -0
  298. package/src/server/text.ts +34 -0
  299. package/src/server/time.ts +116 -0
  300. package/src/server/util.ts +128 -0
  301. package/src/server/vouchers.ts +329 -0
  302. package/src/server/webhooks.ts +191 -0
  303. package/src/testing/d1.ts +69 -0
  304. package/src/testing/index.ts +5 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,44 @@
1
+ # Changelog
2
+
3
+ ## 0.1.1 (2 Dec 2026)
4
+
5
+ Needs `emdash-niaga-commerce-contracts` `^0.3.0` (contract version 0.3.0). Provider plugins must declare `^0.3.0` too:
6
+ Mayar and Biteship 0.1.1 do.
7
+
8
+ ### Added
9
+
10
+ - Order documents for print plugins carry `publicTrackingUrl` (no access token) and `courier` (code, service and the
11
+ label the buyer chose, "JNE Reguler").
12
+ - `paths.track` setting ("Public tracking page" in Store settings): the store's order lookup with `{code}` and no token.
13
+ Empty, the public link is the order page without its token parameter.
14
+ - The document source implements `recordPrint(orderId, docKind, station, reprint)`: a printed shipping label is
15
+ recorded server side (first print time, copy count, a staff-only history line naming the station). The admin's
16
+ "printed" action uses the same code.
17
+
18
+ ### Fixed
19
+
20
+ - Booking the courier again after a cancel failed on `UNIQUE(provider, provider_shipment_id)` with the mock shipping
21
+ provider, which handed back the cancelled shipment. The mock now behaves like Biteship (contracts 0.3.0): a reused
22
+ reference is a conflict while the first shipment is live (core keeps the claim as "check the carrier dashboard"), and
23
+ free again after a cancel, so the rebook gets a new shipment.
24
+ - Sites that install core from GitHub no longer fail the build with TS2883 (inferred JSX types in the admin entry that
25
+ cannot be named from a consumer's node_modules): every admin export has an explicit type, checked by a test.
26
+
27
+ ## 0.1.0 (2 Dec 2026)
28
+
29
+ First version of the store, ported from FaRaQueen's shop and rebuilt as an EmDash plugin.
30
+
31
+ - D1 schema (`niaga_` tables) with migrations applied on first request or by `niaga migrate`.
32
+ - Catalogue, variant sheet that saves stock as a change from what the page showed, option axes, media from the EmDash
33
+ library, `importCatalog()`.
34
+ - Cart and checkout with stock reserved in the order's D1 batch, reservation expiry, vouchers with slot-based limits,
35
+ free shipping programme, Turnstile.
36
+ - Orders, work queue, shipments (duplicate carrier reference means "check the dashboard", never a second booking),
37
+ tracking, hand-over, returns and failed deliveries with refunds through the gateway or by hand.
38
+ - Events outbox with per-subscriber cursors, retries and parking; email notifications; document source for print
39
+ plugins.
40
+ - Buyer accounts with Google sign-in (optional).
41
+ - Commerce roles on top of EmDash's login: staff, owner, super admin.
42
+ - Sales reports with CSV; operator fee module with frozen rates, statements, PDF and CSV (optional).
43
+ - Admin pages in English and Indonesian, built for 390 px first.
44
+ - `niagaStorefront()` with the storefront API and `/api/webhooks/<provider>`; `niagaMockProviders()` for local stores.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Bagus Kurnianto Labamu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.id.md ADDED
@@ -0,0 +1,268 @@
1
+ # emdash-niaga-commerce
2
+
3
+ Toko online untuk situs [EmDash](https://github.com/emdash-cms/emdash), dibuat untuk penjual di Indonesia: produk
4
+ dengan lembar varian ala Shopee, stok yang tidak pernah terjual dua kali, keranjang dan checkout, antrean kerja pesanan
5
+ untuk packing dari HP, retur dan paket gagal kirim, voucher, akun pembeli, notifikasi email, laporan penjualan, dan
6
+ modul biaya operator (opsional). Pembayaran, pengiriman, dan cetak label datang dari plugin terpisah yang mengikuti
7
+ [emdash-niaga-commerce-contracts](https://github.com/baguskto/emdash-niaga-commerce/tree/main/packages/contracts), jadi toko bisa mengganti Mayar dengan gateway lain atau Biteship dengan
8
+ kurir lain tanpa mengubah tokonya.
9
+
10
+ English: [README.md](README.md).
11
+
12
+ **Status: 0.1, sudah di npm.** Sudah berjalan dari ujung ke ujung (lihat `examples/basic-store`) dan sudah diuji,
13
+ tetapi belum dipakai toko produksi.
14
+
15
+ ![Antrean pesanan di 390 px](https://raw.githubusercontent.com/baguskto/emdash-niaga-commerce/main/docs/screenshots/orders-390.png)
16
+
17
+ ## Fiturnya
18
+
19
+ - **Katalog**: produk, sampai dua jenis pilihan (warna, ukuran), lembar varian dengan isi-semua dan tempel satu kolom
20
+ dari spreadsheet, foto dan video dari pustaka media EmDash, kategori, nama dalam Bahasa Indonesia dan Inggris,
21
+ pencarian, dan `importCatalog()` untuk katalog JSON (ekspor Shopee bisa dipetakan ke sini).
22
+ - **Stok**: setiap unit dikunci saat pembeli checkout, dalam batch D1 yang sama dengan pesanannya. Constraint database
23
+ (`reserved <= stock`) menolak unit terakhir yang dipesan dua kali, jadi dua pembeli tidak mungkin membayar satu
24
+ barang. Pesanan yang tidak dibayar melepas stoknya saat batas waktu bayar lewat. Setiap perubahan tercatat di log stok.
25
+ - **Checkout**: checkout tamu dengan nomor WhatsApp, ongkir langsung dari plugin pengiriman, voucher, gratis ongkir dari
26
+ belanja minimum, Cloudflare Turnstile, dan halaman bayar dari plugin pembayaran.
27
+ - **Pesanan**: antrean kerja sesuai urutan kerja: pesan kurir, cetak label, serahkan ke kurir. Aksi massal, ubah alamat
28
+ sebelum kurir dipesan, batalkan pesanan yang belum dibayar (tagihannya ditutup di gateway), cek ulang pembayaran.
29
+ - **Retur dan gagal kirim**: buka kasus, kembalikan barang ke stok, tutup dengan kirim ulang, refund, tukar, saldo toko,
30
+ atau tanpa tindakan.
31
+ - **Voucher**: batch kode kartu sekali pakai atau satu kode kampanye, potongan persen, nominal, atau ongkir, belanja
32
+ minimum, khusus pesanan pertama, batas per pembeli dan kuota, dengan CSV kode untuk dicetak.
33
+ - **Akun pembeli** (opsional): masuk dengan Google, riwayat pesanan, dan alamat tersimpan. Tamu tidak perlu akun.
34
+ - **Event**: setiap perubahan masuk ke outbox (`niaga_events`) dan dikirim ke pelanggan event (email, print station,
35
+ kode Anda sendiri) dengan percobaan ulang; pengiriman yang gagal tampil di admin dan bisa dikirim lagi.
36
+ - **Notifikasi**: email ke pembeli saat dibayar, dikirim, sampai, dan refund, lewat penyedia email EmDash.
37
+ - **Laporan**: penjualan per hari, produk dan varian terlaris, metode bayar, kurir, kota, voucher, pembeli yang kembali,
38
+ dan kecepatan kirim, untuk owner dan super admin, dengan ekspor CSV.
39
+ - **Modul biaya operator** (opsional): untuk operator yang menjalankan toko untuk brand. Tarif dibekukan per pesanan,
40
+ biaya gateway asli dari provider kalau tersedia, tagihan bulanan dalam PDF dan CSV.
41
+ - **Pengaturan toko**: zona waktu, mata uang, bahasa (Indonesia dan Inggris), awalan kode pesanan dan invoice, alamat
42
+ pickup, kurir yang ditawarkan, jam batas kirim hari yang sama.
43
+
44
+ ## Kebutuhan
45
+
46
+ - EmDash 1.7 atau 1.8 di Astro 7.4 dengan adapter Cloudflare, satu database D1, dan satu bucket R2.
47
+ - Paket Cloudflare Workers Paid (situs EmDash dengan plugin native lebih besar dari batas 3 MiB paket gratis).
48
+ - Node 22 atau lebih baru untuk build, disarankan pnpm.
49
+ - Satu plugin pembayaran dan satu plugin pengiriman, misalnya
50
+ [emdash-niaga-payment-mayar](https://github.com/baguskto/emdash-niaga-payment-mayar) dan
51
+ [emdash-niaga-shipping-biteship](https://github.com/baguskto/emdash-niaga-shipping-biteship).
52
+
53
+ ## Instalasi
54
+
55
+ ```bash
56
+ pnpm add emdash-niaga-commerce emdash-niaga-commerce-contracts emdash-niaga-payment-mayar emdash-niaga-shipping-biteship
57
+ ```
58
+
59
+ Core 0.1.1 butuh contracts 0.3 (`^0.3.0`), begitu juga plugin provider yang dimuatnya: provider yang dibuat untuk minor
60
+ contracts lain ditolak saat start.
61
+
62
+ Untuk mencoba commit yang belum dirilis, pasang dari GitHub (`"github:baguskto/emdash-niaga-commerce#<sha>&path:/packages/core"`).
63
+ pnpm hanya mem-build paket git kalau diizinkan: kalau berhenti dengan `ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED`, tambahkan
64
+ kunci persis yang dicetaknya ke `allowBuilds` di `pnpm-workspace.yaml`, lalu jalankan instalasi lagi.
65
+
66
+ ## Pasang di situs
67
+
68
+ `astro.config.mjs`:
69
+
70
+ ```js
71
+ import cloudflare from "@astrojs/cloudflare";
72
+ import react from "@astrojs/react";
73
+ import { d1, r2 } from "@emdash-cms/cloudflare";
74
+ import { defineConfig } from "astro/config";
75
+ import emdash from "emdash/astro";
76
+ import { niagaCommerce, niagaStorefront } from "emdash-niaga-commerce";
77
+ import { niagaPaymentMayar } from "emdash-niaga-payment-mayar";
78
+ import { niagaShippingBiteship } from "emdash-niaga-shipping-biteship";
79
+
80
+ export default defineConfig({
81
+ output: "server",
82
+ adapter: cloudflare(),
83
+ integrations: [
84
+ react(),
85
+ emdash({
86
+ database: d1({ binding: "DB", session: "auto" }),
87
+ storage: r2({ binding: "MEDIA" }),
88
+ plugins: [
89
+ niagaPaymentMayar({ mode: "production" }),
90
+ niagaShippingBiteship(),
91
+ niagaCommerce({
92
+ // Nilai awal; semuanya bisa diubah nanti di Niaga > Store settings, dan yang di sana yang dipakai.
93
+ store: {
94
+ name: "Toko Saya",
95
+ siteUrl: "https://toko-saya.id",
96
+ orderPrefix: "TS",
97
+ origin: { name: "Gudang", phone: "+6281200000000", line1: "Jl. Contoh No. 1", postalCode: "40132", country: "ID" },
98
+ shipping: { provider: null, couriers: ["jne", "sicepat", "jnt", "anteraja"] },
99
+ },
100
+ superadmins: ["anda@contoh.id"],
101
+ owners: ["owner@contoh.id"],
102
+ }),
103
+ ],
104
+ }),
105
+ // API storefront di /api/niaga/* dan webhook di /api/webhooks/<provider>.
106
+ niagaStorefront(),
107
+ ],
108
+ });
109
+ ```
110
+
111
+ Urutan plugin tidak berpengaruh: provider mendaftar ke registry bersama, dan core mencarinya saat dibutuhkan.
112
+ `niagaStorefront({ basePath, webhookPath })` memindahkan kedua kelompok route kalau tema Anda butuh path lain.
113
+
114
+ `wrangler.jsonc` butuh binding D1 dan R2 yang sudah dipakai EmDash, dan cron trigger untuk tugas terjadwal Niaga
115
+ (kedaluwarsa pembayaran tiap menit, pelacakan tiap dua jam, sinkron biaya tiap hari):
116
+
117
+ ```jsonc
118
+ {
119
+ "d1_databases": [{ "binding": "DB", "database_name": "toko-saya" }],
120
+ "r2_buckets": [{ "binding": "MEDIA", "bucket_name": "toko-saya-media" }],
121
+ "triggers": { "crons": ["* * * * *"] }
122
+ }
123
+ ```
124
+
125
+ ### Opsi
126
+
127
+ | Opsi | Bawaan | Fungsinya |
128
+ | --- | --- | --- |
129
+ | `store` | bawaan core | Pengaturan toko awal (lihat `StoreSettings`). Halaman Store settings di admin menimpanya per kunci |
130
+ | `d1Binding` | `"DB"` | Binding D1 berisi tabel EmDash dan tabel `niaga_` |
131
+ | `queueBinding` | tidak ada | Queue untuk webhook: route langsung menjawab dan consumer queue Anda memanggil `processQueuedWebhook(msg)` dari `emdash-niaga-commerce/host`. Tanpa queue, webhook diproses langsung; sisanya diproses tugas tiap menit |
132
+ | `migrations` | `"auto"` | `"auto"` menjalankan migrasi pada request pertama sebuah isolate; `"manual"` mengharapkan `niaga migrate` saat deploy |
133
+ | `superadmins`, `owners` | tidak ada | Mengisi peran commerce sekali; baris yang diubah di admin tidak pernah ditimpa |
134
+ | `providerPlugins` | `{}` | Id provider ke id plugin EmDash, untuk plugin provider yang tidak bernama `niaga-<jenis>-<id>` |
135
+ | `env` | lihat di bawah | Nama variabel Worker, kalau milik Anda berbeda |
136
+
137
+ ## Migrasi database
138
+
139
+ Semua tabel berawalan `niaga_` dan berada di database D1 yang sama dengan EmDash. Dengan `migrations: "auto"` (bawaan)
140
+ skema dipasang pada request pertama tiap isolate; aman diulang dan cepat. Untuk menjalankannya saat deploy:
141
+
142
+ ```bash
143
+ pnpm exec niaga migrate --print # SQL-nya, untuk diperiksa atau dijalankan sendiri
144
+ pnpm exec niaga migrate --database DB --remote # lewat wrangler d1 execute
145
+ pnpm exec niaga migrate --database DB --local # database dev lokal
146
+ ```
147
+
148
+ ## Secret dan pengaturan
149
+
150
+ Kredensial provider disimpan di plugin provider: pasang sebagai secret Worker (nama env ada di README tiap plugin,
151
+ misalnya `MAYAR_API_KEY`) atau di halaman pengaturan plugin di admin EmDash. Secret Worker menang atas pengaturan.
152
+ Core membaca:
153
+
154
+ | Variabel Worker | Untuk |
155
+ | --- | --- |
156
+ | `TURNSTILE_SECRET` | Cek Turnstile saat checkout (opsional; tanpa ini checkout jalan tanpa cek) |
157
+ | `GOOGLE_CLIENT_ID` | Masuk dengan Google untuk pembeli (opsional; tanpa ini akun mati) |
158
+ | `PUBLIC_SITE_URL` | URL toko di email, invoice, dan link kembali dari pembayaran (menimpa pengaturan) |
159
+
160
+ Pasang dengan `wrangler secret put NAMA`. Jangan pernah di-commit; untuk `astro dev` taruh di `.dev.vars`, yang
161
+ diabaikan git.
162
+
163
+ ## Login, peran, dan Cloudflare Access
164
+
165
+ Niaga memakai login EmDash. Di produksi, taruh admin di belakang Cloudflare Access dan biarkan EmDash memetakan grup
166
+ identity provider Anda ke level perannya. Di atas itu, Niaga punya tiga peran commerce:
167
+
168
+ | Peran | Bisa |
169
+ | --- | --- |
170
+ | Staff | Today, Orders, Products, Returns, Vouchers (tanpa angka penjualan) |
171
+ | Owner | Semua yang bisa staff, plus Sales, Store settings, impor katalog, dan batch voucher baru |
172
+ | Super admin | Semuanya, plus modul biaya, tagihan, dan daftar orang dan peran |
173
+
174
+ Baris di daftar orang Niaga (Store settings) menentukan peran berdasarkan email. Tanpa baris, Editor EmDash menjadi
175
+ owner, sedangkan Contributor atau Author menjadi staff. Super admin selalu berupa baris eksplisit (isi dengan
176
+ `superadmins`), kecuali selama belum ada super admin sama sekali, saat Admin EmDash dihitung sebagai super admin supaya
177
+ peran pertama bisa diatur. Super admin aktif terakhir tidak bisa dihapus, dan tidak ada yang bisa mengubah barisnya
178
+ sendiri. Setiap panggilan API admin memeriksa peran lagi di server.
179
+
180
+ ## Halaman admin
181
+
182
+ Halaman muncul di bawah **Niaga** di sidebar admin EmDash (`/_emdash/admin/plugins/niaga-commerce/...`). Halaman memakai
183
+ design token EmDash sendiri, jadi ikut tema terang dan gelapnya, dan dibuat untuk HP 390 px lebih dulu.
184
+
185
+ | Halaman | Untuk apa | Tangkapan layar |
186
+ | --- | --- | --- |
187
+ | Today | Antrean kerja: pesan kurir, cetak, serahkan, masalah, retur terbuka, stok menipis | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/today-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/today-1440.png) |
188
+ | Orders | Tab tahap dengan jumlah, cari, filter, aksi massal; halaman pesanan dengan langkah berikutnya, data label, timeline | [daftar 390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/orders-390.png), [daftar 1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/orders-1440.png), [pesanan 390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/order-detail-390.png), [pesanan 1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/order-detail-1440.png) |
189
+ | Products | Daftar dengan stok menipis; lembar varian, pilihan, foto dari pustaka media, log stok | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/products-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/products-1440.png), [lembar 390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/variant-sheet-390.png), [lembar 1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/variant-sheet-1440.png) |
190
+ | Returns | Kasus terbuka dan selesai, kembalikan ke stok, tutup dengan keputusan | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/returns-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/returns-1440.png) |
191
+ | Vouchers | Batch dengan pemakaian, jeda atau akhiri, kode sebagai CSV, batch baru | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/vouchers-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/vouchers-1440.png) |
192
+ | Sales | Owner dan super admin: satu periode dibanding periode sebelumnya, per hari, yang terjual, CSV | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/reports-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/reports-1440.png) |
193
+ | Fees | Super admin, modul biaya aktif: transaksi, ringkasan, tagihan, tarif | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/fees-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/fees-1440.png) |
194
+ | Store settings | Toko, bahasa dan wilayah, checkout, provider, alamat pickup, notifikasi, peran, pengiriman event | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/settings-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/settings-1440.png) |
195
+
196
+ Admin mengikuti bahasa EmDash kalau Bahasa Indonesia atau Inggris, selain itu bahasa bawaan toko.
197
+
198
+ ## API storefront dan webhook
199
+
200
+ `niagaStorefront()` menambahkan route JSON yang dipanggil tema dari browser (atau impor `emdash-niaga-commerce/server`
201
+ di halaman Astro dan baca database langsung, seperti `examples/basic-store`):
202
+
203
+ | Route | Fungsinya |
204
+ | --- | --- |
205
+ | `GET /api/niaga/catalog?q=&category=&sort=` | Produk dan kategori |
206
+ | `GET /api/niaga/products/<slug>` | Data halaman produk dan JSON-LD |
207
+ | `GET, POST /api/niaga/cart` | Keranjang di cookie httpOnly: tambah, ubah, hapus, voucher, kosongkan |
208
+ | `GET /api/niaga/areas?q=` | Cari area dari plugin pengiriman |
209
+ | `POST /api/niaga/rates` | Ongkir untuk isi keranjang |
210
+ | `POST /api/niaga/checkout` | Membuat pesanan, mengunci stok, mengembalikan URL bayar |
211
+ | `GET /api/niaga/orders/<kode>?t=<token>` | Data halaman pesanan pembeli (pesanan belum dibayar dicek ulang ke gateway) |
212
+ | `GET /api/niaga/vouchers/<kode>` | Halaman voucher; pembukaan pertama memulai masa berlaku kode kartu |
213
+ | `/api/niaga/auth/google/*`, `/api/niaga/account*` | Masuk dengan Google (opsional) dan akun pembeli |
214
+
215
+ Penolakan dari provider (misalnya kode pos yang tidak dikenal kurir) dijawab dengan status 502, `error.code:
216
+ "provider"`, dan alasan dari provider, supaya tema bisa menampilkannya.
217
+
218
+ Gateway dan kurir mengirim ke `/api/webhooks/<id provider>`, misalnya `/api/webhooks/mayar` dan `/api/webhooks/biteship`.
219
+ Setiap kiriman diverifikasi oleh providernya, disimpan sekali per id event, dan diproses berurutan; webhook pembayaran
220
+ tidak pernah dipercaya begitu saja dan status bayar selalu dibaca ulang dari gateway.
221
+
222
+ ## Print station dan kode Anda sendiri
223
+
224
+ Core mengisi document source di registry, jadi plugin cetak seperti
225
+ [emdash-niaga-autoprint](https://github.com/baguskto/emdash-niaga-autoprint) membaca dokumen pesanan dan label kurir
226
+ tanpa berbicara dengan kurir, dan mencatat label yang sudah dicetak dari server lewat `recordPrint()` (pesanan pindah ke
227
+ "serahkan ke kurir", salinan berikutnya bertanda REPRINT, riwayat pesanan menyebut nama stasiunnya). Dokumen pesanan
228
+ membawa `publicTrackingUrl` tanpa token akses untuk QR di paket: isi halaman cek pesanan toko sebagai **Halaman lacak
229
+ publik** di Store settings (`paths.track`, misalnya `/orders/lookup?code={code}`); kosong berarti halaman pesanan tanpa
230
+ tokennya. Untuk bereaksi pada pesanan dari kode Anda sendiri, berlangganan event:
231
+
232
+ ```ts
233
+ import { subscribe } from "emdash-niaga-commerce-contracts";
234
+
235
+ subscribe({ subscriber: "sheet-saya", types: ["order.paid"], handle: async (event) => { /* ... */ } });
236
+ ```
237
+
238
+ Setiap pelanggan punya kursor sendiri, dicoba ulang dengan jeda, dan diparkir setelah gagal berulang kali; kiriman yang
239
+ diparkir tampil di Store settings dengan tombol untuk mengirim lagi.
240
+
241
+ ## Coba di lokal
242
+
243
+ `examples/basic-store` di repositori ini adalah situs EmDash lengkap dengan provider pembayaran dan pengiriman tiruan:
244
+
245
+ ```bash
246
+ pnpm install && pnpm build
247
+ cd examples/basic-store
248
+ pnpm dev # http://localhost:4410, admin di /_emdash/admin (login dev)
249
+ bash scripts/e2e.sh # checkout, bayar lewat webhook, pesan kurir, label, cetak, serahkan: PASS atau FAIL
250
+ ```
251
+
252
+ `NIAGA_PROVIDERS=live pnpm dev` menjalankan toko yang sama dengan Mayar dan Biteship (kredensial di `.dev.vars`).
253
+
254
+ ## Pengembangan
255
+
256
+ ```bash
257
+ pnpm install
258
+ pnpm --filter emdash-niaga-commerce typecheck
259
+ pnpm --filter emdash-niaga-commerce test
260
+ pnpm --filter emdash-niaga-commerce build
261
+ ```
262
+
263
+ Tes berjalan di SQLite bawaan Node lewat shim D1 kecil (`emdash-niaga-commerce/testing`), dengan provider tiruan dari
264
+ contracts. Setelah mengubah `migrations/*.sql`, jalankan `pnpm --filter emdash-niaga-commerce gen`.
265
+
266
+ ## Lisensi
267
+
268
+ MIT
package/README.md CHANGED
@@ -1,3 +1,268 @@
1
- # Temporary Holding Version
1
+ # emdash-niaga-commerce
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ An online store for [EmDash](https://github.com/emdash-cms/emdash) sites, built for Indonesian sellers: products with a
4
+ Shopee-style variant sheet, stock that never oversells, cart and checkout, an order work queue for packing on a phone,
5
+ returns and failed deliveries, vouchers, buyer accounts, email notifications, sales reports and an optional operator
6
+ fee module. Payments, shipping and printing come from separate plugins that implement
7
+ [emdash-niaga-commerce-contracts](https://github.com/baguskto/emdash-niaga-commerce/tree/main/packages/contracts), so a store can swap Mayar for another gateway or Biteship for another
8
+ carrier without touching the store.
9
+
10
+ Bahasa Indonesia: [README.id.md](README.id.md).
11
+
12
+ **Status: 0.1, on npm.** It runs end to end (see `examples/basic-store`) and is tested, but it has not run a production
13
+ store yet.
14
+
15
+ ![Orders work queue at 390 px](https://raw.githubusercontent.com/baguskto/emdash-niaga-commerce/main/docs/screenshots/orders-390.png)
16
+
17
+ ## What it does
18
+
19
+ - **Catalogue**: products, up to two option types (colour, size), a variant sheet with set-all and paste-a-column,
20
+ photos and videos picked from the EmDash media library, categories, Indonesian and English names, search, and
21
+ `importCatalog()` for a JSON catalogue (Shopee exports map onto it).
22
+ - **Stock**: every unit is reserved when a buyer checks out, in the same D1 batch as the order. A database constraint
23
+ (`reserved <= stock`) refuses the last unit twice, so two buyers can never pay for one item. Unpaid orders give their
24
+ units back when the payment deadline passes. Every change is in a stock log.
25
+ - **Checkout**: guest checkout by WhatsApp number, live courier rates from the shipping plugin, vouchers, free shipping
26
+ from a minimum spend, Cloudflare Turnstile, and a hosted payment page from the payment plugin.
27
+ - **Orders**: a work queue in the order things happen: book the courier, print the label, hand over. Bulk actions,
28
+ address edits before booking, cancelling an unpaid order (its invoice is closed at the gateway), re-check payment.
29
+ - **Returns and failed deliveries**: open a case, restock what came back, close it with resend, refund, exchange,
30
+ store credit or no action.
31
+ - **Vouchers**: batches of single-use card codes or one campaign code, percent, amount or shipping discounts, minimum
32
+ spend, first order only, per-buyer limits and quotas, with a CSV of codes for printing.
33
+ - **Buyer accounts** (optional): Google sign-in, order history and saved addresses. Guests never need an account.
34
+ - **Events**: every change goes into an outbox (`niaga_events`) and is delivered to subscribers (emails, the print
35
+ station, your own code) with retries; failed deliveries show in the admin and can be sent again.
36
+ - **Notifications**: emails to buyers on payment, shipping, delivery and refunds through EmDash's email provider.
37
+ - **Reports**: sales by day, best products and variants, payment methods, couriers, cities, vouchers, returning
38
+ buyers and dispatch speed, for owners and super admins, with CSV export.
39
+ - **Operator fee module** (optional): for an operator who runs stores for brands. Frozen rates per order, gateway fees
40
+ from the provider when it reports them, monthly statements with PDF and CSV.
41
+ - **Store settings**: time zone, currency, languages (English and Indonesian), order and invoice prefixes, pickup
42
+ address, couriers offered, dispatch cut-off.
43
+
44
+ ## Requirements
45
+
46
+ - EmDash 1.7 or 1.8 on Astro 7.4 with the Cloudflare adapter, a D1 database and an R2 bucket.
47
+ - A Cloudflare Workers Paid plan (an EmDash site with native plugins is larger than the free plan's 3 MiB).
48
+ - Node 22 or later for builds, pnpm recommended.
49
+ - A payment plugin and a shipping plugin, for example
50
+ [emdash-niaga-payment-mayar](https://github.com/baguskto/emdash-niaga-payment-mayar) and
51
+ [emdash-niaga-shipping-biteship](https://github.com/baguskto/emdash-niaga-shipping-biteship).
52
+
53
+ ## Install
54
+
55
+ ```bash
56
+ pnpm add emdash-niaga-commerce emdash-niaga-commerce-contracts emdash-niaga-payment-mayar emdash-niaga-shipping-biteship
57
+ ```
58
+
59
+ Core 0.1.1 needs contracts 0.3 (`^0.3.0`), and so do the provider plugins it loads: a provider built for another
60
+ contract minor is refused at startup.
61
+
62
+ To try an unreleased commit, install from GitHub instead (`"github:baguskto/emdash-niaga-commerce#<sha>&path:/packages/core"`).
63
+ pnpm builds git packages on install only when allowed: if it stops with `ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED`, add the
64
+ exact key it prints to `allowBuilds` in `pnpm-workspace.yaml` and run the install again.
65
+
66
+ ## Wire it into the site
67
+
68
+ `astro.config.mjs`:
69
+
70
+ ```js
71
+ import cloudflare from "@astrojs/cloudflare";
72
+ import react from "@astrojs/react";
73
+ import { d1, r2 } from "@emdash-cms/cloudflare";
74
+ import { defineConfig } from "astro/config";
75
+ import emdash from "emdash/astro";
76
+ import { niagaCommerce, niagaStorefront } from "emdash-niaga-commerce";
77
+ import { niagaPaymentMayar } from "emdash-niaga-payment-mayar";
78
+ import { niagaShippingBiteship } from "emdash-niaga-shipping-biteship";
79
+
80
+ export default defineConfig({
81
+ output: "server",
82
+ adapter: cloudflare(),
83
+ integrations: [
84
+ react(),
85
+ emdash({
86
+ database: d1({ binding: "DB", session: "auto" }),
87
+ storage: r2({ binding: "MEDIA" }),
88
+ plugins: [
89
+ niagaPaymentMayar({ mode: "production" }),
90
+ niagaShippingBiteship(),
91
+ niagaCommerce({
92
+ // Defaults; every value can be changed later in Niaga > Store settings, which wins.
93
+ store: {
94
+ name: "Toko Saya",
95
+ siteUrl: "https://toko-saya.id",
96
+ orderPrefix: "TS",
97
+ origin: { name: "Gudang", phone: "+6281200000000", line1: "Jl. Contoh No. 1", postalCode: "40132", country: "ID" },
98
+ shipping: { provider: null, couriers: ["jne", "sicepat", "jnt", "anteraja"] },
99
+ },
100
+ superadmins: ["you@example.com"],
101
+ owners: ["owner@example.com"],
102
+ }),
103
+ ],
104
+ }),
105
+ // Storefront API at /api/niaga/* and webhooks at /api/webhooks/<provider>.
106
+ niagaStorefront(),
107
+ ],
108
+ });
109
+ ```
110
+
111
+ The order of plugins does not matter: providers register into a shared registry, and core looks them up when it needs
112
+ them. `niagaStorefront({ basePath, webhookPath })` moves the two route groups if your theme needs other paths.
113
+
114
+ `wrangler.jsonc` needs the D1 and R2 bindings EmDash already uses, and a cron trigger for Niaga's scheduled tasks
115
+ (payment expiry every minute, tracking every two hours, fee sync daily):
116
+
117
+ ```jsonc
118
+ {
119
+ "d1_databases": [{ "binding": "DB", "database_name": "my-store" }],
120
+ "r2_buckets": [{ "binding": "MEDIA", "bucket_name": "my-store-media" }],
121
+ "triggers": { "crons": ["* * * * *"] }
122
+ }
123
+ ```
124
+
125
+ ### Options
126
+
127
+ | Option | Default | What it does |
128
+ | --- | --- | --- |
129
+ | `store` | built-in defaults | Store settings to start from (see `StoreSettings`). The admin's Store settings page overrides them key by key |
130
+ | `d1Binding` | `"DB"` | D1 binding with EmDash's tables and the `niaga_` tables |
131
+ | `queueBinding` | none | Queue for webhooks: the route answers at once and your queue consumer calls `processQueuedWebhook(msg)` from `emdash-niaga-commerce/host`. Without it webhooks are processed inline; anything left over is processed by the minute task |
132
+ | `migrations` | `"auto"` | `"auto"` applies migrations on an isolate's first request; `"manual"` expects `niaga migrate` in your deploy |
133
+ | `superadmins`, `owners` | none | Seeds the commerce roles once; rows changed in the admin are never overwritten |
134
+ | `providerPlugins` | `{}` | Provider id to EmDash plugin id, for provider plugins not named `niaga-<kind>-<id>` |
135
+ | `env` | see below | Names of Worker variables, if yours differ |
136
+
137
+ ## Database migrations
138
+
139
+ All tables start with `niaga_` and live in the same D1 database as EmDash. With `migrations: "auto"` (the default) the
140
+ schema is applied on the first request of each isolate; it is idempotent and quick. To run them in your deploy instead:
141
+
142
+ ```bash
143
+ pnpm exec niaga migrate --print # the SQL, to review or apply yourself
144
+ pnpm exec niaga migrate --database DB --remote # through wrangler d1 execute
145
+ pnpm exec niaga migrate --database DB --local # the local dev database
146
+ ```
147
+
148
+ ## Secrets and settings
149
+
150
+ Provider credentials live with the provider plugin: set them as Worker secrets (the env names are in each plugin's
151
+ README, for example `MAYAR_API_KEY`) or in the plugin's settings page in the EmDash admin. A Worker secret wins over a
152
+ setting. Core reads:
153
+
154
+ | Worker variable | Needed for |
155
+ | --- | --- |
156
+ | `TURNSTILE_SECRET` | Turnstile check on checkout (optional; without it checkout runs without the check) |
157
+ | `GOOGLE_CLIENT_ID` | Google sign-in for buyers (optional; without it accounts are off) |
158
+ | `PUBLIC_SITE_URL` | The storefront URL in emails, invoices and payment return links (overrides the setting) |
159
+
160
+ Set them with `wrangler secret put NAME`. Never commit them; for `astro dev` put them in `.dev.vars`, which is
161
+ git-ignored.
162
+
163
+ ## Sign-in, roles and Cloudflare Access
164
+
165
+ Niaga uses EmDash's own login. In production, put the admin behind Cloudflare Access and let EmDash map your identity
166
+ provider's groups to its role levels. On top of that, Niaga has three commerce roles:
167
+
168
+ | Role | Can |
169
+ | --- | --- |
170
+ | Staff | Today, Orders, Products, Returns, Vouchers (no sales figures) |
171
+ | Owner | Everything staff can, plus Sales, Store settings, catalogue import and new voucher batches |
172
+ | Super admin | Everything, plus the fee module, statements and the list of people and roles |
173
+
174
+ A row in Niaga's list of people (Store settings) decides the role by email. Without a row, an EmDash Editor is an owner
175
+ and a Contributor or Author is staff. Super admin is only ever an explicit row (seed it with `superadmins`), except
176
+ while no super admin exists at all, when an EmDash Admin counts as one so the first roles can be set. The last active
177
+ super admin can never be removed, and nobody changes their own row. Every admin API call checks the role again on the
178
+ server.
179
+
180
+ ## Admin pages
181
+
182
+ The pages appear under **Niaga** in the EmDash admin sidebar (`/_emdash/admin/plugins/niaga-commerce/...`). They use
183
+ EmDash's own design tokens, so they follow its light and dark themes, and they are built for a 390 px phone first.
184
+
185
+ | Page | What it is for | Screenshots |
186
+ | --- | --- | --- |
187
+ | Today | The work queue: book the courier, print, hand over, problems, open returns, low stock | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/today-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/today-1440.png) |
188
+ | Orders | Stage tabs with counts, search, filters, bulk actions; the order page with its next step, label data, timeline | [list 390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/orders-390.png), [list 1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/orders-1440.png), [order 390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/order-detail-390.png), [order 1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/order-detail-1440.png) |
189
+ | Products | The list with low stock; the variant sheet, options, photos from the media library, stock log | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/products-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/products-1440.png), [sheet 390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/variant-sheet-390.png), [sheet 1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/variant-sheet-1440.png) |
190
+ | Returns | Open and closed cases, restock, close with a decision | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/returns-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/returns-1440.png) |
191
+ | Vouchers | Batches with usage, pause or end, codes as CSV, new batch | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/vouchers-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/vouchers-1440.png) |
192
+ | Sales | Owners and super admins: a period against the one before, day by day, what sold, CSV | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/reports-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/reports-1440.png) |
193
+ | Fees | Super admins, fee module on: transactions, summary, statements, rates | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/fees-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/fees-1440.png) |
194
+ | Store settings | Store, language and region, checkout, providers, pickup address, notifications, roles, event deliveries | [390](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/settings-390.png), [1440](https://github.com/baguskto/emdash-niaga-commerce/blob/main/docs/screenshots/settings-1440.png) |
195
+
196
+ The admin follows EmDash's language when it is English or Indonesian, otherwise the store's default language.
197
+
198
+ ## Storefront API and webhooks
199
+
200
+ `niagaStorefront()` adds JSON routes your theme calls from the browser (or import `emdash-niaga-commerce/server` in
201
+ Astro pages and read the database directly, as `examples/basic-store` does):
202
+
203
+ | Route | What it does |
204
+ | --- | --- |
205
+ | `GET /api/niaga/catalog?q=&category=&sort=` | Products and categories |
206
+ | `GET /api/niaga/products/<slug>` | Product page data and JSON-LD |
207
+ | `GET, POST /api/niaga/cart` | The cart in an httpOnly cookie: add, set, remove, voucher, clear |
208
+ | `GET /api/niaga/areas?q=` | Area search from the shipping plugin |
209
+ | `POST /api/niaga/rates` | Courier rates for the cart |
210
+ | `POST /api/niaga/checkout` | Places the order, reserves stock, returns the payment URL |
211
+ | `GET /api/niaga/orders/<code>?t=<token>` | The buyer's order page data (re-checks an unpaid order with the gateway) |
212
+ | `GET /api/niaga/vouchers/<code>` | Voucher landing; the first open starts a card code's validity |
213
+ | `/api/niaga/auth/google/*`, `/api/niaga/account*` | Optional Google sign-in and the buyer's account |
214
+
215
+ A provider refusal (for example a postal code the carrier does not know) comes back as status 502 with
216
+ `error.code: "provider"` and the provider's reason, so the theme can show it.
217
+
218
+ Gateways and carriers post to `/api/webhooks/<provider id>`, for example `/api/webhooks/mayar` and
219
+ `/api/webhooks/biteship`. Every delivery is verified by its provider, stored once by event id and processed in order;
220
+ a payment webhook never trusts the payload and always re-reads the payment from the gateway.
221
+
222
+ ## Print stations and your own code
223
+
224
+ Core sets the registry's document source, so a print plugin such as
225
+ [emdash-niaga-autoprint](https://github.com/baguskto/emdash-niaga-autoprint) reads order documents and carrier labels
226
+ without talking to a carrier, and records printed labels server side with `recordPrint()` (the order moves to "hand
227
+ over", the next copy says REPRINT, the order history names the station). The order document carries a
228
+ `publicTrackingUrl` with no access token for the QR on parcels: set the store's order lookup page as **Public tracking
229
+ page** in Store settings (`paths.track`, for example `/orders/lookup?code={code}`); empty means the order page without
230
+ its token. To react to orders in your own code, subscribe to events:
231
+
232
+ ```ts
233
+ import { subscribe } from "emdash-niaga-commerce-contracts";
234
+
235
+ subscribe({ subscriber: "my-sheet", types: ["order.paid"], handle: async (event) => { /* ... */ } });
236
+ ```
237
+
238
+ Each subscriber has its own cursor, retries with backoff, and is parked after repeated failures; parked deliveries are
239
+ listed in Store settings with a button to send them again.
240
+
241
+ ## Try it locally
242
+
243
+ `examples/basic-store` in this repository is a complete EmDash site with mock payment and shipping providers:
244
+
245
+ ```bash
246
+ pnpm install && pnpm build
247
+ cd examples/basic-store
248
+ pnpm dev # http://localhost:4410, admin at /_emdash/admin (dev sign-in)
249
+ bash scripts/e2e.sh # checkout, pay by webhook, book, label, print, hand over: PASS or FAIL per step
250
+ ```
251
+
252
+ `NIAGA_PROVIDERS=live pnpm dev` runs the same store with Mayar and Biteship instead (credentials in `.dev.vars`).
253
+
254
+ ## Development
255
+
256
+ ```bash
257
+ pnpm install
258
+ pnpm --filter emdash-niaga-commerce typecheck
259
+ pnpm --filter emdash-niaga-commerce test
260
+ pnpm --filter emdash-niaga-commerce build
261
+ ```
262
+
263
+ Tests run on Node's built-in SQLite through a small D1 shim (`emdash-niaga-commerce/testing`), with the contracts'
264
+ mock providers. After editing `migrations/*.sql`, run `pnpm --filter emdash-niaga-commerce gen`.
265
+
266
+ ## Licence
267
+
268
+ MIT