Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Created August 28, 2018 15:42
Show Gist options
  • Select an option

  • Save panoslyrakis/90a056d3a61dcb2c590c8928a20fd9a1 to your computer and use it in GitHub Desktop.

Select an option

Save panoslyrakis/90a056d3a61dcb2c590c8928a20fd9a1 to your computer and use it in GitHub Desktop.

Revisions

  1. panoslyrakis created this gist Aug 28, 2018.
    103 changes: 103 additions & 0 deletions ms-paypal-log.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    <?php
    /**
    * Plugin Name: [Membership 2] - PayPal Payment Logs
    * Plugin URI: https://premium.wpmudev.org/
    * Description: Logs PayPal Payments
    * Author: Panos Lyrakis @ WPMUDEV
    * Author URI: https://premium.wpmudev.org/
    * License: GPLv2 or later
    */
    if ( ! defined( 'ABSPATH' ) ) {
    exit;
    }
    if ( ! class_exists( 'WPMUDEV_MS_PayPal_Loger' ) ) {

    class WPMUDEV_MS_PayPal_Loger {
    private static $_instance = null;
    public static function get_instance() {
    if( is_null( self::$_instance ) ){
    self::$_instance = new WPMUDEV_MS_PayPal_Loger();
    }
    return self::$_instance;

    }
    private function __construct() {
    add_action( 'pre_get_posts', array( $this, 'handle' ), 1 );
    }

    public function handle( $wp_query ) {

    //if ( empty( $wp_query->query_vars['paymentgateway'] ) ) {
    // return;
    //}

    if ( ! isset( $wp_query->query_vars['pagename'] ) || strpos( $wp_query->query_vars['pagename'], 'ms-payment-return' ) === false ) {
    return;
    }

    $now = date( 'd M Y H:i:s' );
    $log_content = '';
    //$gateway = $wp_query->query_vars['paymentgateway'];
    $gateway = explode( '/', $wp_query->query_vars['pagename'] )[1];

    $transaction_type = '';
    $external_id = null;
    $invoice_id = null;

    // Backwards compatibility

    switch ( $gateway ) {
    case 'paypal_single': $gateway = 'paypalsingle'; break;
    case 'paypal_standard': $gateway = 'paypalstandard'; break;
    case 'paypal-single': $gateway = 'paypalsingle'; break;
    case 'paypal-standard': $gateway = 'paypalstandard'; break;
    case 'paypalsolo': $gateway = 'paypalsingle'; break; // M1
    case 'paypalexpress': $gateway = 'paypalstandard'; break; //M1
    case 'paypalstandar': $gateway = 'paypalstandard'; break;
    }

    if ( ! empty( $_POST[ 'payment_status'] ) ) {
    $payment_status = strtolower( $_POST[ 'payment_status'] );
    }

    if ( ! empty( $_POST[ 'txn_type'] ) ) {
    $transaction_type = strtolower( $_POST[ 'txn_type'] );
    }

    if ( ! empty( $_POST['txn_id'] ) ) {
    $external_id = $_POST['txn_id'];
    }

    if ( ! empty( $_POST['invoice'] ) ) {
    $invoice_id = intval( $_POST['invoice'] );
    } elseif ( ! empty( $_POST['rp_invoice'] ) ) {
    $invoice_id = intval( $_POST['rp_invoice'] );
    }

    $log_content = "\n\n\n=======================\n\n\n{$now}\n\n";
    $log_content .= "invoice_id : {$invoice_id}\n";
    $log_content .= "external_id : {$external_id}\n\n";
    $log_content .= "transaction_type : {$transaction_type}\n\n";
    $log_content .= "payment_status : {$payment_status}\n\n";
    $log_content .= "data : " . print_r( $_POST, true );

    self::log( $log_content );
    }

    public static function log( $data, $log_file = null, $log_file_path = null ) {
    $data = is_array( $data ) || is_object( $data ) ? print_r( $data, true ) : $data;
    $log_file = is_null( $log_file ) ? 'ms-paypal.log' : $log_file;
    $log_file_path = is_null( $log_file_path ) ? WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'mu-plugins' : $log_file_path;
    $log_file = $log_file_path . DIRECTORY_SEPARATOR . $log_file;
    $date = date( 'd-m-Y H:i:s' );
    error_log( "\n\n[ {$date} ] \n{$data}", 3, $log_file );
    }


    }

    add_action( 'plugins_loaded', function(){
    $GLOBALS['WPMUDEV_MS_PayPal_Loger'] = WPMUDEV_MS_PayPal_Loger::get_instance();
    }, 10 );

    }