/**
  * Get the prorate credit amount for the user's remaining subscription
  *
  * @since 2.5
  * @return int
  */
 public function get_prorate_credit_amount()
 {
     // make sure this is an active, paying subscriber
     if (!$this->is_active()) {
         return 0;
     }
     if (apply_filters('rcp_disable_prorate_credit', false, $this)) {
         return 0;
     }
     // get the most recent payment
     foreach ($this->get_payments() as $pmt) {
         if ('complete' != $pmt->status) {
             continue;
         }
         $payment = $pmt;
         break;
     }
     if (empty($payment)) {
         return 0;
     }
     $subscription = rcp_get_subscription_details_by_name($payment->subscription);
     $subscription_id = $this->get_subscription_id();
     // make sure the subscription payment matches the existing subscription
     if (empty($subscription->id) || empty($subscription->duration) || $subscription->id != $subscription_id) {
         return 0;
     }
     $exp_date = $this->get_expiration_date();
     // if this is member does not have an expiration date, calculate it
     if ('none' == $exp_date) {
         return 0;
     }
     // make sure we have a valid date
     if (!($exp_date = strtotime($exp_date))) {
         return 0;
     }
     $exp_date_dt = date('Y-m-d', $exp_date) . ' 23:59:59';
     $exp_date = strtotime($exp_date_dt, current_time('timestamp'));
     $time_remaining = $exp_date - current_time('timestamp');
     // Calculate the start date based on the expiration date
     if (!($start_date = strtotime($exp_date_dt . ' -' . $subscription->duration . $subscription->duration_unit, current_time('timestamp')))) {
         return 0;
     }
     $total_time = $exp_date - $start_date;
     if ($time_remaining <= 0) {
         return 0;
     }
     // calculate discount as percentage of subscription remaining
     // use the previous payment amount
     if ($subscription->fee > 0) {
         $payment->amount -= $subscription->fee;
     }
     $payment_amount = abs($payment->amount);
     $percentage_remaining = $time_remaining / $total_time;
     // make sure we don't credit more than 100%
     if ($percentage_remaining > 1) {
         $percentage_remaining = 1;
     }
     $discount = round($payment_amount * $percentage_remaining, 2);
     // make sure they get a discount. This shouldn't ever run
     if (!$discount > 0) {
         $discount = $payment_amount;
     }
     return apply_filters('rcp_member_prorate_credit', floatval($discount), $this->ID, $this);
 }
 /**
  * Create plan in Stripe
  *
  * @since 2.1
  * @return bool
  */
 private function create_plan($plan_name = '')
 {
     global $rcp_options;
     // get all subscription level info for this plan
     $plan = rcp_get_subscription_details_by_name($plan_name);
     $price = $plan->price * 100;
     $interval = $plan->duration_unit;
     $interval_count = $plan->duration;
     $name = $plan->name;
     $plan_id = strtolower(str_replace(' ', '', $plan_name));
     $currency = strtolower($rcp_options['currency']);
     \Stripe\Stripe::setApiKey($this->secret_key);
     try {
         \Stripe\Plan::create(array("amount" => $price, "interval" => $interval, "interval_count" => $interval_count, "name" => $name, "currency" => $currency, "id" => $plan_id));
         // plann successfully created
         return true;
     } catch (Exception $e) {
         // there was a problem
         return false;
     }
 }
 /**
  * Determine if a plan exists
  *
  * @since 2.1
  * @param $plan | The name of the plan to check
  * @return bool | string false if the plan doesn't exist, plan id if it does
  */
 private function plan_exists($plan)
 {
     \Stripe\Stripe::setApiKey($this->secret_key);
     if (!($plan = rcp_get_subscription_details_by_name($plan))) {
         return false;
     }
     // fallback to old plan id if the new plan id does not exist
     $old_plan_id = strtolower(str_replace(' ', '', $plan->name));
     $new_plan_id = sprintf('%s-%s-%s', $old_plan_id, $plan->price, $plan->duration . $plan->duration_unit);
     // check if the plan new plan id structure exists
     try {
         $plan = \Stripe\Plan::retrieve($new_plan_id);
         return $plan->id;
     } catch (Exception $e) {
     }
     try {
         // fall back to the old plan id structure and verify that the plan metadata also matches
         $stripe_plan = \Stripe\Plan::retrieve($old_plan_id);
         if ((int) $stripe_plan->amount !== (int) $plan->price * 100) {
             return false;
         }
         if ($stripe_plan->interval !== $plan->duration_unit) {
             return false;
         }
         if ($stripe_plan->interval_count !== intval($plan->duration)) {
             return false;
         }
         return $old_plan_id;
     } catch (Exception $e) {
         return false;
     }
 }
示例#4
0
function rcp_check_ipn()
{
    global $rcp_options;
    if (!class_exists('IpnListener')) {
        // instantiate the IpnListener class
        include RCP_PLUGIN_DIR . 'includes/gateways/paypal/ipnlistener.php';
    }
    $listener = new IpnListener();
    if (isset($rcp_options['sandbox'])) {
        $listener->use_sandbox = true;
    }
    if (isset($rcp_options['ssl'])) {
        $listener->use_ssl = true;
    } else {
        $listener->use_ssl = false;
    }
    //To post using the fsockopen() function rather than cURL, use:
    if (isset($rcp_options['disable_curl'])) {
        $listener->use_curl = false;
    }
    try {
        $listener->requirePostMethod();
        $verified = $listener->processIpn();
    } catch (Exception $e) {
        //exit(0);
    }
    /*
    The processIpn() method returned true if the IPN was "VERIFIED" and false if it
    was "INVALID".
    */
    if ($verified || isset($_POST['verification_override']) || (isset($rcp_options['sandbox']) || isset($rcp_options['disable_ipn_verify']))) {
        $posted = apply_filters('rcp_ipn_post', $_POST);
        // allow $_POST to be modified
        $user_id = $posted['custom'];
        $subscription_name = $posted['item_name'];
        $subscription_key = $posted['item_number'];
        $amount = number_format((double) $posted['mc_gross'], 2);
        $amount2 = number_format((double) $posted['mc_amount3'], 2);
        $payment_status = $posted['payment_status'];
        $currency_code = $posted['mc_currency'];
        $subscription_id = rcp_get_subscription_id($user_id);
        $subscription_price = number_format((double) rcp_get_subscription_price(rcp_get_subscription_id($user_id)), 2);
        $user_data = get_userdata($user_id);
        if (!$user_data || !$subscription_id) {
            return;
        }
        if (!rcp_get_subscription_details($subscription_id)) {
            return;
        }
        // setup the payment info in an array for storage
        $payment_data = array('date' => date('Y-m-d g:i:s', strtotime($posted['payment_date'])), 'subscription' => $posted['item_name'], 'payment_type' => $posted['txn_type'], 'subscription_key' => $subscription_key, 'amount' => $amount, 'user_id' => $user_id, 'transaction_id' => $posted['txn_id']);
        do_action('rcp_valid_ipn', $payment_data, $user_id, $posted);
        if ($posted['txn_type'] == 'web_accept' || $posted['txn_type'] == 'subscr_payment') {
            // only check for an existing payment if this is a payment IPD request
            if (rcp_check_for_existing_payment($posted['txn_type'], $posted['payment_date'], $subscription_key)) {
                $log_data = array('post_title' => __('Duplicate Payment', 'rcp'), 'post_content' => __('A duplicate payment was detected. The new payment was still recorded, so you may want to check into both payments.', 'rcp'), 'post_parent' => 0, 'log_type' => 'gateway_error');
                $log_meta = array('user_subscription' => $posted['item_name'], 'user_id' => $user_id);
                $log_entry = WP_Logging::insert_log($log_data, $log_meta);
                return;
                // this IPN request has already been processed
            }
            /* do some quick checks to make sure all necessary data validates */
            if ($amount < $subscription_price && $amount2 < $subscription_price) {
                /*
                				// the subscription price doesn't match, so lets check to see if it matches with a discount code
                				if( ! rcp_check_paypal_return_price_after_discount( $subscription_price, $amount, $amount2, $user_id ) ) {
                	$log_data = array(
                					    'post_title'    => __( 'Price Mismatch', 'rcp' ),
                					    'post_content'  =>  sprintf( __( 'The price in an IPN request did not match the subscription price. Payment data: %s', 'rcp' ), json_encode( $payment_data ) ),
                					    'post_parent'   => 0,
                					    'log_type'      => 'gateway_error'
                					);
                	$log_meta = array(
                					    'user_subscription' => $posted['item_name'],
                					    'user_id'           => $user_id
                					);
                					$log_entry = WP_Logging::insert_log( $log_data, $log_meta );
                	//return;
                				}
                */
            }
            if (strtolower($currency_code) != strtolower($rcp_options['currency'])) {
                // the currency code is invalid
                $log_data = array('post_title' => __('Invalid Currency Code', 'rcp'), 'post_content' => sprintf(__('The currency code in an IPN request did not match the site currency code. Payment data: %s', 'rcp'), json_encode($payment_data)), 'post_parent' => 0, 'log_type' => 'gateway_error');
                $log_meta = array('user_subscription' => $posted['item_name'], 'user_id' => $user_id);
                $log_entry = WP_Logging::insert_log($log_data, $log_meta);
                return;
            }
        }
        if (isset($rcp_options['email_ipn_reports'])) {
            wp_mail(get_bloginfo('admin_email'), __('IPN report', 'rcp'), $listener->getTextReport());
        }
        if (rcp_get_subscription_key($user_id) != $subscription_key) {
            // the subscription key is invalid
            $log_data = array('post_title' => __('Subscription Key Mismatch', 'rcp'), 'post_content' => sprintf(__('The subscription key in an IPN request did not match the subscription key recorded for the user. Payment data: %s', 'rcp'), json_encode($payment_data)), 'post_parent' => 0, 'log_type' => 'gateway_error');
            $log_meta = array('user_subscription' => $posted['item_name'], 'user_id' => $user_id);
            $log_entry = WP_Logging::insert_log($log_data, $log_meta);
            return;
        }
        /* now process the kind of subscription/payment */
        $rcp_payments = new RCP_Payments();
        // Subscriptions
        switch ($posted['txn_type']) {
            case "subscr_signup":
                // when a new user signs up
                // store the recurring payment ID
                update_user_meta($user_id, 'rcp_paypal_subscriber', $posted['payer_id']);
                // set the user's status to active
                rcp_set_status($user_id, 'active');
                if (!isset($rcp_options['disable_new_user_notices'])) {
                    wp_new_user_notification($user_id);
                }
                // send welcome email
                rcp_email_subscription_status($user_id, 'active');
                update_user_meta($user_id, 'rcp_recurring', 'yes');
                do_action('rcp_ipn_subscr_signup', $user_id);
                break;
            case "subscr_payment":
                // when a user makes a recurring payment
                // record this payment in the database
                $rcp_payments->insert($payment_data);
                $subscription = rcp_get_subscription_details(rcp_get_subscription_id($user_id));
                // update the user's expiration to correspond with the new payment
                $member_new_expiration = date('Y-m-d H:i:s', strtotime('+' . $subscription->duration . ' ' . $subscription->duration_unit . ' 23:59:59'));
                rcp_set_expiration_date($user_id, $member_new_expiration);
                update_user_meta($user_id, 'rcp_paypal_subscriber', $posted['payer_id']);
                // make sure the user's status is active
                rcp_set_status($user_id, 'active');
                update_user_meta($user_id, 'rcp_recurring', 'yes');
                delete_user_meta($user_id, '_rcp_expired_email_sent');
                do_action('rcp_ipn_subscr_payment', $user_id);
                break;
            case "subscr_cancel":
                // user is marked as cancelled but retains access until end of term
                rcp_set_status($user_id, 'cancelled');
                // set the use to no longer be recurring
                delete_user_meta($user_id, 'rcp_recurring');
                delete_user_meta($user_id, 'rcp_paypal_subscriber');
                // send sub cancelled email
                rcp_email_subscription_status($user_id, 'cancelled');
                do_action('rcp_ipn_subscr_cancel', $user_id);
                break;
            case "subscr_failed":
                do_action('rcp_ipn_subscr_failed');
                break;
            case "subscr_eot":
                // user's subscription has reach the end of its term
                // set the use to no longer be recurring
                delete_user_meta($user_id, 'rcp_recurring');
                if ('cancelled' !== rcp_get_status($user_id)) {
                    rcp_set_status($user_id, 'expired');
                    // send expired email
                    rcp_email_subscription_status($user_id, 'expired');
                }
                do_action('rcp_ipn_subscr_eot', $user_id);
                break;
            case "cart":
                return;
                // get out of here
            // get out of here
            case "express_checkout":
                return;
                // get out of here
            // get out of here
            case "web_accept":
                switch (strtolower($payment_status)) {
                    case 'completed':
                        if (isset($_POST['verification_override'])) {
                            // this is a method for providing a new expiration if it doesn't exist
                            $subscription = rcp_get_subscription_details_by_name($payment_data['subscription']);
                            // update the user's expiration to correspond with the new payment
                            $member_new_expiration = date('Y-m-d H:i:s', strtotime('+' . $subscription->duration . ' ' . $subscription->duration_unit . ' 23:59:59'));
                            rcp_set_expiration_date($user_id, $member_new_expiration);
                        }
                        // set this user to active
                        rcp_set_status($user_id, 'active');
                        $rcp_payments->insert($payment_data);
                        rcp_email_subscription_status($user_id, 'active');
                        if (!isset($rcp_options['disable_new_user_notices'])) {
                            // send welcome email here
                            wp_new_user_notification($user_id);
                        }
                        delete_user_meta($user_id, '_rcp_expired_email_sent');
                        break;
                    case 'denied':
                    case 'expired':
                    case 'failed':
                    case 'voided':
                        rcp_set_status($user_id, 'cancelled');
                        // send cancelled email here
                        break;
                }
                break;
            default:
                break;
        }
    } else {
        if (isset($rcp_options['email_ipn_reports'])) {
            // an invalid IPN attempt was made. Send an email to the admin account to investigate
            wp_mail(get_bloginfo('admin_email'), __('Invalid IPN', 'rcp'), $listener->getTextReport());
        }
    }
}