blob: 64c9f158f8a886a07e0657af04807d5608ba73dc [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly.
4}
5
6/**
7 * Failed Renewal/Pre-Order Authentication Notification
8 *
9 * @extends WC_Email_Customer_Invoice
10 */
11class WC_Stripe_Email_Failed_Renewal_Authentication extends WC_Stripe_Email_Failed_Authentication {
12 /**
13 * Constructor.
14 *
15 * @param WC_Email[] $email_classes All existing instances of WooCommerce emails.
16 */
17 public function __construct( $email_classes = [] ) {
18 $this->id = 'failed_renewal_authentication';
19 $this->title = __( 'Failed Subscription Renewal SCA Authentication', 'woocommerce-gateway-stripe' );
20 $this->description = __( 'Sent to a customer when a renewal fails because the transaction requires an SCA verification. The email contains renewal order information and payment links.', 'woocommerce-gateway-stripe' );
21 $this->customer_email = true;
22
23 $this->template_html = 'emails/failed-renewal-authentication.php';
24 $this->template_plain = 'emails/plain/failed-renewal-authentication.php';
25 $this->template_base = plugin_dir_path( WC_STRIPE_MAIN_FILE ) . 'templates/';
26
27 // Triggers the email at the correct hook.
28 add_action( 'wc_gateway_stripe_process_payment_authentication_required', [ $this, 'trigger' ] );
29
30 if ( isset( $email_classes['WCS_Email_Customer_Renewal_Invoice'] ) ) {
31 $this->original_email = $email_classes['WCS_Email_Customer_Renewal_Invoice'];
32 }
33
34 // We want all the parent's methods, with none of its properties, so call its parent's constructor, rather than my parent constructor.
35 parent::__construct();
36 }
37
38 /**
39 * Triggers the email while also disconnecting the original Subscriptions email.
40 *
41 * @param WC_Order $order The order that is being paid.
42 */
43 public function trigger( $order ) {
44 if ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order->get_id() ) || wcs_is_subscription( $order->get_id() ) || wcs_order_contains_renewal( $order->get_id() ) ) ) {
45 parent::trigger( $order );
46
47 // Prevent the renewal email from WooCommerce Subscriptions from being sent.
48 if ( isset( $this->original_email ) ) {
49 remove_action( 'woocommerce_generated_manual_renewal_order_renewal_notification', [ $this->original_email, 'trigger' ] );
50 remove_action( 'woocommerce_order_status_failed_renewal_notification', [ $this->original_email, 'trigger' ] );
51 }
52
53 // Prevent the retry email from WooCommerce Subscriptions from being sent.
54 add_filter( 'wcs_get_retry_rule_raw', [ $this, 'prevent_retry_notification_email' ], 100, 3 );
55
56 // Send email to store owner indicating communication is happening with the customer to request authentication.
57 add_filter( 'wcs_get_retry_rule_raw', [ $this, 'set_store_owner_custom_email' ], 100, 3 );
58 }
59 }
60
61 /**
62 * Returns the default subject of the email (modifyable in settings).
63 *
64 * @return string
65 */
66 public function get_default_subject() {
67 return __( 'Payment authorization needed for renewal of {site_title} order {order_number}', 'woocommerce-gateway-stripe' );
68 }
69
70 /**
71 * Returns the default heading of the email (modifyable in settings).
72 *
73 * @return string
74 */
75 public function get_default_heading() {
76 return __( 'Payment authorization needed for renewal of order {order_number}', 'woocommerce-gateway-stripe' );
77 }
78
79 /**
80 * Prevent all customer-facing retry notifications from being sent after this email.
81 *
82 * @param array $rule_array The raw details about the retry rule.
83 * @param int $retry_number The number of the retry.
84 * @param int $order_id The ID of the order that needs payment.
85 * @return array
86 */
87 public function prevent_retry_notification_email( $rule_array, $retry_number, $order_id ) {
88 if ( wcs_get_objects_property( $this->object, 'id' ) === $order_id ) {
89 $rule_array['email_template_customer'] = '';
90 }
91
92 return $rule_array;
93 }
94
95 /**
96 * Send store owner a different email when the retry is related to an authentication required error.
97 *
98 * @param array $rule_array The raw details about the retry rule.
99 * @param int $retry_number The number of the retry.
100 * @param int $order_id The ID of the order that needs payment.
101 * @return array
102 */
103 public function set_store_owner_custom_email( $rule_array, $retry_number, $order_id ) {
104 if (
105 wcs_get_objects_property( $this->object, 'id' ) === $order_id &&
106 '' !== $rule_array['email_template_admin'] // Only send our email if a retry admin email was already going to be sent.
107 ) {
108 $rule_array['email_template_admin'] = 'WC_Stripe_Email_Failed_Authentication_Retry';
109 }
110
111 return $rule_array;
112 }
113}