@paynext/sdk 0.0.72 → 0.0.73

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/index.d.ts CHANGED
@@ -492,6 +492,7 @@ export declare interface PayNextConfig {
492
492
  declare class PayPalService {
493
493
  private paypal;
494
494
  init(config: PayPalScriptOptions): Promise<void>;
495
+ isLoaded(): boolean;
495
496
  render(parent: HTMLElement, options?: PayPalButtonsComponentOptions): Promise<void>;
496
497
  }
497
498
 
package/dist/index.es.js CHANGED
@@ -4393,45 +4393,68 @@ class lc {
4393
4393
  // init
4394
4394
  init(t) {
4395
4395
  return q(this, null, function* () {
4396
- var o, s, l;
4397
- if (!this.config) return !1;
4396
+ var o, s;
4397
+ if (!this.config)
4398
+ return !1;
4398
4399
  const e = this.config.state.transaction.getState(), n = this.config.state.checkout.getState(), a = () => q(this, null, function* () {
4399
- return yield jl(n.clientToken, n.environment, n.apiVersion);
4400
+ try {
4401
+ return yield jl(n.clientToken, n.environment, n.apiVersion);
4402
+ } catch (l) {
4403
+ throw l;
4404
+ }
4400
4405
  });
4401
4406
  if (!e || Object.keys(e).length === 0)
4402
4407
  return console.error("PayPal transaction data is empty or invalid"), !1;
4403
- yield this.paypal.init({
4404
- clientId: ((o = e == null ? void 0 : e.paypal_info) == null ? void 0 : o.client_id) || "",
4405
- environment: ((s = e == null ? void 0 : e.paypal_info) == null ? void 0 : s.environment) || "sandbox",
4406
- vault: !0,
4407
- locale: Yl(this.config.state.translate.locale())
4408
- });
4408
+ if (!((o = e == null ? void 0 : e.paypal_info) != null && o.client_id))
4409
+ return !1;
4410
+ try {
4411
+ if (yield this.paypal.init({
4412
+ clientId: e.paypal_info.client_id,
4413
+ environment: e.paypal_info.environment || "sandbox",
4414
+ vault: !0,
4415
+ locale: Yl(this.config.state.translate.locale())
4416
+ }), !this.paypal.isLoaded())
4417
+ return !1;
4418
+ } catch (l) {
4419
+ return !1;
4420
+ }
4409
4421
  const i = t ? ee(K({}, t), {
4410
- onError: (c) => {
4411
- var f, m;
4422
+ onError: (l) => {
4423
+ var p, f;
4412
4424
  if (!this.isInitialized) {
4413
- console.error("PayPal error:", c);
4425
+ console.error("PayPal error:", l);
4414
4426
  return;
4415
4427
  }
4416
- const d = this.config.state.translate.messages(), u = je(c, d), h = xe(d)[u], p = ((f = this.config.state.translate.messages().errors) == null ? void 0 : f.errorText) || "Error";
4417
- this.error.setMessage(`PayPal ${p.toLowerCase()}: ${h != null ? h : u}`), this.error.render().style.display = "", (m = t.onError) == null || m.call(t, c);
4428
+ const c = this.config.state.translate.messages(), d = je(l, c), u = xe(c)[d], h = ((p = this.config.state.translate.messages().errors) == null ? void 0 : p.errorText) || "Error";
4429
+ this.error.setMessage(`PayPal ${h.toLowerCase()}: ${u != null ? u : d}`), this.error.render().style.display = "", (f = t.onError) == null || f.call(t, l);
4418
4430
  },
4419
4431
  onCancel: t.onCancel,
4420
- onSuccess: (c) => {
4421
- var d;
4422
- this.error.render().style.display = "none", (d = t.onSuccess) == null || d.call(t, c);
4432
+ onSuccess: (l) => {
4433
+ var c;
4434
+ this.error.render().style.display = "none", (c = t.onSuccess) == null || c.call(t, l);
4423
4435
  }
4424
4436
  }) : void 0;
4425
- return yield this.paypal.render(this.element, ee(K({}, i), {
4426
- createBillingAgreement: a,
4427
- onApprove: (d) => q(this, [d], function* ({ billingToken: c }) {
4428
- var u;
4429
- (u = i == null ? void 0 : i.onSuccess) == null || u.call(i, c || "");
4430
- }),
4431
- style: (l = this.customButtonStyles) == null ? void 0 : l.styles,
4432
- onCancel: i == null ? void 0 : i.onCancel,
4433
- onError: i == null ? void 0 : i.onError
4434
- })), this.element.addEventListener("click", () => _e.hideAll()), this.element.append(this.error.render()), this.element.classList.remove(Mt.hidden), this.isInitialized = !0, this.events.ready.emit(), !0;
4437
+ try {
4438
+ yield this.paypal.render(this.element, ee(K({}, i), {
4439
+ createBillingAgreement: a,
4440
+ onApprove: (c) => q(this, [c], function* ({ billingToken: l }) {
4441
+ var d;
4442
+ (d = i == null ? void 0 : i.onSuccess) == null || d.call(i, l || "");
4443
+ }),
4444
+ style: (s = this.customButtonStyles) == null ? void 0 : s.styles,
4445
+ onCancel: () => {
4446
+ var l;
4447
+ (l = i == null ? void 0 : i.onCancel) == null || l.call(i);
4448
+ },
4449
+ onError: (l) => {
4450
+ var c;
4451
+ (c = i == null ? void 0 : i.onError) == null || c.call(i, l);
4452
+ }
4453
+ }));
4454
+ } catch (l) {
4455
+ return !1;
4456
+ }
4457
+ return this.element.addEventListener("click", () => _e.hideAll()), this.element.append(this.error.render()), this.element.classList.remove(Mt.hidden), this.isInitialized = !0, this.events.ready.emit(), !0;
4435
4458
  });
4436
4459
  }
4437
4460
  // add event listener
@@ -4686,15 +4709,16 @@ class bc {
4686
4709
  if (this.paypal) {
4687
4710
  this.isPayPalReady = !1;
4688
4711
  try {
4689
- (yield this.paypal.init({
4690
- onError: (n) => this.events.error.emit(n),
4712
+ const e = this.paypal.render();
4713
+ t == null || t.appendChild(e), (yield this.paypal.init({
4714
+ onError: (a) => this.events.error.emit(a),
4691
4715
  onCancel: () => this.events.cancel.emit(),
4692
- onSuccess: (n) => {
4693
- this.lastInteracted = "paypal", this.events.success.emit({ paypalId: n, type: "paypal" });
4716
+ onSuccess: (a) => {
4717
+ this.lastInteracted = "paypal", this.events.success.emit({ paypalId: a, type: "paypal" });
4694
4718
  }
4695
- })) ? t == null || t.appendChild(this.paypal.render()) : t == null || t.remove();
4719
+ })) || t == null || t.remove();
4696
4720
  } catch (e) {
4697
- console.error(e), t == null || t.remove();
4721
+ t == null || t.remove();
4698
4722
  } finally {
4699
4723
  this.isPayPalReady = !0;
4700
4724
  }
@@ -8661,21 +8685,29 @@ class Nu {
8661
8685
  try {
8662
8686
  this.paypal = yield Tu(t);
8663
8687
  } catch (e) {
8664
- console.error("failed to load the PayPal JS SDK script", e);
8688
+ throw this.paypal = null, e;
8665
8689
  }
8666
8690
  });
8667
8691
  }
8692
+ // check if PayPal SDK is loaded
8693
+ isLoaded() {
8694
+ return this.paypal !== null && this.paypal.Buttons !== void 0;
8695
+ }
8668
8696
  // render
8669
8697
  render(t, e) {
8670
8698
  return q(this, null, function* () {
8671
8699
  var n, a;
8700
+ if (!t.isConnected)
8701
+ throw new Error("PayPal parent element must be connected to DOM before render()");
8672
8702
  if (this.paypal) {
8673
- const i = K(K({}, Iu), (e == null ? void 0 : e.style) || {});
8674
- yield (a = (n = this.paypal).Buttons) == null ? void 0 : a.call(n, ee(K({
8703
+ const i = K(K({}, Iu), (e == null ? void 0 : e.style) || {}), o = ee(K({
8675
8704
  fundingSource: "paypal"
8676
8705
  }, e), {
8677
8706
  style: i
8678
- })).render(t);
8707
+ }), s = (a = (n = this.paypal).Buttons) == null ? void 0 : a.call(n, o);
8708
+ if (!s)
8709
+ throw new Error("PayPal Buttons instance is null");
8710
+ console.log("PayPalService: Buttons instance created, calling render()..."), yield s.render(t);
8679
8711
  }
8680
8712
  });
8681
8713
  }