PluginProbe
PayPlug for WooCommerce (Official) / 3.1.0
PayPlug for WooCommerce (Official) v3.1.0
3.1.0 3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 All 102 releases
payplug / resources / js / frontend / wc-payplug-popup-blocks.js

wc-payplug-popup-blocks.js in PayPlug for WooCommerce (Official) 3.1.0, at resources/js/frontend/wc-payplug-popup-blocks.js

108 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { useEffect, useRef } from 'react';
2 import { getPayment} from "./helper/wc-payplug-requests";
3
4 const Popup = ({props: props, settings:_settings}) => {
5 const { eventRegistration, emitResponse, shouldSavePayment } = props;
6 const { onPaymentSetup, onCheckoutSuccess } = eventRegistration;
7 // A plain variable would be reset on every re-render, but it's read from the
8 // onCheckoutSuccess callback below, which can fire well after a re-render has happened.
9 const getPaymentDataRef = useRef(null);
10
11 useEffect(() => {
12 const handlePaymentProcessing = async () => {
13 // Order-pay always has a real, existing order from the start (the one being
14 // repaid), so the payment intent can be created right away.
15 if (_settings.is_order_pay) {
16 try {
17 const response = await getPayment(props, _settings, _settings.order_pay_id);
18 getPaymentDataRef.current = response;
19
20 return {
21 type: 'success'
22 };
23 } catch (error) {
24 return {
25 type: 'error',
26 message: error.message,
27 messageContext: emitResponse.noticeContexts.PAYMENTS
28 };
29 }
30 }
31
32 // Regular checkout: WooCommerce doesn't create the real order until the place-order
33 // submission that follows this step (order creation is deferred until then since
34 // WooCommerce 10.8). The payment intent can only be created once that real order
35 // exists, so that happens server-side in process_payment() and comes back here via
36 // payment_details on the onCheckoutSuccess event below - nothing more to do yet.
37 return {
38 type: 'success'
39 };
40 }
41 const unsubscribeAfterProcessing = onPaymentSetup(handlePaymentProcessing);
42 return () => { unsubscribeAfterProcessing(); };
43
44 }, [
45 shouldSavePayment,
46 onPaymentSetup,
47 emitResponse.noticeContexts.PAYMENTS,
48 emitResponse.responseTypes.ERROR,
49 emitResponse.responseTypes.SUCCESS
50 ]);
51
52 useEffect(() => {
53 const handlePaymentProcessing = async ({ processingResponse }) => {
54 // Order-pay's payment intent was already created in onPaymentSetup above (same
55 // shape as the AJAX response: { data: { redirect, cancel } }), since a real order
56 // already existed there. Regular checkout only gets its payment_id/redirect/cancel
57 // here, via payment_details, once process_payment() has created it server-side.
58 const paymentDetails = _settings.is_order_pay
59 ? (getPaymentDataRef.current?.data || {})
60 : (processingResponse?.paymentDetails || {});
61
62 return await showPopupPayment(paymentDetails).then( () => {
63 return {
64 type: "success"
65 }
66 }).catch( () => {
67 return {
68 type: "error",
69 message: "Timeout",
70 messageContext: emitResponse.noticeContexts.PAYMENTS
71 }
72 })
73
74 function showPopupPayment(paymentDetails) {
75 return new Promise(async (resolve, reject) => {
76 try {
77 window.redirection_url = paymentDetails.cancel || false;
78 await Payplug.showPayment(paymentDetails.redirect);
79 // Deliberately no resolve() here: Payplug's widget navigates window.top
80 // to hosted_payment.return_url itself once the payment finishes inside
81 // the lightbox. Resolving as soon as showPayment() opens the lightbox
82 // would mark checkout "complete" and let WooCommerce Blocks navigate
83 // away on its own (to payment_result.redirect_url) before the widget
84 // gets the chance to - tearing the lightbox down before the customer
85 // can pay. Only the error path settles this promise.
86 } catch (e) {
87 reject(e);
88 }
89
90 })
91 }
92 }
93 const unsubscribeAfterProcessing = onCheckoutSuccess(handlePaymentProcessing);
94 return () => { unsubscribeAfterProcessing(); };
95
96 }, [
97 onCheckoutSuccess
98 ]);
99
100 return (
101 <>
102 </>
103 )
104
105 }
106
107 export default Popup;
108