Skip to content

Commit

Permalink
feat(1.3.23): remove parse_request hook everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
daanrijpkemacb committed Nov 2, 2024
1 parent 3ae3fba commit 13cf782
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 185 deletions.
9 changes: 0 additions & 9 deletions bluem-idin.php
Original file line number Diff line number Diff line change
Expand Up @@ -915,16 +915,11 @@ function bluem_idin_shortcode_idin_execute(): void
bluem_idin_execute(null, true, $goto);
}

add_action('parse_request', 'bluem_idin_shortcode_callback');
/**
* This function is executed at a callback GET request with a given mandateId. This is then, together with the entranceCode in user or Bluem session storage, sent for a SUD to the Bluem API.
*/
function bluem_idin_shortcode_callback(): void
{
if (!str_contains(sanitize_url(wp_unslash($_SERVER['REQUEST_URI'])), 'bluem-woocommerce/idin_shortcode_callback')) {
return;
}

$bluem_config = bluem_woocommerce_get_config();

// fallback until this is corrected in bluem-php
Expand Down Expand Up @@ -1258,17 +1253,13 @@ function bluem_idin_shortcode_callback(): void
exit;
}

add_action('parse_request', 'bluem_idin_webhook');
/**
* Identity webhook action
*
* @return void
*/
function bluem_idin_webhook(): void
{
if (strpos(sanitize_url(wp_unslash($_SERVER['REQUEST_URI'])), 'bluem-woocommerce/bluem_idin_webhook') === false) {
return;
}
http_response_code(200);
exit;
}
Expand Down
16 changes: 0 additions & 16 deletions bluem-integrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,11 @@ function bluem_woocommerce_integration_gform_javascript()
* ContactForm 7 integration.
* AJAX Form submissions.
*/
add_action('parse_request', 'bluem_woocommerce_integration_wpcf7_ajax');

