@swirepay-developer/swirepay-card-sdk 2.0.2 → 2.0.4

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": "@swirepay-developer/swirepay-card-sdk",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "Swirepay Card Payment SDK (Web Component with Modal UI)",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -181,41 +181,33 @@ export class SwirepayCheckout extends HTMLElement {
181
181
  return await response.json();
182
182
  }
183
183
 
184
- async updateFromAttributes() {
184
+ async connectedCallback() {
185
+ this.amount = parseInt(this.getAttribute("amount")) || 0;
186
+ this.test = this.getAttribute("mode") === "test";
187
+ this.currencyCode = this.getAttribute("currencycode");
188
+ this.apiKey = this.getAttribute("api-key");
189
+ this.isAddressRequired = this.getAttribute("isaddressrequired") === "true";
190
+ this.inventory = this.getAttribute("inventory") === "true";
191
+ this.frequency = this.getAttribute("frequency");
192
+ this.description = this.getAttribute("description");
193
+ this.totalPayments = this.getAttribute('totalPayments');
185
194
  try {
186
- this.amount = parseInt(this.getAttribute("amount")) || 0;
187
- this.test = this.getAttribute("mode") === "test";
188
- this.currencyCode = this.getAttribute("currencycode");
189
- this.apiKey = this.getAttribute("api-key");
190
- this.isAddressRequired = this.getAttribute("isaddressrequired") === "true";
191
- this.inventory = this.getAttribute("inventory") === "true";
192
- this.planGid = this.getAttribute("plangid");
193
- this.productName = this.getAttribute("productname");
194
- this.frequency = this.getAttribute("frequency");
195
- this.description = this.getAttribute("description");
196
- this.totalPayments = this.getAttribute("totalpayments");
197
195
  const customerAttr = this.getAttribute("customer");
198
196
  this.customer = customerAttr ? JSON.parse(customerAttr) : {};
199
-
200
- const inventoryOrders = this.getAttribute("inventoryorders");
197
+ const inventoryOrders = this.getAttribute("inventoryOrders");
201
198
  this.inventoryOrder = inventoryOrders ? JSON.parse(inventoryOrders) : {};
202
- if (this.inventory && this.inventoryOrder) {
199
+ if (this.inventory) {
203
200
  const splitUpInfo = await this.getSplitUpDetails(this.inventoryOrder);
204
201
  this.splitUp = splitUpInfo?.entity;
205
202
  }
206
-
207
203
  } catch (e) {
208
- console.error("Attribute parsing error:", e);
204
+ console.error("Invalid JSON", e);
205
+ this.customer = {};
206
+ this.inventoryOrder = {};
209
207
  }
210
- }
211
-
212
- async connectedCallback() {
213
- await this.updateFromAttributes();
214
-
215
208
  await Promise.all([
216
209
  loadScript(SDK_CONFIG.elliptic),
217
- loadScript(SDK_CONFIG.crypto),
218
- loadScript(SDK_CONFIG.plaid)
210
+ loadScript(SDK_CONFIG.crypto)
219
211
  ]);
220
212
 
221
213
  this.render();
@@ -229,8 +221,6 @@ export class SwirepayCheckout extends HTMLElement {
229
221
  "api-key",
230
222
  "isaddressrequired",
231
223
  "inventory",
232
- "plangid",
233
- "productname",
234
224
  "frequency",
235
225
  "description",
236
226
  "totalpayments",
@@ -239,13 +229,72 @@ export class SwirepayCheckout extends HTMLElement {
239
229
  ];
240
230
  }
241
231
 
242
- async attributeChangedCallback(name, oldValue, newValue) {
232
+ attributeChangedCallback(name, oldValue, newValue) {
243
233
  if (oldValue === newValue) return;
244
234
 
245
- await this.updateFromAttributes();
235
+ try {
236
+ switch (name) {
237
+ case "amount":
238
+ this.amount = parseInt(newValue) || 0;
239
+ break;
240
+
241
+ case "mode":
242
+ this.test = newValue === "test";
243
+ break;
244
+
245
+ case "currencycode":
246
+ this.currencyCode = newValue;
247
+ break;
248
+
249
+ case "api-key":
250
+ this.apiKey = newValue;
251
+ break;
252
+
253
+ case "isaddressrequired":
254
+ this.isAddressRequired = newValue === "true";
255
+ break;
256
+
257
+ case "frequency":
258
+ this.frequency = newValue;
259
+ break;
260
+
261
+ case "description":
262
+ this.description = newValue;
263
+ break;
246
264
 
247
- if (this.shadowRoot) {
248
- this.render();
265
+ case "totalpayments":
266
+ this.totalPayments = newValue;
267
+ break;
268
+
269
+ case "customer":
270
+ this.customer = newValue ? JSON.parse(newValue) : {};
271
+ break;
272
+
273
+ case "inventoryorders":
274
+ this.inventoryOrder = newValue ? JSON.parse(newValue) : {};
275
+ break;
276
+
277
+ case "inventory":
278
+ this.inventory = newValue === "true";
279
+
280
+ if (this.inventory && this.inventoryOrder) {
281
+ this.getSplitUpDetails(this.inventoryOrder).then(res => {
282
+ this.splitUp = res?.entity;
283
+ this.render();
284
+ });
285
+ return;
286
+ }
287
+ break;
288
+
289
+ default:
290
+ break;
291
+ }
292
+ if (this.shadowRoot) {
293
+ this.render();
294
+ }
295
+
296
+ } catch (e) {
297
+ console.error(`Invalid value for ${name}`, e);
249
298
  }
250
299
  }
251
300
 
@@ -571,8 +620,8 @@ export class SwirepayCheckout extends HTMLElement {
571
620
  <div class="field">
572
621
  <select id="country" required>
573
622
  ${COUNTRIES_LIST.map(c =>
574
- `<option value="${c.code}" ${c.code === "US" ? "selected" : ""}>${c.name}</option>`
575
- ).join("")}
623
+ `<option value="${c.code}" ${c.code === "US" ? "selected" : ""}>${c.name}</option>`
624
+ ).join("")}
576
625
  </select>
577
626
  <label class="floating">Country *</label>
578
627
  </div>
@@ -589,63 +638,63 @@ export class SwirepayCheckout extends HTMLElement {
589
638
  }
590
639
 
591
640
  validate() {
592
- this.clearErrors();
593
- let isValid = true;
641
+ this.clearErrors();
642
+ let isValid = true;
594
643
 
595
- const get = id => this.shadow.getElementById(id)?.value?.trim();
644
+ const get = id => this.shadow.getElementById(id)?.value?.trim();
596
645
 
597
- if (!get("name")) {
598
- this.setError("name", "Name required");
599
- isValid = false;
600
- }
646
+ if (!get("name")) {
647
+ this.setError("name", "Name required");
648
+ isValid = false;
649
+ }
601
650
 
602
- const email = get("email");
603
- if (!email) {
604
- this.setError("email", "Email required");
605
- isValid = false;
606
- } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
607
- this.setError("email", "Invalid email");
608
- isValid = false;
609
- }
651
+ const email = get("email");
652
+ if (!email) {
653
+ this.setError("email", "Email required");
654
+ isValid = false;
655
+ } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
656
+ this.setError("email", "Invalid email");
657
+ isValid = false;
658
+ }
610
659
 
611
- if (!get("card-name")) {
612
- this.setError("card-name", "Card holder name required");
613
- isValid = false;
614
- }
660
+ if (!get("card-name")) {
661
+ this.setError("card-name", "Card holder name required");
662
+ isValid = false;
663
+ }
615
664
 
616
- const cardNumber = get("card")?.replace(/\s/g, "");
617
- if (!cardNumber || !/^\d{12,19}$/.test(cardNumber)) {
618
- this.setError("card", "Invalid card number");
619
- isValid = false;
620
- }
665
+ const cardNumber = get("card")?.replace(/\s/g, "");
666
+ if (!cardNumber || !/^\d{12,19}$/.test(cardNumber)) {
667
+ this.setError("card", "Invalid card number");
668
+ isValid = false;
669
+ }
621
670
 
622
- const expiry = get("expiry");
623
- if (!expiry || !/^\d{2}\/\d{2}$/.test(expiry)) {
624
- this.setError("expiry", "Invalid expiry");
671
+ const expiry = get("expiry");
672
+ if (!expiry || !/^\d{2}\/\d{2}$/.test(expiry)) {
673
+ this.setError("expiry", "Invalid expiry");
674
+ isValid = false;
675
+ } else {
676
+ const [mm, yy] = expiry.split("/").map(Number);
677
+ const now = new Date();
678
+ const currentYear = now.getFullYear() % 100;
679
+ const currentMonth = now.getMonth() + 1;
680
+
681
+ if (mm < 1 || mm > 12) {
682
+ this.setError("expiry", "Invalid month");
625
683
  isValid = false;
626
- } else {
627
- const [mm, yy] = expiry.split("/").map(Number);
628
- const now = new Date();
629
- const currentYear = now.getFullYear() % 100;
630
- const currentMonth = now.getMonth() + 1;
631
-
632
- if (mm < 1 || mm > 12) {
633
- this.setError("expiry", "Invalid month");
634
- isValid = false;
635
- } else if (yy < currentYear || (yy === currentYear && mm < currentMonth)) {
636
- this.setError("expiry", "Card expired");
637
- isValid = false;
638
- }
639
- }
640
-
641
- const cvv = get("cvv");
642
- if (!cvv || !/^\d{3,4}$/.test(cvv)) {
643
- this.setError("cvv", "Invalid CVV");
684
+ } else if (yy < currentYear || (yy === currentYear && mm < currentMonth)) {
685
+ this.setError("expiry", "Card expired");
644
686
  isValid = false;
645
687
  }
688
+ }
689
+
690
+ const cvv = get("cvv");
691
+ if (!cvv || !/^\d{3,4}$/.test(cvv)) {
692
+ this.setError("cvv", "Invalid CVV");
693
+ isValid = false;
694
+ }
646
695
 
647
- const phone = get("phone")?.replace(/\D/g, "");
648
- const code = get("phone-code");
696
+ const phone = get("phone")?.replace(/\D/g, "");
697
+ const code = get("phone-code");
649
698
  if (!phone || !code) {
650
699
  this.setError("phone", "Phone number required");
651
700
  isValid = false;
@@ -657,41 +706,41 @@ export class SwirepayCheckout extends HTMLElement {
657
706
  }
658
707
  }
659
708
 
660
- if (this.isAddressRequired) {
661
- if (!get("street")) {
662
- this.setError("street", "Street required");
663
- isValid = false;
664
- }
709
+ if (this.isAddressRequired) {
710
+ if (!get("street")) {
711
+ this.setError("street", "Street required");
712
+ isValid = false;
713
+ }
665
714
 
666
- if (!get("city")) {
667
- this.setError("city", "City required");
668
- isValid = false;
669
- }
715
+ if (!get("city")) {
716
+ this.setError("city", "City required");
717
+ isValid = false;
718
+ }
670
719
 
671
- if (!get("state")) {
672
- this.setError("state", "State required");
673
- isValid = false;
674
- }
720
+ if (!get("state")) {
721
+ this.setError("state", "State required");
722
+ isValid = false;
723
+ }
675
724
 
676
- if (!get("country")) {
677
- this.setError("country", "Country required");
678
- isValid = false;
679
- }
725
+ if (!get("country")) {
726
+ this.setError("country", "Country required");
727
+ isValid = false;
728
+ }
680
729
 
681
- const zip = get("zip");
730
+ const zip = get("zip");
682
731
 
683
- if (!zip) {
684
- this.setError("zip", "ZIP required");
685
- isValid = false;
686
- } else if (postalCodes.validate(get("country"), zip) !== true) {
687
- this.setError("zip", "Invalid ZIP code");
688
- isValid = false;
689
- }
732
+ if (!zip) {
733
+ this.setError("zip", "ZIP required");
734
+ isValid = false;
735
+ } else if (postalCodes.validate(get("country"), zip) !== true) {
736
+ this.setError("zip", "Invalid ZIP code");
737
+ isValid = false;
690
738
  }
691
-
692
- return isValid;
693
739
  }
694
740
 
741
+ return isValid;
742
+ }
743
+
695
744
  async oneTimePayment() {
696
745
  try {
697
746
  const values = await this.getServerEncription();
@@ -799,7 +848,7 @@ export class SwirepayCheckout extends HTMLElement {
799
848
  }
800
849
  }
801
850
 
802
- async recurringPayment() {
851
+ async recurringPayment () {
803
852
  try {
804
853
  const values = await this.getServerEncription();
805
854
  if (values?.public_key) {
@@ -846,8 +895,8 @@ export class SwirepayCheckout extends HTMLElement {
846
895
  if (itemInfo?.entity?.content[0]?.gid) {
847
896
  product = itemInfo?.entity?.content[0];
848
897
  } else {
849
- const newItemInfo = await this.addedItemInfo();
850
- product = newItemInfo?.entity;
898
+ const newItemInfo = await this.addedItemInfo();
899
+ product = newItemInfo?.entity;
851
900
  }
852
901
  let plan;
853
902
  const planInfo = await this.getPlan();