logix-payment-shift4 1.0.9 → 1.1.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/index.cjs.js CHANGED
@@ -192,7 +192,8 @@ function PaymentForm(_ref) {
192
192
  _ref$formbackgroundCo = _ref.formbackgroundColor,
193
193
  formbackgroundColor = _ref$formbackgroundCo === void 0 ? "#fff" : _ref$formbackgroundCo,
194
194
  _ref$inputpadding = _ref.inputpadding,
195
- inputpadding = _ref$inputpadding === void 0 ? 12 : _ref$inputpadding;
195
+ inputpadding = _ref$inputpadding === void 0 ? 12 : _ref$inputpadding,
196
+ apiUrl = _ref.apiUrl;
196
197
  var formRef = React.useRef();
197
198
  var initializedRef = React.useRef(false); // yalnız bir dəfə mount üçün
198
199
  var shift4Ref = React.useRef(null);
@@ -218,56 +219,104 @@ function PaymentForm(_ref) {
218
219
  React.useEffect(function () {
219
220
  if (initializedRef.current) return;
220
221
  initializedRef.current = true;
221
- var script = document.createElement("script");
222
- script.src = "https://js.dev.shift4.com/shift4.js";
223
- script.async = true;
224
- script.onload = function () {
225
- if (!window.Shift4) return;
226
- shift4Ref.current = window.Shift4("pk_test_SeDEbtFlcz4LqVe1zxMYZxQO");
227
- componentsRef.current = shift4Ref.current.createComponentGroup().automount("#payment-form");
228
- setIsReady(true);
229
- };
230
- script.onerror = function () {
231
- setError("Payment provider script failed to load. Please refresh and try again.");
232
- };
233
- document.body.appendChild(script);
222
+ var initPayment = /*#__PURE__*/function () {
223
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
224
+ var response, _yield$response$json, publicKeys, script, _t;
225
+ return _regenerator().w(function (_context) {
226
+ while (1) switch (_context.p = _context.n) {
227
+ case 0:
228
+ _context.p = 0;
229
+ _context.n = 1;
230
+ return fetch(apiUrl);
231
+ case 1:
232
+ response = _context.v;
233
+ if (response.ok) {
234
+ _context.n = 2;
235
+ break;
236
+ }
237
+ throw new Error("Failed to fetch public key");
238
+ case 2:
239
+ _context.n = 3;
240
+ return response.json();
241
+ case 3:
242
+ _yield$response$json = _context.v;
243
+ publicKeys = _yield$response$json.publicKeys;
244
+ if (publicKeys) {
245
+ _context.n = 4;
246
+ break;
247
+ }
248
+ throw new Error("Public key is empty");
249
+ case 4:
250
+ // 🔹 2. Shift4 script-i yükləyirik
251
+ script = document.createElement("script");
252
+ script.src = "https://js.dev.shift4.com/shift4.js";
253
+ script.async = true;
254
+ script.onload = function () {
255
+ if (!window.Shift4) return;
256
+
257
+ // 🔹 3. Dinamik key ilə init
258
+ shift4Ref.current = window.Shift4(publicKeys);
259
+ componentsRef.current = shift4Ref.current.createComponentGroup().automount("#payment-form");
260
+ setIsReady(true);
261
+ };
262
+ script.onerror = function () {
263
+ setError("Payment provider script failed to load.");
264
+ };
265
+ document.body.appendChild(script);
266
+ _context.n = 6;
267
+ break;
268
+ case 5:
269
+ _context.p = 5;
270
+ _t = _context.v;
271
+ console.error(_t);
272
+ setError("Payment initialization failed. Please try again.");
273
+ case 6:
274
+ return _context.a(2);
275
+ }
276
+ }, _callee, null, [[0, 5]]);
277
+ }));
278
+ return function initPayment() {
279
+ return _ref2.apply(this, arguments);
280
+ };
281
+ }();
282
+ initPayment();
234
283
  }, []);
