blob: 01af4641364a2b70d1a0add80757ce4098b9366f [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly.
4}
5
6/**
7 * Base for Failed Renewal/Pre-Order Authentication Notifications.
8 *
9 * @extends WC_Email
10 */
11abstract class WC_Stripe_Email_Failed_Authentication extends WC_Email {
12 /**
13 * An instance of the email, which would normally be sent after a failed payment.
14 *
15 * @var WC_Email
16 */
17 public $original_email;
18
19 /**
20 * Generates the HTML for the email while keeping the `template_base` in mind.
21 *
22 * @return string
23 */
24 public function get_content_html() {
25 ob_start();
26 wc_get_template(
27 $this->template_html,
28 [
29 'order' => $this->object,
30 'email_heading' => $this->get_heading(),
31 'sent_to_admin' => false,
32 'plain_text' => false,
33 'authorization_url' => $this->get_authorization_url( $this->object ),
34 'email' => $this,
35 ],
36 '',
37 $this->template_base
38 );
39 return ob_get_clean();
40 }
41
42 /**
43 * Generates the plain text for the email while keeping the `template_base` in mind.
44 *
45 * @return string
46 */
47 public function get_content_plain() {
48 ob_start();
49 wc_get_template(
50 $this->template_plain,
51 [
52 'order' => $this->object,
53 'email_heading' => $this->get_heading(),
54 'sent_to_admin' => false,
55 'plain_text' => true,
56 'authorization_url' => $this->get_authorization_url( $this->object ),
57 'email' => $this,
58 ],
59 '',
60 $this->template_base
61 );
62 return ob_get_clean();
63 }
64
65 /**
66 * Generates the URL, which will be used to authenticate the payment.
67 *
68 * @param WC_Order $order The order whose payment needs authentication.
69 * @return string
70 */
71 public function get_authorization_url( $order ) {
72 return add_query_arg( 'wc-stripe-confirmation', 1, $order->get_checkout_payment_url( false ) );
73 }
74
75 /**
76 * Uses specific fields from `WC_Email_Customer_Invoice` for this email.
77 */
78 public function init_form_fields() {
79 parent::init_form_fields();
80 $base_fields = $this->form_fields;
81
82 $this->form_fields = [
83 'enabled' => [
84 'title' => _x( 'Enable/Disable', 'an email notification', 'woocommerce-gateway-stripe' ),
85 'type' => 'checkbox',
86 'label' => __( 'Enable this email notification', 'woocommerce-gateway-stripe' ),
87 'default' => 'yes',
88 ],
89
90 'subject' => $base_fields['subject'],
91 'heading' => $base_fields['heading'],
92 'email_type' => $base_fields['email_type'],
93 ];
94 }
95
96 /**
97 * Triggers the email.
98 *
99 * @param WC_Order $order The renewal order whose payment failed.
100 */
101 public function trigger( $order ) {
102 if ( ! $this->is_enabled() ) {
103 return;
104 }
105
106 $this->object = $order;
107
108 if ( method_exists( $order, 'get_billing_email' ) ) {
109 $this->recipient = $order->get_billing_email();
110 } else {
111 $this->recipient = $order->billing_email;
112 }
113
114 $this->find['order_date'] = '{order_date}';
115 if ( function_exists( 'wc_format_datetime' ) ) { // WC 3.0+
116 $this->replace['order_date'] = wc_format_datetime( $order->get_date_created() );
117 } else { // WC < 3.0
118 $this->replace['order_date'] = $order->date_created->date_i18n( wc_date_format() );
119 }
120
121 $this->find['order_number'] = '{order_number}';
122 $this->replace['order_number'] = $order->get_order_number();
123
124 $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
125 }
126}