| 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 |
|