function pmprodev_checkout_debug_email($level)
{
    global $pmprodev_options, $current_user, $wpdb;
    if (empty($pmprodev_options['checkout_debug_email'])) {
        return $level;
    }
    $email = new PMProEmail();
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
        $http = 'https://';
    } else {
        $http = 'http://';
    }
    $email->subject = sprintf('%s Checkout Page Debug Log', get_bloginfo('name'));
    $email->recipient = $pmprodev_options['checkout_debug_email'];
    $email->template = 'checkout_debug';
    $email->body = file_get_contents(plugin_dir_path(__FILE__) . '/email/checkout_debug.html');
    $email->data = array('sitename' => get_bloginfo('sitename'), 'checkout_url' => $http . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 'submit' => print_r($_REQUEST['submit-checkout'], true), 'level' => print_r($level, true), 'user' => print_r($current_user->data, true), 'request' => print_r($_REQUEST, true));
    $order = new MemberOrder();
    $order->getLastMemberOrder($current_user->user_id);
    if (!empty($order)) {
        $email->data['order'] = print_r($order, true);
    }
    $email->sendEmail();
    return $level;
}
function pmproeewe_extra_emails()
{
    global $wpdb;
    //make sure we only run once a day
    $today = date("Y-m-d 00:00:00");
    /*
    	Here is where you set how many emails you want to send, how early, and which template files to e-mail.
    	If you set the template file to an empty string '' then it will send the default PMPro expiring e-mail.
    	Place your email templates in a subfolder of your active theme. Create a paid-memberships-pro folder in your theme folder,
    	and then create an email folder within that. Your template files should have a suffix of .html, but you don't put it below. So if you
    	create a file in there called myexpirationemail.html, then you'd just put 'myexpirationemail' in the array below.
    	(PMPro will fill in the .html for you.)
    */
    $emails = array(30 => 'mem_expiring_30days', 60 => 'mem_expiring_60days', 90 => 'mem_expiring_90days');
    //<--- !!! UPDATE THIS ARRAY TO CHANGE WHEN EMAILS GO OUT AND THEIR TEMPLATE FILES !!! -->
    ksort($emails, SORT_NUMERIC);
    //array to store ids of folks we sent emails to so we don't email them twice
    $sent_emails = array();
    foreach (array_keys($emails) as $days) {
        //look for memberships that are going to expire within one week (but we haven't emailed them within a week)
        $sqlQuery = "SELECT mu.user_id, mu.membership_id, mu.startdate, mu.enddate FROM {$wpdb->pmpro_memberships_users} mu LEFT JOIN {$wpdb->usermeta} um ON um.user_id = mu.user_id AND um.meta_key = 'pmpro_expiration_notice_" . $days . "' WHERE mu.status = 'active' AND mu.enddate IS NOT NULL AND mu.enddate <> '' AND mu.enddate <> '0000-00-00 00:00:00' AND DATE_SUB(mu.enddate, INTERVAL " . $days . " Day) <= '" . $today . "' AND (um.meta_value IS NULL OR DATE_ADD(um.meta_value, INTERVAL " . $days . " Day) <= '" . $today . "') ORDER BY mu.enddate";
        $expiring_soon = $wpdb->get_results($sqlQuery);
        foreach ($expiring_soon as $e) {
            if (!in_array($e->user_id, $sent_emails)) {
                //send an email
                $pmproemail = new PMProEmail();
                $euser = get_userdata($e->user_id);
                if ($euser) {
                    $euser->membership_level = pmpro_getMembershipLevelForUser($euser->ID);
                    $pmproemail->email = $euser->user_email;
                    $pmproemail->subject = sprintf(__("Your membership at %s will end soon", "pmpro"), get_option("blogname"));
                    if (strlen($emails[$days]) > 0) {
                        $pmproemail->template = $emails[$days];
                    } else {
                        $pmproemail->template = "membership_expiring";
                    }
                    $pmproemail->data = array("subject" => $pmproemail->subject, "name" => $euser->display_name, "user_login" => $euser->user_login, "sitename" => get_option("blogname"), "membership_id" => $euser->membership_level->id, "membership_level_name" => $euser->membership_level->name, "siteemail" => pmpro_getOption("from_email"), "login_link" => wp_login_url(), "enddate" => date(get_option('date_format'), $euser->membership_level->enddate), "display_name" => $euser->display_name, "user_email" => $euser->user_email);
                    $pmproemail->sendEmail();
                    printf(__("Membership expiring email sent to %s. ", "pmpro"), $euser->user_email);
                    $sent_emails[] = $e->user_id;
                }
            }
            //update user meta so we don't email them again
            update_user_meta($e->user_id, "pmpro_expiration_notice_" . $days, $today);
        }
    }
}
function pmprosl_pmpro_default_registration_level($user_id)
{
    global $pmpro_level;
    //if default is set and we're not otherwise checking out
    $default_level = get_option('pmpro_social_login_default_level');
    if (!empty($default_level) && empty($pmpro_level) && empty($_REQUEST['level'])) {
        pmpro_changeMembershipLevel($default_level, $user_id);
        $user = get_userdata($user_id);
        $user->membership_level = pmpro_getMembershipLevelForUser($user->ID);
        //send email to member
        $pmproemail = new PMProEmail();
        $pmproemail->sendCheckoutEmail($user, false);
        //send email to admin
        $pmproemail = new PMProEmail();
        $pmproemail->sendCheckoutAdminEmail($user, false);
    }
}
function pmpro_delete_user($user_id = NULL)
{
    global $wpdb;
    //changing their membership level to 0 will cancel any subscription and remove their membership level entry
    //we don't remove the orders because it would affect reporting
    if (pmpro_changeMembershipLevel(0, $user_id)) {
        //okay
    } else {
        //couldn't delete the subscription
        //we should probably notify the admin
        global $pmpro_error;
        if (!empty($pmpro_error)) {
            $pmproemail = new PMProEmail();
            $pmproemail->data = array("body" => "<p>" . sprintf(__("There was an error canceling the subscription for user with ID=%s. You will want to check your payment gateway to see if their subscription is still active.", "pmpro"), strval($user_id)) . "</p><p>Error: " . $pmpro_error . "</p>");
            $last_order = $wpdb->get_row("SELECT * FROM {$wpdb->pmpro_membership_orders} WHERE user_id = '" . $user_id . "' ORDER BY timestamp DESC LIMIT 1");
            if (!empty($last_order)) {
                $pmproemail->data["body"] .= "<p>Last Invoice:<br />" . nl2br(var_export($last_order, true)) . "</p>";
            }
            $pmproemail->sendEmail(get_bloginfo("admin_email"));
        }
    }
}
function pmpro_cron_trial_ending_warnings()
{
    global $wpdb;
    //make sure we only run once a day
    $today = date("Y-m-d 00:00:00");
    $pmpro_email_days_before_trial_end = apply_filters("pmpro_email_days_before_trial_end", 7);
    //look for memberships with trials ending soon (but we haven't emailed them within a week)
    $sqlQuery = "\n\t\tSELECT \n\t\t\tmu.user_id, mu.membership_id, mu.startdate, mu.cycle_period, mu.trial_limit FROM {$wpdb->pmpro_memberships_users} mu LEFT JOIN {$wpdb->usermeta} um ON um.user_id = mu.user_id AND um.meta_key = 'pmpro_trial_ending_notice' \n\t\tWHERE \n\t\t\tmu.status = 'active' AND mu.trial_limit IS NOT NULL AND mu.trial_limit > 0 AND\n\t\t\t(\n\t\t\t\t(cycle_period = 'Day' AND DATE_ADD(mu.startdate, INTERVAL mu.trial_limit Day) <= DATE_ADD('" . $today . "', INTERVAL " . $pmpro_email_days_before_trial_end . " Day)) OR\n\t\t\t\t(cycle_period = 'Week' AND DATE_ADD(mu.startdate, INTERVAL mu.trial_limit Week) <= DATE_ADD('" . $today . "', INTERVAL " . $pmpro_email_days_before_trial_end . " Day)) OR\n\t\t\t\t(cycle_period = 'Month' AND DATE_ADD(mu.startdate, INTERVAL mu.trial_limit Month) <= DATE_ADD('" . $today . "', INTERVAL " . $pmpro_email_days_before_trial_end . " Day)) OR\n\t\t\t\t(cycle_period = 'Year' AND DATE_ADD(mu.startdate, INTERVAL mu.trial_limit Year) <= DATE_ADD('" . $today . "', INTERVAL " . $pmpro_email_days_before_trial_end . " Day)) \n\t\t\t)\t\t\n\t\t\t\t\t\t\n\t\t\tAND (um.meta_value IS NULL OR um.meta_value = '' OR DATE_ADD(um.meta_value, INTERVAL " . $pmpro_email_days_before_trial_end . " Day) <= '" . $today . "') \n\t\tORDER BY mu.startdate";
    $trial_ending_soon = $wpdb->get_results($sqlQuery);
    foreach ($trial_ending_soon as $e) {
        $send_email = apply_filters("pmpro_send_trial_ending_email", true, $e->user_id);
        if ($send_email) {
            //send an email
            $pmproemail = new PMProEmail();
            $euser = get_userdata($e->user_id);
            $pmproemail->sendTrialEndingEmail($euser);
            printf(__("Trial ending email sent to %s. ", "pmpro"), $euser->user_email);
        }
        //update user meta so we don't email them again
        update_user_meta($euser->ID, "pmpro_trial_ending_notice", $today);
    }
}
function pmpro_insSaveOrder($txnref, $last_order)
{
    global $wpdb;
    //check that txn_id has not been previously processed
    $old_txn = $wpdb->get_var("SELECT payment_transaction_id FROM {$wpdb->pmpro_membership_orders} WHERE payment_transaction_id = '" . $txnref . "' LIMIT 1");
    if (empty($old_txn)) {
        //hook for successful subscription payments
        do_action("pmpro_subscription_payment_completed");
        //save order
        $morder = new MemberOrder();
        $morder->user_id = $last_order->user_id;
        $morder->membership_id = $last_order->membership_id;
        $morder->payment_transaction_id = $txnref;
        $morder->subscription_transaction_id = $last_order->subscription_transaction_id;
        $morder->InitialPayment = $last_order->InitialPayment;
        //$_POST['item_list_amount_1'];	//not the initial payment, but the class is expecting that
        $morder->PaymentAmount = $last_order->PaymentAmount;
        //$_POST['item_list_amount_1'];
        $morder->gateway = $last_order->gateway;
        $morder->gateway_environment = $last_order->gateway_environment;
        //save
        $morder->saveOrder();
        $pmproemail = new PMProEmail();
        $pmproemail->sendInvoiceEmail($user_id, $morder);
        $user = get_userdata($morder->user_id);
        $user->membership_level = $morder->membership_level;
        //make sure they have the right level info
        //send email to member
        $pmproemail = new PMProEmail();
        $pmproemail->sendCheckoutEmail($user_id, $morder);
        //send email to admin
        $pmproemail = new PMProEmail();
        $pmproemail->sendCheckoutAdminEmail($user_id, $morder);
        $morder->getMemberOrderByID($morder->id);
        // //email the user their invoice
        $pmproemail = new PMProEmail();
        $pmproemail->sendInvoiceEmail(get_userdata($last_order->user_id), $morder);
        if (strpos(PMPRO_INS_DEBUG, "@")) {
            $log_email = PMPRO_INS_DEBUG;
        } else {
            $log_email = get_option("admin_email");
        }
        inslog("New order (" . $morder->code . ") created.");
        return true;
    } else {
        inslog("Duplicate Transaction ID: " . $txnref);
        return false;
    }
}
function pmpro_ipnSaveOrder($txn_id, $last_order)
{
    global $wpdb;
    //check that txn_id has not been previously processed
    $old_txn = $wpdb->get_var("SELECT payment_transaction_id FROM {$wpdb->pmpro_membership_orders} WHERE payment_transaction_id = '" . $txn_id . "' LIMIT 1");
    if (empty($old_txn)) {
        //hook for successful subscription payments
        do_action("pmpro_subscription_payment_completed");
        //save order
        $morder = new MemberOrder();
        $morder->user_id = $last_order->user_id;
        $morder->membership_id = $last_order->membership_id;
        $morder->payment_transaction_id = $txn_id;
        $morder->subscription_transaction_id = $last_order->subscription_transaction_id;
        $morder->gateway = $last_order->gateway;
        $morder->gateway_environment = $last_order->gateway_environment;
        // Payment Status
        $morder->status = 'success';
        // We have confirmed that and thats the reason we are here.
        // Payment Type.
        $morder->payment_type = $last_order->payment_type;
        //set amount based on which PayPal type
        if ($last_order->gateway == "paypal") {
            $morder->InitialPayment = $_POST['amount'];
            //not the initial payment, but the class is expecting that
            $morder->PaymentAmount = $_POST['amount'];
        } elseif ($last_order->gateway == "paypalexpress") {
            $morder->InitialPayment = $_POST['amount'];
            //not the initial payment, but the class is expecting that
            $morder->PaymentAmount = $_POST['amount'];
        } elseif ($last_order->gateway == "paypalstandard") {
            $morder->InitialPayment = $_POST['mc_gross'];
            //not the initial payment, but the class is expecting that
            $morder->PaymentAmount = $_POST['mc_gross'];
        }
        $morder->FirstName = $_POST['first_name'];
        $morder->LastName = $_POST['last_name'];
        $morder->Email = $_POST['payer_email'];
        //get address info if appropriate
        if ($last_order->gateway == "paypal") {
            $morder->Address1 = get_user_meta($last_order->user_id, "pmpro_baddress1", true);
            $morder->City = get_user_meta($last_order->user_id, "pmpro_bcity", true);
            $morder->State = get_user_meta($last_order->user_id, "pmpro_bstate", true);
            $morder->CountryCode = "US";
            $morder->Zip = get_user_meta($last_order->user_id, "pmpro_bzip", true);
            $morder->PhoneNumber = get_user_meta($last_order->user_id, "pmpro_bphone", true);
            $morder->billing->name = $_POST['first_name'] . " " . $_POST['last_name'];
            $morder->billing->street = get_user_meta($last_order->user_id, "pmpro_baddress1", true);
            $morder->billing->city = get_user_meta($last_order->user_id, "pmpro_bcity", true);
            $morder->billing->state = get_user_meta($last_order->user_id, "pmpro_bstate", true);
            $morder->billing->zip = get_user_meta($last_order->user_id, "pmpro_bzip", true);
            $morder->billing->country = get_user_meta($last_order->user_id, "pmpro_bcountry", true);
            $morder->billing->phone = get_user_meta($last_order->user_id, "pmpro_bphone", true);
            //get CC info that is on file
            $morder->cardtype = get_user_meta($last_order->user_id, "pmpro_CardType", true);
            $morder->accountnumber = hideCardNumber(get_user_meta($last_order->user_id, "pmpro_AccountNumber", true), false);
            $morder->expirationmonth = get_user_meta($last_order->user_id, "pmpro_ExpirationMonth", true);
            $morder->expirationyear = get_user_meta($last_order->user_id, "pmpro_ExpirationYear", true);
            $morder->ExpirationDate = $morder->expirationmonth . $morder->expirationyear;
            $morder->ExpirationDate_YdashM = $morder->expirationyear . "-" . $morder->expirationmonth;
        }
        //save
        $morder->saveOrder();
        $morder->getMemberOrderByID($morder->id);
        //email the user their invoice
        $pmproemail = new PMProEmail();
        $pmproemail->sendInvoiceEmail(get_userdata($last_order->user_id), $morder);
        ipnlog("New order (" . $morder->code . ") created.");
        return true;
    } else {
        ipnlog("Duplicate Transaction ID: " . $txn_id);
        return false;
    }
}
     $morder->billing->street = trim($baddress1 . " " . $baddress2);
     $morder->billing->city = $bcity;
     $morder->billing->state = $bstate;
     $morder->billing->country = $bcountry;
     $morder->billing->zip = $bzipcode;
     $morder->billing->phone = $bphone;
     //$gateway = pmpro_getOption("gateway");
     $morder->gateway = $gateway;
     $morder->setGateway();
     $worked = $morder->updateBilling();
     if ($worked) {
         //send email to member
         $pmproemail = new PMProEmail();
         $pmproemail->sendBillingEmail($current_user, $morder);
         //send email to admin
         $pmproemail = new PMProEmail();
         $pmproemail->sendBillingAdminEmail($current_user, $morder);
     }
 } else {
     $worked = true;
 }
 if ($worked) {
     //update the user meta too
     $meta_keys = array("pmpro_bfirstname", "pmpro_blastname", "pmpro_baddress1", "pmpro_baddress2", "pmpro_bcity", "pmpro_bstate", "pmpro_bzipcode", "pmpro_bphone", "pmpro_bemail", "pmpro_CardType", "pmpro_AccountNumber", "pmpro_ExpirationMonth", "pmpro_ExpirationYear");
     $meta_values = array($bfirstname, $blastname, $baddress1, $baddress2, $bcity, $bstate, $bzipcode, $bphone, $bemail, $CardType, hideCardNumber($AccountNumber), $ExpirationMonth, $ExpirationYear);
     pmpro_replaceUserMeta($current_user->ID, $meta_keys, $meta_values);
     //message
     $pmpro_msg = sprintf(__('Information updated. <a href="%s">&laquo; back to my account</a>', 'pmpro'), pmpro_url("account"));
     $pmpro_msgt = "pmpro_success";
 } else {
     $pmpro_msg = $morder->error;
    $old_order->getLastMemberOrderBySubscriptionTransactionID($webhookNotification->subscription->id);
    if (empty($old_order)) {
        die("Couldn't find old order for failed payment with subscription id=" . $webhookNotification->subscription->id);
    }
    //generate billing failure email
    do_action("pmpro_subscription_cancelled", $old_order);
    $transaction = $webhookNotification->transactions[0];
    //prep this order for the failure emails
    $morder = new MemberOrder();
    $morder->user_id = $user_id;
    $morder->billing->name = trim($transaction->billing_details->first_name . " " . $transaction->billing_details->first_name);
    $morder->billing->street = $transaction->billing_details->street_address;
    $morder->billing->city = $transaction->billing_details->locality;
    $morder->billing->state = $transaction->billing_details->region;
    $morder->billing->zip = $transaction->billing_details->postal_code;
    $morder->billing->country = $transaction->billing_details->country_code_alpha2;
    $morder->billing->phone = $old_order->billing->phone;
    //get CC info that is on file
    $morder->cardtype = get_user_meta($user_id, "pmpro_CardType", true);
    $morder->accountnumber = hideCardNumber(get_user_meta($user_id, "pmpro_AccountNumber", true), false);
    $morder->expirationmonth = get_user_meta($user_id, "pmpro_ExpirationMonth", true);
    $morder->expirationyear = get_user_meta($user_id, "pmpro_ExpirationYear", true);
    // Email the user and ask them to update their credit card information
    $pmproemail = new PMProEmail();
    $pmproemail->sendBillingFailureEmail($user, $morder);
    // Email admin so they are aware of the failure
    $pmproemail = new PMProEmail();
    $pmproemail->sendBillingFailureAdminEmail(get_bloginfo("admin_email"), $morder);
    echo "Sent email to the member and site admin. Thanks.";
    exit;
}
function pmpro_ipnSaveOrder($txn_id, $last_order)
{
    global $wpdb;
    //check that txn_id has not been previously processed
    $old_txn = $wpdb->get_var("SELECT payment_transaction_id FROM {$wpdb->pmpro_membership_orders} WHERE payment_transaction_id = '" . $txn_id . "' LIMIT 1");
    if (empty($old_txn)) {
        //save order
        $morder = new MemberOrder();
        $morder->user_id = $last_order->user_id;
        $morder->membership_id = $last_order->membership_id;
        $morder->payment_transaction_id = $txn_id;
        $morder->subscription_transaction_id = $last_order->subscription_transaction_id;
        $morder->gateway = $last_order->gateway;
        $morder->gateway_environment = $last_order->gateway_environment;
        // Payment Status
        $morder->status = 'success';
        // We have confirmed that and thats the reason we are here.
        // Payment Type.
        $morder->payment_type = $last_order->payment_type;
        //set amount based on which PayPal type
        if (false !== stripos($last_order->gateway, "paypal")) {
            if (isset($_POST['amount']) && !empty($_POST['amount'])) {
                $morder->InitialPayment = $_POST['amount'];
                //not the initial payment, but the class is expecting that
                $morder->PaymentAmount = $_POST['amount'];
            } elseif (isset($_POST['mc_gross']) && !empty($_POST['mc_gross'])) {
                $morder->InitialPayment = $_POST['mc_gross'];
                //not the initial payment, but the class is expecting that
                $morder->PaymentAmount = $_POST['mc_gross'];
            } elseif (isset($_POST['payment_gross']) && !empty($_POST['payment_gross'])) {
                $morder->InitialPayment = $_POST['payment_gross'];
                //not the initial payment, but the class is expecting that
                $morder->PaymentAmount = $_POST['payment_gross'];
            }
        }
        $morder->FirstName = $_POST['first_name'];
        $morder->LastName = $_POST['last_name'];
        $morder->Email = $_POST['payer_email'];
        //get address info if appropriate
        if ($last_order->gateway == "paypal") {
            $morder->Address1 = get_user_meta($last_order->user_id, "pmpro_baddress1", true);
            $morder->City = get_user_meta($last_order->user_id, "pmpro_bcity", true);
            $morder->State = get_user_meta($last_order->user_id, "pmpro_bstate", true);
            $morder->CountryCode = "US";
            $morder->Zip = get_user_meta($last_order->user_id, "pmpro_bzip", true);
            $morder->PhoneNumber = get_user_meta($last_order->user_id, "pmpro_bphone", true);
            $morder->billing->name = $_POST['first_name'] . " " . $_POST['last_name'];
            $morder->billing->street = get_user_meta($last_order->user_id, "pmpro_baddress1", true);
            $morder->billing->city = get_user_meta($last_order->user_id, "pmpro_bcity", true);
            $morder->billing->state = get_user_meta($last_order->user_id, "pmpro_bstate", true);
            $morder->billing->zip = get_user_meta($last_order->user_id, "pmpro_bzip", true);
            $morder->billing->country = get_user_meta($last_order->user_id, "pmpro_bcountry", true);
            $morder->billing->phone = get_user_meta($last_order->user_id, "pmpro_bphone", true);
            //get CC info that is on file
            $morder->cardtype = get_user_meta($last_order->user_id, "pmpro_CardType", true);
            $morder->accountnumber = hideCardNumber(get_user_meta($last_order->user_id, "pmpro_AccountNumber", true), false);
            $morder->expirationmonth = get_user_meta($last_order->user_id, "pmpro_ExpirationMonth", true);
            $morder->expirationyear = get_user_meta($last_order->user_id, "pmpro_ExpirationYear", true);
            $morder->ExpirationDate = $morder->expirationmonth . $morder->expirationyear;
            $morder->ExpirationDate_YdashM = $morder->expirationyear . "-" . $morder->expirationmonth;
        }
        //figure out timestamp or default to none (today)
        if (!empty($_POST['payment_date'])) {
            $morder->timestamp = strtotime($_POST['payment_date']);
        }
        // Save the event ID for the last processed user/IPN (in case we want to be able to replay IPN requests)
        $ipn_id = isset($_POST['ipn_track_id']) ? sanitize_text_field($_POST['ipn_track_id']) : null;
        // Allow extraction of the IPN Track ID from the order notes (if needed)
        $morder->notes = "{$morder->notes} [IPN_ID]{$ipn_id}[/IPN_ID]";
        /**
         * Post processing for a specific subscription related IPN event ID
         *
         * @param       string      $ipn_id     - The ipn_track_id from the PayPal IPN request
         * @param       MemberOrder $morder     - The completed Member Order object for the IPN request
         */
        do_action('pmpro_subscription_ipn_event_processed', $ipn_id, $morder);
        if (!is_null($ipn_id)) {
            if (false === update_user_meta($morder->user_id, "pmpro_last_{$morder->gateway}_ipn_id", $ipn_id)) {
                ipnlog("Unable to save the IPN event ID ({$ipn_id}) to usermeta for {$morder->user_id} ");
            }
        }
        //save
        $morder->saveOrder();
        $morder->getMemberOrderByID($morder->id);
        //email the user their invoice
        $pmproemail = new PMProEmail();
        $pmproemail->sendInvoiceEmail(get_userdata($last_order->user_id), $morder);
        //hook for successful subscription payments
        do_action("pmpro_subscription_payment_completed", $morder);
        ipnlog("New order (" . $morder->code . ") created.");
        return true;
    } else {
        ipnlog("Duplicate Transaction ID: " . $txn_id);
        return false;
    }
}
 function sendEmail($post_ids, $user_id)
 {
     if (!class_exists("PMProEmail")) {
         return;
     }
     $email = new PMProEmail();
     $user = get_user_by('id', $user_id);
     //build list of posts
     $post_list = "<ul>\n";
     foreach ($post_ids as $post_id) {
         $post_list .= '<li><a href="' . get_permalink($post_id) . '">' . get_the_title($post_id) . '</a></li>' . "\n";
     }
     $post_list .= "</ul>\n";
     $email->email = $user->user_email;
     $email->subject = sprintf(__("New content is available at %s", "pmpro"), get_option("blogname"));
     $email->template = "new_content";
     //check for custom email template
     if (file_exists(get_stylesheet_directory() . '/paid-memberships-pro/series/new_content.html')) {
         $template_path = get_stylesheet_directory() . '/paid-memberships-pro/series/new_content.html';
     } elseif (file_exists(get_template_directory() . '/paid-memberships-pro/series/new_content.html')) {
         $template_path = get_template_directory() . '/paid-memberships-pro/series/new_content.html';
     } else {
         $template_path = plugins_url('email/new_content.html', dirname(__FILE__));
     }
     $email->body = file_get_contents($template_path);
     $email->data = array("name" => $user->display_name, "sitename" => get_option("blogname"), "post_list" => $post_list, "login_link" => wp_login_url());
     if (!empty($post->post_excerpt)) {
         $email->data['excerpt'] = '<p>An excerpt of the post is below.</p><p>' . $post->post_excerpt . '</p>';
     } else {
         $email->data['excerpt'] = '';
     }
     $email->sendEmail();
 }
Example #12
0
global $besecure;
$besecure = false;
global $current_user, $pmpro_msg, $pmpro_msgt, $pmpro_confirm, $pmpro_error;
//if they don't have a membership, send them back to the subscription page
if (empty($current_user->membership_level->ID)) {
    wp_redirect(pmpro_url("levels"));
}
if (isset($_REQUEST['confirm'])) {
    $pmpro_confirm = $_REQUEST['confirm'];
} else {
    $pmpro_confirm = false;
}
if ($pmpro_confirm) {
    $old_level_id = $current_user->membership_level->id;
    $worked = pmpro_changeMembershipLevel(false, $current_user->ID);
    if ($worked === true && empty($pmpro_error)) {
        $pmpro_msg = __("Your membership has been cancelled.", 'pmpro');
        $pmpro_msgt = "pmpro_success";
        //send an email to the member
        $myemail = new PMProEmail();
        $myemail->sendCancelEmail();
        //send an email to the admin
        $myemail = new PMProEmail();
        $myemail->sendCancelAdminEmail($current_user, $old_level_id);
    } else {
        global $pmpro_error;
        $pmpro_msg = $pmpro_error;
        $pmpro_msgt = "pmpro_error";
    }
}
         $pmproemail->sendBillingFailureAdminEmail(get_bloginfo("admin_email"), $morder);
         $logstr .= "Subscription payment failed on order ID #" . $old_order->id . ". Sent email to the member and site admin.";
         pmpro_stripeWebhookExit();
     } else {
         $logstr .= "Could not find the related subscription for event with ID #" . $event->id . ".";
         if (!empty($event->data->object->customer)) {
             $logstr .= " Customer ID #" . $event->data->object->customer . ".";
         }
         pmpro_stripeWebhookExit();
     }
 } elseif ($event->type == "customer.subscription.deleted") {
     //for one of our users? if they still have a membership, notify the admin
     $user = getUserFromCustomerEvent($event, "success", true);
     if (!empty($user->ID)) {
         do_action("pmpro_stripe_subscription_deleted", $user->ID);
         $pmproemail = new PMProEmail();
         $pmproemail->data = array("body" => "<p>" . sprintf(__("%s has had their payment subscription cancelled by Stripe. Please check that this user's membership is cancelled on your site if it should be.", "pmpro"), $user->display_name . " (" . $user->user_login . ", " . $user->user_email . ")") . "</p>");
         $pmproemail->sendEmail(get_bloginfo("admin_email"));
         $logstr .= "Subscription deleted for user ID #" . $user->ID . ". Event ID #" . $event->id . ".";
         pmpro_stripeWebhookExit();
     } else {
         //check for any user at all
         $user = getUserFromCustomerEvent($event);
         if (!empty($user->ID)) {
             $logstr .= "Stripe tells us a subscription is deleted. This was probably initiated from PMPro and the membership/order is already cancelled. Event ID #" . $event->id . ".";
         } else {
             $logstr .= "Stripe tells us a subscription is deleted, but we could not find a user here for that subscription. Could be a subscription managed by a different app or plugin. Event ID #" . $event->id . ".";
         }
         pmpro_stripeWebhookExit();
     }
 }
Example #14
0
function pmpro_membership_level_profile_fields_update()
{
    //get the user id
    global $wpdb, $current_user, $user_ID;
    get_currentuserinfo();
    if (!empty($_REQUEST['user_id'])) {
        $user_ID = $_REQUEST['user_id'];
    }
    $membership_level_capability = apply_filters("pmpro_edit_member_capability", "manage_options");
    if (!current_user_can($membership_level_capability)) {
        return false;
    }
    //level change
    if (isset($_REQUEST['membership_level'])) {
        //if the level is being set to 0 by the admin, it's a cancellation.
        $changed_or_cancelled = '';
        if ($_REQUEST['membership_level'] === 0 || $_REQUEST['membership_level'] === '0' || $_REQUEST['membership_level'] == '') {
            $changed_or_cancelled = 'admin_cancelled';
        } else {
            $changed_or_cancelled = 'admin_changed';
        }
        //if the cancel at gateway box is not checked, don't cancel
        if (empty($_REQUEST['cancel_subscription'])) {
            add_filter('pmpro_cancel_previous_subscriptions', 'pmpro_cancel_previous_subscriptions_false');
        }
        //do the change
        if (pmpro_changeMembershipLevel($_REQUEST['membership_level'], $user_ID, $changed_or_cancelled)) {
            //it changed. send email
            $level_changed = true;
        }
        //remove filter after ward
        if (empty($_REQUEST['cancel_subscription'])) {
            remove_filter('pmpro_cancel_previous_subscriptions', 'pmpro_cancel_previous_subscriptions_false');
        }
    }
    //expiration change
    if (!empty($_REQUEST['expires'])) {
        //update the expiration date
        $expiration_date = intval($_REQUEST['expires_year']) . "-" . str_pad(intval($_REQUEST['expires_month']), 2, "0", STR_PAD_LEFT) . "-" . str_pad(intval($_REQUEST['expires_day']), 2, "0", STR_PAD_LEFT);
        $sqlQuery = "UPDATE {$wpdb->pmpro_memberships_users} SET enddate = '" . $expiration_date . "' WHERE status = 'active' AND membership_id = '" . intval($_REQUEST['membership_level']) . "' AND user_id = '" . $user_ID . "' LIMIT 1";
        if ($wpdb->query($sqlQuery)) {
            $expiration_changed = true;
        }
    } elseif (isset($_REQUEST['expires'])) {
        //already blank? have to check for null or '0000-00-00 00:00:00' or '' here.
        $sqlQuery = "SELECT user_id FROM {$wpdb->pmpro_memberships_users} WHERE (enddate IS NULL OR enddate = '' OR enddate = '0000-00-00 00:00:00') AND status = 'active' AND user_id = '" . $user_ID . "' LIMIT 1";
        $blank = $wpdb->get_var($sqlQuery);
        if (empty($blank)) {
            //null out the expiration
            $sqlQuery = "UPDATE {$wpdb->pmpro_memberships_users} SET enddate = NULL WHERE status = 'active' AND membership_id = '" . intval($_REQUEST['membership_level']) . "' AND user_id = '" . $user_ID . "' LIMIT 1";
            if ($wpdb->query($sqlQuery)) {
                $expiration_changed = true;
            }
        }
    }
    //emails if there was a change
    if (!empty($level_changed) || !empty($expiration_changed)) {
        //email to admin
        $pmproemail = new PMProEmail();
        if (!empty($expiration_changed)) {
            $pmproemail->expiration_changed = true;
        }
        $pmproemail->sendAdminChangeAdminEmail(get_userdata($user_ID));
        //send email
        if (!empty($_REQUEST['send_admin_change_email'])) {
            //email to member
            $pmproemail = new PMProEmail();
            if (!empty($expiration_changed)) {
                $pmproemail->expiration_changed = true;
            }
            $pmproemail->sendAdminChangeEmail(get_userdata($user_ID));
        }
    }
}
 /**
  * Cancel an order and call the cancel step of the gateway class if needed.
  */
 function cancel()
 {
     //only need to cancel on the gateway if there is a subscription id
     if (empty($this->subscription_transaction_id)) {
         //just mark as cancelled
         $this->updateStatus("cancelled");
         return true;
     } else {
         //cancel the gateway subscription first
         $result = $this->Gateway->cancel($this);
         if ($result == false) {
             //there was an error, but cancel the order no matter what
             $this->updateStatus("cancelled");
             //we should probably notify the admin
             $pmproemail = new PMProEmail();
             $pmproemail->template = "subscription_cancel_error";
             $pmproemail->data = array("body" => "<p>" . sprintf(__("There was an error canceling the subscription for user with ID=%s. You will want to check your payment gateway to see if their subscription is still active.", "pmpro"), strval($this->user_id)) . "</p><p>Error: " . $this->error . "</p>");
             $pmproemail->data["body"] .= "<p>Associated Order:<br />" . nl2br(var_export($this, true)) . "</p>";
             $pmproemail->sendEmail(get_bloginfo("admin_email"));
             return false;
         } else {
             //Note: status would have been set to cancelled by the gateway class. So we don't have to update it here.
             //remove billing numbers in pmpro_memberships_users if the membership is still active
             global $wpdb;
             $sqlQuery = "UPDATE {$wpdb->pmpro_memberships_users} SET initial_payment = 0, billing_amount = 0, cycle_number = 0 WHERE user_id = '" . $this->user_id . "' AND membership_id = '" . $this->membership_id . "' AND status = 'active'";
             $wpdb->query($sqlQuery);
             return $result;
         }
     }
 }
function pmpro_itnChangeMembershipLevel($txn_id, &$morder)
{
    //filter for level
    $morder->membership_level = apply_filters("pmpro_ipnhandler_level", $morder->membership_level, $morder->user_id);
    //fix expiration date
    if (!empty($morder->membership_level->expiration_number)) {
        $enddate = "'" . date("Y-m-d", strtotime("+ " . $morder->membership_level->expiration_number . " " . $morder->membership_level->expiration_period)) . "'";
    } else {
        $enddate = "NULL";
    }
    //get discount code     (NOTE: but discount_code isn't set here. How to handle discount codes for PayPal Standard?)
    $use_discount_code = true;
    //assume yes
    if (!empty($discount_code) && !empty($use_discount_code)) {
        $discount_code_id = $wpdb->get_var("SELECT id FROM {$wpdb->pmpro_discount_codes} WHERE code = '" . $discount_code . "' LIMIT 1");
    } else {
        $discount_code_id = "";
    }
    //set the start date to NOW() but allow filters
    $startdate = apply_filters("pmpro_checkout_start_date", "NOW()", $morder->user_id, $morder->membership_level);
    //custom level to change user to
    $custom_level = array('user_id' => $morder->user_id, 'membership_id' => $morder->membership_level->id, 'code_id' => $discount_code_id, 'initial_payment' => $morder->membership_level->initial_payment, 'billing_amount' => $morder->membership_level->billing_amount, 'cycle_number' => $morder->membership_level->cycle_number, 'cycle_period' => $morder->membership_level->cycle_period, 'billing_limit' => $morder->membership_level->billing_limit, 'trial_amount' => $morder->membership_level->trial_amount, 'trial_limit' => $morder->membership_level->trial_limit, 'startdate' => $startdate, 'enddate' => $enddate);
    global $pmpro_error;
    if (!empty($pmpro_error)) {
        echo $pmpro_error;
        ipnlog($pmpro_error);
    }
    //change level and continue "checkout"
    if (pmpro_changeMembershipLevel($custom_level, $morder->user_id) !== false) {
        //update order status and transaction ids
        $morder->status = "success";
        $morder->payment_transaction_id = $txn_id;
        if (!empty($_POST['subscr_id'])) {
            $morder->subscription_transaction_id = $_POST['subscr_id'];
        } else {
            $morder->subscription_transaction_id = "";
        }
        $morder->saveOrder();
        //add discount code use
        if (!empty($discount_code) && !empty($use_discount_code)) {
            $wpdb->query("INSERT INTO {$wpdb->pmpro_discount_codes_uses} (code_id, user_id, order_id, timestamp) VALUES('" . $discount_code_id . "', '" . $morder->user_id . "', '" . $morder->id . "', now())");
        }
        //save first and last name fields
        if (!empty($_POST['first_name'])) {
            $old_firstname = get_user_meta($morder->user_id, "first_name", true);
            if (!empty($old_firstname)) {
                update_user_meta($morder->user_id, "first_name", $_POST['first_name']);
            }
        }
        if (!empty($_POST['last_name'])) {
            $old_lastname = get_user_meta($morder->user_id, "last_name", true);
            if (!empty($old_lastname)) {
                update_user_meta($morder->user_id, "last_name", $_POST['last_name']);
            }
        }
        //hook
        do_action("pmpro_after_checkout", $morder->user_id);
        //setup some values for the emails
        if (!empty($morder)) {
            $invoice = new MemberOrder($morder->id);
        } else {
            $invoice = NULL;
        }
        $user = get_userdata($morder->user_id);
        $user->membership_level = $morder->membership_level;
        //make sure they have the right level info
        //send email to member
        $pmproemail = new PMProEmail();
        $pmproemail->sendCheckoutEmail($user, $invoice);
        //send email to admin
        $pmproemail = new PMProEmail();
        $pmproemail->sendCheckoutAdminEmail($user, $invoice);
        return true;
    } else {
        return false;
    }
}
Example #17
0
function pmpro_insChangeMembershipLevel($txn_id, &$morder)
{
    $recurring = pmpro_getParam('rectoken', 'POST');
    //filter for level
    $morder->membership_level = apply_filters("pmpro_inshandler_level", $morder->membership_level, $morder->user_id);
    //set the start date to current_time('mysql') but allow filters (documented in preheaders/checkout.php)
    $startdate = apply_filters("pmpro_checkout_start_date", "'" . current_time('mysql') . "'", $morder->user_id, $morder->membership_level);
    //fix expiration date
    if (!empty($morder->membership_level->expiration_number)) {
        $enddate = "'" . date("Y-m-d", strtotime("+ " . $morder->membership_level->expiration_number . " " . $morder->membership_level->expiration_period, current_time("timestamp"))) . "'";
    } else {
        $enddate = "NULL";
    }
    //filter the enddate (documented in preheaders/checkout.php)
    $enddate = apply_filters("pmpro_checkout_end_date", $enddate, $morder->user_id, $morder->membership_level, $startdate);
    //get discount code
    $morder->getDiscountCode();
    if (!empty($morder->discount_code)) {
        //update membership level
        $morder->getMembershipLevel(true);
        $discount_code_id = $morder->discount_code->id;
    } else {
        $discount_code_id = "";
    }
    //custom level to change user to
    $custom_level = array('user_id' => $morder->user_id, 'membership_id' => $morder->membership_level->id, 'code_id' => $discount_code_id, 'initial_payment' => $morder->membership_level->initial_payment, 'billing_amount' => $morder->membership_level->billing_amount, 'cycle_number' => $morder->membership_level->cycle_number, 'cycle_period' => $morder->membership_level->cycle_period, 'billing_limit' => $morder->membership_level->billing_limit, 'trial_amount' => $morder->membership_level->trial_amount, 'trial_limit' => $morder->membership_level->trial_limit, 'startdate' => $startdate, 'enddate' => $enddate);
    global $pmpro_error;
    if (!empty($pmpro_error)) {
        //echo $pmpro_error;
        fnlog($pmpro_error);
    }
    if (pmpro_changeMembershipLevel($custom_level, $morder->user_id) !== false) {
        //update order status and transaction ids
        $morder->status = "success";
        $morder->payment_transaction_id = $txn_id;
        if (!$recurring) {
            $morder->subscription_transaction_id = $txn_id;
        } else {
            $morder->subscription_transaction_id = '';
        }
        $morder->saveOrder();
        //add discount code use
        if (!empty($discount_code) && !empty($use_discount_code)) {
            $wpdb->query("INSERT INTO {$wpdb->pmpro_discount_codes_uses} (code_id, user_id, order_id, timestamp) VALUES('" . $discount_code_id . "', '" . $morder->user_id . "', '" . $morder->id . "', '" . current_time('mysql') . "')");
        }
        //hook
        do_action("pmpro_after_checkout", $morder->user_id);
        //print_r ($morder); die;
        //setup some values for the emails
        if (!empty($morder)) {
            $invoice = new MemberOrder($morder->id);
        } else {
            $invoice = NULL;
        }
        fnlog("CHANGEMEMBERSHIPLEVEL: ORDER: " . var_export($morder, true) . "\n---\n");
        $user = get_userdata($morder->user_id);
        if (empty($user)) {
            return false;
        }
        $user->membership_level = $morder->membership_level;
        //make sure they have the right level info
        //send email to member
        $pmproemail = new PMProEmail();
        $pmproemail->sendCheckoutEmail($user, $invoice);
        //send email to admin
        $pmproemail = new PMProEmail();
        $pmproemail->sendCheckoutAdminEmail($user, $invoice);
        return true;
    } else {
        return false;
    }
}
function pmpropbc_cancel_overdue_orders()
{
    global $wpdb;
    //make sure we only run once a day
    $now = current_time('timestamp');
    $today = date("Y-m-d", $now);
    //have to run for each level, so get levels
    $levels = pmpro_getAllLevels(true, true);
    if (empty($levels)) {
        return;
    }
    foreach ($levels as $level) {
        //get options
        $options = pmpropbc_getOptions($level->id);
        if (!empty($options['cancel_days'])) {
            $date = date("Y-m-d", strtotime("+ " . $options['cancel_days'] . " days", $now));
        } else {
            $date = $today;
        }
        //need to get all combos of pay cycle and period
        $sqlQuery = "SELECT DISTINCT(CONCAT(cycle_number, ' ', cycle_period)) FROM {$wpdb->pmpro_memberships_users} WHERE membership_id = '" . $level->id . "' AND cycle_number > 0 AND status = 'active'";
        $combos = $wpdb->get_col($sqlQuery);
        if (empty($combos)) {
            continue;
        }
        foreach ($combos as $combo) {
            //get all check orders still pending after X days
            $sqlQuery = "\r\n\t\t\t\tSELECT id \r\n\t\t\t\tFROM {$wpdb->pmpro_membership_orders} \r\n\t\t\t\tWHERE membership_id = {$level->id} \r\n\t\t\t\t\tAND gateway = 'check' \r\n\t\t\t\t\tAND status = 'pending' \r\n\t\t\t\t\tAND DATE_ADD(timestamp, INTERVAL {$combo}) <= '" . $date . "'\r\n\t\t\t\t\tAND notes NOT LIKE '%Cancelled:%' AND notes NOT LIKE '%Cancellation Skipped:%'\r\n\t\t\t\tORDER BY id\r\n\t\t\t";
            if (defined('PMPRO_CRON_LIMIT')) {
                $sqlQuery .= " LIMIT " . PMPRO_CRON_LIMIT;
            }
            $orders = $wpdb->get_col($sqlQuery);
            if (empty($orders)) {
                continue;
            }
            foreach ($orders as $order_id) {
                //get the order and user data
                $order = new MemberOrder($order_id);
                $user = get_userdata($order->user_id);
                $user->membership_level = pmpro_getMembershipLevelForUser($order->user_id);
                //if they are no longer a member, let's not send them an email
                if (empty($user->membership_level) || empty($user->membership_level->ID) || $user->membership_level->id != $order->membership_id) {
                    //note when we send the reminder
                    $new_notes = $order->notes . "Cancellation Skipped:" . $today . "\n";
                    $wpdb->query("UPDATE {$wpdb->pmpro_membership_orders} SET notes = '" . esc_sql($new_notes) . "' WHERE id = '" . $order_id . "' LIMIT 1");
                    continue;
                }
                //cancel the order and subscription
                do_action("pmpro_membership_pre_membership_expiry", $order->user_id, $order->membership_id);
                //remove their membership
                pmpro_changeMembershipLevel(false, $order->user_id, 'expired');
                do_action("pmpro_membership_post_membership_expiry", $order->user_id, $order->membership_id);
                $send_email = apply_filters("pmpro_send_expiration_email", true, $order->user_id);
                if ($send_email) {
                    //send an email
                    $pmproemail = new PMProEmail();
                    $euser = get_userdata($order->user_id);
                    $pmproemail->sendMembershipExpiredEmail($euser);
                    if (current_user_can('manage_options')) {
                        printf(__("Membership expired email sent to %s. ", "pmpro"), $euser->user_email);
                    } else {
                        echo ". ";
                    }
                }
            }
        }
    }
}
function pmpro_membership_level_profile_fields_update()
{
    //get the user id
    global $wpdb, $current_user, $user_ID;
    get_currentuserinfo();
    if (!empty($_REQUEST['user_id'])) {
        $user_ID = $_REQUEST['user_id'];
    }
    if (!current_user_can('edit_user', $user_ID)) {
        return false;
    }
    //level change
    if (isset($_REQUEST['membership_level'])) {
        if (pmpro_changeMembershipLevel($_REQUEST['membership_level'], $user_ID)) {
            //it changed. send email
            $level_changed = true;
        }
    }
    //expiration change
    if (!empty($_REQUEST['expires'])) {
        //update the expiration date
        $expiration_date = intval($_REQUEST['expires_year']) . "-" . intval($_REQUEST['expires_month']) . "-" . intval($_REQUEST['expires_day']);
        $sqlQuery = "UPDATE {$wpdb->pmpro_memberships_users} SET enddate = '" . $expiration_date . "' WHERE status = 'active' AND user_id = '" . $user_ID . "' LIMIT 1";
        if ($wpdb->query($sqlQuery)) {
            $expiration_changed = true;
        }
    } elseif (isset($_REQUEST['expires'])) {
        //already blank? have to check for null or '0000-00-00 00:00:00' or '' here.
        $sqlQuery = "SELECT user_id FROM {$wpdb->pmpro_memberships_users} WHERE (enddate IS NULL OR enddate = '' OR enddate = '0000-00-00 00:00:00') AND status = 'active' AND user_id = '" . $user_ID . "' LIMIT 1";
        $blank = $wpdb->get_var($sqlQuery);
        if (empty($blank)) {
            //null out the expiration
            $sqlQuery = "UPDATE {$wpdb->pmpro_memberships_users} SET enddate = NULL WHERE status = 'active' AND user_id = '" . $user_ID . "' LIMIT 1";
            if ($wpdb->query($sqlQuery)) {
                $expiration_changed = true;
            }
        }
    }
    //send email
    if (!empty($level_changed) || !empty($expiration_changed)) {
        //email to member
        $pmproemail = new PMProEmail();
        if (!empty($expiration_changed)) {
            $pmproemail->expiration_changed = true;
        }
        $pmproemail->sendAdminChangeEmail(get_userdata($user_ID));
        //email to admin
        $pmproemail = new PMProEmail();
        if (!empty($expiration_changed)) {
            $pmproemail->expiration_changed = true;
        }
        $pmproemail->sendAdminChangeAdminEmail(get_userdata($user_ID));
    }
}
     //hook
     do_action("pmpro_after_checkout", $user_id, $morder);
     //added $morder param in v2.0
     //setup some values for the emails
     if (!empty($morder)) {
         $invoice = new MemberOrder($morder->id);
     } else {
         $invoice = NULL;
     }
     $current_user->membership_level = $pmpro_level;
     //make sure they have the right level info
     //send email to member
     $pmproemail = new PMProEmail();
     $pmproemail->sendCheckoutEmail($current_user, $invoice);
     //send email to admin
     $pmproemail = new PMProEmail();
     $pmproemail->sendCheckoutAdminEmail($current_user, $invoice);
     //redirect to confirmation
     $rurl = pmpro_url("confirmation", "?level=" . $pmpro_level->id);
     $rurl = apply_filters("pmpro_confirmation_url", $rurl, $user_id, $pmpro_level);
     wp_redirect($rurl);
     exit;
 } else {
     //uh oh. we charged them then the membership creation failed
     if (isset($morder) && $morder->cancel()) {
         $pmpro_msg = __("IMPORTANT: Something went wrong during membership creation. Your credit card authorized, but we cancelled the order immediately. You should not try to submit this form again. Please contact the site owner to fix this issue.", "pmpro");
         $morder = NULL;
     } else {
         $pmpro_msg = __("IMPORTANT: Something went wrong during membership creation. Your credit card was charged, but we couldn't assign your membership. You should not submit this form again. Please contact the site owner to fix this issue.", "pmpro");
     }
 }
 /**
  * Cancel an order and call the cancel step of the gateway class if needed.
  */
 function cancel()
 {
     //only need to cancel on the gateway if there is a subscription id
     if (empty($this->subscription_transaction_id)) {
         //just mark as cancelled
         $this->updateStatus("cancelled");
         return true;
     } else {
         //cancel the gateway subscription first
         $result = $this->Gateway->cancel($this);
         if ($result == false) {
             //there was an error, but cancel the order no matter what
             $this->updateStatus("cancelled");
             //we should probably notify the admin
             $pmproemail = new PMProEmail();
             $pmproemail->template = "subscription_cancel_error";
             $pmproemail->data = array("body" => "<p>" . sprintf(__("There was an error canceling the subscription for user with ID=%s. You will want to check your payment gateway to see if their subscription is still active.", "pmpro"), strval($this->user_id)) . "</p><p>Error: " . $this->error . "</p>");
             $pmproemail->data["body"] .= "<p>Associated Order:<br />" . nl2br(var_export($this, true)) . "</p>";
             $pmproemail->sendEmail(get_bloginfo("admin_email"));
             return false;
         } else {
             //would have been cancelled by the gateway class
             return $result;
         }
     }
 }
function pmpro_insRecurringRestarted($morder)
{
    global $pmpro_error;
    //hook to do other stuff when payments restart
    do_action("pmpro_subscription_recuring_restarted", $last_order);
    $worked = pmpro_changeMembershipLevel($morder->membership_level->id, $morder->user->ID);
    if ($worked === true) {
        //$pmpro_msg = __("Your membership has been cancelled.", 'pmpro');
        //$pmpro_msgt = "pmpro_success";
        //send an email to the member
        $pmproemail = new PMProEmail();
        $pmproemail->sendCheckoutEmail($morder->user, $morder);
        //send email to admin
        $pmproemail = new PMProEmail();
        $pmproemail->sendCheckoutAdminEmail($morder->user, $morder);
        inslog("Subscription restarted due to 'recurring restarted' INS notification.");
        return true;
    } else {
        return false;
    }
}
Example #23
0
        $year = date_i18n('Y') - 1;
        $start_date = date_i18n("Y-m-d", strtotime("first day of January {$year}", current_time("timestamp")));
        $end_date = date_i18n("Y-m-d", strtotime("last day of December {$year}", current_time("timestamp")));
    }
    //add times to dates
    $start_date = $start_date . " 00:00:00";
    $end_date = $end_date . " 23:59:59";
    $condition = "timestamp BETWEEN '" . esc_sql($start_date) . "' AND '" . esc_sql($end_date) . "'";
} elseif ($filter == "within-a-level") {
    $condition = "membership_id = " . esc_sql($l);
} elseif ($filter == "within-a-status") {
    $condition = "status = '" . esc_sql($status) . "' ";
}
//emailing?
if (!empty($_REQUEST['email']) && !empty($_REQUEST['order'])) {
    $email = new PMProEmail();
    $user = get_user_by('email', $_REQUEST['email']);
    $order = new MemberOrder($_REQUEST['order']);
    if ($email->sendBillableInvoiceEmail($user, $order)) {
        $pmpro_msg = __("Invoice emailed successfully.", "pmpro");
        $pmpro_msgt = "success";
    } else {
        $pmpro_msg = __("Error emailing invoice.", "pmpro");
        $pmpro_msgt = "error";
    }
    //clean up so we stay on the orders list view
    unset($_REQUEST['order']);
    $order = null;
}
//deleting?
if (!empty($_REQUEST['delete'])) {
function pmpro_payflow_recurring_orders()
{
    //is PMPro even active?
    if (!function_exists('pmpro_hasMembershipLevel')) {
        return;
    }
    global $wpdb;
    $now = current_time('timestamp');
    //between what hours should the cron run?
    $cron_start = 1;
    //1 AM
    $cron_end = 6;
    //6 AM
    $current_hour = date('G', $now);
    if ($current_hour > $cron_end || $current_hour < $cron_start) {
        return;
    }
    //where did we leave off?
    if (isset($_REQUEST['start'])) {
        $start = intval($_REQUEST['start']);
        delete_option('pmpro_pfro_paused');
    } else {
        $start = get_option('payflow_recurring_orders_cron_count', 0);
    }
    //are we paused? value is timestamp. if set wait until then
    $paused = get_option('pmpro_pfro_paused', false);
    if (!empty($paused) && $paused > $now) {
        return;
    }
    //how many subscriptions to run at one time. based on your server speed and timeout limits/etc.
    $nper = 50;
    //next one
    $end = intval($start) + intval($nper);
    //get subs
    $sqlQuery = "\n\t\tSELECT SQL_CALC_FOUND_ROWS user_id FROM\n\t\t(\n\t\t\tSELECT mu.user_id,\n\t\t\t\tCASE mu.cycle_period\n\t\t\t\t\tWHEN 'Day' THEN date_add(mo.timestamp, INTERVAL mu.cycle_number DAY)\n\t\t\t\t\tWHEN 'Month' THEN date_add(mo.timestamp, INTERVAL mu.cycle_number MONTH)\n\t\t\t\t\tWHEN 'Week' THEN date_add(mo.timestamp, INTERVAL mu.cycle_number WEEK)\n\t\t\t\t\tWHEN 'Year' THEN date_add(mo.timestamp, INTERVAL mu.cycle_number YEAR)\n\t\t\t\tEND as next_payment_date\n\t\t\tFROM {$wpdb->pmpro_memberships_users} mu\n\t\t\t\tLEFT JOIN {$wpdb->pmpro_membership_orders} mo\n\t\t\t\t\tON mo.id = (\n\t\t\t\t\t\tSELECT id FROM {$wpdb->pmpro_membership_orders} WHERE gateway = 'payflowpro' AND status NOT IN('review', 'token', 'pending') AND user_id = mu.user_id ORDER BY id DESC LIMIT 1\n\t\t\t\t\t)\n\t\t\tWHERE mu.status = 'active'\n\t\t\t\tAND mo.subscription_transaction_id <> ''\n\t\t) members\n\t\tWHERE DATEDIFF('" . current_time('mysql') . "', next_payment_date) >= 0\n\t\tLIMIT {$start}, {$nper}\n\t";
    $sub_user_ids = $wpdb->get_col($sqlQuery);
    $count = $wpdb->get_var('SELECT FOUND_ROWS()');
    echo "Processing " . intval($start) . " to " . (intval($start) + intval($nper)) . " of " . $count . " subscriptions.<hr />";
    //if no more subs, pause until tomorrow
    if (empty($sub_user_ids)) {
        echo "All done. Pausing until tomorrow.<br />";
        $tomorrow = strtotime(date("Y-m-d 00:00:00", $now + 3600 * 24));
        update_option('pmpro_pfro_paused', $tomorrow);
        update_option('payflow_recurring_orders_cron_count', 0);
        return;
    }
    $failed_payment_emails = array();
    //loop through subs
    foreach ($sub_user_ids as $user_id) {
        $user = get_userdata($user_id);
        if (empty($user->ID)) {
            echo "Coundn't find user #" . $user_id . "...<br /><hr />";
            continue;
        }
        echo "Checking for recurring orders for user #" . $user->ID . " " . $user->user_login . " (" . $user->user_email . ")...<br />";
        $last_order = new MemberOrder();
        $last_order->getLastMemberOrder($user_id);
        if (!empty($last_order->id)) {
            echo "- Last order found. #" . $last_order->id . ", Code: " . $last_order->code . ", SubID: " . $last_order->subscription_transaction_id . ".<br />";
        } else {
            echo "- No last order. Skipping.";
            echo "<hr />";
            continue;
        }
        //is it even Payflow?
        if ($last_order->gateway != "payflowpro") {
            echo "- Order is for '" . $last_order->gateway . "' gateway.<br />";
            echo "<hr />";
            continue;
        }
        //check subscription
        if (!empty($last_order->subscription_transaction_id)) {
            echo "- Checking subscription #" . $last_order->subscription_transaction_id . ".<br />";
            $status = pmpropfro_getSubscriptionPayments($last_order);
            //find orders
            $payments = pmpropfro_processPaymentHistory($status);
            if (!empty($payments)) {
                foreach ($payments as $payment) {
                    if ($payment['P_TRANSTATE'] == 1 || $payment['P_TRANSTATE'] == 11) {
                        echo "- Failed payment #" . $payment['P_PNREF'] . ".";
                        //check if we have this one already
                        $old_order = new MemberOrder();
                        $old_order->getMemberOrderByPaymentTransactionID($payment['P_PNREF']);
                        if (empty($old_order->id)) {
                            $failed_payment_emails[] = $user->user_email;
                            //not there yet, add it
                            $morder = new MemberOrder();
                            $morder->user_id = $last_order->user_id;
                            $morder->membership_id = $last_order->membership_id;
                            $morder->payment_transaction_id = $payment['P_PNREF'];
                            $morder->subscription_transaction_id = $last_order->subscription_transaction_id;
                            $morder->InitialPayment = $payment['P_AMT'];
                            //not the initial payment, but the class is expecting that
                            $morder->PaymentAmount = $payment['P_AMT'];
                            $morder->status = "error";
                            //save
                            $morder->saveOrder();
                            $morder->getMemberOrderByID($morder->id);
                            echo " Saving order.";
                            //this will affect the main query, so need to roll back the "end" 1 space
                            $end--;
                            //unless there is another non-failed payment more recent, cancel their membership
                            if (!pmpropfro_paymentAfter($payments, strtotime($payment['P_TRANSTIME']))) {
                                //cancel membership
                                pmpro_changeMembershipLevel(0, $user_id);
                                echo " Membership cancelled. Member emailed.";
                                //notify them
                                $myemail = new PMProEmail();
                                $myemail->sendCancelEmail($user);
                            } else {
                                echo " More recent successful order. So not cancelling membership.";
                            }
                        } else {
                            echo " Already logged.";
                        }
                        echo "<br />";
                    } elseif ($payment['P_TRANSTATE'] == 8) {
                        //check if we have this one already
                        $old_order = new MemberOrder();
                        $old_order->getMemberOrderByPaymentTransactionID($payment['P_PNREF']);
                        if (empty($old_order->id)) {
                            //not there yet, add it
                            $morder = new MemberOrder();
                            $morder->user_id = $last_order->user_id;
                            $morder->membership_id = $last_order->membership_id;
                            $morder->payment_transaction_id = $payment['P_PNREF'];
                            $morder->subscription_transaction_id = $last_order->subscription_transaction_id;
                            $morder->InitialPayment = $payment['P_AMT'];
                            //not the initial payment, but the class is expecting that
                            $morder->PaymentAmount = $payment['P_AMT'];
                            $morder->status = "success";
                            //save
                            $morder->saveOrder();
                            $morder->getMemberOrderByID($morder->id);
                            //this will affect the main query, so need to roll back the "end" 1 space
                            $end--;
                            if (!empty($morder->id)) {
                                //update the timestamp
                                $timestamp = date("Y-m-d H:i:s", strtotime($payment['P_TRANSTIME']));
                                $wpdb->query("UPDATE {$wpdb->pmpro_membership_orders} SET timestamp = '" . $timestamp . "' WHERE id = '" . $morder->id . "' LIMIT 1");
                                echo "<strong>- Order added. #" . $morder->id . ".</strong><br />";
                                //email the user their invoice
                                $pmproemail = new PMProEmail();
                                $pmproemail->sendInvoiceEmail($user, $morder);
                                echo "- Invoice email sent to " . $user->user_email . ".";
                            } else {
                                echo "- Error adding order.";
                            }
                        } else {
                            echo "- Order already saved for #" . $payment['P_TRANSTATE'] . ".<br />";
                        }
                    } else {
                        echo "<strong>- Payment " . $payment['P_PNREF'] . " has status #" . $payment['P_TRANSTATE'] . " so not saving.</strong><br />";
                    }
                }
            } else {
                echo "- No payments found.<br />";
            }
        }
        echo "<hr />";
        echo "Going to start with #" . $end . " next time.";
        update_option('payflow_recurring_orders_cron_count', $end);
    }
    echo "<hr />";
    foreach ($failed_payment_emails as $email) {
        echo $email . "<br />";
    }
}