235
284
  var handleSubmit = /*#__PURE__*/function () {
236
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(e) {
237
- var _result$charge, token, request, threeDSecureToken, apiBase, response, result, _t;
238
- return _regenerator().w(function (_context) {
239
- while (1) switch (_context.p = _context.n) {
285
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(e) {
286
+ var _result$charge, token, request, threeDSecureToken, apiBase, response, result, _t2;
287
+ return _regenerator().w(function (_context2) {
288
+ while (1) switch (_context2.p = _context2.n) {
240
289
  case 0:
241
290
  e.preventDefault();
242
291
  if (!isSubmitting) {
243
- _context.n = 1;
292
+ _context2.n = 1;
244
293
  break;
245
294
  }
246
- return _context.a(2);
295
+ return _context2.a(2);
247
296
  case 1:
248
297
  if (formRef.current) {
249
- _context.n = 2;
298
+ _context2.n = 2;
250
299
  break;
251
300
  }
252
- return _context.a(2);
301
+ return _context2.a(2);
253
302
  case 2:
254
303
  if (!(!shift4Ref.current || !componentsRef.current)) {
255
- _context.n = 3;
304
+ _context2.n = 3;
256
305
  break;
257
306
  }
258
307
  setError("Payment form is not ready yet. Please try again.");
259
- return _context.a(2);
308
+ return _context2.a(2);
260
309
  case 3:
261
310
  setError("");
262
311
  setSuccess("");
263
312
  setIsSubmitting(true);
264
- _context.p = 4;
265
- _context.n = 5;
313
+ _context2.p = 4;
314
+ _context2.n = 5;
266
315
  return shift4Ref.current.createToken(componentsRef.current);
267
316
  case 5:
268
- token = _context.v;
317
+ token = _context2.v;
269
318
  if (token !== null && token !== void 0 && token.id) {
270
- _context.n = 6;
319
+ _context2.n = 6;
271
320
  break;
272
321
  }
273
322
  throw new Error("Card tokenization failed. Please check your details and try again.");
@@ -277,18 +326,18 @@ function PaymentForm(_ref) {
277
326
  currency: currency,
278
327
  card: token.id
279
328
  };
280
- _context.n = 7;
329
+ _context2.n = 7;
281
330
  return shift4Ref.current.verifyThreeDSecure(request);
282
331
  case 7:
283
- threeDSecureToken = _context.v;
332
+ threeDSecureToken = _context2.v;
284
333
  if (threeDSecureToken !== null && threeDSecureToken !== void 0 && threeDSecureToken.id) {
285
- _context.n = 8;
334
+ _context2.n = 8;
286
335
  break;
287
336
  }
288
337
  throw new Error("3D Secure verification failed. Please try again.");
289
338
  case 8:
290
339
  apiBase = process.env.REACT_APP_API_BASE || "http://localhost:4242";
291
- _context.n = 9;
340
+ _context2.n = 9;
292
341
  return fetch("".concat(apiBase, "/charge"), {
293
342
  method: "POST",
294
343
  headers: {
@@ -301,35 +350,35 @@ function PaymentForm(_ref) {
301
350
  })
302
351
  });
303
352
  case 9:
304
- response = _context.v;
305
- _context.n = 10;
353
+ response = _context2.v;
354
+ _context2.n = 10;
306
355
  return response.json();
307
356
  case 10:
308
- result = _context.v;
357
+ result = _context2.v;
309
358
  if (response.ok) {
310
- _context.n = 11;
359
+ _context2.n = 11;
311
360
  break;
312
361
  }
313
362
  throw new Error((result === null || result === void 0 ? void 0 : result.error) || "Payment failed. Please try again.");
314
363
  case 11:
315
364
  setSuccess("Payment approved. Charge ID: ".concat((result === null || result === void 0 || (_result$charge = result.charge) === null || _result$charge === void 0 ? void 0 : _result$charge.id) || "unknown"));
316
- _context.n = 13;
365
+ _context2.n = 13;
317
366
  break;
318
367
  case 12:
319
- _context.p = 12;
320
- _t = _context.v;
321
- setError((_t === null || _t === void 0 ? void 0 : _t.message) || "Payment failed. Please try again.");
368
+ _context2.p = 12;
369
+ _t2 = _context2.v;
370
+ setError((_t2 === null || _t2 === void 0 ? void 0 : _t2.message) || "Payment failed. Please try again.");
322
371
  case 13:
323
- _context.p = 13;
372
+ _context2.p = 13;
324
373
  setIsSubmitting(false);
325
- return _context.f(13);
374
+ return _context2.f(13);
326
375
  case 14:
327
- return _context.a(2);
376
+ return _context2.a(2);
328
377
  }
329
- }, _callee, null, [[4, 12, 13, 14]]);
378
+ }, _callee2, null, [[4, 12, 13, 14]]);
330
379
  }));
331
380
  return function handleSubmit(_x) {
332
- return _ref2.apply(this, arguments);
381
+ return _ref3.apply(this, arguments);
333
382
  };
334
383
  }();