function bluem_woocommerce_integration_wpcf7_ajax()
{
$bluem_config = bluem_woocommerce_get_config();

if (!isset($_SERVER['REQUEST_URI']) || strpos(sanitize_url(wp_unslash($_SERVER['REQUEST_URI'])), 'bluem-woocommerce/bluem-integrations/wpcf7_mandate') === false) {
return;
}

$bluem_mandate_approve = !empty($_POST['bluem_mandate_approve']) ? sanitize_text_field(wp_unslash($_POST['bluem_mandate_approve'])) : '';

if ($bluem_config->wpcf7Active !== 'Y' || empty($bluem_mandate_approve)) {
Expand Down Expand Up @@ -541,18 +536,12 @@ function bluem_woocommerce_integration_wpcf7_submit()
* ContactForm 7 integration.
* Callback for requests.
*/
add_action('parse_request', 'bluem_woocommerce_integration_wpcf7_callback');

function bluem_woocommerce_integration_wpcf7_callback()
{
$bluem_config = bluem_woocommerce_get_config();

$storage = bluem_db_get_storage();

if (empty($_SERVER['REQUEST_URI']) || strpos(sanitize_url(wp_unslash($_SERVER['REQUEST_URI'])), 'bluem-woocommerce/bluem-integrations/wpcf7_callback') === false) {
return;
}

if ($bluem_config->wpcf7Active !== 'Y') {
return;
}
Expand Down Expand Up @@ -1025,18 +1014,13 @@ function bluem_woocommerce_integration_gform_submit($entry, $form)
* Gravity Forms integration.
* Callback after request
*/
add_action('parse_request', 'bluem_woocommerce_integration_gform_callback');

function bluem_woocommerce_integration_gform_callback()
{
$bluem_config = bluem_woocommerce_get_config();

$storage = bluem_db_get_storage();

if (strpos(sanitize_url(wp_unslash($_SERVER['REQUEST_URI'])), 'bluem-woocommerce/bluem-integrations/gform_callback') === false) {
return;
}

if ($bluem_config->gformActive !== 'Y') {
return;
}
Expand Down
277 changes: 140 additions & 137 deletions bluem-mandates-instant.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,155 +6,158 @@

use Bluem\BluemPHP\Bluem;

add_action( 'parse_request', 'bluem_mandates_instant_request' );

function bluem_mandates_instant_request(): void {
if ( empty( $_SERVER['REQUEST_URI'] ) || ! str_contains( sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'bluem-woocommerce/mandate_instant_request' ) ) {
return;
}

$bluem_config = bluem_woocommerce_get_config();

$debtorReference = ! empty( $_GET['debtorreference'] ) ? sanitize_text_field( wp_unslash( $_GET['debtorreference'] ) ) : '';

if ( ! empty( $debtorReference ) ) {
$debtorReference = sanitize_text_field( $debtorReference );

$db_results = bluem_db_get_requests_by_keyvalues(
array(
'debtor_reference' => $debtorReference,
'status' => 'Success',
)
);

// Check the sequence type or previous success results
if ( $bluem_config->sequenceType === 'OOFF' || sizeof( $db_results ) === 0 ) {
$bluem_config->merchantReturnURLBase = home_url(
'bluem-woocommerce/mandates_instant_callback'
);

$preferences = get_option( 'bluem_woocommerce_options' );

// Convert UTF-8 to ISO
if ( ! empty( $bluem_config->eMandateReason ) ) {
$bluem_config->eMandateReason = mb_convert_encoding( $bluem_config->eMandateReason, 'ISO-8859-1', 'UTF-8' );
} else {
$bluem_config->eMandateReason = esc_html__( 'Incasso machtiging ', 'bluem' ) . $debtorReference;
}

$bluem = new Bluem( $bluem_config );

$mandate_id_counter = get_option( 'bluem_woocommerce_mandate_id_counter' );

if ( ! isset( $mandate_id_counter ) ) {
$mandate_id_counter = $preferences['mandate_id_counter'];
}

$mandate_id = $mandate_id_counter + 1;

update_option( 'bluem_woocommerce_mandate_id_counter', $mandate_id );

$request = $bluem->CreateMandateRequest(
$debtorReference,
$debtorReference,
$mandate_id
);

// Actually perform the request.
try {
$response = $bluem->PerformRequest( $request );

if ( ! isset( $response->EMandateTransactionResponse->TransactionURL ) ) {
$msg = esc_html__(
'Er ging iets mis bij het aanmaken van de transactie.<br>
Vermeld onderstaande informatie aan het websitebeheer:',
'bluem'
);

if ( isset( $response->EMandateTransactionResponse->Error->ErrorMessage ) ) {
$msg .= '<br>' .
$response->EMandateTransactionResponse->Error->ErrorMessage;
} elseif ( $response instanceof \Bluem\BluemPHP\ErrorBluemResponse ) {
$msg .= '<br>' .
$response->Error();
} else {
$msg .= '<br>Algemene fout';
}
bluem_error_report_email(
array(
'service' => 'mandates',
'function' => 'shortcode_execute',
'message' => $msg,
)
);
bluem_dialogs_render_prompt( $msg );
exit;
}

$mandate_id = $response->EMandateTransactionResponse->MandateID . '';

// redirect cast to string, necessary for AJAX response handling
$transactionURL = ( $response->EMandateTransactionResponse->TransactionURL . '' );

bluem_db_insert_storage(
array(
'bluem_mandate_transaction_id' => $mandate_id,
'bluem_mandate_transaction_url' => $transactionURL,
'bluem_mandate_entrance_code' => $request->entranceCode,
)
);

$db_creation_result = bluem_db_create_request(
array(
'entrance_code' => $request->entranceCode,
'transaction_id' => $request->mandateID,
'transaction_url' => $transactionURL,
'user_id' => 0,
'timestamp' => gmdate( 'Y-m-d H:i:s' ),
'description' => 'Mandate request',
'debtor_reference' => $debtorReference,
'type' => 'mandates',
'order_id' => '',
'payload' => wp_json_encode(
array(
'created_via' => 'instant_request',
'environment' => $bluem->getConfig( 'environment' ),
'created_mandate_id' => $mandate_id,
)
),
)
);

if ( ob_get_length() !== false && ob_get_length() > 0 ) {
ob_clean();
}

ob_start();
wp_redirect( $transactionURL );
exit;
} catch ( \Exception $e ) {

}
} else {
wp_redirect( $bluem_config->instantMandatesResponseURI . '?result=true' );
exit;
}
}
exit;
$debtorReference = !empty( $_GET['debtorreference'] ) ? sanitize_text_field( wp_unslash( $_GET['debtorreference'] ) ) : '';
// get from either casing for the key
if(empty($debtorReference)) {
$debtorReference = !empty( $_GET['debtorReference'] ) ? sanitize_text_field( wp_unslash( $_GET['debtorReference'] ) ) : '';
}

if ( empty( $debtorReference ) ) {
$errormessage = esc_html__( 'Fout: geen debtorReferentie opgegeven', 'bluem' );
bluem_error_report_email(
array(
'service' => 'mandates',
'function' => 'shortcode_execute',
'message' => $errormessage,
)
);
bluem_dialogs_render_prompt( $errormessage );
exit;
}

$debtorReference = sanitize_text_field( $debtorReference );

$db_results = bluem_db_get_requests_by_keyvalues(
array(
'debtor_reference' => $debtorReference,
'status' => 'Success',
)
);

// Check the sequence type or previous success results
if ( $bluem_config->sequenceType === 'OOFF' || sizeof( $db_results ) === 0 ) {
$bluem_config->merchantReturnURLBase = home_url(
'bluem-woocommerce/mandates_instant_callback'
);

$preferences = get_option( 'bluem_woocommerce_options' );

// Convert UTF-8 to ISO
if ( ! empty( $bluem_config->eMandateReason ) ) {
$bluem_config->eMandateReason = mb_convert_encoding( $bluem_config->eMandateReason, 'ISO-8859-1', 'UTF-8' );
} else {
$bluem_config->eMandateReason = esc_html__( 'Incasso machtiging ', 'bluem' ) . $debtorReference;
}

$bluem = new Bluem( $bluem_config );

$mandate_id_counter = get_option( 'bluem_woocommerce_mandate_id_counter' );

if ( ! isset( $mandate_id_counter ) ) {
$mandate_id_counter = $preferences['mandate_id_counter'];
}

$mandate_id = $mandate_id_counter + 1;

update_option( 'bluem_woocommerce_mandate_id_counter', $mandate_id );

$request = $bluem->CreateMandateRequest(
$debtorReference,
$debtorReference,
$mandate_id
);

// Actually perform the request.
try {
$response = $bluem->PerformRequest( $request );

if ( ! isset( $response->EMandateTransactionResponse->TransactionURL ) ) {
$msg = esc_html__(
'Er ging iets mis bij het aanmaken van de transactie.<br>
Vermeld onderstaande informatie aan het websitebeheer:',
'bluem'
);

if ( isset( $response->EMandateTransactionResponse->Error->ErrorMessage ) ) {
$msg .= '<br>' .
$response->EMandateTransactionResponse->Error->ErrorMessage;
} elseif ( $response instanceof \Bluem\BluemPHP\ErrorBluemResponse ) {
$msg .= '<br>' .
$response->Error();
} else {
$msg .= '<br>Algemene fout';
}
bluem_error_report_email(
array(
'service' => 'mandates',
'function' => 'shortcode_execute',
'message' => $msg,
)
);
bluem_dialogs_render_prompt( $msg );
exit;
}

$mandate_id = $response->EMandateTransactionResponse->MandateID . '';

// redirect cast to string, necessary for AJAX response handling
$transactionURL = ( $response->EMandateTransactionResponse->TransactionURL . '' );

bluem_db_insert_storage(
array(
'bluem_mandate_transaction_id' => $mandate_id,
'bluem_mandate_transaction_url' => $transactionURL,
'bluem_mandate_entrance_code' => $request->entranceCode,
)
);

$db_creation_result = bluem_db_create_request(
array(
'entrance_code' => $request->entranceCode,
'transaction_id' => $request->mandateID,
'transaction_url' => $transactionURL,
'user_id' => 0,
'timestamp' => gmdate( 'Y-m-d H:i:s' ),
'description' => 'Mandate request',
'debtor_reference' => $debtorReference,
'type' => 'mandates',
'order_id' => '',
'payload' => wp_json_encode(
array(
'created_via' => 'instant_request',
'environment' => $bluem->getConfig( 'environment' ),
'created_mandate_id' => $mandate_id,
)
),
)
);

if ( ob_get_length() !== false && ob_get_length() > 0 ) {
ob_clean();
}

ob_start();
wp_redirect( $transactionURL );
exit;
} catch ( \Exception $e ) {

}
} else {
wp_redirect( $bluem_config->instantMandatesResponseURI . '?result=true' );
exit;
}
}

add_action( 'parse_request', 'bluem_mandates_instant_callback' );

/**
* This function is executed at a callback GET request with a given mandateId. This is then, together with the entranceCode in Cookie, sent for a SUD to the Bluem API.
*
* @return void
*/
function bluem_mandates_instant_callback() {
if ( empty( $_SERVER['REQUEST_URI'] ) || ( strpos( sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'bluem-woocommerce/mandates_instant_callback' ) === false ) ) {
return;
}

$bluem_config = bluem_woocommerce_get_config();

try {
Expand Down
Loading

0 comments on commit 13cf782

Please sign in to comment.