Skip to content

Instantly share code, notes, and snippets.

@alanef
Last active April 19, 2021 09:24
Show Gist options
  • Select an option

  • Save alanef/9035131bfad53e083201d1e1e8ecceb3 to your computer and use it in GitHub Desktop.

Select an option

Save alanef/9035131bfad53e083201d1e1e8ecceb3 to your computer and use it in GitHub Desktop.
<?php
/*
* Plugin Name: IPN Debug
*/
add_action( 'template_redirect', 'test_qem_ipn' );
function test_qem_ipn() {
global $qem_fs;
if ( ! isset ( $_GET['test_qem_ipn'] ) ) {
return;
}
$payment = qem_get_stored_payment();
if ( ! qem_get_element( $payment, 'ipn', false ) ) {
return;
}
if ( ! defined( "DEBUG" ) ) {
define( "DEBUG", 0 );
}
if ( ! defined( "LOG_FILE" ) ) {
define( "LOG_FILE", "./ipn.log" );
}
$raw_post_data = file_get_contents( 'php://input' );
$raw_post_array = explode( '&', $raw_post_data );
$myPost = array();
foreach ( $raw_post_array as $keyval ) {
$keyval = explode( '=', $keyval );
if ( count( $keyval ) == 2 ) {
$myPost[ $keyval[0] ] = urldecode( $keyval[1] );
}
}
// see https://developer.paypal.com/docs/ipn/integration-guide/ht-ipn/#do-it
$req = 'cmd=_notify-validate';
if ( function_exists( 'get_magic_quotes_gpc' ) ) {
$get_magic_quotes_exists = true;
}
foreach ( $myPost as $key => $value ) {
if ( $get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1 ) {
$value = urlencode( stripslashes( $value ) );
} else {
$value = urlencode( $value );
}
$req .= "&$key=$value";
}
if ( qem_get_element( $payment, 'sandbox', false ) ) {
$paypal_url = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr';
} else {
$paypal_url = "https://ipnpb.paypal.com/cgi-bin/webscr";
}
$response = wp_remote_post( $paypal_url, array(
'timeout' => 30,
'body' => $req,
)
);
if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "Can't connect to PayPal to validate IPN message: Indetermined" . PHP_EOL, 3, LOG_FILE );
}
return;
}
$status = wp_remote_retrieve_body( $response );
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "HTTP request of validation request: for IPN payload: $req" .
print_r( wp_remote_retrieve_headers( $response ), true ) .
PHP_EOL, 3, LOG_FILE );
error_log( date( '[Y-m-d H:i e] ' ) . "HTTP response of validation request: $status" . PHP_EOL, 3, LOG_FILE );
}
if ( 'VERIFIED' == $status ) {
$custom = $_POST['custom'];
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "Got this custom key: " .
$custom .
PHP_EOL, 3, LOG_FILE );
}
$args = array( 'post_type' => 'event', 'posts_per_page' => - 1 );
query_posts( $args );
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$id = get_the_id();
$message = get_option( 'qem_messages_' . $id );
if ( $message ) {
$count = count( $message );
for ( $i = 0; $i <= $count; $i ++ ) {
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "Checking entry:" .
print_r( $message[$i], true ) .
PHP_EOL, 3, LOG_FILE );
if ( $message[ $i ]['ipn'] == $custom ) {
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "Found a key match " .
PHP_EOL, 3, LOG_FILE );
}
$message[ $i ]['ipn'] = 'Paid';
$auto = qem_get_stored_autoresponder();
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "after qem_get_stored_autoresponder" . PHP_EOL, 3, LOG_FILE );
}
$register = get_custom_registration_form();
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "after custom reg form" . PHP_EOL, 3, LOG_FILE );
}
$addons = qem_get_addons();
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "after qem_get_addons" . PHP_EOL, 3, LOG_FILE );
}
$payment = qem_get_stored_payment();
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "after qem_get_stored_payment" . PHP_EOL, 3, LOG_FILE );
}
$values = array(
'yourname' => $message[ $i ]['yourname'],
'youremail' => $message[ $i ]['youremail'],
'yourtelephone' => $message[ $i ]['yourtelephone'],
'yourmessage' => $message[ $i ]['yourmessage'],
'yourplaces' => $message[ $i ]['yourplaces'],
'yourblank1' => $message[ $i ]['yourblank1'],
'yourdropdown' => $message[ $i ]['yourdropdown'],
'yourselector' => $message[ $i ]['yourselector'],
'yournumber1' => $message[ $i ]['yournumber1'],
'morenames' => $message[ $i ]['morenames'],
'ignore' => $message[ $i ]['ignore'],
'donation_amount' => $message[ $i ]['donation_amount']
);
if ( $qem_fs->is__premium_only() ) {
if ( $qem_fs->can_use_premium_code() ) {
$values['donation_amount'] = $message[ $i ]['donation_amount'];
}
}
$date = get_post_meta( $id, 'event_date', true );
$enddate = get_post_meta( $id, 'event_end_date', true );
$date = date_i18n( "d M Y", $date );
$enddate = date_i18n( "d M Y", $enddate );
$start = get_post_meta( $id, 'event_start', true );
$finish = get_post_meta( $id, 'event_finish', true );
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "before message logic" . PHP_EOL, 3, LOG_FILE );
}
if ( $auto['enable'] && $message[ $i ]['youremail'] && $auto['whenconfirm'] == 'afterpayment' ) {
$content = qem_build_event_message( $values, $register );
qem_send_confirmation( $auto, $values, $content, $register, $id );
}
if (
$auto['whenconfirm'] == 'afterpaymnet'
) {
qem_admin_notification( $id, $register, $addons, $values, $auto, $enddate, $date, $start, $finish, $payment );
}
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "before update uption" . PHP_EOL, 3, LOG_FILE );
}
$up_res = update_option( 'qem_messages_' . $id, $message );
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "after update option" . PHP_EOL, 3, LOG_FILE );
}
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "Option update result: " .
$up_res .
PHP_EOL, 3, LOG_FILE );
}
}
if ( DEBUG == true ) {
error_log( date( '[Y-m-d H:i e] ' ) . "No key match: " .
$up_res .
PHP_EOL, 3, LOG_FILE );
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment