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