335
384
  return /*#__PURE__*/React.createElement("div", {
package/dist/index.esm.js CHANGED
@@ -190,7 +190,8 @@ function PaymentForm(_ref) {
190
190
  _ref$formbackgroundCo = _ref.formbackgroundColor,
191
191
  formbackgroundColor = _ref$formbackgroundCo === void 0 ? "#fff" : _ref$formbackgroundCo,
192
192
  _ref$inputpadding = _ref.inputpadding,
193
- inputpadding = _ref$inputpadding === void 0 ? 12 : _ref$inputpadding;
193
+ inputpadding = _ref$inputpadding === void 0 ? 12 : _ref$inputpadding,
194
+ apiUrl = _ref.apiUrl;
194
195
  var formRef = useRef();
195
196
  var initializedRef = useRef(false); // yalnız bir dəfə mount üçün
196
197
  var shift4Ref = useRef(null);
@@ -216,56 +217,104 @@ function PaymentForm(_ref) {
216
217
  useEffect(function () {
217
218
  if (initializedRef.current) return;
218
219
  initializedRef.current = true;
219
- var script = document.createElement("script");
220
- script.src = "https://js.dev.shift4.com/shift4.js";
221
- script.async = true;
222
- script.onload = function () {
223
- if (!window.Shift4) return;
224
- shift4Ref.current = window.Shift4("pk_test_SeDEbtFlcz4LqVe1zxMYZxQO");
225
- componentsRef.current = shift4Ref.current.createComponentGroup().automount("#payment-form");
226
- setIsReady(true);
227
- };
228
- script.onerror = function () {
229
- setError("Payment provider script failed to load. Please refresh and try again.");
230
- };
231
- document.body.appendChild(script);
220
+ var initPayment = /*#__PURE__*/function () {
221
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
222
+ var response, _yield$response$json, publicKeys, script, _t;
223
+ return _regenerator().w(function (_context) {
224
+ while (1) switch (_context.p = _context.n) {
225
+ case 0:
226
+ _context.p = 0;
227
+ _context.n = 1;
228
+ return fetch(apiUrl);
229
+ case 1:
230
+ response = _context.v;
231
+ if (response.ok) {
232
+ _context.n = 2;
233
+ break;
234
+ }
235
+ throw new Error("Failed to fetch public key");
236
+ case 2:
237
+ _context.n = 3;
238
+ return response.json();
239
+ case 3:
240
+ _yield$response$json = _context.v;
241
+ publicKeys = _yield$response$json.publicKeys;
242
+ if (publicKeys) {
243
+ _context.n = 4;
244
+ break;
245
+ }
246
+ throw new Error("Public key is empty");
247
+ case 4:
248
+ // 🔹 2. Shift4 script-i yükləyirik
249
+ script = document.createElement("script");
250
+ script.src = "https://js.dev.shift4.com/shift4.js";
251
+ script.async = true;
252
+ script.onload = function () {
253
+ if (!window.Shift4) return;
254
+
255
+ // 🔹 3. Dinamik key ilə init
256
+ shift4Ref.current = window.Shift4(publicKeys);
257
+ componentsRef.current = shift4Ref.current.createComponentGroup().automount("#payment-form");
258
+ setIsReady(true);
259
+ };
260
+ script.onerror = function () {
261
+ setError("Payment provider script failed to load.");
262
+ };
263
+ document.body.appendChild(script);
264
+ _context.n = 6;
265
+ break;
266
+ case 5:
267
+ _context.p = 5;
268
+ _t = _context.v;
269
+ console.error(_t);
270
+ setError("Payment initialization failed. Please try again.");
271
+ case 6:
272
+ return _context.a(2);
273
+ }
274
+ }, _callee, null, [[0, 5]]);
275
+ }));
276
+ return function initPayment() {
277
+ return _ref2.apply(this, arguments);
278
+ };
279
+ }();
280
+ initPayment();
232
281
  }, []);
233
282
  var handleSubmit = /*#__PURE__*/function () {
234
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(e) {
235
- var _result$charge, token, request, threeDSecureToken, apiBase, response, result, _t;
236
- return _regenerator().w(function (_context) {
237
- while (1) switch (_context.p = _context.n) {
283
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(e) {
284
+ var _result$charge, token, request, threeDSecureToken, apiBase, response, result, _t2;
285
+ return _regenerator().w(function (_context2) {
286
+ while (1) switch (_context2.p = _context2.n) {
238
287
  case 0:
239
288
  e.preventDefault();
240
289
  if (!isSubmitting) {
241
- _context.n = 1;
290
+ _context2.n = 1;
242
291
  break;
243
292
  }
244
- return _context.a(2);
293
+ return _context2.a(2);
245
294
  case 1:
246
295
  if (formRef.current) {
247
- _context.n = 2;
296
+ _context2.n = 2;
248
297
  break;
249
298
  }
250
- return _context.a(2);
299
+ return _context2.a(2);
251
300
  case 2:
252
301
  if (!(!shift4Ref.current || !componentsRef.current)) {
253
- _context.n = 3;
302
+ _context2.n = 3;
254
303
  break;
255
304
  }
256
305
  setError("Payment form is not ready yet. Please try again.");
257
- return _context.a(2);
306
+ return _context2.a(2);
258
307
  case 3:
259
308
  setError("");
260
309
  setSuccess("");
261
310
  setIsSubmitting(true);
262
- _context.p = 4;
263
- _context.n = 5;
311
+ _context2.p = 4;
312
+ _context2.n = 5;
264
313
  return shift4Ref.current.createToken(componentsRef.current);
265
314
  case 5:
266
- token = _context.v;
315
+ token = _context2.v;
267
316
  if (token !== null && token !== void 0 && token.id) {
268
- _context.n = 6;
317
+ _context2.n = 6;
269
318
  break;
270
319
  }
271
320
  throw new Error("Card tokenization failed. Please check your details and try again.");
@@ -275,18 +324,18 @@ function PaymentForm(_ref) {
275
324
  currency: currency,
276
325
  card: token.id
277
326
  };
278
- _context.n = 7;
327
+ _context2.n = 7;
279
328
  return shift4Ref.current.verifyThreeDSecure(request);
280
329
  case 7:
281
- threeDSecureToken = _context.v;
330
+ threeDSecureToken = _context2.v;
282
331
  if (threeDSecureToken !== null && threeDSecureToken !== void 0 && threeDSecureToken.id) {
283
- _context.n = 8;
332
+ _context2.n = 8;
284
333
  break;
285
334
  }
286
335
  throw new Error("3D Secure verification failed. Please try again.");
287
336
  case 8:
288
337
  apiBase = process.env.REACT_APP_API_BASE || "http://localhost:4242";
289
- _context.n = 9;
338
+ _context2.n = 9;
290
339
  return fetch("".concat(apiBase, "/charge"), {
291
340
  method: "POST",
292
341
  headers: {
@@ -299,35 +348,35 @@ function PaymentForm(_ref) {
299
348
  })
300
349
  });
301
350
  case 9:
302
- response = _context.v;
303
- _context.n = 10;
351
+ response = _context2.v;
352
+ _context2.n = 10;
304
353
  return response.json();
305
354
  case 10:
306
- result = _context.v;
355
+ result = _context2.v;
307
356
  if (response.ok) {
308
- _context.n = 11;
357
+ _context2.n = 11;
309
358
  break;
310
359
  }
311
360
  throw new Error((result === null || result === void 0 ? void 0 : result.error) || "Payment failed. Please try again.");
312
361
  case 11:
313
362
  setSuccess("Payment approved. Charge ID: ".concat((result === null || result === void 0 || (_result$charge = result.charge) === null || _result$charge === void 0 ? void 0 : _result$charge.id) || "unknown"));
314
- _context.n = 13;
363
+ _context2.n = 13;
315
364
  break;
316
365
  case 12:
317
- _context.p = 12;
318
- _t = _context.v;
319
- setError((_t === null || _t === void 0 ? void 0 : _t.message) || "Payment failed. Please try again.");
366
+ _context2.p = 12;
367
+ _t2 = _context2.v;
368
+ setError((_t2 === null || _t2 === void 0 ? void 0 : _t2.message) || "Payment failed. Please try again.");
320
369
  case 13:
321
- _context.p = 13;
370
+ _context2.p = 13;
322
371
  setIsSubmitting(false);
323
- return _context.f(13);
372
+ return _context2.f(13);
324
373
  case 14:
325
- return _context.a(2);
374
+ return _context2.a(2);
326
375
  }
327
- }, _callee, null, [[4, 12, 13, 14]]);
376
+ }, _callee2, null, [[4, 12, 13, 14]]);
328
377
  }));
329
378
  return function handleSubmit(_x) {
330
- return _ref2.apply(this, arguments);
379
+ return _ref3.apply(this, arguments);
331
380
  };
332
381
  }();
333
382
  return /*#__PURE__*/React.createElement("div", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "logix-payment-shift4",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "description": "A React component library for integrating Shift4 payments with Logix applications.",
@@ -1,6 +1,6 @@
1
1
  import React, { useEffect, useRef, useState } from "react";
2
2
 
3
- export default function PaymentForm({amount,currency,color,formwidth=400,formborderRadius=20,formbackgroundColor="#fff",inputpadding=12}) {
3
+ export default function PaymentForm({amount,currency,color,formwidth=400,formborderRadius=20,formbackgroundColor="#fff",inputpadding=12,apiUrl}) {
4
4
  const formRef = useRef();
5
5
  const initializedRef = useRef(false); // yalnız bir dəfə mount üçün
6
6
  const shift4Ref = useRef(null);
@@ -13,26 +13,51 @@ console.log(error);
13
13
  console.log(success);
14
14
 
15
15
 
16
- useEffect(() => {
17
- if (initializedRef.current) return;
18
- initializedRef.current = true;
16
+ useEffect(() => {
17
+ if (initializedRef.current) return;
18
+ initializedRef.current = true;
19
19
 
20
- const script = document.createElement("script");
21
- script.src = "https://js.dev.shift4.com/shift4.js";
22
- script.async = true;
23
- script.onload = () => {
24
- if (!window.Shift4) return;
20
+ const initPayment = async () => {
21
+ try {
22
+ // 🔹 1. Public key-ni backend-dən çəkirik
23
+ const response = await fetch(apiUrl);
24
+ if (!response.ok) throw new Error("Failed to fetch public key");
25
+
26
+ const { publicKeys} = await response.json();
27
+ if (!publicKeys) throw new Error("Public key is empty");
28
+
29
+ // 🔹 2. Shift4 script-i yükləyirik
30
+ const script = document.createElement("script");
31
+ script.src = "https://js.dev.shift4.com/shift4.js";
32
+ script.async = true;
33
+
34
+ script.onload = () => {
35
+ if (!window.Shift4) return;
36
+
37
+ // 🔹 3. Dinamik key ilə init
38
+ shift4Ref.current = window.Shift4(publicKeys);
39
+
40
+ componentsRef.current = shift4Ref.current
41
+ .createComponentGroup()
42
+ .automount("#payment-form");
43
+
44
+ setIsReady(true);
45
+ };
46
+
47
+ script.onerror = () => {
48
+ setError("Payment provider script failed to load.");
49
+ };
50
+
51
+ document.body.appendChild(script);
52
+ } catch (err) {
53
+ console.error(err);
54
+ setError("Payment initialization failed. Please try again.");
55
+ }
56
+ };
25
57
 
26
- shift4Ref.current = window.Shift4("pk_test_SeDEbtFlcz4LqVe1zxMYZxQO");
27
- componentsRef.current = shift4Ref.current.createComponentGroup().automount("#payment-form");
28
- setIsReady(true);
29
- };
30
- script.onerror = () => {
31
- setError("Payment provider script failed to load. Please refresh and try again.");
32
- };
58
+ initPayment();
59
+ }, []);
33
60
 
34
- document.body.appendChild(script);
35
- }, []);
36
61
 
37
62
  const handleSubmit = async (e) => {
38
63
  e.preventDefault();