strapi-plugin-payone-provider 1.5.2 → 1.5.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.
@@ -553,36 +553,83 @@ const ApplePayButton = ({
553
553
  }
554
554
 
555
555
  // Call the callback with the token BEFORE completing payment
556
- // This ensures the token is processed before the dialog closes
556
+ // This ensures the token is saved before the dialog closes
557
557
  console.log("[Apple Pay] Sending token to callback");
558
558
  let callbackSuccess = true;
559
+ let callbackError = null;
560
+
559
561
  if (onTokenReceived) {
560
562
  try {
561
- // If callback is async, wait for it
563
+ // Call the callback to save the token
564
+ // The callback should set the token in state and return success immediately
565
+ // It should NOT process the payment yet - that will happen when user clicks the button
562
566
  const callbackResult = onTokenReceived(tokenString, {
563
567
  paymentToken: paymentToken,
564
568
  billingContact: response.payerName || response.details?.billingContact,
565
569
  shippingContact: response.shippingAddress || response.details?.shippingAddress,
566
570
  shippingOption: response.shippingOption || response.details?.shippingOption
567
571
  });
568
-
569
- // If callback returns a promise, wait for it
572
+
573
+ // If callback returns a promise, wait for it to resolve or reject
570
574
  if (callbackResult && typeof callbackResult.then === 'function') {
571
- await callbackResult;
575
+ try {
576
+ const result = await callbackResult;
577
+ console.log("[Apple Pay] Token callback completed successfully:", result);
578
+ // Check if result indicates success
579
+ if (result && result.success === false) {
580
+ callbackSuccess = false;
581
+ callbackError = new Error(result.message || "Token callback returned failure");
582
+ }
583
+ } catch (error) {
584
+ console.error("[Apple Pay] Token callback promise rejected:", error);
585
+ callbackSuccess = false;
586
+ callbackError = error;
587
+ }
588
+ } else if (callbackResult === false) {
589
+ // If callback explicitly returns false, treat as failure
590
+ callbackSuccess = false;
591
+ console.warn("[Apple Pay] Token callback returned false");
592
+ } else {
593
+ // If callback returns a value (not a promise), assume success
594
+ console.log("[Apple Pay] Token callback returned synchronously");
572
595
  }
573
- } catch (callbackError) {
574
- console.error("[Apple Pay] Error in token callback:", callbackError);
596
+ } catch (error) {
597
+ console.error("[Apple Pay] Error in token callback:", error);
575
598
  callbackSuccess = false;
599
+ callbackError = error;
576
600
  }
601
+ } else {
602
+ console.warn("[Apple Pay] No onTokenReceived callback provided");
603
+ // If no callback, we should still complete the payment
604
+ // But mark as success since we can't determine the result
605
+ callbackSuccess = true;
577
606
  }
578
607
 
579
608
  // Complete payment with success or fail based on callback result
609
+ // IMPORTANT: Only call complete() after the callback has fully finished
580
610
  console.log("[Apple Pay] Completing payment with status:", callbackSuccess ? "success" : "fail");
611
+
581
612
  try {
582
- await response.complete(callbackSuccess ? "success" : "fail");
583
- console.log("[Apple Pay] Payment completed successfully");
613
+ // Use a small delay to ensure state updates are complete
614
+ // This prevents the dialog from closing before the token is saved
615
+ await new Promise(resolve => setTimeout(resolve, 200));
616
+
617
+ const completionStatus = callbackSuccess ? "success" : "fail";
618
+ await response.complete(completionStatus);
619
+ console.log("[Apple Pay] Payment completed with status:", completionStatus);
620
+
621
+ // If there was an error, notify the error handler
622
+ if (!callbackSuccess && callbackError && onError) {
623
+ onError(callbackError);
624
+ }
584
625
  } catch (completeError) {
585
626
  console.error("[Apple Pay] Error completing payment:", completeError);
627
+ // Try to complete with fail status if there's an error
628
+ try {
629
+ await response.complete("fail");
630
+ } catch (finalError) {
631
+ console.error("[Apple Pay] Failed to complete payment even with fail status:", finalError);
632
+ }
586
633
  if (onError) {
587
634
  onError(completeError);
588
635
  }
@@ -44,16 +44,33 @@ const AuthorizationForm = ({
44
44
 
45
45
  const handleApplePayToken = async (token, paymentData) => {
46
46
  if (!token) {
47
+ console.error("[Apple Pay] Token is missing in handleApplePayToken");
47
48
  return Promise.reject(new Error("Token is missing"));
48
49
  }
50
+
51
+ console.log("[Apple Pay] handleApplePayToken called with token:", {
52
+ hasToken: !!token,
53
+ tokenLength: token?.length,
54
+ paymentData: !!paymentData
55
+ });
56
+
57
+ // IMPORTANT: Set token in state immediately (synchronously)
58
+ // This ensures the token is saved before the dialog closes
49
59
  setApplePayToken(token);
50
- try {
51
- await onAuthorization(token);
52
- return Promise.resolve();
53
- } catch (error) {
54
- console.error("[Apple Pay] Error in authorization:", error);
55
- return Promise.reject(error);
56
- }
60
+
61
+ console.log("[Apple Pay] Token saved to state successfully");
62
+
63
+ // Don't call onAuthorization immediately
64
+ // Let the user manually trigger the payment using the button
65
+ // This prevents the dialog from closing prematurely if there's an error
66
+ // The dialog will close with success, and the user will see the "Process Authorization" button
67
+
68
+ // Return success immediately so the dialog closes properly
69
+ // The actual payment processing will happen when the user clicks the button
70
+ return Promise.resolve({
71
+ success: true,
72
+ message: "Token received successfully. Please click 'Process Authorization' to complete the payment."
73
+ });
57
74
  };
58
75
 
59
76
  const handleApplePayError = (error) => {
@@ -44,16 +44,33 @@ const PreauthorizationForm = ({
44
44
 
45
45
  const handleApplePayToken = async (token, paymentData) => {
46
46
  if (!token) {
47
+ console.error("[Apple Pay] Token is missing in handleApplePayToken");
47
48
  return Promise.reject(new Error("Token is missing"));
48
49
  }
50
+
51
+ console.log("[Apple Pay] handleApplePayToken called with token:", {
52
+ hasToken: !!token,
53
+ tokenLength: token?.length,
54
+ paymentData: !!paymentData
55
+ });
56
+
57
+ // IMPORTANT: Set token in state immediately (synchronously)
58
+ // This ensures the token is saved before the dialog closes
49
59
  setApplePayToken(token);
50
- try {
51
- await onPreauthorization(token);
52
- return Promise.resolve();
53
- } catch (error) {
54
- console.error("[Apple Pay] Error in preauthorization:", error);
55
- return Promise.reject(error);
56
- }
60
+
61
+ console.log("[Apple Pay] Token saved to state successfully");
62
+
63
+ // Don't call onPreauthorization immediately
64
+ // Let the user manually trigger the payment using the button
65
+ // This prevents the dialog from closing prematurely if there's an error
66
+ // The dialog will close with success, and the user will see the "Process Preauthorization" button
67
+
68
+ // Return success immediately so the dialog closes properly
69
+ // The actual payment processing will happen when the user clicks the button
70
+ return Promise.resolve({
71
+ success: true,
72
+ message: "Token received successfully. Please click 'Process Preauthorization' to complete the payment."
73
+ });
57
74
  };
58
75
 
59
76
  const handleApplePayError = (error) => {
@@ -83,6 +83,12 @@ const usePaymentActions = () => {
83
83
  };
84
84
 
85
85
  const handlePreauthorization = async (tokenParam = null) => {
86
+ console.log("[Payment] handlePreauthorization called", {
87
+ hasToken: !!tokenParam,
88
+ paymentMethod,
89
+ amount: paymentAmount
90
+ });
91
+
86
92
  setIsProcessingPayment(true);
87
93
  setPaymentError(null);
88
94
  setPaymentResult(null);
@@ -211,16 +217,31 @@ const usePaymentActions = () => {
211
217
 
212
218
  setPaymentResult(responseData);
213
219
 
220
+ console.log("[Payment] Preauthorization result:", {
221
+ status,
222
+ hasError: !!errorCode,
223
+ errorCode,
224
+ errorMessage
225
+ });
226
+
214
227
  if (status === "APPROVED") {
215
228
  handlePaymentSuccess("Preauthorization completed successfully");
229
+ // Return success result for Apple Pay callback
230
+ return { success: true, data: responseData };
216
231
  } else {
232
+ const errorMsg = errorMessage || `Unexpected status: ${status}`;
217
233
  handlePaymentError(
218
- { message: `Unexpected status: ${status}` },
234
+ { message: errorMsg },
219
235
  `Preauthorization completed with status: ${status}`
220
236
  );
237
+ // Return error result for Apple Pay callback
238
+ throw new Error(errorMsg);
221
239
  }
222
240
  } catch (error) {
241
+ console.error("[Payment] Preauthorization error:", error);
223
242
  handlePaymentError(error, "Preauthorization failed");
243
+ // Re-throw error so Apple Pay callback knows it failed
244
+ throw error;
224
245
  } finally {
225
246
  setIsProcessingPayment(false);
226
247
  }
@@ -356,16 +377,31 @@ const usePaymentActions = () => {
356
377
 
357
378
  setPaymentResult(responseData);
358
379
 
380
+ console.log("[Payment] Authorization result:", {
381
+ status,
382
+ hasError: !!errorCode,
383
+ errorCode,
384
+ errorMessage
385
+ });
386
+
359
387
  if (status === "APPROVED") {
360
388
  handlePaymentSuccess("Authorization completed successfully");
389
+ // Return success result for Apple Pay callback
390
+ return { success: true, data: responseData };
361
391
  } else {
392
+ const errorMsg = errorMessage || `Unexpected status: ${status}`;
362
393
  handlePaymentError(
363
- { message: `Unexpected status: ${status}` },
394
+ { message: errorMsg },
364
395
  `Authorization completed with status: ${status}`
365
396
  );
397
+ // Return error result for Apple Pay callback
398
+ throw new Error(errorMsg);
366
399
  }
367
400
  } catch (error) {
401
+ console.error("[Payment] Authorization error:", error);
368
402
  handlePaymentError(error, "Authorization failed");
403
+ // Re-throw error so Apple Pay callback knows it failed
404
+ throw error;
369
405
  } finally {
370
406
  setIsProcessingPayment(false);
371
407
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strapi-plugin-payone-provider",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "description": "Strapi plugin for Payone payment gateway integration",
5
5
  "license": "MIT",
6
6
  "maintainers": [