forked from strangerstudios/pmpro-failed-payment-limit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmpro-failed-payment-limit.php
executable file
·110 lines (95 loc) · 3.77 KB
/
pmpro-failed-payment-limit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
Plugin Name: Paid Memberships Pro - Failed Payment Limit Add On
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-failed-payment-limit/
Description: Cancel members subscriptions after 1-3 failed payments.
Version: .2
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
*/
//define('PMPRO_FAILED_PAYMENT_LIMIT', 3); //uncomment this or add a similar line to a custom plugin
/*
If a user has X failed payments without a successful order in between, their subscription is cancelled.
*/
//add setting to advanced settings
function pmprofpl_pmpro_custom_advanced_settings($settings) {
if(!is_array($settings))
$settings = array();
$settings[] = array(
'field_name' => 'pmpro_failed_payment_limit',
'field_type' => 'select',
'label' => 'Failed Payment Limit',
'description' => '',
'options' => array(''=>'None. Let the gateway handle it.', '1' => '1. Cancel after the first failed payment.', '2' => '2. Cancel after the second failed payment.', '3' => '3. Cancel after the third failed payment.')
);
return $settings;
}
add_filter('pmpro_custom_advanced_settings', 'pmprofpl_pmpro_custom_advanced_settings');
//helper function to get the limit
function pmprofpl_getLimit() {
//defined constant overrides everything
if(defined('PMPRO_FAILED_PAYMENT_LIMIT'))
return PMPRO_FAILED_PAYMENT_LIMIT;
else
return pmpro_getOption('pmpro_failed_payment_limit');
}
//handle payment failures
function pmprofpl_pmpro_subscription_payment_failed($order) {
//get user from order
$user = get_userdata($order->user_id);
//get their failed payment count
$count = get_user_meta($user->ID, "pmpro_failed_payment_count", true);
//increment it
if(empty($count))
$count = 1;
else
$count = $count + 1;
//if we hit X, cancel the user
if($count >= pmprofpl_getLimit()) {
$old_level_id = pmpro_getMembershipLevelForUser($user->ID)->ID;
//cancel subscription
$worked = pmpro_changeMembershipLevel(false, $user->ID);
if($worked === true) {
//send an email to the member
$myemail = new PMProEmail();
$myemail->sendCancelEmail($user->ID);
//send an email to the admin
$myemail = new PMProEmail();
$myemail->sendCancelAdminEmail($user, $old_level_id);
//update count in meta
delete_user_meta($user->ID, "pmpro_failed_payment_count");
//exit so we don't send failed payment email/etc
exit;
} else {
//shouldn't get here, but keep track of count anyway
update_user_meta($user->ID, "pmpro_failed_payment_count", $count);
}
} else {
//update count in meta
update_user_meta($user->ID, "pmpro_failed_payment_count", $count);
}
}
add_action('pmpro_subscription_payment_failed', 'pmprofpl_pmpro_subscription_payment_failed');
//update count on new orders
function pmprofpl_pmpro_added_order($order) {
//success?
if($order->status == "success") {
//remove any failed payment count they might have
delete_user_meta($order->user_id, "pmpro_failed_payment_count");
}
}
add_action('pmpro_added_order', 'pmprofpl_pmpro_added_order');
add_action('pmpro_updated_order', 'pmprofpl_pmpro_added_order'); //update too for cases where a temp order is made at checkout then updated
/*
Function to add links to the plugin row meta
*/
function pmprofpl_plugin_row_meta($links, $file) {
if(strpos($file, 'pmpro-failed-payment-limit.php') !== false) {
$new_links = array(
'<a href="' . esc_url('http://paidmembershipspro.com/support/') . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro' ) ) . '">' . __( 'Support', 'pmpro' ) . '</a>',
);
$links = array_merge($links, $new_links);
}
return $links;
}
add_filter('plugin_row_meta', 'pmprofpl_plugin_row_meta', 10, 2);