logix-payment-shift4 1.0.8 → 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,19 +192,20 @@ 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);
199
200
  var componentsRef = React.useRef(null);
200
201
  var _useState = React.useState(""),
201
- _useState2 = _slicedToArray(_useState, 2);
202
- _useState2[0];
203
- var setError = _useState2[1];
202
+ _useState2 = _slicedToArray(_useState, 2),
203
+ error = _useState2[0],
204
+ setError = _useState2[1];
204
205
  var _useState3 = React.useState(""),
205
- _useState4 = _slicedToArray(_useState3, 2);
206
- _useState4[0];
207
- var setSuccess = _useState4[1];
206
+ _useState4 = _slicedToArray(_useState3, 2),
207
+ success = _useState4[0],
208
+ setSuccess = _useState4[1];
208
209
  var _useState5 = React.useState(false),
209
210
  _useState6 = _slicedToArray(_useState5, 2),
210
211
  isSubmitting = _useState6[0],
@@ -213,59 +214,109 @@ function PaymentForm(_ref) {
213
214
  _useState8 = _slicedToArray(_useState7, 2),
214
215
  isReady = _useState8[0],
215
216
  setIsReady = _useState8[1];
217
+ console.log(error);
218
+ console.log(success);
216
219
  React.useEffect(function () {
217
220
  if (initializedRef.current) return;
218
221
  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);
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();
232
283
  }, []);
233
284
  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) {
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) {
238
289
  case 0:
239
290
  e.preventDefault();
240
291
  if (!isSubmitting) {
241
- _context.n = 1;
292
+ _context2.n = 1;
242
293
  break;
243
294
  }
244
- return _context.a(2);
295
+ return _context2.a(2);
245
296
  case 1:
246
297
  if (formRef.current) {
247
- _context.n = 2;
298
+ _context2.n = 2;
248
299
  break;
249
300
  }
250
- return _context.a(2);
301
+ return _context2.a(2);
251
302
  case 2:
252
303
  if (!(!shift4Ref.current || !componentsRef.current)) {
253
- _context.n = 3;
304
+ _context2.n = 3;
254
305
  break;
255
306
  }
256
307
  setError("Payment form is not ready yet. Please try again.");
257
- return _context.a(2);
308
+ return _context2.a(2);
258
309
  case 3:
259
310
  setError("");
260
311
  setSuccess("");
261
312
  setIsSubmitting(true);
262
- _context.p = 4;
263
- _context.n = 5;
313
+ _context2.p = 4;
314
+ _context2.n = 5;
264
315
  return shift4Ref.current.createToken(componentsRef.current);
265
316
  case 5:
266
- token = _context.v;
317
+ token = _context2.v;
267
318
  if (token !== null && token !== void 0 && token.id) {
268
- _context.n = 6;
319
+ _context2.n = 6;
269
320
  break;
270
321
  }
271
322
  throw new Error("Card tokenization failed. Please check your details and try again.");
@@ -275,18 +326,18 @@ function PaymentForm(_ref) {
275
326
  currency: currency,
276
327
  card: token.id
277
328
  };
278
- _context.n = 7;
329
+ _context2.n = 7;
279
330
  return shift4Ref.current.verifyThreeDSecure(request);
280
331
  case 7:
281
- threeDSecureToken = _context.v;
332
+ threeDSecureToken = _context2.v;
282
333
  if (threeDSecureToken !== null && threeDSecureToken !== void 0 && threeDSecureToken.id) {
283
- _context.n = 8;
334
+ _context2.n = 8;
284
335
  break;
285
336
  }
286
337
  throw new Error("3D Secure verification failed. Please try again.");
287
338
  case 8:
288
339
  apiBase = process.env.REACT_APP_API_BASE || "http://localhost:4242";
289
- _context.n = 9;
340
+ _context2.n = 9;
290
341
  return fetch("".concat(apiBase, "/charge"), {
291
342
  method: "POST",
292
343
  headers: {
@@ -299,35 +350,35 @@ function PaymentForm(_ref) {
299
350
  })
300
351
  });
301
352
  case 9:
302
- response = _context.v;
303
- _context.n = 10;
353
+ response = _context2.v;
354
+ _context2.n = 10;
304
355
  return response.json();
305
356
  case 10:
306
- result = _context.v;
357
+ result = _context2.v;
307
358
  if (response.ok) {
308
- _context.n = 11;
359
+ _context2.n = 11;
309
360
  break;
310
361
  }
311
362
  throw new Error((result === null || result === void 0 ? void 0 : result.error) || "Payment failed. Please try again.");
312
363
  case 11:
313
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"));
314
- _context.n = 13;
365
+ _context2.n = 13;
315
366
  break;
316
367
  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.");
368
+ _context2.p = 12;
369
+ _t2 = _context2.v;
370
+ setError((_t2 === null || _t2 === void 0 ? void 0 : _t2.message) || "Payment failed. Please try again.");
320
371
  case 13:
321
- _context.p = 13;
372
+ _context2.p = 13;
322
373
  setIsSubmitting(false);
323
- return _context.f(13);
374
+ return _context2.f(13);
324
375
  case 14:
325
- return _context.a(2);
376
+ return _context2.a(2);
326
377
  }
327
- }, _callee, null, [[4, 12, 13, 14]]);
378
+ }, _callee2, null, [[4, 12, 13, 14]]);
328
379
  }));
329
380
  return function handleSubmit(_x) {
330
- return _ref2.apply(this, arguments);
381
+ return _ref3.apply(this, arguments);
331
382
  };
332
383
  }();
333
384
  return /*#__PURE__*/React.createElement("div", {
package/dist/index.esm.js CHANGED
@@ -190,19 +190,20 @@ 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);
197
198
  var componentsRef = useRef(null);
198
199
  var _useState = useState(""),
199
- _useState2 = _slicedToArray(_useState, 2);
200
- _useState2[0];
201
- var setError = _useState2[1];
200
+ _useState2 = _slicedToArray(_useState, 2),
201
+ error = _useState2[0],
202
+ setError = _useState2[1];
202
203
  var _useState3 = useState(""),
203
- _useState4 = _slicedToArray(_useState3, 2);
204
- _useState4[0];
205
- var setSuccess = _useState4[1];
204
+ _useState4 = _slicedToArray(_useState3, 2),
205
+ success = _useState4[0],
206
+ setSuccess = _useState4[1];
206
207
  var _useState5 = useState(false),
207
208
  _useState6 = _slicedToArray(_useState5, 2),
208
209
  isSubmitting = _useState6[0],
@@ -211,59 +212,109 @@ function PaymentForm(_ref) {
211
212
  _useState8 = _slicedToArray(_useState7, 2),
212
213
  isReady = _useState8[0],
213
214
  setIsReady = _useState8[1];
215
+ console.log(error);
216
+ console.log(success);
214
217
  useEffect(function () {
215
218
  if (initializedRef.current) return;
216
219
  initializedRef.current = true;
217
- var script = document.createElement("script");
218
- script.src = "https://js.dev.shift4.com/shift4.js";
219
- script.async = true;
220
- script.onload = function () {
221
- if (!window.Shift4) return;
222
- shift4Ref.current = window.Shift4("pk_test_SeDEbtFlcz4LqVe1zxMYZxQO");
223
- componentsRef.current = shift4Ref.current.createComponentGroup().automount("#payment-form");
224
- setIsReady(true);
225
- };
226
- script.onerror = function () {
227
- setError("Payment provider script failed to load. Please refresh and try again.");
228
- };
229
- 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();
230
281
  }, []);
231
282
  var handleSubmit = /*#__PURE__*/function () {
232
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(e) {
233
- var _result$charge, token, request, threeDSecureToken, apiBase, response, result, _t;
234
- return _regenerator().w(function (_context) {
235
- 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) {
236
287
  case 0:
237
288
  e.preventDefault();
238
289
  if (!isSubmitting) {
239
- _context.n = 1;
290
+ _context2.n = 1;
240
291
  break;
241
292
  }
242
- return _context.a(2);
293
+ return _context2.a(2);
243
294
  case 1:
244
295
  if (formRef.current) {
245
- _context.n = 2;
296
+ _context2.n = 2;
246
297
  break;
247
298
  }
248
- return _context.a(2);
299
+ return _context2.a(2);
249
300
  case 2:
250
301
  if (!(!shift4Ref.current || !componentsRef.current)) {
251
- _context.n = 3;
302
+ _context2.n = 3;
252
303
  break;
253
304
  }
254
305
  setError("Payment form is not ready yet. Please try again.");
255
- return _context.a(2);
306
+ return _context2.a(2);
256
307
  case 3:
257
308
  setError("");
258
309
  setSuccess("");
259
310
  setIsSubmitting(true);
260
- _context.p = 4;
261
- _context.n = 5;
311
+ _context2.p = 4;
312
+ _context2.n = 5;
262
313
  return shift4Ref.current.createToken(componentsRef.current);
263
314
  case 5:
264
- token = _context.v;
315
+ token = _context2.v;
265
316
  if (token !== null && token !== void 0 && token.id) {
266
- _context.n = 6;
317
+ _context2.n = 6;
267
318
  break;
268
319
  }
269
320
  throw new Error("Card tokenization failed. Please check your details and try again.");
@@ -273,18 +324,18 @@ function PaymentForm(_ref) {
273
324
  currency: currency,
274
325
  card: token.id
275
326
  };
276
- _context.n = 7;
327
+ _context2.n = 7;
277
328
  return shift4Ref.current.verifyThreeDSecure(request);
278
329
  case 7:
279
- threeDSecureToken = _context.v;
330
+ threeDSecureToken = _context2.v;
280
331
  if (threeDSecureToken !== null && threeDSecureToken !== void 0 && threeDSecureToken.id) {
281
- _context.n = 8;
332
+ _context2.n = 8;
282
333
  break;
283
334
  }
284
335
  throw new Error("3D Secure verification failed. Please try again.");
285
336
  case 8:
286
337
  apiBase = process.env.REACT_APP_API_BASE || "http://localhost:4242";
287
- _context.n = 9;
338
+ _context2.n = 9;
288
339
  return fetch("".concat(apiBase, "/charge"), {
289
340
  method: "POST",
290
341
  headers: {
@@ -297,35 +348,35 @@ function PaymentForm(_ref) {
297
348
  })
298
349
  });
299
350
  case 9:
300
- response = _context.v;
301
- _context.n = 10;
351
+ response = _context2.v;
352
+ _context2.n = 10;
302
353
  return response.json();
303
354
  case 10:
304
- result = _context.v;
355
+ result = _context2.v;
305
356
  if (response.ok) {
306
- _context.n = 11;
357
+ _context2.n = 11;
307
358
  break;
308
359
  }
309
360
  throw new Error((result === null || result === void 0 ? void 0 : result.error) || "Payment failed. Please try again.");
310
361
  case 11:
311
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"));
312
- _context.n = 13;
363
+ _context2.n = 13;
313
364
  break;
314
365
  case 12:
315
- _context.p = 12;
316
- _t = _context.v;
317
- 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.");
318
369
  case 13:
319
- _context.p = 13;
370
+ _context2.p = 13;
320
371
  setIsSubmitting(false);
321
- return _context.f(13);
372
+ return _context2.f(13);
322
373
  case 14:
323
- return _context.a(2);
374
+ return _context2.a(2);
324
375
  }
325
- }, _callee, null, [[4, 12, 13, 14]]);
376
+ }, _callee2, null, [[4, 12, 13, 14]]);
326
377
  }));
327
378
  return function handleSubmit(_x) {
328
- return _ref2.apply(this, arguments);
379
+ return _ref3.apply(this, arguments);
329
380
  };
330
381
  }();
331
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.8",
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);
@@ -9,27 +9,55 @@ export default function PaymentForm({amount,currency,color,formwidth=400,formbor
9
9
  const [success, setSuccess] = useState("");
10
10
  const [isSubmitting, setIsSubmitting] = useState(false);
11
11
  const [isReady, setIsReady] = useState(false);
12
+ console.log(error);
13
+ console.log(success);
14
+
15
+
16
+ useEffect(() => {
17
+ if (initializedRef.current) return;
18
+ initializedRef.current = true;
19
+
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
+ };
57
+
58
+ initPayment();
59
+ }, []);
12
60
 
13
- useEffect(() => {
14
- if (initializedRef.current) return;
15
- initializedRef.current = true;
16
-
17
- const script = document.createElement("script");
18
- script.src = "https://js.dev.shift4.com/shift4.js";
19
- script.async = true;
20
- script.onload = () => {
21
- if (!window.Shift4) return;
22
-
23
- shift4Ref.current = window.Shift4("pk_test_SeDEbtFlcz4LqVe1zxMYZxQO");
24
- componentsRef.current = shift4Ref.current.createComponentGroup().automount("#payment-form");
25
- setIsReady(true);
26
- };
27
- script.onerror = () => {
28
- setError("Payment provider script failed to load. Please refresh and try again.");
29
- };
30
-
31
- document.body.appendChild(script);
32
- }, []);
33
61
 
34
62
  const handleSubmit = async (e) => {
35
63
  e.preventDefault();