From 2bef7f226663dfda7a1f908fe6b309c768046981 Mon Sep 17 00:00:00 2001 From: maximilianoRicoTabo Date: Tue, 12 Nov 2024 18:55:11 -0300 Subject: [PATCH] Add payment failure classes --- classes/class.pmproemail.php | 105 +-------- ...o-email-template-billing-failure-admin.php | 207 ++++++++++++++++++ ...s-pmpro-email-template-billing-failure.php | 200 +++++++++++++++++ includes/email-templates.php | 20 -- paid-memberships-pro.php | 2 + 5 files changed, 418 insertions(+), 116 deletions(-) create mode 100644 classes/email-templates/class-pmpro-email-template-billing-failure-admin.php create mode 100644 classes/email-templates/class-pmpro-email-template-billing-failure.php diff --git a/classes/class.pmproemail.php b/classes/class.pmproemail.php index 8816f189c..b50ef6cea 100644 --- a/classes/class.pmproemail.php +++ b/classes/class.pmproemail.php @@ -946,62 +946,14 @@ function sendBillingFailureEmail( $user = NULL, $order = NULL ) { global $current_user; if(!$user) $user = $current_user; - + if(!$user || !$order) return false; - //get Level from constructor - $membership_level = new PMPro_Membership_Level( $order->membership_id ); - - // Try to get the subscription ID. - $subscription = $order->get_subscription(); - $subscription_id = ! empty( $subscription ) ? $subscription->get_id() : null; - - $this->email = $user->user_email; - $this->subject = sprintf( __("Membership payment for level %s failed at %s", "paid-memberships-pro"), - $membership_level->name, get_option("blogname") ); - - $this->data = array( - 'subject' => $this->subject, - 'header_name' => $user->display_name, - 'name' => $user->display_name, - 'user_login' => $user->user_login, - 'sitename' => get_option( 'blogname' ), - 'siteemail' => get_option( 'pmpro_from_email' ), - 'membership_id' => $membership_level->id, - 'membership_level_name' => $membership_level->name, - 'display_name' => $user->display_name, - 'user_email' => $user->user_email, - 'billing_name' => $order->billing->name, - 'billing_street' => $order->billing->street, - 'billing_street2' => $order->billing->street2, - 'billing_city' => $order->billing->city, - 'billing_state' => $order->billing->state, - 'billing_zip' => $order->billing->zip, - 'billing_country' => $order->billing->country, - 'billing_phone' => $order->billing->phone, - 'cardtype' => $order->cardtype, - 'accountnumber' => hideCardNumber($order->accountnumber), - 'expirationmonth' => $order->expirationmonth, - 'expirationyear' => $order->expirationyear, - 'login_link' => pmpro_login_url( pmpro_url( 'billing', empty( $subscription_id ) ? '' : '?subscription_id=' . $subscription_id ) ), - 'login_url' => pmpro_login_url( pmpro_url( 'billing', empty( $subscription_id ) ? '' : '?subscription_id=' . $subscription_id ) ), - 'levels_url' => pmpro_url( 'levels' ) - ); - $this->data["billing_address"] = pmpro_formatAddress($order->billing->name, - $order->billing->street, - $order->billing->street2, - $order->billing->city, - $order->billing->state, - $order->billing->zip, - $order->billing->country, - $order->billing->phone); - - $this->template = apply_filters("pmpro_email_template", "billing_failure", $this); + $email = new PMPro_Email_Template_Billing_Failure( $user, $order ); + $email->send(); + } - return $this->sendEmail(); - } - /** * Send the admin an email when their recurring payment has failed. * @@ -1011,52 +963,13 @@ function sendBillingFailureEmail( $user = NULL, $order = NULL ) { function sendBillingFailureAdminEmail($email, $order = NULL) { if(!$order) return false; - - $user = get_userdata($order->user_id); - $membership_level = new PMPro_Membership_Level( $order->membership_id ); - - $this->email = $email; - $this->subject = sprintf(__("Membership payment failed For %s at %s", "paid-memberships-pro"), $user->display_name, get_option("blogname")); - - $this->data = array( - 'subject' => $this->subject, - 'header_name' => $this->get_admin_name( $email ), - 'name' => 'Admin', - 'user_login' => $user->user_login, - 'sitename' => get_option( 'blogname' ), - 'siteemail' => get_option( 'pmpro_from_email' ), - 'membership_id' => $membership_level->id, - 'membership_level_name' => $membership_level->name, - 'display_name' => $user->display_name, - 'user_email' => $user->user_email, - 'billing_name' => $order->billing->name, - 'billing_street' => $order->billing->street, - 'billing_street2' => $order->billing->street2, - 'billing_city' => $order->billing->city, - 'billing_state' => $order->billing->state, - 'billing_zip' => $order->billing->zip, - 'billing_country' => $order->billing->country, - 'billing_phone' => $order->billing->phone, - 'cardtype' => $order->cardtype, - 'accountnumber' => hideCardNumber($order->accountnumber), - 'expirationmonth' => $order->expirationmonth, - 'expirationyear' => $order->expirationyear, - 'login_link' => pmpro_login_url( get_edit_user_link( $user->ID ) ), - 'login_url' => pmpro_login_url( get_edit_user_link( $user->ID ) ), - 'levels_url' => pmpro_url( 'levels' ), - ); - $this->data["billing_address"] = pmpro_formatAddress($order->billing->name, - $order->billing->street, - $order->billing->street2, - $order->billing->city, - $order->billing->state, - $order->billing->zip, - $order->billing->country, - $order->billing->phone); - $this->template = apply_filters("pmpro_email_template", "billing_failure_admin", $this); + $user = get_userdata( $order->user_id ); + if(!$user) + return false; - return $this->sendEmail(); + $email = new PMPro_Email_Template_Billing_Failure_Admin( $user, $order ); + $email->send(); } /** diff --git a/classes/email-templates/class-pmpro-email-template-billing-failure-admin.php b/classes/email-templates/class-pmpro-email-template-billing-failure-admin.php new file mode 100644 index 000000000..a9babdcf7 --- /dev/null +++ b/classes/email-templates/class-pmpro-email-template-billing-failure-admin.php @@ -0,0 +1,207 @@ +user = $user; + $this->order = $order; + } + + /** + * Get the email template slug. + * + * @since TBD + * + * @return string The email template slug. + */ + public static function get_template_slug() { + return 'billing_failure_admin'; + } + + /** + * Get the "nice name" of the email template. + * + * @since TBD + * + * @return string The "nice name" of the email template. + */ + public static function get_template_name() { + return __( 'Payment Failure (admin)', 'paid-memberships-pro' ); + } + + /** + * Get the email template description. + * + * @since TBD + * + * @return string The email template description. + */ + public static function get_template_description() { + return __( 'This email is sent to the site admin when a member\'s payment fails.', 'paid-memberships-pro' ); + } + + /** + * Get the email subject. + * + * @since TBD + * + * @return string The email subject. + */ + public static function get_default_subject() { + return __( "Membership payment failed for !!display_name!! at !!sitename!!", 'paid-memberships-pro' ); + } + + /** + * Get the email body. + * + * @since TBD + * + * @return string The email body. + */ + public static function get_default_body() { + return __( '

The subscription payment for !!user_login!! for level !!membership_level_name!! at !!sitename!! has failed.

+

Account: !!display_name!! (!!user_email!!)

+

Membership Level: !!membership_level_name!!

+ +

Log in to your WordPress admin here: !!login_url!!

', 'paid-memberships-pro' ); + } + + /** + * Get the email address to send the email to. + * + * @since TBD + * + * @return string The email address to send the email to. + */ + public function get_recipient_email() { + return get_bloginfo( 'admin_email' ); + } + + /** + * Get the name of the email recipient. + * + * @since TBD + * + * @return string The name of the email recipient. + */ + public function get_recipient_name() { + //get user by email + $user = get_user_by( 'email', $this->get_recipient_email() ); + return $user->display_name; + } + + /** + * Get the email template variables for the email paired with a description of the variable. + * + * @since TBD + * + * @return array The email template variables for the email (key => value pairs). + */ + public static function get_email_template_variables_with_description() { + return array( + '!!subject!!' => __( 'The default subject for the email. This will be removed in a future version.', 'paid-memberships-pro' ), + '!!header_name!!' => __( 'The name of the email recipient.', 'paid-memberships-pro' ), + '!!name!!' => __( 'The display name of the user.', 'paid-memberships-pro' ), + '!!user_login!!' => __( 'The username of the user.', 'paid-memberships-pro' ), + '!!user_email!!' => __( 'The email address of the user billing failed', 'paid-memberships-pro' ), + '!!display_name!!' => __( 'The display name of the user billing failed', 'paid-memberships-pro' ), + '!!membership_id!!' => __( 'The ID of the membership level.', 'paid-memberships-pro' ), + '!!membership_level_name!!' => __( 'The name of the membership level.', 'paid-memberships-pro' ), + '!!billing_name!!' => __( 'Billing Info Name', 'paid-memberships-pro' ), + '!!billing_street!!' => __( 'Billing Info Street', 'paid-memberships-pro' ), + '!!billing_street2!!' => __( 'Billing Info Street 2', 'paid-memberships-pro' ), + '!!billing_city!!' => __( 'Billing Info City', 'paid-memberships-pro' ), + '!!billing_state!!' => __( 'Billing Info State', 'paid-memberships-pro' ), + '!!billing_zip!!' => __( 'Billing Info Zip', 'paid-memberships-pro' ), + '!!billing_country!!' => __( 'Billing Info Country', 'paid-memberships-pro' ), + '!!billing_phone!!' => __( 'Billing Info Phone', 'paid-memberships-pro' ), + '!!billing_address!!' => __( 'Billing Info Complete Address', 'paid-memberships-pro' ), + '!!cardtype!!' => __( 'Credit Card Type', 'paid-memberships-pro' ), + '!!accountnumber!!' => __( 'Credit Card Number (last 4 digits)', 'paid-memberships-pro' ), + '!!expirationmonth!!' => __( 'Credit Card Expiration Month (mm format)', 'paid-memberships-pro' ), + '!!expirationyear!!' => __( 'Credit Card Expiration Year (yyyy format)', 'paid-memberships-pro' ), + + ); + } + + /** + * Get the email template variables for the email. + * + * @since TBD + * + * @return array The email template variables for the email (key => value pairs). + */ + public function get_email_template_variables() { + $order = $this->order; + $user = $this->user; + $membership_level = pmpro_getLevel( $order->membership_id ); + return array( + 'subject' => $this->get_default_subject(), + 'header_name' => $user->display_name, + 'name' => $user->display_name, + 'user_login' => $user->user_login, + 'membership_id' => $membership_level->id, + 'membership_level_name' => $membership_level->name, + 'display_name' => $user->display_name, + 'user_email' => $user->user_email, + 'billing_name' => $order->billing->name, + 'billing_street' => $order->billing->street, + 'billing_street2' => $order->billing->street2, + 'billing_city' => $order->billing->city, + 'billing_state' => $order->billing->state, + 'billing_zip' => $order->billing->zip, + 'billing_country' => $order->billing->country, + 'billing_phone' => $order->billing->phone, + 'billing_address'=> pmpro_formatAddress( $order->billing->name, + $order->billing->street, + $order->billing->street2, + $order->billing->city, + $order->billing->state, + $order->billing->zip, + $order->billing->country, + $order->billing->phone ), + 'cardtype' => $order->cardtype, + 'accountnumber' => hideCardNumber( $order->accountnumber ), + 'expirationmonth' => $order->expirationmonth, + 'expirationyear' => $order->expirationyear, + ); + } +} + +/** + * Register the email template. + * + * @since TBD + * + * @param array $email_templates The email templates (template slug => email template class name) + * @return array The modified email templates array. + */ +function pmpro_email_templates_billing_failure_admin( $email_templates ) { + $email_templates['billing_failure_admin'] = 'PMPro_Email_Template_Billing_Failure_Admin'; + return $email_templates; +} +add_filter( 'pmpro_email_templates', 'pmpro_email_templates_billing_failure_admin' ); + + diff --git a/classes/email-templates/class-pmpro-email-template-billing-failure.php b/classes/email-templates/class-pmpro-email-template-billing-failure.php new file mode 100644 index 000000000..9ae2a9ad1 --- /dev/null +++ b/classes/email-templates/class-pmpro-email-template-billing-failure.php @@ -0,0 +1,200 @@ +user = $user; + $this->order = $order; + } + + /** + * Get the email template slug. + * + * @since TBD + * + * @return string The email template slug. + */ + public static function get_template_slug() { + return 'billing_failure'; + } + + /** + * Get the "nice name" of the email template. + * + * @since TBD + * + * @return string The "nice name" of the email template. + */ + public static function get_template_name() { + return __( 'Payment Failure', 'paid-memberships-pro' ); + } + + /** + * Get "help text" to display to the admin when editing the email template. + * + * @since TBD + * + * @return string The help text. + */ + public static function get_template_description() { + return __( 'This email is sent out if a recurring payment has failed, usually due to an expired or cancelled credit card. This email is sent to the member to allowing them time to update payment information without a disruption in access to your site.', 'paid-memberships-pro' ); + } + + /** + * Get the email subject. + * + * @since TBD + * + * @return string The email subject. + */ + public static function get_default_subject() { + return __( "Membership payment failed at !!sitename!!", 'paid-memberships-pro' ); + } + + /** + * Get the email body. + * + * @since TBD + * + * @return string The email body. + */ + public static function get_default_body() { + return __( '

The current subscription payment for level !!membership_level_name!! at !!sitename!! membership has failed. Please click the following link to log in and update your billing information to avoid account suspension.
!!login_url!!

+

Account: !!display_name!! (!!user_email!!)

', 'paid-memberships-pro' ); + } + + /** + * Get the email address to send the email to. + * + * @since TBD + * + * @return string The email address to send the email to. + */ + public function get_recipient_email() { + return $this->user->user_email; + } + + /** + * Get the name of the email recipient. + * + * @since TBD + * + * @return string The name of the email recipient. + */ + public function get_recipient_name() { + return $this->user->display_name; + } + + /** + * Get the email template variables for the email paired with a description of the variable. + * + * @since TBD + * + * @return array The email template variables for the email (key => value pairs). + */ + public static function get_email_template_variables_with_description() { + return array( + '!!subject!!' => __( 'The default subject for the email. This will be removed in a future version.', 'paid-memberships-pro' ), + '!!header_name!!' => __( 'The name of the email recipient.', 'paid-memberships-pro' ), + '!!name!!' => __( 'The display name of the user.', 'paid-memberships-pro' ), + '!!user_login!!' => __( 'The username of the user.', 'paid-memberships-pro' ), + '!!user_email!!' => __( 'The email address of the user billing failed', 'paid-memberships-pro' ), + '!!display_name!!' => __( 'The display name of the user billing failed', 'paid-memberships-pro' ), + '!!membership_id!!' => __( 'The ID of the membership level.', 'paid-memberships-pro' ), + '!!membership_level_name!!' => __( 'The name of the membership level.', 'paid-memberships-pro' ), + '!!billing_name!!' => __( 'Billing Info Name', 'paid-memberships-pro' ), + '!!billing_street!!' => __( 'Billing Info Street', 'paid-memberships-pro' ), + '!!billing_street2!!' => __( 'Billing Info Street 2', 'paid-memberships-pro' ), + '!!billing_city!!' => __( 'Billing Info City', 'paid-memberships-pro' ), + '!!billing_state!!' => __( 'Billing Info State', 'paid-memberships-pro' ), + '!!billing_zip!!' => __( 'Billing Info Zip', 'paid-memberships-pro' ), + '!!billing_country!!' => __( 'Billing Info Country', 'paid-memberships-pro' ), + '!!billing_phone!!' => __( 'Billing Info Phone', 'paid-memberships-pro' ), + '!!billing_address!!' => __( 'Billing Info Complete Address', 'paid-memberships-pro' ), + '!!cardtype!!' => __( 'Credit Card Type', 'paid-memberships-pro' ), + '!!accountnumber!!' => __( 'Credit Card Number (last 4 digits)', 'paid-memberships-pro' ), + '!!expirationmonth!!' => __( 'Credit Card Expiration Month (mm format)', 'paid-memberships-pro' ), + '!!expirationyear!!' => __( 'Credit Card Expiration Year (yyyy format)', 'paid-memberships-pro' ), + + ); + } + + /** + * Get the email template variables for the email. + * + * @since TBD + * + * @return array The email template variables for the email (key => value pairs). + */ + public function get_email_template_variables() { + $order = $this->order; + $user = $this->user; + $membership_level = pmpro_getLevel( $order->membership_id ); + return array( + 'subject' => $this->get_default_subject(), + 'header_name' => $user->display_name, + 'name' => $user->display_name, + 'user_login' => $user->user_login, + 'membership_id' => $membership_level->id, + 'membership_level_name' => $membership_level->name, + 'display_name' => $user->display_name, + 'user_email' => $user->user_email, + 'billing_name' => $order->billing->name, + 'billing_street' => $order->billing->street, + 'billing_street2' => $order->billing->street2, + 'billing_city' => $order->billing->city, + 'billing_state' => $order->billing->state, + 'billing_zip' => $order->billing->zip, + 'billing_country' => $order->billing->country, + 'billing_phone' => $order->billing->phone, + 'billing_address'=> pmpro_formatAddress( $order->billing->name, + $order->billing->street, + $order->billing->street2, + $order->billing->city, + $order->billing->state, + $order->billing->zip, + $order->billing->country, + $order->billing->phone ), + 'cardtype' => $order->cardtype, + 'accountnumber' => hideCardNumber( $order->accountnumber ), + 'expirationmonth' => $order->expirationmonth, + 'expirationyear' => $order->expirationyear, + ); + } +} + +/** + * Register the email template. + * + * @since TBD + * + * @param array $email_templates The email templates (template slug => email template class name) + * @return array The modified email templates array. + */ +function pmpro_email_templates_billing_failure( $email_templates ) { + $email_templates['billing_failure'] = 'PMPro_Email_Template_Billing_Failure'; + return $email_templates; +} +add_filter( 'pmpro_email_templates', 'pmpro_email_templates_billing_failure' ); \ No newline at end of file diff --git a/includes/email-templates.php b/includes/email-templates.php index e0d2734a2..534eb9360 100644 --- a/includes/email-templates.php +++ b/includes/email-templates.php @@ -60,26 +60,6 @@

Log in to your WordPress dashboard here: !!login_url!!

', 'paid-memberships-pro' ), 'help_text' => __( 'Members can update the payment method associated with their recurring subscription. This email is sent to the site administrator as a confirmation of a payment method update.', 'paid-memberships-pro' ) ), - 'billing_failure' => array( - 'subject' => __( "Membership payment failed at !!sitename!!", 'paid-memberships-pro' ), - 'description' => __( 'Payment Failure', 'paid-memberships-pro' ), - 'body' => __( '

The current subscription payment for level !!membership_level_name!! at !!sitename!! membership has failed. Please click the following link to log in and update your billing information to avoid account suspension.
!!login_url!!

- -

Account: !!display_name!! (!!user_email!!)

', 'paid-memberships-pro' ), - 'help_text' => __( 'This email is sent out if a recurring payment has failed, usually due to an expired or cancelled credit card. This email is sent to the member to allowing them time to update payment information without a disruption in access to your site.', 'paid-memberships-pro' ) - ), - 'billing_failure_admin' => array( - 'subject' => __( "Membership payment failed for !!display_name!! at !!sitename!!", 'paid-memberships-pro' ), - 'description' => __( 'Payment Failure (admin)', 'paid-memberships-pro' ), - 'body' => __( '

The subscription payment for !!user_login!! for level !!membership_level_name!! at !!sitename!! has failed.

- -

Account: !!display_name!! (!!user_email!!)

-

Membership Level: !!membership_level_name!!

- -

Log in to your WordPress admin here: !!login_url!!

-', 'paid-memberships-pro' ), - 'help_text' => __( 'This email is sent out if a recurring payment has failed for a member, usually due to an expired or cancelled credit card. This email is sent to the site administrator.', 'paid-memberships-pro' ) - ), 'cancel_on_next_payment_date' => array( 'subject' => __( "Your payment subscription at !!sitename!! has been CANCELLED", 'paid-memberships-pro' ), 'description' => __('Canceled Auto-Renewals', 'paid-memberships-pro'), diff --git a/paid-memberships-pro.php b/paid-memberships-pro.php index fc919808d..55f827140 100644 --- a/paid-memberships-pro.php +++ b/paid-memberships-pro.php @@ -55,6 +55,8 @@ require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-cancel-admin.php' ); // cancel email template require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-admin-change.php' ); // change email template require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-admin-change-admin.php' ); // change email template +require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-billing-failure.php' ); // billing failure email template +require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-billing-failure-admin.php' ); // billing failure email template require_once( PMPRO_DIR . '/includes/filters.php' ); // filters, hacks, etc, moved into the plugin require_once( PMPRO_DIR . '/includes/reports.php' ); // load reports for admin (reports may also include tracking code, etc)