@@ -12,455 +12,455 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Invoice_Notification_Emails { |
14 | 14 | |
15 | - /** |
|
16 | - * The array of invoice email actions. |
|
17 | - * |
|
18 | - * @param array |
|
19 | - */ |
|
20 | - public $invoice_actions; |
|
21 | - |
|
22 | - /** |
|
23 | - * Class constructor |
|
24 | - * |
|
25 | - */ |
|
26 | - public function __construct() { |
|
27 | - |
|
28 | - $this->invoice_actions = apply_filters( |
|
29 | - 'getpaid_notification_email_invoice_triggers', |
|
30 | - array( |
|
31 | - 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
32 | - 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
33 | - 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
34 | - 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
35 | - 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
36 | - 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
37 | - 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
38 | - 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
39 | - 'getpaid_new_customer_note' => 'user_note', |
|
40 | - 'getpaid_daily_maintenance' => 'overdue', |
|
41 | - ) |
|
42 | - ); |
|
43 | - |
|
44 | - $this->init_hooks(); |
|
45 | - |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Registers email hooks. |
|
50 | - */ |
|
51 | - public function init_hooks() { |
|
52 | - |
|
53 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
54 | - add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
55 | - |
|
56 | - foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
57 | - $this->init_email_type_hook( $hook, $email_type ); |
|
58 | - } |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Registers an email hook for an invoice action. |
|
63 | - * |
|
64 | - * @param string $hook |
|
65 | - * @param string|array $email_type |
|
66 | - */ |
|
67 | - public function init_email_type_hook( $hook, $email_type ) { |
|
68 | - |
|
69 | - $email_type = wpinv_parse_list( $email_type ); |
|
70 | - |
|
71 | - foreach ( $email_type as $type ) { |
|
72 | - |
|
73 | - $email = new GetPaid_Notification_Email( $type ); |
|
74 | - |
|
75 | - // Abort if it is not active. |
|
76 | - if ( ! $email->is_active() ) { |
|
77 | - continue; |
|
78 | - } |
|
79 | - |
|
80 | - if ( method_exists( $this, $type ) ) { |
|
81 | - add_action( $hook, array( $this, $type ), 100, 2 ); |
|
82 | - continue; |
|
83 | - } |
|
84 | - |
|
85 | - do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
86 | - } |
|
87 | - |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Filters invoice merge tags. |
|
92 | - * |
|
93 | - * @param array $merge_tags |
|
94 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
95 | - */ |
|
96 | - public function invoice_merge_tags( $merge_tags, $object ) { |
|
97 | - |
|
98 | - if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
99 | - return array_merge( |
|
100 | - $merge_tags, |
|
101 | - $this->get_invoice_merge_tags( $object ) |
|
102 | - ); |
|
103 | - } |
|
104 | - |
|
105 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
106 | - return array_merge( |
|
107 | - $merge_tags, |
|
108 | - $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
109 | - ); |
|
110 | - } |
|
111 | - |
|
112 | - return $merge_tags; |
|
113 | - |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Generates invoice merge tags. |
|
118 | - * |
|
119 | - * @param WPInv_Invoice $invoice |
|
120 | - * @return array |
|
121 | - */ |
|
122 | - public function get_invoice_merge_tags( $invoice ) { |
|
123 | - |
|
124 | - // Abort if it does not exist. |
|
125 | - if ( ! $invoice->get_id() ) { |
|
126 | - return array(); |
|
127 | - } |
|
128 | - |
|
129 | - $merge_tags = array( |
|
130 | - '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
131 | - '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
132 | - '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
133 | - '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
134 | - '{email}' => sanitize_email( $invoice->get_email() ), |
|
135 | - '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
136 | - '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
137 | - '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
138 | - '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
139 | - '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
140 | - '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
141 | - '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
142 | - '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
143 | - '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
144 | - '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
145 | - '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
146 | - '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
147 | - '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
148 | - ); |
|
149 | - |
|
150 | - return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Helper function to send an email. |
|
155 | - * |
|
156 | - * @param WPInv_Invoice $invoice |
|
157 | - * @param GetPaid_Notification_Email $email |
|
158 | - * @param string $type |
|
159 | - * @param string|array $recipients |
|
160 | - * @param array $extra_args Extra template args. |
|
161 | - */ |
|
162 | - public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
163 | - |
|
164 | - do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
165 | - |
|
166 | - if ( apply_filters( 'getpaid_skip_invoice_email', false, $type, $invoice ) ) { |
|
167 | - return; |
|
168 | - } |
|
169 | - |
|
170 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
171 | - $merge_tags = $email->get_merge_tags(); |
|
172 | - |
|
173 | - $result = $mailer->send( |
|
174 | - apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
175 | - $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
176 | - $email->get_content( $merge_tags, $extra_args ), |
|
177 | - $email->get_attachments() |
|
178 | - ); |
|
179 | - |
|
180 | - // Maybe send a copy to the admin. |
|
181 | - if ( $email->include_admin_bcc() ) { |
|
182 | - $mailer->send( |
|
183 | - wpinv_get_admin_email(), |
|
184 | - $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
185 | - $email->get_content( $merge_tags ), |
|
186 | - $email->get_attachments() |
|
187 | - ); |
|
188 | - } |
|
189 | - |
|
190 | - if ( $result ) { |
|
191 | - $invoice->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
192 | - } else { |
|
193 | - $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
194 | - } |
|
195 | - |
|
196 | - do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
197 | - |
|
198 | - return $result; |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Also send emails to any cc users. |
|
203 | - * |
|
204 | - * @param array $recipients |
|
205 | - * @param GetPaid_Notification_Email $email |
|
206 | - */ |
|
207 | - public function filter_email_recipients( $recipients, $email ) { |
|
208 | - |
|
209 | - if ( ! $email->is_admin_email() ) { |
|
210 | - $cc = $email->object->get_email_cc(); |
|
211 | - |
|
212 | - if ( ! empty( $cc ) ) { |
|
213 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
214 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
215 | - } |
|
216 | - |
|
217 | - } |
|
218 | - |
|
219 | - return $recipients; |
|
220 | - |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Sends a new invoice notification. |
|
225 | - * |
|
226 | - * @param WPInv_Invoice $invoice |
|
227 | - */ |
|
228 | - public function new_invoice( $invoice ) { |
|
229 | - |
|
230 | - // Only send this email for invoices created via the admin page. |
|
231 | - if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
232 | - return; |
|
233 | - } |
|
234 | - |
|
235 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
236 | - $recipient = wpinv_get_admin_email(); |
|
237 | - |
|
238 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
239 | - |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Sends a cancelled invoice notification. |
|
244 | - * |
|
245 | - * @param WPInv_Invoice $invoice |
|
246 | - */ |
|
247 | - public function cancelled_invoice( $invoice ) { |
|
15 | + /** |
|
16 | + * The array of invoice email actions. |
|
17 | + * |
|
18 | + * @param array |
|
19 | + */ |
|
20 | + public $invoice_actions; |
|
21 | + |
|
22 | + /** |
|
23 | + * Class constructor |
|
24 | + * |
|
25 | + */ |
|
26 | + public function __construct() { |
|
27 | + |
|
28 | + $this->invoice_actions = apply_filters( |
|
29 | + 'getpaid_notification_email_invoice_triggers', |
|
30 | + array( |
|
31 | + 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
32 | + 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
33 | + 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
34 | + 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
35 | + 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
36 | + 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
37 | + 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
38 | + 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
39 | + 'getpaid_new_customer_note' => 'user_note', |
|
40 | + 'getpaid_daily_maintenance' => 'overdue', |
|
41 | + ) |
|
42 | + ); |
|
43 | + |
|
44 | + $this->init_hooks(); |
|
45 | + |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Registers email hooks. |
|
50 | + */ |
|
51 | + public function init_hooks() { |
|
52 | + |
|
53 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
54 | + add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
55 | + |
|
56 | + foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
57 | + $this->init_email_type_hook( $hook, $email_type ); |
|
58 | + } |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Registers an email hook for an invoice action. |
|
63 | + * |
|
64 | + * @param string $hook |
|
65 | + * @param string|array $email_type |
|
66 | + */ |
|
67 | + public function init_email_type_hook( $hook, $email_type ) { |
|
68 | + |
|
69 | + $email_type = wpinv_parse_list( $email_type ); |
|
70 | + |
|
71 | + foreach ( $email_type as $type ) { |
|
72 | + |
|
73 | + $email = new GetPaid_Notification_Email( $type ); |
|
74 | + |
|
75 | + // Abort if it is not active. |
|
76 | + if ( ! $email->is_active() ) { |
|
77 | + continue; |
|
78 | + } |
|
79 | + |
|
80 | + if ( method_exists( $this, $type ) ) { |
|
81 | + add_action( $hook, array( $this, $type ), 100, 2 ); |
|
82 | + continue; |
|
83 | + } |
|
84 | + |
|
85 | + do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
86 | + } |
|
87 | + |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Filters invoice merge tags. |
|
92 | + * |
|
93 | + * @param array $merge_tags |
|
94 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
95 | + */ |
|
96 | + public function invoice_merge_tags( $merge_tags, $object ) { |
|
97 | + |
|
98 | + if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
99 | + return array_merge( |
|
100 | + $merge_tags, |
|
101 | + $this->get_invoice_merge_tags( $object ) |
|
102 | + ); |
|
103 | + } |
|
104 | + |
|
105 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
106 | + return array_merge( |
|
107 | + $merge_tags, |
|
108 | + $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
109 | + ); |
|
110 | + } |
|
111 | + |
|
112 | + return $merge_tags; |
|
113 | + |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Generates invoice merge tags. |
|
118 | + * |
|
119 | + * @param WPInv_Invoice $invoice |
|
120 | + * @return array |
|
121 | + */ |
|
122 | + public function get_invoice_merge_tags( $invoice ) { |
|
123 | + |
|
124 | + // Abort if it does not exist. |
|
125 | + if ( ! $invoice->get_id() ) { |
|
126 | + return array(); |
|
127 | + } |
|
128 | + |
|
129 | + $merge_tags = array( |
|
130 | + '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
131 | + '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
132 | + '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
133 | + '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
134 | + '{email}' => sanitize_email( $invoice->get_email() ), |
|
135 | + '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
136 | + '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
137 | + '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
138 | + '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
139 | + '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
140 | + '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
141 | + '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
142 | + '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
143 | + '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
144 | + '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
145 | + '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
146 | + '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
147 | + '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
148 | + ); |
|
149 | + |
|
150 | + return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Helper function to send an email. |
|
155 | + * |
|
156 | + * @param WPInv_Invoice $invoice |
|
157 | + * @param GetPaid_Notification_Email $email |
|
158 | + * @param string $type |
|
159 | + * @param string|array $recipients |
|
160 | + * @param array $extra_args Extra template args. |
|
161 | + */ |
|
162 | + public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
163 | + |
|
164 | + do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
165 | + |
|
166 | + if ( apply_filters( 'getpaid_skip_invoice_email', false, $type, $invoice ) ) { |
|
167 | + return; |
|
168 | + } |
|
169 | + |
|
170 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
171 | + $merge_tags = $email->get_merge_tags(); |
|
172 | + |
|
173 | + $result = $mailer->send( |
|
174 | + apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
175 | + $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
176 | + $email->get_content( $merge_tags, $extra_args ), |
|
177 | + $email->get_attachments() |
|
178 | + ); |
|
179 | + |
|
180 | + // Maybe send a copy to the admin. |
|
181 | + if ( $email->include_admin_bcc() ) { |
|
182 | + $mailer->send( |
|
183 | + wpinv_get_admin_email(), |
|
184 | + $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
185 | + $email->get_content( $merge_tags ), |
|
186 | + $email->get_attachments() |
|
187 | + ); |
|
188 | + } |
|
189 | + |
|
190 | + if ( $result ) { |
|
191 | + $invoice->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
192 | + } else { |
|
193 | + $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
194 | + } |
|
195 | + |
|
196 | + do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
197 | + |
|
198 | + return $result; |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Also send emails to any cc users. |
|
203 | + * |
|
204 | + * @param array $recipients |
|
205 | + * @param GetPaid_Notification_Email $email |
|
206 | + */ |
|
207 | + public function filter_email_recipients( $recipients, $email ) { |
|
208 | + |
|
209 | + if ( ! $email->is_admin_email() ) { |
|
210 | + $cc = $email->object->get_email_cc(); |
|
211 | + |
|
212 | + if ( ! empty( $cc ) ) { |
|
213 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
214 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
215 | + } |
|
216 | + |
|
217 | + } |
|
218 | + |
|
219 | + return $recipients; |
|
220 | + |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Sends a new invoice notification. |
|
225 | + * |
|
226 | + * @param WPInv_Invoice $invoice |
|
227 | + */ |
|
228 | + public function new_invoice( $invoice ) { |
|
229 | + |
|
230 | + // Only send this email for invoices created via the admin page. |
|
231 | + if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
232 | + return; |
|
233 | + } |
|
234 | + |
|
235 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
236 | + $recipient = wpinv_get_admin_email(); |
|
237 | + |
|
238 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
239 | + |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Sends a cancelled invoice notification. |
|
244 | + * |
|
245 | + * @param WPInv_Invoice $invoice |
|
246 | + */ |
|
247 | + public function cancelled_invoice( $invoice ) { |
|
248 | 248 | |
249 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
250 | - $recipient = wpinv_get_admin_email(); |
|
249 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
250 | + $recipient = wpinv_get_admin_email(); |
|
251 | 251 | |
252 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
252 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
253 | 253 | |
254 | - } |
|
254 | + } |
|
255 | 255 | |
256 | - /** |
|
257 | - * Sends a failed invoice notification. |
|
258 | - * |
|
259 | - * @param WPInv_Invoice $invoice |
|
260 | - */ |
|
261 | - public function failed_invoice( $invoice ) { |
|
256 | + /** |
|
257 | + * Sends a failed invoice notification. |
|
258 | + * |
|
259 | + * @param WPInv_Invoice $invoice |
|
260 | + */ |
|
261 | + public function failed_invoice( $invoice ) { |
|
262 | 262 | |
263 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
264 | - $recipient = wpinv_get_admin_email(); |
|
263 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
264 | + $recipient = wpinv_get_admin_email(); |
|
265 | 265 | |
266 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
266 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
267 | 267 | |
268 | - } |
|
268 | + } |
|
269 | 269 | |
270 | - /** |
|
271 | - * Sends a notification whenever an invoice is put on hold. |
|
272 | - * |
|
273 | - * @param WPInv_Invoice $invoice |
|
274 | - */ |
|
275 | - public function onhold_invoice( $invoice ) { |
|
270 | + /** |
|
271 | + * Sends a notification whenever an invoice is put on hold. |
|
272 | + * |
|
273 | + * @param WPInv_Invoice $invoice |
|
274 | + */ |
|
275 | + public function onhold_invoice( $invoice ) { |
|
276 | 276 | |
277 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
278 | - $recipient = $invoice->get_email(); |
|
277 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
278 | + $recipient = $invoice->get_email(); |
|
279 | 279 | |
280 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
280 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
281 | 281 | |
282 | - } |
|
282 | + } |
|
283 | 283 | |
284 | - /** |
|
285 | - * Sends a notification whenever an invoice is marked as processing payment. |
|
286 | - * |
|
287 | - * @param WPInv_Invoice $invoice |
|
288 | - */ |
|
289 | - public function processing_invoice( $invoice ) { |
|
284 | + /** |
|
285 | + * Sends a notification whenever an invoice is marked as processing payment. |
|
286 | + * |
|
287 | + * @param WPInv_Invoice $invoice |
|
288 | + */ |
|
289 | + public function processing_invoice( $invoice ) { |
|
290 | 290 | |
291 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
292 | - $recipient = $invoice->get_email(); |
|
291 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
292 | + $recipient = $invoice->get_email(); |
|
293 | 293 | |
294 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
294 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
295 | 295 | |
296 | - } |
|
296 | + } |
|
297 | 297 | |
298 | - /** |
|
299 | - * Sends a notification whenever an invoice is paid. |
|
300 | - * |
|
301 | - * @param WPInv_Invoice $invoice |
|
302 | - */ |
|
303 | - public function completed_invoice( $invoice ) { |
|
298 | + /** |
|
299 | + * Sends a notification whenever an invoice is paid. |
|
300 | + * |
|
301 | + * @param WPInv_Invoice $invoice |
|
302 | + */ |
|
303 | + public function completed_invoice( $invoice ) { |
|
304 | 304 | |
305 | - // (Maybe) abort if it is a renewal invoice. |
|
306 | - if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
307 | - return; |
|
308 | - } |
|
305 | + // (Maybe) abort if it is a renewal invoice. |
|
306 | + if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
307 | + return; |
|
308 | + } |
|
309 | 309 | |
310 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
311 | - $recipient = $invoice->get_email(); |
|
310 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
311 | + $recipient = $invoice->get_email(); |
|
312 | 312 | |
313 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
313 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
314 | 314 | |
315 | - } |
|
315 | + } |
|
316 | 316 | |
317 | - /** |
|
318 | - * Sends a notification whenever an invoice is refunded. |
|
319 | - * |
|
320 | - * @param WPInv_Invoice $invoice |
|
321 | - */ |
|
322 | - public function refunded_invoice( $invoice ) { |
|
317 | + /** |
|
318 | + * Sends a notification whenever an invoice is refunded. |
|
319 | + * |
|
320 | + * @param WPInv_Invoice $invoice |
|
321 | + */ |
|
322 | + public function refunded_invoice( $invoice ) { |
|
323 | 323 | |
324 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
325 | - $recipient = $invoice->get_email(); |
|
324 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
325 | + $recipient = $invoice->get_email(); |
|
326 | 326 | |
327 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
327 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
328 | 328 | |
329 | - } |
|
329 | + } |
|
330 | 330 | |
331 | - /** |
|
332 | - * Notifies a user about new invoices |
|
333 | - * |
|
334 | - * @param WPInv_Invoice $invoice |
|
335 | - * @param bool $force |
|
336 | - */ |
|
337 | - public function user_invoice( $invoice, $force = false ) { |
|
331 | + /** |
|
332 | + * Notifies a user about new invoices |
|
333 | + * |
|
334 | + * @param WPInv_Invoice $invoice |
|
335 | + * @param bool $force |
|
336 | + */ |
|
337 | + public function user_invoice( $invoice, $force = false ) { |
|
338 | 338 | |
339 | - if ( ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) { |
|
340 | - return; |
|
341 | - } |
|
339 | + if ( ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) { |
|
340 | + return; |
|
341 | + } |
|
342 | 342 | |
343 | - // Only send this email for invoices created via the admin page. |
|
344 | - if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) { |
|
345 | - return; |
|
346 | - } |
|
343 | + // Only send this email for invoices created via the admin page. |
|
344 | + if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) { |
|
345 | + return; |
|
346 | + } |
|
347 | 347 | |
348 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
349 | - $recipient = $invoice->get_email(); |
|
348 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
349 | + $recipient = $invoice->get_email(); |
|
350 | 350 | |
351 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
351 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
352 | 352 | |
353 | - } |
|
353 | + } |
|
354 | 354 | |
355 | - /** |
|
356 | - * Checks if an invoice is a payment form invoice. |
|
357 | - * |
|
358 | - * @param int $invoice |
|
359 | - * @return bool |
|
360 | - */ |
|
361 | - public function is_payment_form_invoice( $invoice ) { |
|
362 | - return empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
363 | - } |
|
355 | + /** |
|
356 | + * Checks if an invoice is a payment form invoice. |
|
357 | + * |
|
358 | + * @param int $invoice |
|
359 | + * @return bool |
|
360 | + */ |
|
361 | + public function is_payment_form_invoice( $invoice ) { |
|
362 | + return empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
363 | + } |
|
364 | 364 | |
365 | - /** |
|
366 | - * Notifies admin about new invoice notes |
|
367 | - * |
|
368 | - * @param WPInv_Invoice $invoice |
|
369 | - * @param string $note |
|
370 | - */ |
|
371 | - public function user_note( $invoice, $note ) { |
|
372 | - |
|
373 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
374 | - $recipient = $invoice->get_email(); |
|
365 | + /** |
|
366 | + * Notifies admin about new invoice notes |
|
367 | + * |
|
368 | + * @param WPInv_Invoice $invoice |
|
369 | + * @param string $note |
|
370 | + */ |
|
371 | + public function user_note( $invoice, $note ) { |
|
372 | + |
|
373 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
374 | + $recipient = $invoice->get_email(); |
|
375 | 375 | |
376 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
377 | - |
|
378 | - } |
|
376 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
377 | + |
|
378 | + } |
|
379 | 379 | |
380 | - /** |
|
381 | - * (Force) Sends overdue notices. |
|
382 | - * |
|
383 | - * @param WPInv_Invoice $invoice |
|
384 | - */ |
|
385 | - public function force_send_overdue_notice( $invoice ) { |
|
386 | - $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
387 | - return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Sends overdue notices. |
|
392 | - * |
|
393 | - * @TODO: Create an invoices query class. |
|
394 | - */ |
|
395 | - public function overdue() { |
|
396 | - global $wpdb; |
|
397 | - |
|
398 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
399 | - |
|
400 | - // Fetch reminder days. |
|
401 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
402 | - |
|
403 | - // Abort if non is set. |
|
404 | - if ( empty( $reminder_days ) ) { |
|
405 | - return; |
|
406 | - } |
|
407 | - |
|
408 | - // Retrieve date query. |
|
409 | - $date_query = $this->get_date_query( $reminder_days ); |
|
410 | - |
|
411 | - // Invoices table. |
|
412 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
413 | - |
|
414 | - // Fetch invoices. |
|
415 | - $invoices = $wpdb->get_col( |
|
416 | - "SELECT posts.ID FROM $wpdb->posts as posts |
|
380 | + /** |
|
381 | + * (Force) Sends overdue notices. |
|
382 | + * |
|
383 | + * @param WPInv_Invoice $invoice |
|
384 | + */ |
|
385 | + public function force_send_overdue_notice( $invoice ) { |
|
386 | + $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
387 | + return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Sends overdue notices. |
|
392 | + * |
|
393 | + * @TODO: Create an invoices query class. |
|
394 | + */ |
|
395 | + public function overdue() { |
|
396 | + global $wpdb; |
|
397 | + |
|
398 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
399 | + |
|
400 | + // Fetch reminder days. |
|
401 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
402 | + |
|
403 | + // Abort if non is set. |
|
404 | + if ( empty( $reminder_days ) ) { |
|
405 | + return; |
|
406 | + } |
|
407 | + |
|
408 | + // Retrieve date query. |
|
409 | + $date_query = $this->get_date_query( $reminder_days ); |
|
410 | + |
|
411 | + // Invoices table. |
|
412 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
413 | + |
|
414 | + // Fetch invoices. |
|
415 | + $invoices = $wpdb->get_col( |
|
416 | + "SELECT posts.ID FROM $wpdb->posts as posts |
|
417 | 417 | LEFT JOIN $table as invoices ON invoices.post_id = posts.ID |
418 | 418 | WHERE posts.post_type = 'wpi_invoice' AND posts.post_status = 'wpi-pending' $date_query"); |
419 | 419 | |
420 | - foreach ( $invoices as $invoice ) { |
|
420 | + foreach ( $invoices as $invoice ) { |
|
421 | 421 | |
422 | - // Only send this email for invoices created via the admin page. |
|
423 | - if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
424 | - $invoice = new WPInv_Invoice( $invoice ); |
|
425 | - $email->object = $invoice; |
|
422 | + // Only send this email for invoices created via the admin page. |
|
423 | + if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
424 | + $invoice = new WPInv_Invoice( $invoice ); |
|
425 | + $email->object = $invoice; |
|
426 | 426 | |
427 | - if ( $invoice->needs_payment() ) { |
|
428 | - $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
429 | - } |
|
427 | + if ( $invoice->needs_payment() ) { |
|
428 | + $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
429 | + } |
|
430 | 430 | |
431 | - } |
|
431 | + } |
|
432 | 432 | |
433 | - } |
|
433 | + } |
|
434 | 434 | |
435 | - } |
|
435 | + } |
|
436 | 436 | |
437 | - /** |
|
438 | - * Calculates the date query for an invoices query |
|
439 | - * |
|
440 | - * @param array $reminder_days |
|
441 | - * @return string |
|
442 | - */ |
|
443 | - public function get_date_query( $reminder_days ) { |
|
437 | + /** |
|
438 | + * Calculates the date query for an invoices query |
|
439 | + * |
|
440 | + * @param array $reminder_days |
|
441 | + * @return string |
|
442 | + */ |
|
443 | + public function get_date_query( $reminder_days ) { |
|
444 | 444 | |
445 | - $date_query = array( |
|
446 | - 'relation' => 'OR' |
|
447 | - ); |
|
445 | + $date_query = array( |
|
446 | + 'relation' => 'OR' |
|
447 | + ); |
|
448 | 448 | |
449 | - foreach ( $reminder_days as $days ) { |
|
450 | - $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
449 | + foreach ( $reminder_days as $days ) { |
|
450 | + $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
451 | 451 | |
452 | - $date_query[] = array( |
|
453 | - 'year' => $date['year'], |
|
454 | - 'month' => $date['month'], |
|
455 | - 'day' => $date['day'], |
|
456 | - ); |
|
452 | + $date_query[] = array( |
|
453 | + 'year' => $date['year'], |
|
454 | + 'month' => $date['month'], |
|
455 | + 'day' => $date['day'], |
|
456 | + ); |
|
457 | 457 | |
458 | - } |
|
458 | + } |
|
459 | 459 | |
460 | - $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
460 | + $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
461 | 461 | |
462 | - return $date_query->get_sql(); |
|
462 | + return $date_query->get_sql(); |
|
463 | 463 | |
464 | - } |
|
464 | + } |
|
465 | 465 | |
466 | 466 | } |
@@ -12,456 +12,456 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Paypal_Gateway_IPN_Handler { |
14 | 14 | |
15 | - /** |
|
16 | - * Payment method id. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - protected $id = 'paypal'; |
|
21 | - |
|
22 | - /** |
|
23 | - * Payment method object. |
|
24 | - * |
|
25 | - * @var GetPaid_Paypal_Gateway |
|
26 | - */ |
|
27 | - protected $gateway; |
|
28 | - |
|
29 | - /** |
|
30 | - * Class constructor. |
|
31 | - * |
|
32 | - * @param GetPaid_Paypal_Gateway $gateway |
|
33 | - */ |
|
34 | - public function __construct( $gateway ) { |
|
35 | - $this->gateway = $gateway; |
|
36 | - $this->verify_ipn(); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Processes ipns and marks payments as complete. |
|
41 | - * |
|
42 | - * @return void |
|
43 | - */ |
|
44 | - public function verify_ipn() { |
|
45 | - |
|
46 | - wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
47 | - |
|
48 | - // Validate the IPN. |
|
49 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
50 | - wp_die( 'PayPal IPN Request Failure', 500 ); |
|
51 | - } |
|
52 | - |
|
53 | - // Process the IPN. |
|
54 | - $posted = wp_unslash( $_POST ); |
|
55 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
56 | - |
|
57 | - // Abort if it was not paid by our gateway. |
|
58 | - if ( $this->id != $invoice->get_gateway() ) { |
|
59 | - wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
60 | - wp_die( 'Invoice not paid via PayPal', 500 ); |
|
61 | - } |
|
62 | - |
|
63 | - $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
64 | - $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
65 | - |
|
66 | - wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
67 | - wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
68 | - |
|
69 | - if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
70 | - call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
71 | - wpinv_error_log( 'Done processing IPN', false ); |
|
72 | - wp_die( 'Processed', 200 ); |
|
73 | - } |
|
74 | - |
|
75 | - wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
76 | - wp_die( 'Unsupported IPN type', 200 ); |
|
77 | - |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Retrieves IPN Invoice. |
|
82 | - * |
|
83 | - * @param array $posted |
|
84 | - * @return WPInv_Invoice |
|
85 | - */ |
|
86 | - protected function get_ipn_invoice( $posted ) { |
|
87 | - |
|
88 | - wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
89 | - |
|
90 | - if ( ! empty( $posted['custom'] ) ) { |
|
91 | - $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
92 | - |
|
93 | - if ( $invoice->exists() ) { |
|
94 | - wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
95 | - return $invoice; |
|
96 | - } |
|
97 | - |
|
98 | - } |
|
99 | - |
|
100 | - wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
101 | - wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Check PayPal IPN validity. |
|
106 | - */ |
|
107 | - protected function validate_ipn() { |
|
108 | - |
|
109 | - wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
110 | - |
|
111 | - // Retrieve the associated invoice. |
|
112 | - $posted = wp_unslash( $_POST ); |
|
113 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
114 | - |
|
115 | - if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
116 | - wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data' ); |
|
117 | - } |
|
118 | - |
|
119 | - // Validate the IPN. |
|
120 | - $posted['cmd'] = '_notify-validate'; |
|
121 | - |
|
122 | - // Send back post vars to paypal. |
|
123 | - $params = array( |
|
124 | - 'body' => $posted, |
|
125 | - 'timeout' => 60, |
|
126 | - 'httpversion' => '1.1', |
|
127 | - 'compress' => false, |
|
128 | - 'decompress' => false, |
|
129 | - 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
130 | - ); |
|
131 | - |
|
132 | - // Post back to get a response. |
|
133 | - $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
134 | - |
|
135 | - // Check to see if the request was valid. |
|
136 | - if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
137 | - wpinv_error_log( $response['body'], 'Received valid response from PayPal IPN' ); |
|
138 | - return true; |
|
139 | - } |
|
140 | - |
|
141 | - if ( is_wp_error( $response ) ) { |
|
142 | - wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
143 | - return false; |
|
144 | - } |
|
15 | + /** |
|
16 | + * Payment method id. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + protected $id = 'paypal'; |
|
21 | + |
|
22 | + /** |
|
23 | + * Payment method object. |
|
24 | + * |
|
25 | + * @var GetPaid_Paypal_Gateway |
|
26 | + */ |
|
27 | + protected $gateway; |
|
28 | + |
|
29 | + /** |
|
30 | + * Class constructor. |
|
31 | + * |
|
32 | + * @param GetPaid_Paypal_Gateway $gateway |
|
33 | + */ |
|
34 | + public function __construct( $gateway ) { |
|
35 | + $this->gateway = $gateway; |
|
36 | + $this->verify_ipn(); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Processes ipns and marks payments as complete. |
|
41 | + * |
|
42 | + * @return void |
|
43 | + */ |
|
44 | + public function verify_ipn() { |
|
45 | + |
|
46 | + wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
47 | + |
|
48 | + // Validate the IPN. |
|
49 | + if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
50 | + wp_die( 'PayPal IPN Request Failure', 500 ); |
|
51 | + } |
|
52 | + |
|
53 | + // Process the IPN. |
|
54 | + $posted = wp_unslash( $_POST ); |
|
55 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
56 | + |
|
57 | + // Abort if it was not paid by our gateway. |
|
58 | + if ( $this->id != $invoice->get_gateway() ) { |
|
59 | + wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
60 | + wp_die( 'Invoice not paid via PayPal', 500 ); |
|
61 | + } |
|
62 | + |
|
63 | + $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
64 | + $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
65 | + |
|
66 | + wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
67 | + wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
68 | + |
|
69 | + if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
70 | + call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
71 | + wpinv_error_log( 'Done processing IPN', false ); |
|
72 | + wp_die( 'Processed', 200 ); |
|
73 | + } |
|
74 | + |
|
75 | + wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
76 | + wp_die( 'Unsupported IPN type', 200 ); |
|
77 | + |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Retrieves IPN Invoice. |
|
82 | + * |
|
83 | + * @param array $posted |
|
84 | + * @return WPInv_Invoice |
|
85 | + */ |
|
86 | + protected function get_ipn_invoice( $posted ) { |
|
87 | + |
|
88 | + wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
89 | + |
|
90 | + if ( ! empty( $posted['custom'] ) ) { |
|
91 | + $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
92 | + |
|
93 | + if ( $invoice->exists() ) { |
|
94 | + wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
95 | + return $invoice; |
|
96 | + } |
|
97 | + |
|
98 | + } |
|
99 | + |
|
100 | + wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
101 | + wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Check PayPal IPN validity. |
|
106 | + */ |
|
107 | + protected function validate_ipn() { |
|
108 | + |
|
109 | + wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
110 | + |
|
111 | + // Retrieve the associated invoice. |
|
112 | + $posted = wp_unslash( $_POST ); |
|
113 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
114 | + |
|
115 | + if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
116 | + wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data' ); |
|
117 | + } |
|
118 | + |
|
119 | + // Validate the IPN. |
|
120 | + $posted['cmd'] = '_notify-validate'; |
|
121 | + |
|
122 | + // Send back post vars to paypal. |
|
123 | + $params = array( |
|
124 | + 'body' => $posted, |
|
125 | + 'timeout' => 60, |
|
126 | + 'httpversion' => '1.1', |
|
127 | + 'compress' => false, |
|
128 | + 'decompress' => false, |
|
129 | + 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
130 | + ); |
|
131 | + |
|
132 | + // Post back to get a response. |
|
133 | + $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
134 | + |
|
135 | + // Check to see if the request was valid. |
|
136 | + if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
137 | + wpinv_error_log( $response['body'], 'Received valid response from PayPal IPN' ); |
|
138 | + return true; |
|
139 | + } |
|
140 | + |
|
141 | + if ( is_wp_error( $response ) ) { |
|
142 | + wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
143 | + return false; |
|
144 | + } |
|
145 | 145 | |
146 | - wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
147 | - return false; |
|
148 | - |
|
149 | - } |
|
146 | + wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
147 | + return false; |
|
148 | + |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Check currency from IPN matches the invoice. |
|
153 | - * |
|
154 | - * @param WPInv_Invoice $invoice Invoice object. |
|
155 | - * @param string $currency currency to validate. |
|
156 | - */ |
|
157 | - protected function validate_ipn_currency( $invoice, $currency ) { |
|
151 | + /** |
|
152 | + * Check currency from IPN matches the invoice. |
|
153 | + * |
|
154 | + * @param WPInv_Invoice $invoice Invoice object. |
|
155 | + * @param string $currency currency to validate. |
|
156 | + */ |
|
157 | + protected function validate_ipn_currency( $invoice, $currency ) { |
|
158 | 158 | |
159 | - if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
159 | + if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
160 | 160 | |
161 | - /* translators: %s: currency code. */ |
|
162 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
161 | + /* translators: %s: currency code. */ |
|
162 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
163 | 163 | |
164 | - wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
165 | - } |
|
164 | + wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
165 | + } |
|
166 | 166 | |
167 | - wpinv_error_log( $currency, 'Validated IPN Currency' ); |
|
168 | - } |
|
167 | + wpinv_error_log( $currency, 'Validated IPN Currency' ); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Check payment amount from IPN matches the invoice. |
|
172 | - * |
|
173 | - * @param WPInv_Invoice $invoice Invoice object. |
|
174 | - * @param float $amount amount to validate. |
|
175 | - */ |
|
176 | - protected function validate_ipn_amount( $invoice, $amount ) { |
|
177 | - if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
170 | + /** |
|
171 | + * Check payment amount from IPN matches the invoice. |
|
172 | + * |
|
173 | + * @param WPInv_Invoice $invoice Invoice object. |
|
174 | + * @param float $amount amount to validate. |
|
175 | + */ |
|
176 | + protected function validate_ipn_amount( $invoice, $amount ) { |
|
177 | + if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
178 | 178 | |
179 | - /* translators: %s: Amount. */ |
|
180 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
179 | + /* translators: %s: Amount. */ |
|
180 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
181 | 181 | |
182 | - wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
183 | - } |
|
182 | + wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
183 | + } |
|
184 | 184 | |
185 | - wpinv_error_log( $amount, 'Validated IPN Amount' ); |
|
186 | - } |
|
185 | + wpinv_error_log( $amount, 'Validated IPN Amount' ); |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * Verify receiver email from PayPal. |
|
190 | - * |
|
191 | - * @param WPInv_Invoice $invoice Invoice object. |
|
192 | - * @param string $receiver_email Email to validate. |
|
193 | - */ |
|
194 | - protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
195 | - $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
188 | + /** |
|
189 | + * Verify receiver email from PayPal. |
|
190 | + * |
|
191 | + * @param WPInv_Invoice $invoice Invoice object. |
|
192 | + * @param string $receiver_email Email to validate. |
|
193 | + */ |
|
194 | + protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
195 | + $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
196 | 196 | |
197 | - if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
198 | - wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
197 | + if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
198 | + wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
199 | 199 | |
200 | - /* translators: %s: email address . */ |
|
201 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
200 | + /* translators: %s: email address . */ |
|
201 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
202 | 202 | |
203 | - return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
204 | - } |
|
203 | + return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
204 | + } |
|
205 | 205 | |
206 | - wpinv_error_log( 'Validated PayPal Email', false ); |
|
207 | - } |
|
206 | + wpinv_error_log( 'Validated PayPal Email', false ); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Handles one time payments. |
|
211 | - * |
|
212 | - * @param WPInv_Invoice $invoice Invoice object. |
|
213 | - * @param array $posted Posted data. |
|
214 | - */ |
|
215 | - protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
216 | - |
|
217 | - // Collect payment details |
|
218 | - $payment_status = strtolower( $posted['payment_status'] ); |
|
219 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
220 | - |
|
221 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
222 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
223 | - |
|
224 | - // Update the transaction id. |
|
225 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
226 | - $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
227 | - $invoice->save(); |
|
228 | - } |
|
209 | + /** |
|
210 | + * Handles one time payments. |
|
211 | + * |
|
212 | + * @param WPInv_Invoice $invoice Invoice object. |
|
213 | + * @param array $posted Posted data. |
|
214 | + */ |
|
215 | + protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
216 | + |
|
217 | + // Collect payment details |
|
218 | + $payment_status = strtolower( $posted['payment_status'] ); |
|
219 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
220 | + |
|
221 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
222 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
223 | + |
|
224 | + // Update the transaction id. |
|
225 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
226 | + $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
227 | + $invoice->save(); |
|
228 | + } |
|
229 | 229 | |
230 | - // Process a refund. |
|
231 | - if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
230 | + // Process a refund. |
|
231 | + if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
232 | 232 | |
233 | - update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
233 | + update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
234 | 234 | |
235 | - if ( ! $invoice->is_refunded() ) { |
|
236 | - $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
237 | - } |
|
235 | + if ( ! $invoice->is_refunded() ) { |
|
236 | + $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
237 | + } |
|
238 | 238 | |
239 | - return wpinv_error_log( $posted['reason_code'], false ); |
|
240 | - } |
|
239 | + return wpinv_error_log( $posted['reason_code'], false ); |
|
240 | + } |
|
241 | 241 | |
242 | - // Process payments. |
|
243 | - if ( $payment_status == 'completed' ) { |
|
242 | + // Process payments. |
|
243 | + if ( $payment_status == 'completed' ) { |
|
244 | 244 | |
245 | - if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
246 | - return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
247 | - } |
|
245 | + if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
246 | + return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
247 | + } |
|
248 | 248 | |
249 | - $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
249 | + $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
250 | 250 | |
251 | - $note = ''; |
|
251 | + $note = ''; |
|
252 | 252 | |
253 | - if ( ! empty( $posted['mc_fee'] ) ) { |
|
254 | - $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
255 | - } |
|
253 | + if ( ! empty( $posted['mc_fee'] ) ) { |
|
254 | + $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
255 | + } |
|
256 | 256 | |
257 | - if ( ! empty( $posted['payer_status'] ) ) { |
|
258 | - $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
259 | - } |
|
257 | + if ( ! empty( $posted['payer_status'] ) ) { |
|
258 | + $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
259 | + } |
|
260 | 260 | |
261 | - $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
262 | - return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
261 | + $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
262 | + return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
263 | 263 | |
264 | - } |
|
264 | + } |
|
265 | 265 | |
266 | - // Pending payments. |
|
267 | - if ( $payment_status == 'pending' ) { |
|
266 | + // Pending payments. |
|
267 | + if ( $payment_status == 'pending' ) { |
|
268 | 268 | |
269 | - /* translators: %s: pending reason. */ |
|
270 | - $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
269 | + /* translators: %s: pending reason. */ |
|
270 | + $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
271 | 271 | |
272 | - return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
273 | - } |
|
272 | + return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
273 | + } |
|
274 | 274 | |
275 | - /* translators: %s: payment status. */ |
|
276 | - $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
275 | + /* translators: %s: payment status. */ |
|
276 | + $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
277 | 277 | |
278 | - } |
|
278 | + } |
|
279 | 279 | |
280 | - /** |
|
281 | - * Handles one time payments. |
|
282 | - * |
|
283 | - * @param WPInv_Invoice $invoice Invoice object. |
|
284 | - * @param array $posted Posted data. |
|
285 | - */ |
|
286 | - protected function ipn_txn_cart( $invoice, $posted ) { |
|
287 | - $this->ipn_txn_web_accept( $invoice, $posted ); |
|
288 | - } |
|
280 | + /** |
|
281 | + * Handles one time payments. |
|
282 | + * |
|
283 | + * @param WPInv_Invoice $invoice Invoice object. |
|
284 | + * @param array $posted Posted data. |
|
285 | + */ |
|
286 | + protected function ipn_txn_cart( $invoice, $posted ) { |
|
287 | + $this->ipn_txn_web_accept( $invoice, $posted ); |
|
288 | + } |
|
289 | 289 | |
290 | - /** |
|
291 | - * Handles subscription sign ups. |
|
292 | - * |
|
293 | - * @param WPInv_Invoice $invoice Invoice object. |
|
294 | - * @param array $posted Posted data. |
|
295 | - */ |
|
296 | - protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
290 | + /** |
|
291 | + * Handles subscription sign ups. |
|
292 | + * |
|
293 | + * @param WPInv_Invoice $invoice Invoice object. |
|
294 | + * @param array $posted Posted data. |
|
295 | + */ |
|
296 | + protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
297 | 297 | |
298 | - wpinv_error_log( 'Processing subscription signup', false ); |
|
298 | + wpinv_error_log( 'Processing subscription signup', false ); |
|
299 | 299 | |
300 | - // Make sure the invoice has a subscription. |
|
301 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
300 | + // Make sure the invoice has a subscription. |
|
301 | + $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
302 | 302 | |
303 | - if ( empty( $subscription ) ) { |
|
304 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
305 | - } |
|
303 | + if ( empty( $subscription ) ) { |
|
304 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
305 | + } |
|
306 | 306 | |
307 | - // Validate the IPN. |
|
308 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
309 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
310 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
307 | + // Validate the IPN. |
|
308 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
309 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
310 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
311 | 311 | |
312 | - // Activate the subscription. |
|
313 | - $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
314 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
315 | - $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
316 | - $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
317 | - $subscription->activate(); |
|
312 | + // Activate the subscription. |
|
313 | + $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
314 | + $subscription->set_date_created( current_time( 'mysql' ) ); |
|
315 | + $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
316 | + $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
317 | + $subscription->activate(); |
|
318 | 318 | |
319 | - // Set the transaction id. |
|
320 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
321 | - $invoice->set_transaction_id( $posted['txn_id'] ); |
|
322 | - } |
|
319 | + // Set the transaction id. |
|
320 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
321 | + $invoice->set_transaction_id( $posted['txn_id'] ); |
|
322 | + } |
|
323 | 323 | |
324 | - // Update the payment status. |
|
325 | - $invoice->mark_paid(); |
|
324 | + // Update the payment status. |
|
325 | + $invoice->mark_paid(); |
|
326 | 326 | |
327 | - $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
327 | + $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
328 | 328 | |
329 | - wpinv_error_log( 'Subscription started.', false ); |
|
330 | - } |
|
329 | + wpinv_error_log( 'Subscription started.', false ); |
|
330 | + } |
|
331 | 331 | |
332 | - /** |
|
333 | - * Handles subscription renewals. |
|
334 | - * |
|
335 | - * @param WPInv_Invoice $invoice Invoice object. |
|
336 | - * @param array $posted Posted data. |
|
337 | - */ |
|
338 | - protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
332 | + /** |
|
333 | + * Handles subscription renewals. |
|
334 | + * |
|
335 | + * @param WPInv_Invoice $invoice Invoice object. |
|
336 | + * @param array $posted Posted data. |
|
337 | + */ |
|
338 | + protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
339 | 339 | |
340 | - // Make sure the invoice has a subscription. |
|
341 | - $subscription = wpinv_get_subscription( $invoice ); |
|
340 | + // Make sure the invoice has a subscription. |
|
341 | + $subscription = wpinv_get_subscription( $invoice ); |
|
342 | 342 | |
343 | - if ( empty( $subscription ) ) { |
|
344 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
345 | - } |
|
343 | + if ( empty( $subscription ) ) { |
|
344 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
345 | + } |
|
346 | 346 | |
347 | - // Abort if this is the first payment. |
|
348 | - if ( ( $invoice->is_paid() && date( 'Ynd', strtotime( $invoice->get_date_completed() ) ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) || date( 'Ynd', $subscription->get_time_created() ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) { |
|
347 | + // Abort if this is the first payment. |
|
348 | + if ( ( $invoice->is_paid() && date( 'Ynd', strtotime( $invoice->get_date_completed() ) ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) || date( 'Ynd', $subscription->get_time_created() ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) { |
|
349 | 349 | |
350 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
351 | - $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
352 | - $invoice->mark_paid(); |
|
353 | - $invoice->save(); |
|
354 | - } |
|
350 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
351 | + $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
352 | + $invoice->mark_paid(); |
|
353 | + $invoice->save(); |
|
354 | + } |
|
355 | 355 | |
356 | - return; |
|
357 | - } |
|
356 | + return; |
|
357 | + } |
|
358 | 358 | |
359 | - wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
359 | + wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
360 | 360 | |
361 | - // Abort if the payment is already recorded. |
|
362 | - if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
363 | - return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
364 | - } |
|
365 | - |
|
366 | - $args = array( |
|
367 | - 'transaction_id' => $posted['txn_id'], |
|
368 | - 'gateway' => $this->id, |
|
369 | - ); |
|
370 | - |
|
371 | - $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
372 | - |
|
373 | - if ( empty( $invoice ) ) { |
|
374 | - return; |
|
375 | - } |
|
376 | - |
|
377 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
378 | - $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
379 | - |
|
380 | - $subscription->renew(); |
|
381 | - wpinv_error_log( 'Subscription renewed.', false ); |
|
382 | - |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Handles subscription cancelations. |
|
387 | - * |
|
388 | - * @param WPInv_Invoice $invoice Invoice object. |
|
389 | - */ |
|
390 | - protected function ipn_txn_subscr_cancel( $invoice ) { |
|
391 | - |
|
392 | - // Make sure the invoice has a subscription. |
|
393 | - $subscription = wpinv_get_subscription( $invoice ); |
|
394 | - |
|
395 | - if ( empty( $subscription ) ) { |
|
396 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
397 | - } |
|
398 | - |
|
399 | - wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
400 | - $subscription->cancel(); |
|
401 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
402 | - |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Handles subscription completions. |
|
407 | - * |
|
408 | - * @param WPInv_Invoice $invoice Invoice object. |
|
409 | - * @param array $posted Posted data. |
|
410 | - */ |
|
411 | - protected function ipn_txn_subscr_eot( $invoice ) { |
|
412 | - |
|
413 | - // Make sure the invoice has a subscription. |
|
414 | - $subscription = wpinv_get_subscription( $invoice ); |
|
361 | + // Abort if the payment is already recorded. |
|
362 | + if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
363 | + return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
364 | + } |
|
365 | + |
|
366 | + $args = array( |
|
367 | + 'transaction_id' => $posted['txn_id'], |
|
368 | + 'gateway' => $this->id, |
|
369 | + ); |
|
370 | + |
|
371 | + $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
372 | + |
|
373 | + if ( empty( $invoice ) ) { |
|
374 | + return; |
|
375 | + } |
|
376 | + |
|
377 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
378 | + $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
379 | + |
|
380 | + $subscription->renew(); |
|
381 | + wpinv_error_log( 'Subscription renewed.', false ); |
|
382 | + |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Handles subscription cancelations. |
|
387 | + * |
|
388 | + * @param WPInv_Invoice $invoice Invoice object. |
|
389 | + */ |
|
390 | + protected function ipn_txn_subscr_cancel( $invoice ) { |
|
391 | + |
|
392 | + // Make sure the invoice has a subscription. |
|
393 | + $subscription = wpinv_get_subscription( $invoice ); |
|
394 | + |
|
395 | + if ( empty( $subscription ) ) { |
|
396 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
397 | + } |
|
398 | + |
|
399 | + wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
400 | + $subscription->cancel(); |
|
401 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
402 | + |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Handles subscription completions. |
|
407 | + * |
|
408 | + * @param WPInv_Invoice $invoice Invoice object. |
|
409 | + * @param array $posted Posted data. |
|
410 | + */ |
|
411 | + protected function ipn_txn_subscr_eot( $invoice ) { |
|
412 | + |
|
413 | + // Make sure the invoice has a subscription. |
|
414 | + $subscription = wpinv_get_subscription( $invoice ); |
|
415 | 415 | |
416 | - if ( empty( $subscription ) ) { |
|
417 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
418 | - } |
|
416 | + if ( empty( $subscription ) ) { |
|
417 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
418 | + } |
|
419 | 419 | |
420 | - wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
421 | - $subscription->complete(); |
|
422 | - wpinv_error_log( 'Subscription completed.', false ); |
|
420 | + wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
421 | + $subscription->complete(); |
|
422 | + wpinv_error_log( 'Subscription completed.', false ); |
|
423 | 423 | |
424 | - } |
|
424 | + } |
|
425 | 425 | |
426 | - /** |
|
427 | - * Handles subscription fails. |
|
428 | - * |
|
429 | - * @param WPInv_Invoice $invoice Invoice object. |
|
430 | - * @param array $posted Posted data. |
|
431 | - */ |
|
432 | - protected function ipn_txn_subscr_failed( $invoice ) { |
|
433 | - |
|
434 | - // Make sure the invoice has a subscription. |
|
435 | - $subscription = wpinv_get_subscription( $invoice ); |
|
426 | + /** |
|
427 | + * Handles subscription fails. |
|
428 | + * |
|
429 | + * @param WPInv_Invoice $invoice Invoice object. |
|
430 | + * @param array $posted Posted data. |
|
431 | + */ |
|
432 | + protected function ipn_txn_subscr_failed( $invoice ) { |
|
433 | + |
|
434 | + // Make sure the invoice has a subscription. |
|
435 | + $subscription = wpinv_get_subscription( $invoice ); |
|
436 | 436 | |
437 | - if ( empty( $subscription ) ) { |
|
438 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
439 | - } |
|
437 | + if ( empty( $subscription ) ) { |
|
438 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
439 | + } |
|
440 | 440 | |
441 | - wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
442 | - $subscription->failing(); |
|
443 | - wpinv_error_log( 'Subscription marked as failing.', false ); |
|
441 | + wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
442 | + $subscription->failing(); |
|
443 | + wpinv_error_log( 'Subscription marked as failing.', false ); |
|
444 | 444 | |
445 | - } |
|
445 | + } |
|
446 | 446 | |
447 | - /** |
|
448 | - * Handles subscription suspensions. |
|
449 | - * |
|
450 | - * @param WPInv_Invoice $invoice Invoice object. |
|
451 | - * @param array $posted Posted data. |
|
452 | - */ |
|
453 | - protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
447 | + /** |
|
448 | + * Handles subscription suspensions. |
|
449 | + * |
|
450 | + * @param WPInv_Invoice $invoice Invoice object. |
|
451 | + * @param array $posted Posted data. |
|
452 | + */ |
|
453 | + protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
454 | 454 | |
455 | - // Make sure the invoice has a subscription. |
|
456 | - $subscription = wpinv_get_subscription( $invoice ); |
|
455 | + // Make sure the invoice has a subscription. |
|
456 | + $subscription = wpinv_get_subscription( $invoice ); |
|
457 | 457 | |
458 | - if ( empty( $subscription ) ) { |
|
459 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
460 | - } |
|
461 | - |
|
462 | - wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
463 | - $subscription->cancel(); |
|
464 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
465 | - } |
|
458 | + if ( empty( $subscription ) ) { |
|
459 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
460 | + } |
|
461 | + |
|
462 | + wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
463 | + $subscription->cancel(); |
|
464 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
465 | + } |
|
466 | 466 | |
467 | 467 | } |
@@ -13,288 +13,288 @@ |
||
13 | 13 | class GetPaid_Subscription_Notification_Emails { |
14 | 14 | |
15 | 15 | /** |
16 | - * The array of subscription email actions. |
|
17 | - * |
|
18 | - * @param array |
|
19 | - */ |
|
20 | - public $subscription_actions; |
|
16 | + * The array of subscription email actions. |
|
17 | + * |
|
18 | + * @param array |
|
19 | + */ |
|
20 | + public $subscription_actions; |
|
21 | 21 | |
22 | 22 | /** |
23 | - * Class constructor |
|
23 | + * Class constructor |
|
24 | 24 | * |
25 | - */ |
|
26 | - public function __construct() { |
|
27 | - |
|
28 | - $this->subscription_actions = apply_filters( |
|
29 | - 'getpaid_notification_email_subscription_triggers', |
|
30 | - array( |
|
31 | - 'getpaid_subscription_trialling' => 'subscription_trial', |
|
32 | - 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
33 | - 'getpaid_subscription_expired' => 'subscription_expired', |
|
34 | - 'getpaid_subscription_completed' => 'subscription_complete', |
|
35 | - 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
36 | - ) |
|
37 | - ); |
|
38 | - |
|
39 | - $this->init_hooks(); |
|
25 | + */ |
|
26 | + public function __construct() { |
|
27 | + |
|
28 | + $this->subscription_actions = apply_filters( |
|
29 | + 'getpaid_notification_email_subscription_triggers', |
|
30 | + array( |
|
31 | + 'getpaid_subscription_trialling' => 'subscription_trial', |
|
32 | + 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
33 | + 'getpaid_subscription_expired' => 'subscription_expired', |
|
34 | + 'getpaid_subscription_completed' => 'subscription_complete', |
|
35 | + 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
36 | + ) |
|
37 | + ); |
|
38 | + |
|
39 | + $this->init_hooks(); |
|
40 | 40 | |
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
44 | - * Registers email hooks. |
|
45 | - */ |
|
46 | - public function init_hooks() { |
|
47 | - |
|
48 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
49 | - foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
50 | - |
|
51 | - $email = new GetPaid_Notification_Email( $email_type ); |
|
52 | - |
|
53 | - if ( ! $email->is_active() ) { |
|
54 | - continue; |
|
55 | - } |
|
56 | - |
|
57 | - if ( method_exists( $this, $email_type ) ) { |
|
58 | - add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
59 | - continue; |
|
60 | - } |
|
61 | - |
|
62 | - do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
63 | - |
|
64 | - } |
|
65 | - |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Filters subscription merge tags. |
|
70 | - * |
|
71 | - * @param array $merge_tags |
|
72 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
73 | - */ |
|
74 | - public function subscription_merge_tags( $merge_tags, $object ) { |
|
75 | - |
|
76 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
77 | - $merge_tags = array_merge( |
|
78 | - $merge_tags, |
|
79 | - $this->get_subscription_merge_tags( $object ) |
|
80 | - ); |
|
81 | - } |
|
82 | - |
|
83 | - return $merge_tags; |
|
84 | - |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Generates subscription merge tags. |
|
89 | - * |
|
90 | - * @param WPInv_Subscription $subscription |
|
91 | - * @return array |
|
92 | - */ |
|
93 | - public function get_subscription_merge_tags( $subscription ) { |
|
94 | - |
|
95 | - // Abort if it does not exist. |
|
96 | - if ( ! $subscription->get_id() ) { |
|
97 | - return array(); |
|
98 | - } |
|
99 | - |
|
100 | - $invoice = $subscription->get_parent_invoice(); |
|
101 | - return array( |
|
102 | - '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
103 | - '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
104 | - '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
105 | - '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
106 | - '{subscription_id}' => absint( $subscription->get_id() ), |
|
107 | - '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
108 | - '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
109 | - '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
110 | - '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
111 | - '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
112 | - ); |
|
113 | - |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Checks if we should send a notification for a subscription. |
|
118 | - * |
|
119 | - * @param WPInv_Invoice $invoice |
|
120 | - * @return bool |
|
121 | - */ |
|
122 | - public function should_send_notification( $invoice ) { |
|
123 | - return 0 != $invoice->get_id(); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Returns notification recipients. |
|
128 | - * |
|
129 | - * @param WPInv_Invoice $invoice |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - public function get_recipients( $invoice ) { |
|
133 | - $recipients = array( $invoice->get_email() ); |
|
134 | - |
|
135 | - $cc = $invoice->get_email_cc(); |
|
136 | - |
|
137 | - if ( ! empty( $cc ) ) { |
|
138 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
139 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
140 | - } |
|
141 | - |
|
142 | - return $recipients; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Helper function to send an email. |
|
147 | - * |
|
148 | - * @param WPInv_Subscription $subscription |
|
149 | - * @param GetPaid_Notification_Email $email |
|
150 | - * @param string $type |
|
151 | - * @param array $extra_args Extra template args. |
|
152 | - */ |
|
153 | - public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
154 | - |
|
155 | - // Abort in case the parent invoice does not exist. |
|
156 | - $invoice = $subscription->get_parent_invoice(); |
|
157 | - if ( ! $this->should_send_notification( $invoice ) ) { |
|
158 | - return; |
|
159 | - } |
|
160 | - |
|
161 | - if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
162 | - return; |
|
163 | - } |
|
164 | - |
|
165 | - do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
166 | - |
|
167 | - $recipients = $this->get_recipients( $invoice ); |
|
168 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
169 | - $merge_tags = $email->get_merge_tags(); |
|
170 | - $content = $email->get_content( $merge_tags, $extra_args ); |
|
171 | - $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
172 | - $attachments = $email->get_attachments(); |
|
173 | - |
|
174 | - $result = $mailer->send( |
|
175 | - apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
176 | - $subject, |
|
177 | - $content, |
|
178 | - $attachments |
|
179 | - ); |
|
180 | - |
|
181 | - // Maybe send a copy to the admin. |
|
182 | - if ( $email->include_admin_bcc() ) { |
|
183 | - $mailer->send( |
|
184 | - wpinv_get_admin_email(), |
|
185 | - $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
186 | - $content, |
|
187 | - $attachments |
|
188 | - ); |
|
189 | - } |
|
190 | - |
|
191 | - if ( $result ) { |
|
192 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
193 | - } else { |
|
194 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
195 | - } |
|
196 | - |
|
197 | - do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
198 | - |
|
199 | - } |
|
44 | + * Registers email hooks. |
|
45 | + */ |
|
46 | + public function init_hooks() { |
|
47 | + |
|
48 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
49 | + foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
50 | + |
|
51 | + $email = new GetPaid_Notification_Email( $email_type ); |
|
52 | + |
|
53 | + if ( ! $email->is_active() ) { |
|
54 | + continue; |
|
55 | + } |
|
56 | + |
|
57 | + if ( method_exists( $this, $email_type ) ) { |
|
58 | + add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
59 | + continue; |
|
60 | + } |
|
61 | + |
|
62 | + do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
63 | + |
|
64 | + } |
|
65 | + |
|
66 | + } |
|
200 | 67 | |
201 | 68 | /** |
202 | - * Sends a new trial notification. |
|
203 | - * |
|
204 | - * @param WPInv_Subscription $subscription |
|
205 | - */ |
|
206 | - public function subscription_trial( $subscription ) { |
|
69 | + * Filters subscription merge tags. |
|
70 | + * |
|
71 | + * @param array $merge_tags |
|
72 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
73 | + */ |
|
74 | + public function subscription_merge_tags( $merge_tags, $object ) { |
|
207 | 75 | |
208 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
209 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
76 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
77 | + $merge_tags = array_merge( |
|
78 | + $merge_tags, |
|
79 | + $this->get_subscription_merge_tags( $object ) |
|
80 | + ); |
|
81 | + } |
|
210 | 82 | |
211 | - } |
|
83 | + return $merge_tags; |
|
212 | 84 | |
213 | - /** |
|
214 | - * Sends a cancelled subscription notification. |
|
215 | - * |
|
216 | - * @param WPInv_Subscription $subscription |
|
217 | - */ |
|
218 | - public function subscription_cancelled( $subscription ) { |
|
85 | + } |
|
219 | 86 | |
220 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
221 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
87 | + /** |
|
88 | + * Generates subscription merge tags. |
|
89 | + * |
|
90 | + * @param WPInv_Subscription $subscription |
|
91 | + * @return array |
|
92 | + */ |
|
93 | + public function get_subscription_merge_tags( $subscription ) { |
|
94 | + |
|
95 | + // Abort if it does not exist. |
|
96 | + if ( ! $subscription->get_id() ) { |
|
97 | + return array(); |
|
98 | + } |
|
99 | + |
|
100 | + $invoice = $subscription->get_parent_invoice(); |
|
101 | + return array( |
|
102 | + '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
103 | + '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
104 | + '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
105 | + '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
106 | + '{subscription_id}' => absint( $subscription->get_id() ), |
|
107 | + '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
108 | + '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
109 | + '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
110 | + '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
111 | + '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
112 | + ); |
|
222 | 113 | |
223 | - } |
|
114 | + } |
|
224 | 115 | |
225 | - /** |
|
226 | - * Sends a subscription expired notification. |
|
227 | - * |
|
228 | - * @param WPInv_Subscription $subscription |
|
229 | - */ |
|
230 | - public function subscription_expired( $subscription ) { |
|
116 | + /** |
|
117 | + * Checks if we should send a notification for a subscription. |
|
118 | + * |
|
119 | + * @param WPInv_Invoice $invoice |
|
120 | + * @return bool |
|
121 | + */ |
|
122 | + public function should_send_notification( $invoice ) { |
|
123 | + return 0 != $invoice->get_id(); |
|
124 | + } |
|
231 | 125 | |
232 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
233 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
126 | + /** |
|
127 | + * Returns notification recipients. |
|
128 | + * |
|
129 | + * @param WPInv_Invoice $invoice |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + public function get_recipients( $invoice ) { |
|
133 | + $recipients = array( $invoice->get_email() ); |
|
234 | 134 | |
235 | - } |
|
135 | + $cc = $invoice->get_email_cc(); |
|
236 | 136 | |
237 | - /** |
|
238 | - * Sends a completed subscription notification. |
|
239 | - * |
|
240 | - * @param WPInv_Subscription $subscription |
|
241 | - */ |
|
242 | - public function subscription_complete( $subscription ) { |
|
137 | + if ( ! empty( $cc ) ) { |
|
138 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
139 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
140 | + } |
|
243 | 141 | |
244 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
245 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
142 | + return $recipients; |
|
143 | + } |
|
246 | 144 | |
247 | - } |
|
145 | + /** |
|
146 | + * Helper function to send an email. |
|
147 | + * |
|
148 | + * @param WPInv_Subscription $subscription |
|
149 | + * @param GetPaid_Notification_Email $email |
|
150 | + * @param string $type |
|
151 | + * @param array $extra_args Extra template args. |
|
152 | + */ |
|
153 | + public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
154 | + |
|
155 | + // Abort in case the parent invoice does not exist. |
|
156 | + $invoice = $subscription->get_parent_invoice(); |
|
157 | + if ( ! $this->should_send_notification( $invoice ) ) { |
|
158 | + return; |
|
159 | + } |
|
160 | + |
|
161 | + if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
162 | + return; |
|
163 | + } |
|
164 | + |
|
165 | + do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
166 | + |
|
167 | + $recipients = $this->get_recipients( $invoice ); |
|
168 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
169 | + $merge_tags = $email->get_merge_tags(); |
|
170 | + $content = $email->get_content( $merge_tags, $extra_args ); |
|
171 | + $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
172 | + $attachments = $email->get_attachments(); |
|
173 | + |
|
174 | + $result = $mailer->send( |
|
175 | + apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
176 | + $subject, |
|
177 | + $content, |
|
178 | + $attachments |
|
179 | + ); |
|
180 | + |
|
181 | + // Maybe send a copy to the admin. |
|
182 | + if ( $email->include_admin_bcc() ) { |
|
183 | + $mailer->send( |
|
184 | + wpinv_get_admin_email(), |
|
185 | + $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
186 | + $content, |
|
187 | + $attachments |
|
188 | + ); |
|
189 | + } |
|
190 | + |
|
191 | + if ( $result ) { |
|
192 | + $subscription->get_parent_invoice()->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
193 | + } else { |
|
194 | + $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
195 | + } |
|
196 | + |
|
197 | + do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
248 | 198 | |
249 | - /** |
|
250 | - * Sends a subscription renewal reminder notification. |
|
251 | - * |
|
252 | - */ |
|
253 | - public function renewal_reminder() { |
|
199 | + } |
|
254 | 200 | |
255 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
201 | + /** |
|
202 | + * Sends a new trial notification. |
|
203 | + * |
|
204 | + * @param WPInv_Subscription $subscription |
|
205 | + */ |
|
206 | + public function subscription_trial( $subscription ) { |
|
256 | 207 | |
257 | - // Fetch reminder days. |
|
258 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
208 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
209 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
259 | 210 | |
260 | - // Abort if non is set. |
|
261 | - if ( empty( $reminder_days ) ) { |
|
262 | - return; |
|
263 | - } |
|
211 | + } |
|
264 | 212 | |
265 | - // Fetch matching subscriptions. |
|
213 | + /** |
|
214 | + * Sends a cancelled subscription notification. |
|
215 | + * |
|
216 | + * @param WPInv_Subscription $subscription |
|
217 | + */ |
|
218 | + public function subscription_cancelled( $subscription ) { |
|
219 | + |
|
220 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
221 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
222 | + |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Sends a subscription expired notification. |
|
227 | + * |
|
228 | + * @param WPInv_Subscription $subscription |
|
229 | + */ |
|
230 | + public function subscription_expired( $subscription ) { |
|
231 | + |
|
232 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
233 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
234 | + |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Sends a completed subscription notification. |
|
239 | + * |
|
240 | + * @param WPInv_Subscription $subscription |
|
241 | + */ |
|
242 | + public function subscription_complete( $subscription ) { |
|
243 | + |
|
244 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
245 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
246 | + |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * Sends a subscription renewal reminder notification. |
|
251 | + * |
|
252 | + */ |
|
253 | + public function renewal_reminder() { |
|
254 | + |
|
255 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
256 | + |
|
257 | + // Fetch reminder days. |
|
258 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
259 | + |
|
260 | + // Abort if non is set. |
|
261 | + if ( empty( $reminder_days ) ) { |
|
262 | + return; |
|
263 | + } |
|
264 | + |
|
265 | + // Fetch matching subscriptions. |
|
266 | 266 | $args = array( |
267 | 267 | 'number' => -1, |
268 | - 'count_total' => false, |
|
269 | - 'status' => 'trialling active', |
|
268 | + 'count_total' => false, |
|
269 | + 'status' => 'trialling active', |
|
270 | 270 | 'date_expires_query' => array( |
271 | - 'relation' => 'OR' |
|
271 | + 'relation' => 'OR' |
|
272 | 272 | ), |
273 | - ); |
|
273 | + ); |
|
274 | 274 | |
275 | - foreach ( $reminder_days as $days ) { |
|
276 | - $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
275 | + foreach ( $reminder_days as $days ) { |
|
276 | + $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
277 | 277 | |
278 | - $args['date_expires_query'][] = array( |
|
279 | - 'year' => $date['year'], |
|
280 | - 'month' => $date['month'], |
|
281 | - 'day' => $date['day'], |
|
282 | - ); |
|
278 | + $args['date_expires_query'][] = array( |
|
279 | + 'year' => $date['year'], |
|
280 | + 'month' => $date['month'], |
|
281 | + 'day' => $date['day'], |
|
282 | + ); |
|
283 | 283 | |
284 | - } |
|
284 | + } |
|
285 | 285 | |
286 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
286 | + $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
287 | 287 | |
288 | 288 | foreach ( $subscriptions as $subscription ) { |
289 | 289 | |
290 | - // Skip packages. |
|
291 | - if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
292 | - $email->object = $subscription; |
|
293 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
294 | - } |
|
290 | + // Skip packages. |
|
291 | + if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
292 | + $email->object = $subscription; |
|
293 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
294 | + } |
|
295 | 295 | |
296 | - } |
|
296 | + } |
|
297 | 297 | |
298 | - } |
|
298 | + } |
|
299 | 299 | |
300 | 300 | } |