Last active
March 22, 2021 08:37
-
-
Save vanbo/c01465f09227a118e4c6ebcc777f75d6 to your computer and use it in GitHub Desktop.
WooCommerce Psigate limit transaction attempts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add to "wp-content\themes\child-theme\functions.php" file | |
*/ | |
add_filter( 'wc_psigate_process_acc_manager_payment_parameters', 'vanbodevelops_filter_acc_parameters', 10, 2 ); | |
add_filter( 'wc_psigate_process_credit_card_payment_parameters', 'vanbodevelops_filter_acc_parameters', 10, 2 ); | |
function vanbodevelops_filter_acc_parameters( $parameters, $order ) { | |
$transaction_count = get_incremented_transaction_attempt( $order ); | |
// Check if the order is paid and which attempt of transaction it is | |
// Enter the number of attempts you want to perform before they are no longer allowed | |
// | | |
// | | |
// V | |
if ( ! $order->is_paid() && 10 < $transaction_count ) { | |
// Ends execution and shows an error | |
vanbodevelops_end_order_process_execution( $order->get_id(), 'Transactions for this order are no longer allowed.' ); | |
} | |
return $parameters; | |
} | |
/** | |
* @param \WC_Order $order | |
* | |
* @return int | |
*/ | |
function get_incremented_transaction_attempt( $order ) { | |
$order_new = wc_get_order( $order->get_id() ); | |
$current = (int) $order_new->get_meta( 'wc_psigate_trans_attempt', true ); | |
$current += 1; | |
$order_new->update_meta_data( 'wc_psigate_trans_attempt', wc_clean( $current ) ); | |
$order_new->save(); | |
return $current; | |
} | |
/** | |
* End Checkout process execution and return the message | |
* | |
* @param $order_id | |
* @param $message | |
*/ | |
function vanbodevelops_end_order_process_execution( $order_id, $message ) { | |
ob_clean(); | |
wc_add_notice( $message, 'error' ); | |
// You can also add a timeout to slowdown a possible attach | |
// Just uncomment the line below and enter the seconds you want the script to wait | |
// sleep(5); | |
$messages = wc_print_notices( true ); | |
$response = array( | |
'result' => 'failure', | |
'messages' => isset( $messages ) ? $messages : '', | |
'refresh' => false, | |
'reload' => false, | |
); | |
wp_send_json( $response ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment