/**
  * Process registration
  *
  * @since 4.0.0
  */
 public function process_signup()
 {
     if (empty($_POST['stripeToken'])) {
         wp_die(__('Missing Stripe token, please try again or contact support if the issue persists.', 'issuem-leaky-paywall'), __('Error', 'issuem-leaky-paywall'), array('response' => 400));
     }
     Stripe::setApiKey($this->secret_key);
     $paid = false;
     $customer_exists = false;
     $settings = get_leaky_paywall_settings();
     $mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
     $level = get_leaky_paywall_subscription_level($this->level_id);
     $cu = false;
     try {
         if (is_multisite_premium() && !empty($level['site']) && !is_main_site($level['site'])) {
             $site = '_' . $level['site'];
         } else {
             $site = '';
         }
         if (is_user_logged_in() && !is_admin()) {
             //Update the existing user
             $user_id = get_current_user_id();
             $subscriber_id = get_user_meta($user_id, '_issuem_leaky_paywall_' . $mode . '_subscriber_id' . $site, true);
         }
         if (!empty($subscriber_id)) {
             $cu = Stripe_Customer::retrieve(get_user_meta($user_id, '_issuem_leaky_paywall_' . $mode . '_subscriber_id' . $site, true));
         }
         if (empty($cu)) {
             if ($user = get_user_by('email', $this->email)) {
                 try {
                     $subscriber_id = get_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_subscriber_id' . $site, true);
                     if (!empty($subscriber_id)) {
                         $cu = Stripe_Customer::retrieve($subscriber_id);
                     } else {
                         throw new Exception(__('Unable to find valid Stripe customer ID.', 'issuem-leaky-paywall'));
                     }
                 } catch (Exception $e) {
                     $cu = false;
                 }
             }
         }
         if (!empty($cu)) {
             if (true === $cu->deleted) {
                 $cu = array();
             } else {
                 $existing_customer = true;
             }
         }
         $customer_array = array('email' => $this->email, 'source' => $_POST['stripeToken'], 'description' => $this->level_name);
         $customer_array = apply_filters('leaky_paywall_process_stripe_payment_customer_array', $customer_array);
         // recurring subscription
         if (!empty($this->recurring) && 'on' === $this->recurring && !empty($this->plan_id)) {
             $customer_array['plan'] = $this->plan_id;
             if (!empty($cu)) {
                 $subscriptions = $cu->subscriptions->all('limit=1');
                 if (!empty($subscriptions->data)) {
                     foreach ($subscriptions->data as $subscription) {
                         $sub = $cu->subscriptions->retrieve($subscription->id);
                         $sub->plan = $this->plan_id;
                         $sub->save();
                     }
                 } else {
                     $cu->subscriptions->create(array('plan' => $this->plan_id));
                 }
             } else {
                 // new customer, and this will charge them?
                 $cu = Stripe_Customer::create($customer_array);
             }
         } else {
             // Create a Customer
             if (empty($cu)) {
                 $cu = Stripe_Customer::create($customer_array);
             } else {
                 $cu->sources->create(array('source' => $_POST['stripeToken']));
             }
             $charge_array = array('customer' => $cu->id, 'amount' => number_format($this->amount, 2, '', ''), 'currency' => apply_filters('leaky_paywall_stripe_currency', strtolower($this->currency)), 'description' => $this->level_name);
             $charge = Stripe_Charge::create($charge_array);
         }
     } catch (Exception $e) {
         echo '<pre>';
         print_r($e);
         echo '</pre>';
         die('stripe error');
         return new WP_Error('broke', sprintf(__('Error processing request: %s', 'issuem-leaky-paywall'), $e->getMessage()));
     }
     $customer_id = $cu->id;
     $meta_args = array('level_id' => $this->level_id, 'subscriber_id' => $customer_id, 'subscriber_email' => $this->email, 'price' => $this->level_price, 'description' => $this->level_name, 'payment_gateway' => 'stripe', 'payment_status' => 'active', 'interval' => $this->length_unit, 'interval_count' => $this->length, 'site' => !empty($level['site']) ? $level['site'] : '', 'plan' => !empty($customer_array['plan']) ? $customer_array['plan'] : '');
     if (is_user_logged_in() || !empty($existing_customer)) {
         $user_id = leaky_paywall_update_subscriber(NULL, $this->email, $customer_id, $meta_args);
         //if the email already exists, we want to update the subscriber, not create a new one
     } else {
         // create the new customer as a leaky paywall subscriber
         $user_id = leaky_paywall_new_subscriber(NULL, $this->email, $customer_id, $meta_args);
     }
     if ($user_id) {
         do_action('leaky_paywall_stripe_signup', $user_id);
         // log the user in
         wp_set_current_user($user_id);
         wp_set_auth_cookie($user_id, true);
         // redirect user after sign up
         if (!empty($settings['page_for_after_subscribe'])) {
             wp_safe_redirect(get_page_link($settings['page_for_after_subscribe']));
         } else {
             if (!empty($settings['page_for_profile'])) {
                 wp_safe_redirect(get_page_link($settings['page_for_profile']));
             } else {
                 if (!empty($settings['page_for_subscription'])) {
                     wp_safe_redirect(get_page_link($settings['page_for_subscription']));
                 }
             }
         }
         exit;
     } else {
         wp_die(__('An error occurred, please contact the site administrator: ', 'leaky-paywall') . get_bloginfo('admin_email'), __('Error', 'leaky-paywall'), array('response' => '401'));
     }
 }
 /**
  * Process PayPal IPN. This is also where Paypal Subscribe buttons are processed.
  * 
  * @since 4.0.0
  */
 public function process_webhooks()
 {
     if (!isset($_GET['listener']) || strtoupper($_GET['listener']) != 'IPN') {
         return;
     }
     $site = '';
     $mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
     $settings = get_leaky_paywall_settings();
     $payload['cmd'] = '_notify-validate';
     foreach ($_POST as $key => $value) {
         $payload[$key] = stripslashes($value);
     }
     $paypal_api_url = !empty($_REQUEST['test_ipn']) ? PAYPAL_PAYMENT_SANDBOX_URL : PAYPAL_PAYMENT_LIVE_URL;
     $response = wp_remote_post($paypal_api_url, array('body' => $payload, 'httpversion' => '1.1'));
     $body = wp_remote_retrieve_body($response);
     if ('VERIFIED' === $body) {
         if (!empty($_REQUEST['txn_type'])) {
             $args = array('level_id' => isset($_REQUEST['item_number']) ? $_REQUEST['item_number'] : $_REQUEST['custom'], 'description' => $_REQUEST['item_name'], 'payment_gateway' => 'paypal_standard');
             $level = get_leaky_paywall_subscription_level($args['level_id']);
             $args['interval'] = $level['interval'];
             $args['interval_count'] = $level['interval_count'];
             if (is_multisite_premium() && !empty($level['site']) && !is_main_site($level['site'])) {
                 $site = '_' . $level['site'];
             } else {
                 $site = '';
             }
             switch ($_REQUEST['txn_type']) {
                 case 'web_accept':
                     if (isset($_REQUEST['mc_gross'])) {
                         //subscr_payment
                         $args['price'] = $_REQUEST['mc_gross'];
                     } else {
                         if (isset($_REQUEST['payment_gross'])) {
                             //subscr_payment
                             $args['price'] = $_REQUEST['payment_gross'];
                         }
                     }
                     if (isset($_REQUEST['txn_id'])) {
                         //subscr_payment
                         $args['subscr_id'] = $_REQUEST['txn_id'];
                     }
                     $args['plan'] = '';
                     if ('completed' === strtolower($_REQUEST['payment_status'])) {
                         $args['payment_status'] = 'active';
                     } else {
                         $args['payment_status'] = 'deactivated';
                     }
                     break;
                 case 'subscr_signup':
                     if (isset($_REQUEST['mc_amount3'])) {
                         //subscr_payment
                         $args['price'] = $_REQUEST['mc_amount3'];
                     } else {
                         if (isset($_REQUEST['amount3'])) {
                             //subscr_payment
                             $args['price'] = $_REQUEST['amount3'];
                         }
                     }
                     if (isset($_REQUEST['subscr_id'])) {
                         //subscr_payment
                         $args['subscr_id'] = $_REQUEST['subscr_id'];
                     }
                     if (isset($_REQUEST['period3'])) {
                         $args['plan'] = $_REQUEST['period3'];
                         $new_expiration = date_i18n('Y-m-d 23:59:59', strtotime('+' . str_replace(array('D', 'W', 'M', 'Y'), array('Days', 'Weeks', 'Months', 'Years'), $args['plan']), strtotime($_REQUEST['subscr_date'])));
                         $args['expires'] = $new_expiration;
                     }
                     $args['payment_status'] = 'active';
                     //It's a signup, of course it's active!
                     break;
                 case 'subscr_payment':
                     if (isset($_REQUEST['mc_gross'])) {
                         //subscr_payment
                         $args['price'] = $_REQUEST['mc_gross'];
                     } else {
                         if (isset($_REQUEST['payment_gross'])) {
                             //subscr_payment
                             $args['price'] = $_REQUEST['payment_gross'];
                         }
                     }
                     if (!empty($_REQUEST['subscr_id'])) {
                         //subscr_payment
                         $args['subscr_id'] = $_REQUEST['subscr_id'];
                     }
                     if ('completed' === strtolower($_REQUEST['payment_status'])) {
                         $args['payment_status'] = 'active';
                     } else {
                         $args['payment_status'] = 'deactivated';
                     }
                     $user = get_leaky_paywall_subscriber_by_subscriber_id($args['subscr_id'], $mode);
                     if (is_multisite_premium()) {
                         if ($site_id = get_leaky_paywall_subscribers_site_id_by_subscriber_id($_REQUEST['subscr_id'])) {
                             $site = '_' . $site_id;
                         }
                     }
                     if (!empty($user) && 0 !== $user->ID && ($plan = get_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_plan' . $site, true)) && 'completed' === strtolower($_REQUEST['payment_status'])) {
                         $args['plan'] = $plan;
                         $new_expiration = date_i18n('Y-m-d 23:59:59', strtotime('+' . str_replace(array('D', 'W', 'M', 'Y'), array('Days', 'Weeks', 'Months', 'Years'), $plan), strtotime($_REQUEST['payment_date'])));
                         $args['expires'] = $new_expiration;
                     } else {
                         $args['plan'] = $level['interval_count'] . ' ' . strtoupper(substr($level['interval'], 0, 1));
                         $new_expiration = date_i18n('Y-m-d 23:59:59', strtotime('+' . str_replace(array('D', 'W', 'M', 'Y'), array('Days', 'Weeks', 'Months', 'Years'), $args['plan']), strtotime($_REQUEST['payment_date'])));
                         $args['expires'] = $new_expiration;
                     }
                     break;
                 case 'subscr_cancel':
                     if (isset($_REQUEST['subscr_id'])) {
                         //subscr_payment
                         $user = get_leaky_paywall_subscriber_by_subscriber_id($_REQUEST['subscr_id'], $mode);
                         if (is_multisite_premium()) {
                             if ($site_id = get_leaky_paywall_subscribers_site_id_by_subscriber_id($_REQUEST['subscr_id'])) {
                                 $site = '_' . $site_id;
                             }
                         }
                         if (!empty($user) && 0 !== $user->ID) {
                             update_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_status' . $site, 'canceled');
                         }
                     }
                     return true;
                     //We don't need to process anymore
                 //We don't need to process anymore
                 case 'subscr_eot':
                     if (isset($_REQUEST['subscr_id'])) {
                         //subscr_payment
                         $user = get_leaky_paywall_subscriber_by_subscriber_id($_REQUEST['subscr_id'], $mode);
                         if (is_multisite_premium()) {
                             if ($site_id = get_leaky_paywall_subscribers_site_id_by_subscriber_id($_REQUEST['subscr_id'])) {
                                 $site = '_' . $site_id;
                             }
                         }
                         if (!empty($user) && 0 !== $user->ID) {
                             update_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_status' . $site, 'expired');
                         }
                     }
                     return true;
                     //We don't need to process anymore
                 //We don't need to process anymore
                 case 'recurring_payment_suspended_due_to_max_failed_payment':
                     if (isset($_REQUEST['recurring_payment_id'])) {
                         //subscr_payment
                         $user = get_leaky_paywall_subscriber_by_subscriber_id($args['recurring_payment_id'], $mode);
                         if (is_multisite_premium()) {
                             if ($site_id = get_leaky_paywall_subscribers_site_id_by_subscriber_id($_REQUEST['recurring_payment_id'])) {
                                 $site = '_' . $site_id;
                             }
                         }
                         if (!empty($user) && 0 !== $user->ID) {
                             update_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_status' . $site, 'deactivated');
                         }
                     }
                     return true;
                     //We don't need to process anymore
                 //We don't need to process anymore
                 case 'recurring_payment_suspended':
                     if (isset($_REQUEST['subscr_id'])) {
                         //subscr_payment
                         $user = get_leaky_paywall_subscriber_by_subscriber_id($_REQUEST['subscr_id'], $mode);
                         if (is_multisite_premium()) {
                             if ($site_id = get_leaky_paywall_subscribers_site_id_by_subscriber_id($_REQUEST['subscr_id'])) {
                                 $site = '_' . $site_id;
                             }
                         }
                         if (!empty($user) && 0 !== $user->ID) {
                             update_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_status' . $site, 'suspended');
                         }
                     } else {
                         if (isset($_REQUEST['recurring_payment_id'])) {
                             //subscr_payment
                             $user = get_leaky_paywall_subscriber_by_subscriber_id($args['recurring_payment_id'], $mode);
                             if (is_multisite_premium()) {
                                 if ($site_id = get_leaky_paywall_subscribers_site_id_by_subscriber_id($_REQUEST['recurring_payment_id'])) {
                                     $site = '_' . $site_id;
                                 }
                             }
                             if (!empty($user) && 0 !== $user->ID) {
                                 update_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_status' . $site, 'suspended');
                             }
                         }
                     }
                     return true;
                     //We don't need to process anymore
             }
             if (!empty($_REQUEST['custom']) && is_email($_REQUEST['custom'])) {
                 $user = get_user_by('email', $_REQUEST['custom']);
                 if (empty($user)) {
                     $user = get_leaky_paywall_subscriber_by_subscriber_email($_REQUEST['custom'], $mode);
                     if (is_multisite_premium()) {
                         if ($site_id = get_leaky_paywall_subscribers_site_id_by_subscriber_email($_REQUEST['custom'])) {
                             $args['site'] = $site_id;
                         }
                     }
                 }
             }
             if (empty($user) && !empty($_REQUEST['payer_email']) && is_email($_REQUEST['payer_email'])) {
                 $user = get_user_by('email', $_REQUEST['payer_email']);
                 if (empty($user)) {
                     $user = get_leaky_paywall_subscriber_by_subscriber_email($_REQUEST['payer_email'], $mode);
                     if (is_multisite_premium()) {
                         if ($site_id = get_leaky_paywall_subscribers_site_id_by_subscriber_email($_REQUEST['payer_email'])) {
                             $args['site'] = $site_id;
                         }
                     }
                 }
             }
             if (empty($user) && !empty($_REQUEST['txn_id'])) {
                 $user = get_leaky_paywall_subscriber_by_subscriber_id($_REQUEST['txn_id'], $mode);
                 if (is_multisite_premium()) {
                     if ($site_id = get_leaky_paywall_subscribers_site_id_by_subscriber_id($_REQUEST['txn_id'])) {
                         $args['site'] = $site_id;
                     }
                 }
             }
             if (empty($user) && !empty($_REQUEST['subscr_id'])) {
                 $user = get_leaky_paywall_subscriber_by_subscriber_id($_REQUEST['subscr_id'], $mode);
                 if (is_multisite_premium()) {
                     if ($site_id = get_leaky_paywall_subscribers_site_id_by_subscriber_id($_REQUEST['subscr_id'])) {
                         $args['site'] = $site_id;
                     }
                 }
             }
             if (!empty($user)) {
                 //WordPress user exists
                 $args['subscriber_email'] = $user->user_email;
                 leaky_paywall_update_subscriber(NULL, $args['subscriber_email'], $args['subscr_id'], $args);
             } else {
                 //Need to create a new user
                 $args['subscriber_email'] = is_email($_REQUEST['custom']) ? $_REQUEST['custom'] : $_REQUEST['payer_email'];
                 leaky_paywall_new_subscriber(NULL, $args['subscriber_email'], $args['subscr_id'], $args);
             }
         }
     } else {
         error_log(sprintf(__('Invalid IPN sent from PayPal: %s', 'issuem-leaky-paywall'), maybe_serialize($payload)));
     }
     return true;
 }