@pybara/pybara-js 0.4.0 → 0.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/pybara-js.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /* Pybara.js - pybara.com */
2
2
  import { n as e, r as t, t as n } from "./admin-B1pVP25-.js";
3
- import { t as r } from "./modal-DCwwkKZ7.js";
3
+ import { t as r } from "./modal-CvQ7qr5d.js";
4
4
  var i = {
5
5
  widget: "donate-widget",
6
6
  recurring: "donate-widget-recurring"
@@ -24,21 +24,25 @@ async function s(t, n, r) {
24
24
  } catch (e) {
25
25
  return {
26
26
  show: !1,
27
+ cause: "unreadable",
27
28
  reason: `could not read the widget's configuration (${e instanceof Error ? e.message : String(e)})`
28
29
  };
29
30
  }
30
31
  let f = u.kill_switch?.plugins ?? {};
31
32
  return f[i.widget]?.enabled === !0 ? !d.signed || !d.version ? {
32
33
  show: !1,
34
+ cause: "terms",
33
35
  reason: `${n} has not accepted the widget's terms in Pybara Wallet (Manage › Sites › Add a site without a plugin)`
34
36
  } : a(d.version, "1-0") ? {
35
37
  show: !0,
36
38
  recurring: f[i.recurring]?.enabled === !0
37
39
  } : {
38
40
  show: !1,
41
+ cause: "terms",
39
42
  reason: `this widget version requires terms 1-0, and ${n} accepted ${d.version}: accept the new terms in Pybara Wallet (Manage › Sites)`
40
43
  } : {
41
44
  show: !1,
45
+ cause: "switched-off",
42
46
  reason: "the widget is switched off by Pybara for now"
43
47
  };
44
48
  }
@@ -50,7 +54,40 @@ var c = class extends Error {
50
54
  }
51
55
  }, l = /^(0|[1-9]\d*)(\.\d+)?$/, u = /^[a-z0-9-]{1,64}$/, d = class {
52
56
  constructor(e) {
53
- this.adminClient = null, this.modal = null, this.gateOutcome = null, this.isOpen = !1, this.completedPayment = !1, this.activePayment = null, this.config = m(e), this.ready = this.init();
57
+ this.adminClient = null, this.modal = null, this.gateOutcome = null, this.isOpen = !1, this.completedPayment = !1, this.activePayment = null, this.lastPrecheck = {}, this.config = m(e), this.ready = this.init(), this.config.onAvailability && this.precheck();
58
+ }
59
+ precheck(e = {}) {
60
+ let t = typeof e.amount == "number" && Number.isFinite(e.amount) ? String(e.amount) : e.amount;
61
+ this.lastPrecheck = {
62
+ amount: typeof t == "string" ? t : void 0,
63
+ currency: e.currency ?? this.config.currency
64
+ }, this.sendPrecheck();
65
+ }
66
+ async sendPrecheck() {
67
+ let e = this.config.onAvailability;
68
+ if (!e) return;
69
+ await this.ready;
70
+ let t = this.gateOutcome;
71
+ if (t && !t.show) {
72
+ let n = t.cause === "unreadable" ? {
73
+ status: "retry-later",
74
+ reason: "GATE_UNREADABLE",
75
+ detail: `Pybara: ${t.reason}; the checkout tries again at the click.`
76
+ } : t.cause === "switched-off" ? {
77
+ status: "unavailable",
78
+ reason: "SWITCHED_OFF",
79
+ detail: `Pybara: ${t.reason}.`
80
+ } : {
81
+ status: "unavailable",
82
+ reason: "SITE_NOT_READY",
83
+ detail: `Pybara: ${t.reason}; the site owner does this, not the page. https://docs.pybara.com/pybara-js/setup/`
84
+ };
85
+ n.detail && console.warn(n.detail), e(n);
86
+ } else this.modal?.precheck({
87
+ beneficiary: this.config.recipientPrincipal,
88
+ siteUrl: this.config.siteUrl,
89
+ ...this.lastPrecheck
90
+ });
54
91
  }
55
92
  get gate() {
56
93
  return this.gateOutcome;
@@ -127,6 +164,8 @@ var c = class extends Error {
127
164
  walletUrl: t[e],
128
165
  context: "donation",
129
166
  thankYouMessage: n.thankYouMessage,
167
+ locale: n.locale,
168
+ onAvailability: n.onAvailability,
130
169
  onSuccess: (e) => {
131
170
  i.completedPayment = !0;
132
171
  let t = i.activePayment, r = t?.frequency != null, a = {
@@ -152,7 +191,7 @@ var c = class extends Error {
152
191
  let e = i.activePayment;
153
192
  i.activePayment = null, e?.onClose?.(), i.completedPayment && n.thankYouRedirectUrl ? window.location.href = n.thankYouRedirectUrl : (i.completedPayment = !1, i.modal = null, i.initCheckoutModal());
154
193
  }
155
- });
194
+ }), n.onAvailability && this.modal && Object.keys(this.lastPrecheck).length > 0 && this.sendPrecheck();
156
195
  }
157
196
  checkoutFor(e, t, n) {
158
197
  let r = this.config;
package/dist/types.d.ts CHANGED
@@ -1,9 +1,4 @@
1
- /**
2
- * Public types for @pybara/pybara-js.
3
- *
4
- * The surface is deliberately shaped like stripe-js and paypal-js: a brand-level object (`Pybara`) with a verb (`checkout`) that returns a callable, instead of a rendered component.
5
- * The host owns its DOM — amount selector, payment-method list, form fields — and Pybara opens its own checkout surface over it.
6
- */
1
+ import { Availability } from './modal/modal';
7
2
  /** Recurring frequencies — must match subs.mo's allowedFrequencies, the canister that schedules the pulls. 'every2min' exists for testing. */
8
3
  export type Frequency = 'weekly' | 'monthly' | 'yearly' | 'every2min';
9
4
  /** Site-level configuration, bound once by Pybara.checkout(). */
@@ -18,6 +13,8 @@ export interface CheckoutConfig {
18
13
  currency?: string;
19
14
  /** Replaces the success screen's default title. */
20
15
  thankYouMessage?: string;
16
+ /** The checkout's language, a BCP 47 tag ('fr', 'de-DE', 'zh-CN'), usually your site's: shown when Pybara has it, otherwise the payer's own language. */
17
+ locale?: string;
21
18
  /** Redirect target once the payer leaves the success screen (PDF receipt, member area). */
22
19
  thankYouRedirectUrl?: string;
23
20
  /** Site base country (ISO 3166-1 alpha-2). */
@@ -38,6 +35,13 @@ export interface CheckoutConfig {
38
35
  staging?: boolean;
39
36
  /** Debug logging. Default false. */
40
37
  debug?: boolean;
38
+ /**
39
+ * Whether a payment would go through right now, before any click: called once the checkout is ready, then after each precheck({ amount, currency }).
40
+ * Act on `status`: `available`, show Pybara; `unavailable`, hide it (nothing on the page can fix it: the site isn't registered, its DNS record or terms are missing, the currency isn't served); `below-minimum`, keep it but disable the button, showing `message` ("Minimum €0.70"); `retry-later`, keep it.
41
+ * `message` is in the payer's language; `detail`, in English, says what is missing and who fixes it, and is also printed to the console.
42
+ * Without this callback nothing changes: open() checks everything at the click, as always.
43
+ */
44
+ onAvailability?: (availability: Availability) => void;
41
45
  }
42
46
  /** One payment attempt — the params of a single open() call. */
43
47
  export interface PaymentParams {
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,8IAA8I;AAC9I,MAAM,MAAM,SAAS,GAAG,QAAQ,GAAG,SAAS,GAAG,QAAQ,GAAG,WAAW,CAAC;AAEtE,iEAAiE;AACjE,MAAM,WAAW,cAAc;IAC7B,uKAAuK;IACvK,kBAAkB,EAAE,MAAM,CAAC;IAC3B,gIAAgI;IAChI,OAAO,EAAE,MAAM,CAAC;IAChB,iHAAiH;IACjH,QAAQ,EAAE,MAAM,CAAC;IACjB,uLAAuL;IACvL,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,mDAAmD;IACnD,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,2FAA2F;IAC3F,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,8CAA8C;IAC9C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,2PAA2P;IAC3P,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uFAAuF;IACvF,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,sFAAsF;IACtF,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,6KAA6K;IAC7K,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,mHAAmH;IACnH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,wFAAwF;IACxF,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,iFAAiF;IACjF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,oCAAoC;IACpC,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,gEAAgE;AAChE,MAAM,WAAW,aAAa;IAC5B;;;;OAIG;IACH,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,kKAAkK;IAClK,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,yDAAyD;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FAAiF;IACjF,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gFAAgF;IAChF,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,qDAAqD;IACrD,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,IAAI,CAAC;IAC7C,oEAAoE;IACpE,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACjC,iFAAiF;IACjF,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACtB;AAED,6EAA6E;AAC7E,MAAM,WAAW,cAAc;IAC7B,gFAAgF;IAChF,UAAU,EAAE,MAAM,CAAC;IACnB,oEAAoE;IACpE,SAAS,EAAE,MAAM,CAAC;IAClB,yCAAyC;IACzC,KAAK,EAAE,MAAM,CAAC;IACd,wHAAwH;IACxH,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,8DAA8D;IAC9D,WAAW,EAAE,OAAO,CAAC;IACrB,oCAAoC;IACpC,cAAc,EAAE,MAAM,CAAC;IACvB,wFAAwF;IACxF,kBAAkB,EAAE,MAAM,CAAC;CAC5B;AAED;;;GAGG;AACH,MAAM,MAAM,eAAe,GACvB,gBAAgB,GAChB,cAAc,GACd,qBAAqB,GACrB,iBAAiB,CAAC;AAEtB,qBAAa,WAAY,SAAQ,KAAK;IACpC,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC;gBAEnB,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM;CAKnD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pybara/pybara-js",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Accept crypto payments and donations with Pybara on any page — a callable checkout, in the stripe-js / paypal-js convention.",
5
5
  "keywords": [
6
6
  "pybara",