Last active
July 3, 2018 03:07
-
-
Save wpmudev-sls/8a7bfc7ca5b1c9a2c6af358be0a0d172 to your computer and use it in GitHub Desktop.
Logs Stripe Payments in 3 log files both located in mu-plugins dir. One with specific info about payment, one that contains the raw request, and one that contains the stripe subscription object
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
<?php | |
/** | |
* Plugin Name: [Pro Sites] - Stripe Payment Logs | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Logs Stripe Payments in 3 log files both located in mu-plugins dir. One with specific info about payment, one that contains the raw request, and one that contains the stripe subscription object | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_PS_Stripe_Loger' ) ) { | |
class WPMUDEV_PS_Stripe_Loger { | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_PS_Stripe_Loger(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_action( 'wp_ajax_nopriv_psts_stripe_webhook', array( &$this, 'payment_received' ) ); | |
add_action( 'wp_ajax_psts_stripe_webhook', array( &$this, 'payment_received' ) ); | |
} | |
public function payment_received() { | |
$body = @file_get_contents( 'php://input' ); | |
$event_json = json_decode( $body ); | |
$now = date( 'd M Y H:i:s' ); | |
$log_content = ''; | |
$customer_id = ''; | |
if ( ! empty( $event_json->data->object->object ) && 'customer' == $event_json->data->object->object ) { | |
$customer_id = $event_json->data->object->id; | |
} elseif ( ! empty( $event_json->data->object->customer ) ) { | |
$customer_id = $event_json->data->object->customer; | |
} | |
$subscription = ProSites_Gateway_Stripe::get_subscription( $event_json ); | |
if ( isset( $subscription->blog_id ) && ! empty( $subscription->blog_id ) ) { | |
$blog_id = (int) $subscription->blog_id; | |
} | |
if ( '' == $blog_id && ! empty( $subscription ) && ! isset( $subscription->metadata->blog_id ) && ! isset( $subscription->blog_id ) ) { | |
$blog_id = ProSites_Gateway_Stripe::get_blog_id( $customer_id ); | |
} | |
$event_type = $event_json->type; | |
$plan = ! empty( $subscription->plan ) ? $subscription->plan->id : null; | |
$is_trial = ! empty( $subscription ) ? $subscription->is_trial : null; | |
$plan_end = ! empty( $subscription ) ? date( 'd-m-Y H:i:s', $subscription->period_end ) : null; | |
$plan_amount = ! empty( $subscription ) ? $subscription->plan_amount : null; | |
$amount = ! empty( $subscription ) ? $subscription->subscription_amount : null; | |
$invoice_items = ! empty( $subscription ) ? $subscription->invoice_items : null; | |
$log_content = "\n\n\n=======================\n\n\n{$now}\n\n"; | |
$log_content .= "blog_id : {$blog_id}\n"; | |
$log_content .= "customer_id : {$customer_id}\n\n"; | |
$log_content .= "event_type : {$event_type}\n\n"; | |
if ( ! is_null( $plan ) ) { | |
$log_content .= "plan : {$plan}\n"; | |
} | |
if( $is_trial ){ | |
$log_content .= "trial : YES\n"; | |
} | |
else { | |
$log_content .= "trial : NO\n"; | |
} | |
if ( ! is_null( $plan_end ) ) { | |
$log_content .= "plan_end : {$plan_end}\n"; | |
} | |
if ( ! is_null( $plan_amount ) ) { | |
$log_content .= "plan_amount : {$plan_amount}\n"; | |
} | |
if ( ! is_null( $invoice_items ) ) { | |
$log_content .= "invoice_items : {$invoice_items}\n"; | |
} | |
self::log( $subscription, 'ps-stripe-subscriptions.log' ); | |
self::log( $body, 'ps-stripe-raw.log' ); | |
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 ) ? 'ps-stripe.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_PS_Stripe_Loger'] = WPMUDEV_PS_Stripe_Loger::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment