Example #1
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";
    }
}
function pmpro_insRecurringStopped($morder)
{
    global $pmpro_error;
    //hook to do other stuff when payments stop
    do_action("pmpro_subscription_recuring_stopped", $last_order);
    $worked = pmpro_changeMembershipLevel(false, $morder->user->ID);
    if ($worked === true) {
        //$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($morder->user, $morder->membership_level->id);
        inslog("Subscription cancelled due to 'recurring stopped' INS notification.");
        return true;
    } else {
        return false;
    }
}
Example #3
0
//are we confirming a cancellation?
if (isset($_REQUEST['confirm'])) {
    $pmpro_confirm = $_REQUEST['confirm'];
} else {
    $pmpro_confirm = false;
}
if ($pmpro_confirm) {
    if (!empty($old_level_ids)) {
        $worked = true;
        foreach ($old_level_ids as $old_level_id) {
            $worked = $worked && pmpro_cancelMembershipLevel($old_level_id, $current_user->ID, 'cancelled');
        }
    } else {
        $old_level_ids = $wpdb->get_col("SELECT id FROM {$wpdb->pmpro_memberships_users} WHERE user_id = '" . $current_user->ID . "' AND status = 'active'");
        $worked = pmpro_changeMembershipLevel(0, $current_user->ID, 'cancelled');
    }
    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($current_user, $old_level_ids);
        //send an email to the admin
        $myemail = new PMProEmail();
        $myemail->sendCancelAdminEmail($current_user, $old_level_ids);
    } else {
        global $pmpro_error;
        $pmpro_msg = $pmpro_error;
        $pmpro_msgt = "pmpro_error";
    }
}
            	Also, if a user is changing levels, we don't want to cancel their new membership, just the old subscription at PayPal.
            	
            	So we check 2 things and don't cancel if:
            	(1) This order already has "cancelled" status.
            	(2) The user doesn't currently have the level attached to this order.
            */
            if ($last_subscr_order->status == "cancelled") {
                ipnlog("We've already processed this cancellation. Probably originated from WP/PMPro. (Order #" . $last_subscr_order->id . ", Subscription Transaction ID #" . $subscr_id . ")");
            } elseif (!pmpro_hasMembershipLevel($last_subsc_order->membership_id, $user->ID)) {
                ipnlog("This user has a different level than the one associated with this order. Their membership was probably changed by an admin or through an upgrade/downgrade. (Order #" . $last_subscr_order->id . ", Subscription Transaction ID #" . $subscr_id . ")");
            } else {
                pmpro_changeMembershipLevel(0, $last_subscr_order->user_id, 'cancelled');
                ipnlog("Canceled membership for user with id = " . $last_subscr_order->user_id . ". Subscription transaction id = " . $subscr_id . ".");
                //send an email to the member
                $myemail = new PMProEmail();
                $myemail->sendCancelEmail($user);
                //send an email to the admin
                $myemail = new PMProEmail();
                $myemail->sendCancelAdminEmail($user, $last_subscr_order->membership_id);
            }
        }
        pmpro_ipnExit();
    }
}
//Other
//if we got here, this is a different kind of txn
ipnlog("No recurring payment id or item number. txn_type = " . $txn_type);
pmpro_ipnExit();
/*
	Add message to ipnlog string
*/
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 />";
    }
}