@swirepay-developer/swirepay-card-sdk 2.0.2 → 2.0.3
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 +150 -109
package/package.json
CHANGED
|
@@ -181,41 +181,33 @@ export class SwirepayCheckout extends HTMLElement {
|
|
|
181
181
|
return await response.json();
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
async
|
|
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
|
|
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("
|
|
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,64 @@ export class SwirepayCheckout extends HTMLElement {
|
|
|
239
229
|
];
|
|
240
230
|
}
|
|
241
231
|
|
|
242
|
-
|
|
232
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
243
233
|
if (oldValue === newValue) return;
|
|
244
234
|
|
|
245
|
-
|
|
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 "inventory":
|
|
258
|
+
this.inventory = newValue === "true";
|
|
259
|
+
break;
|
|
260
|
+
|
|
261
|
+
case "frequency":
|
|
262
|
+
this.frequency = newValue;
|
|
263
|
+
break;
|
|
246
264
|
|
|
247
|
-
|
|
248
|
-
|
|
265
|
+
case "description":
|
|
266
|
+
this.description = newValue;
|
|
267
|
+
break;
|
|
268
|
+
|
|
269
|
+
case "totalpayments":
|
|
270
|
+
this.totalPayments = newValue;
|
|
271
|
+
break;
|
|
272
|
+
|
|
273
|
+
case "customer":
|
|
274
|
+
this.customer = newValue ? JSON.parse(newValue) : {};
|
|
275
|
+
break;
|
|
276
|
+
|
|
277
|
+
case "inventoryorders":
|
|
278
|
+
this.inventoryOrder = newValue ? JSON.parse(newValue) : {};
|
|
279
|
+
break;
|
|
280
|
+
|
|
281
|
+
default:
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
if (this.shadowRoot) {
|
|
285
|
+
this.render();
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
} catch (e) {
|
|
289
|
+
console.error(`Invalid value for ${name}`, e);
|
|
249
290
|
}
|
|
250
291
|
}
|
|
251
292
|
|
|
@@ -571,8 +612,8 @@ export class SwirepayCheckout extends HTMLElement {
|
|
|
571
612
|
<div class="field">
|
|
572
613
|
<select id="country" required>
|
|
573
614
|
${COUNTRIES_LIST.map(c =>
|
|
574
|
-
|
|
575
|
-
|
|
615
|
+
`<option value="${c.code}" ${c.code === "US" ? "selected" : ""}>${c.name}</option>`
|
|
616
|
+
).join("")}
|
|
576
617
|
</select>
|
|
577
618
|
<label class="floating">Country *</label>
|
|
578
619
|
</div>
|
|
@@ -589,63 +630,63 @@ export class SwirepayCheckout extends HTMLElement {
|
|
|
589
630
|
}
|
|
590
631
|
|
|
591
632
|
validate() {
|
|
592
|
-
|
|
593
|
-
|
|
633
|
+
this.clearErrors();
|
|
634
|
+
let isValid = true;
|
|
594
635
|
|
|
595
|
-
|
|
636
|
+
const get = id => this.shadow.getElementById(id)?.value?.trim();
|
|
596
637
|
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
638
|
+
if (!get("name")) {
|
|
639
|
+
this.setError("name", "Name required");
|
|
640
|
+
isValid = false;
|
|
641
|
+
}
|
|
601
642
|
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
643
|
+
const email = get("email");
|
|
644
|
+
if (!email) {
|
|
645
|
+
this.setError("email", "Email required");
|
|
646
|
+
isValid = false;
|
|
647
|
+
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
|
648
|
+
this.setError("email", "Invalid email");
|
|
649
|
+
isValid = false;
|
|
650
|
+
}
|
|
610
651
|
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
652
|
+
if (!get("card-name")) {
|
|
653
|
+
this.setError("card-name", "Card holder name required");
|
|
654
|
+
isValid = false;
|
|
655
|
+
}
|
|
615
656
|
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
657
|
+
const cardNumber = get("card")?.replace(/\s/g, "");
|
|
658
|
+
if (!cardNumber || !/^\d{12,19}$/.test(cardNumber)) {
|
|
659
|
+
this.setError("card", "Invalid card number");
|
|
660
|
+
isValid = false;
|
|
661
|
+
}
|
|
621
662
|
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
663
|
+
const expiry = get("expiry");
|
|
664
|
+
if (!expiry || !/^\d{2}\/\d{2}$/.test(expiry)) {
|
|
665
|
+
this.setError("expiry", "Invalid expiry");
|
|
666
|
+
isValid = false;
|
|
667
|
+
} else {
|
|
668
|
+
const [mm, yy] = expiry.split("/").map(Number);
|
|
669
|
+
const now = new Date();
|
|
670
|
+
const currentYear = now.getFullYear() % 100;
|
|
671
|
+
const currentMonth = now.getMonth() + 1;
|
|
672
|
+
|
|
673
|
+
if (mm < 1 || mm > 12) {
|
|
674
|
+
this.setError("expiry", "Invalid month");
|
|
625
675
|
isValid = false;
|
|
626
|
-
} else {
|
|
627
|
-
|
|
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");
|
|
676
|
+
} else if (yy < currentYear || (yy === currentYear && mm < currentMonth)) {
|
|
677
|
+
this.setError("expiry", "Card expired");
|
|
644
678
|
isValid = false;
|
|
645
679
|
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const cvv = get("cvv");
|
|
683
|
+
if (!cvv || !/^\d{3,4}$/.test(cvv)) {
|
|
684
|
+
this.setError("cvv", "Invalid CVV");
|
|
685
|
+
isValid = false;
|
|
686
|
+
}
|
|
646
687
|
|
|
647
|
-
|
|
648
|
-
|
|
688
|
+
const phone = get("phone")?.replace(/\D/g, "");
|
|
689
|
+
const code = get("phone-code");
|
|
649
690
|
if (!phone || !code) {
|
|
650
691
|
this.setError("phone", "Phone number required");
|
|
651
692
|
isValid = false;
|
|
@@ -657,41 +698,41 @@ export class SwirepayCheckout extends HTMLElement {
|
|
|
657
698
|
}
|
|
658
699
|
}
|
|
659
700
|
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
701
|
+
if (this.isAddressRequired) {
|
|
702
|
+
if (!get("street")) {
|
|
703
|
+
this.setError("street", "Street required");
|
|
704
|
+
isValid = false;
|
|
705
|
+
}
|
|
665
706
|
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
707
|
+
if (!get("city")) {
|
|
708
|
+
this.setError("city", "City required");
|
|
709
|
+
isValid = false;
|
|
710
|
+
}
|
|
670
711
|
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
712
|
+
if (!get("state")) {
|
|
713
|
+
this.setError("state", "State required");
|
|
714
|
+
isValid = false;
|
|
715
|
+
}
|
|
675
716
|
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
717
|
+
if (!get("country")) {
|
|
718
|
+
this.setError("country", "Country required");
|
|
719
|
+
isValid = false;
|
|
720
|
+
}
|
|
680
721
|
|
|
681
|
-
|
|
722
|
+
const zip = get("zip");
|
|
682
723
|
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
}
|
|
724
|
+
if (!zip) {
|
|
725
|
+
this.setError("zip", "ZIP required");
|
|
726
|
+
isValid = false;
|
|
727
|
+
} else if (postalCodes.validate(get("country"), zip) !== true) {
|
|
728
|
+
this.setError("zip", "Invalid ZIP code");
|
|
729
|
+
isValid = false;
|
|
690
730
|
}
|
|
691
|
-
|
|
692
|
-
return isValid;
|
|
693
731
|
}
|
|
694
732
|
|
|
733
|
+
return isValid;
|
|
734
|
+
}
|
|
735
|
+
|
|
695
736
|
async oneTimePayment() {
|
|
696
737
|
try {
|
|
697
738
|
const values = await this.getServerEncription();
|
|
@@ -799,7 +840,7 @@ export class SwirepayCheckout extends HTMLElement {
|
|
|
799
840
|
}
|
|
800
841
|
}
|
|
801
842
|
|
|
802
|
-
async recurringPayment() {
|
|
843
|
+
async recurringPayment () {
|
|
803
844
|
try {
|
|
804
845
|
const values = await this.getServerEncription();
|
|
805
846
|
if (values?.public_key) {
|
|
@@ -846,8 +887,8 @@ export class SwirepayCheckout extends HTMLElement {
|
|
|
846
887
|
if (itemInfo?.entity?.content[0]?.gid) {
|
|
847
888
|
product = itemInfo?.entity?.content[0];
|
|
848
889
|
} else {
|
|
849
|
-
|
|
850
|
-
|
|
890
|
+
const newItemInfo = await this.addedItemInfo();
|
|
891
|
+
product = newItemInfo?.entity;
|
|
851
892
|
}
|
|
852
893
|
let plan;
|
|
853
894
|
const planInfo = await this.getPlan();
|