Skip to content

Instantly share code, notes, and snippets.

@vanbo
Last active September 12, 2022 13:11
Show Gist options
  • Select an option

  • Save vanbo/a68bcf844262de9962ac11d6df21a29d to your computer and use it in GitHub Desktop.

Select an option

Save vanbo/a68bcf844262de9962ac11d6df21a29d to your computer and use it in GitHub Desktop.
WooCommerce ePaybg: Do not allow Pay page reload after customer is sent to ePaybg
add_action( 'woocommerce_receipt_epaybg', 'vanbo_initiate_epaybg_request_hooks', 9 );
/**
* Replaces the gateway original action with our own
*/
function vanbo_initiate_epaybg_request_hooks() {
$gateway = vanbo_get_epaybg_gateway();
if ( false == $gateway ) {
return;
}
// Removes the original action
remove_action( 'woocommerce_receipt_epaybg', array( $gateway, 'receipt_page' ) );
add_action( 'woocommerce_receipt_epaybg', 'vanbo_epaybg_payment_request_form', 11 );
}
/**
* Executes the same form action, but skips when the page is loaded with the 'mashsb-refresh' and 'tb-preview' parameters.
*
* The page should be loaded only once and any reloading of the page will result in a different invoice number,
* which in turn will affect the response handling of the gateway. This behavior should not be allowed
* and that is why we resorted to this workaround
*
* @param int $order_id
*/
function vanbo_epaybg_payment_request_form( $order_id ) {
$gateway = vanbo_get_epaybg_gateway();
if ( false == $gateway ) {
return;
}
if ( is_ajax() ) {
return;
}
echo '<p>' . __( 'Thank you for your order, please click the button below to pay with ePay.bg.', 'wc_epaybg' ) . '</p>';
echo $gateway->generate_epaybg_form( $order_id );
}
/**
* @return WC_Gateway_Epaybg|false False will be returned if the gateway is not active on the site.
*/
function vanbo_get_epaybg_gateway() {
foreach ( WC()->payment_gateways()->payment_gateways() as $gateway ) {
if ( 'epaybg' == $gateway->id ) {
return $gateway;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment