Last active
July 16, 2024 19:03
-
-
Save cartpauj/256e893ed3de276f8604aba01ef71bb8 to your computer and use it in GitHub Desktop.
Various Subscription and Transaction status hooks for MemberPress with some helpful comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Capture a new member signup. Only ever triggers once for each new member. | |
//Does not trigger for exising members who have subscribed to a Membership before. | |
// | |
//The user may not be logged in when this is called | |
//as it is triggered when a user is added over the REST API, | |
//and also when a user is added from the dashboard (MemberPress -> Members -> Add New) | |
function mepr_capture_new_member_signup_completed($event) { | |
$user = $event->get_data(); | |
$txn_data = json_decode($event->args); | |
//Do what you need | |
} | |
add_action('mepr-event-member-signup-completed', 'mepr_capture_new_member_signup_completed'); | |
//Capture a new Recurring Subscription created event | |
function mepr_capture_new_recurring_sub($event) { | |
$subscription = $event->get_data(); | |
$user = $subscription->user(); | |
//Do what you need | |
} | |
add_action('mepr-event-subscription-created', 'mepr_capture_new_recurring_sub'); | |
//Capture any completed transaction (recurring and non-recurring) event | |
function mepr_capture_completed_transaction($event) { | |
$transaction = $event->get_data(); | |
$user = $transaction->user(); | |
if(($subscription = $transaction->subscription_id)) { | |
$subscription_number = $subscription->subscr_id; | |
//This transaction belongs to a recurring subscription | |
} | |
else { | |
$transaction_number = $transaction->trans_num; | |
//This is a non-recurring transaction | |
} | |
//Do what you need | |
} | |
add_action('mepr-event-transaction-completed', 'mepr_capture_completed_transaction'); | |
//Capture a new One-Time Subscription created event | |
function mepr_capture_new_one_time_sub($event) { | |
$transaction = $event->get_data(); | |
$user = $transaction->user(); | |
//Do what you need | |
} | |
add_action('mepr-event-non-recurring-transaction-completed', 'mepr_capture_new_one_time_sub'); | |
//Capture a Recurring Subscription paused event | |
function mepr_capture_paused_sub($event) { | |
$subscription = $event->get_data(); | |
$user = $subscription->user(); | |
//Do what you need | |
} | |
add_action('mepr-event-subscription-paused', 'mepr_capture_paused_sub'); | |
//Capture a Recurring Subscription resumed event | |
function mepr_capture_resumed_sub($event) { | |
$subscription = $event->get_data(); | |
$user = $subscription->user(); | |
//Do what you need | |
} | |
add_action('mepr-event-subscription-resumed', 'mepr_capture_resumed_sub'); | |
//Capture a Recurring Subscription cancelled event (and detect who did it) | |
function mepr_capture_stopped_sub($event) { | |
$subscription = $event->get_data(); | |
$user = $subscription->user(); | |
if(is_user_logged_in()) { | |
if(current_user_can('manage_options')) { | |
//An admin cancelled this from the dashboard | |
} | |
else { | |
//User cancelled from their account page or upgraded to a new plan | |
} | |
} | |
else { | |
//Cancelled from a gateway Webhook or IPN notification | |
//Not really a way to tell why but it could have been an | |
// - admin cancelling it at the gateway instead of in the dashboard | |
// - or the gateway could have cancelled it because of too many failed payments | |
// - or the gateway could have cancelled it because the max billing cycles was reached | |
// - or in the case of PayPal a user might have cancelled their recurring profile from in their PayPal account | |
} | |
} | |
add_action('mepr-event-subscription-stopped', 'mepr_capture_stopped_sub'); | |
//Capture a failed Transaction event | |
function mepr_capture_failed_transaction($event) { | |
$transaction = $event->get_data(); | |
$subscription = $transaction->subscription(); | |
$user = $transaction->user(); | |
//Do what you need | |
} | |
add_action('mepr-event-recurring-transaction-failed', 'mepr_capture_failed_transaction'); | |
//Capture a one-time completed Transaction event | |
//See mepr_capture_new_one_time_sub() above | |
//Capture a recurring completed Transaction event | |
function mepr_capture_completed_recurring_transaction($event) { | |
$transaction = $event->get_data(); | |
$subscription = $transaction->subscription(); | |
$user = $transaction->user(); | |
//Do what you need | |
} | |
add_action('mepr-event-recurring-transaction-completed', 'mepr_capture_completed_recurring_transaction'); | |
//Capture a refunded Transaction event | |
function mepr_capture_refunded_transaction($event) { | |
$transaction = $event->get_data(); | |
$subscription = $transaction->subscription(); //This may return false if it's a one-time transaction that was refunded | |
$user = $transaction->user(); | |
//Do what you need | |
} | |
add_action('mepr-event-transaction-refunded', 'mepr_capture_refunded_transaction'); | |
//Capture a Transaction expired event | |
function mepr_capture_expired_transaction($event) { | |
//BE CAREFUL WITH THIS ONE | |
//This could be a prior recurring transaction that has expired | |
//So the user might still be active on the subscription with a new transaction | |
//So you might check | |
// - if the $subscription exists | |
// - if so, then is $subscription->status = 'active' still | |
// - if so, then it's possible the user is not really expired on it | |
// - to check this use the $user->is_already_subscribed_to($transaction->product_id) method | |
$transaction = $event->get_data(); | |
$subscription = $transaction->subscription(); //This may return false if it's a one-time transaction that has expired | |
$user = $transaction->user(); | |
//Do what you need | |
} | |
add_action('mepr-event-transaction-expired', 'mepr_capture_expired_transaction'); | |
//Capture a signup (user completed step 1, but hasn't necessarily paid yet) | |
function mepr_capture_new_member_added($event) { | |
$user = $event->get_data(); | |
//Do what you need | |
} | |
add_action('mepr-event-member-added', 'mepr_capture_new_member_added'); | |
Is this still valid? I am not sure they are firing at all
They are event hooks only. These are event fired and sent to a predefined webhook url and do not work called as normal WordPress actions/filters.
For completed transaction this one works:
function track_completed_transaction($event) {
$data = $event->get_data();
$user = $data->user();
$membership = $data->product();
}
add_action( 'mepr-txn-status-complete', 'track_completed_transaction', 10, 1 );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
None of the completed transaction actions seem to fire when a pending transaction is marked complete (standard Offline Payment Gateway procedure).