| 1 |
<?php |
| 2 |
/** |
| 3 |
* Emails: Subscriber |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @copyright Copyright (c) 2023, Sandhills Development, LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 4.7.3 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SimplePay\Core\Emails; |
| 13 |
|
| 14 |
use SimplePay\Core\EventManagement\SubscriberInterface; |
| 15 |
use SimplePay\Core\License\LicenseAwareInterface; |
| 16 |
use SimplePay\Core\License\LicenseAwareTrait; |
| 17 |
use SimplePay\Core\Payments\Payment_Confirmation; |
| 18 |
use SimplePay\Core\API; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* EmailSubscriber class. |
| 26 |
* |
| 27 |
* @since 4.7.3 |
| 28 |
*/ |
| 29 |
class EmailSubscriber implements SubscriberInterface, LicenseAwareInterface { |
| 30 |
|
| 31 |
use LicenseAwareTrait; |
| 32 |
|
| 33 |
/** |
| 34 |
* Registered emails. |
| 35 |
* |
| 36 |
* This should probably be handled by a more advanced service/registery. For now it's just a list. |
| 37 |
* |
| 38 |
* @since 4.7.3 |
| 39 |
* @var array<string, \SimplePay\Core\Emails\Email\EmailInterface> |
| 40 |
*/ |
| 41 |
private $emails; |
| 42 |
|
| 43 |
/** |
| 44 |
* EmailSubscriber. |
| 45 |
* |
| 46 |
* @since 4.7.3 |
| 47 |
* |
| 48 |
* @param array<string, \SimplePay\Core\Emails\Email\EmailInterface> $emails Emails. |
| 49 |
*/ |
| 50 |
public function __construct( $emails ) { |
| 51 |
$this->emails = $emails; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* {@inheritdoc} |
| 56 |
*/ |
| 57 |
public function get_subscribed_events() { |
| 58 |
$subscribers = array( |
| 59 |
// Summary report. |
| 60 |
'simpay_send_summary_report_email' => 'summary_report', |
| 61 |
); |
| 62 |
|
| 63 |
if ( $this->license->is_lite() ) { |
| 64 |
return $subscribers; |
| 65 |
} |
| 66 |
|
| 67 |
$payment_confirmation = array( |
| 68 |
'payment_confirmation', |
| 69 |
10, |
| 70 |
2, |
| 71 |
); |
| 72 |
|
| 73 |
$payment_notification = array( |
| 74 |
'payment_notification', |
| 75 |
10, |
| 76 |
2, |
| 77 |
); |
| 78 |
|
| 79 |
// Subscription created. |
| 80 |
$subscribers['simpay_webhook_subscription_created'] = array( |
| 81 |
$payment_notification, |
| 82 |
$payment_confirmation, |
| 83 |
); |
| 84 |
|
| 85 |
// One-time payment created. |
| 86 |
$subscribers['simpay_webhook_payment_intent_succeeded'] = array( |
| 87 |
$payment_notification, |
| 88 |
$payment_confirmation, |
| 89 |
); |
| 90 |
|
| 91 |
// Invoice payment created. |
| 92 |
$subscribers['simpay_webhook_invoice_paid'] = array( |
| 93 |
$payment_notification, |
| 94 |
$payment_confirmation, |
| 95 |
); |
| 96 |
|
| 97 |
// Payment confirmation resent. |
| 98 |
$subscribers['simpay_resend_payment_confirmation'] = $payment_confirmation; |
| 99 |
|
| 100 |
// Upcoming invoice. |
| 101 |
$subscribers['simpay_webhook_invoice_upcoming'] = array( |
| 102 |
'upcoming_invoice', |
| 103 |
10, |
| 104 |
3, |
| 105 |
); |
| 106 |
|
| 107 |
// Invoice confirmations. |
| 108 |
$subscribers['simpay_webhook_invoice_payment_succeeded'][] = array( |
| 109 |
'invoice_confirmation', |
| 110 |
10, |
| 111 |
3, |
| 112 |
); |
| 113 |
|
| 114 |
// Payment processing. |
| 115 |
$subscribers['simpay_webhook_payment_intent_processing'] = array( |
| 116 |
'payment_processing', |
| 117 |
10, |
| 118 |
3, |
| 119 |
); |
| 120 |
|
| 121 |
// Refunded payment confirmation. |
| 122 |
$subscribers['simpay_webhook_charge_refunded'] = array( |
| 123 |
'payment_refunded_confirmation', |
| 124 |
10, |
| 125 |
3, |
| 126 |
); |
| 127 |
|
| 128 |
// Legacy. |
| 129 |
// These aren't used anymore, but we'll keep it here for backwards compatibility. |
| 130 |
// `simpay_webhook_payment_intent_succeeded` is used instead instead. |
| 131 |
$subscribers['simpay_webhook_charge_succeeded'] = array( |
| 132 |
$payment_notification, |
| 133 |
$payment_confirmation, |
| 134 |
); |
| 135 |
|
| 136 |
$subscribers['simpay_webhook_subscription_cancel'] = array( |
| 137 |
'subscription_cancel', |
| 138 |
10, |
| 139 |
3, |
| 140 |
); |
| 141 |
|
| 142 |
return $subscribers; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Mails the "Payment Confirmation" email. |
| 147 |
* |
| 148 |
* @since 4.7.3 |
| 149 |
* |
| 150 |
* @param \SimplePay\Vendor\Stripe\Event $event Stripe Event. |
| 151 |
* @param \SimplePay\Vendor\Stripe\Subscription|\SimplePay\Vendor\Stripe\PaymentIntent $object Stripe object. |
| 152 |
* @return void |
| 153 |
*/ |
| 154 |
public function payment_confirmation( $event, $object ) { |
| 155 |
/** @var \SimplePay\Core\Emails\Email\PaymentConfirmationEmail $email */ |
| 156 |
$email = $this->emails['payment-confirmation']; |
| 157 |
|
| 158 |
// If the email is not enabled, do nothing. |
| 159 |
if ( ! $email->is_enabled() ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
// If the object was not created by WP Simple Pay, do nothing. |
| 164 |
if ( ! isset( $object->metadata->simpay_form_id ) ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$form_id = $object->metadata->simpay_form_id; |
| 169 |
|
| 170 |
// Retrieve the payment confirmation data. |
| 171 |
if ( $object->customer instanceof \SimplePay\Vendor\Stripe\Customer ) { |
| 172 |
/** @var \SimplePay\Vendor\Stripe\Customer $customer */ |
| 173 |
$customer = $object->customer; |
| 174 |
|
| 175 |
} elseif ( $object instanceof \SimplePay\Vendor\Stripe\Customer ) { |
| 176 |
$customer = $object; |
| 177 |
} else { |
| 178 |
$form = simpay_get_form( $form_id ); |
| 179 |
|
| 180 |
if ( false === $form || null === $object->customer ) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
$customer = API\Customers\retrieve( |
| 185 |
$object->customer, |
| 186 |
$form->get_api_request_args() |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
// Ensure we have data before proceeding. |
| 191 |
$payment_confirmation_data = Payment_Confirmation\get_confirmation_data( |
| 192 |
$customer->id, |
| 193 |
false, |
| 194 |
$form_id |
| 195 |
); |
| 196 |
|
| 197 |
// @todo log why the email didn't send. |
| 198 |
if ( empty( $payment_confirmation_data ) ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
// Setup the mailer. |
| 203 |
$mailer = new Mailer( $email ); |
| 204 |
|
| 205 |
// ...hydrate with the payment confirmation data. |
| 206 |
$mailer->set_data( $payment_confirmation_data ); |
| 207 |
|
| 208 |
// ...then set the payment mode. |
| 209 |
$mailer->set_livemode( |
| 210 |
$payment_confirmation_data['form']->is_livemode() |
| 211 |
); |
| 212 |
|
| 213 |
// ...then set the address(es). |
| 214 |
/** @var string $to_address WP Simple Pay requires an email. */ |
| 215 |
$to_address = $customer->email; |
| 216 |
$mailer->set_to( trim( $to_address ) ); |
| 217 |
|
| 218 |
// ...then set subject. |
| 219 |
$subject = $email->get_subject(); |
| 220 |
|
| 221 |
// Per-form subject override. |
| 222 |
$form = simpay_get_form( $form_id ); |
| 223 |
|
| 224 |
if ( false !== $form ) { |
| 225 |
$form_subject = $form->get_email_confirmation_subject(); |
| 226 |
|
| 227 |
if ( ! empty( $form_subject ) ) { |
| 228 |
$subject = $form_subject; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
$mailer->set_subject( $subject ); |
| 233 |
|
| 234 |
// ...then set the body content. |
| 235 |
$type = 'one_time'; |
| 236 |
|
| 237 |
if ( ! empty( $payment_confirmation_data['subscriptions'] ) ) { |
| 238 |
$type = 'subscription'; |
| 239 |
|
| 240 |
if ( 'trialing' === $object->status ) { |
| 241 |
$type = 'trial'; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
$body = ''; |
| 246 |
|
| 247 |
if ( false !== $form ) { |
| 248 |
$confirmation_message = $form->get_email_confirmation_message(); |
| 249 |
|
| 250 |
if ( ! empty( $confirmation_message ) ) { |
| 251 |
$body = $confirmation_message; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
// Otherwise use the default message. |
| 256 |
if ( empty( $body ) ) { |
| 257 |
$body = $email->get_body( $type ); |
| 258 |
} |
| 259 |
|
| 260 |
$mailer->set_body( $body ); |
| 261 |
|
| 262 |
// Finally, send the email. |
| 263 |
$mailer->send(); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Mails the "Payment Notification" email. |
| 268 |
* |
| 269 |
* @since 4.7.3 |
| 270 |
* |
| 271 |
* @param \SimplePay\Vendor\Stripe\Event $event Stripe Event. |
| 272 |
* @param \SimplePay\Vendor\Stripe\Subscription|\SimplePay\Vendor\Stripe\PaymentIntent $object Stripe object. |
| 273 |
* @return void |
| 274 |
*/ |
| 275 |
function payment_notification( $event, $object ) { |
| 276 |
/** @var \SimplePay\Core\Emails\Email\PaymentNotificationEmail $email */ |
| 277 |
$email = $this->emails['payment-notification']; |
| 278 |
|
| 279 |
// If the email is not enabled, do nothing. |
| 280 |
if ( ! $email->is_enabled() ) { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
// If the object was not created by WP Simple Pay, do nothing. |
| 285 |
if ( ! isset( $object->metadata->simpay_form_id ) ) { |
| 286 |
return; |
| 287 |
} |
| 288 |
|
| 289 |
$form_id = $object->metadata->simpay_form_id; |
| 290 |
|
| 291 |
// Retrieve the payment confirmation data. |
| 292 |
$form = simpay_get_form( $form_id ); |
| 293 |
|
| 294 |
if ( false === $form ) { |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
if ( $object->customer instanceof \SimplePay\Vendor\Stripe\Customer ) { |
| 299 |
/** @var \SimplePay\Vendor\Stripe\Customer $customer */ |
| 300 |
$customer = $object->customer; |
| 301 |
|
| 302 |
} elseif ( null !== $object->customer ) { |
| 303 |
$customer = API\Customers\retrieve( |
| 304 |
$object->customer, |
| 305 |
$form->get_api_request_args() |
| 306 |
); |
| 307 |
} else { |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
// Ensure we have data before proceeding. |
| 312 |
$payment_confirmation_data = Payment_Confirmation\get_confirmation_data( |
| 313 |
$customer->id, |
| 314 |
false, |
| 315 |
$form_id |
| 316 |
); |
| 317 |
|
| 318 |
// @todo log why the email didn't send. |
| 319 |
if ( empty( $payment_confirmation_data ) ) { |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
// Setup the mailer. |
| 324 |
$mailer = new Mailer( $email ); |
| 325 |
|
| 326 |
// ...hydrate with the payment confirmation data. |
| 327 |
$mailer->set_data( $payment_confirmation_data ); |
| 328 |
|
| 329 |
// ...then set the payment mode. |
| 330 |
$mailer->set_livemode( |
| 331 |
$payment_confirmation_data['form']->is_livemode() |
| 332 |
); |
| 333 |
|
| 334 |
// ...then set the address(es). |
| 335 |
$to = $email->get_to(); |
| 336 |
|
| 337 |
/** |
| 338 |
* Filters the email address the payment notification is sent to. |
| 339 |
* |
| 340 |
* @since 4.7.12 |
| 341 |
* |
| 342 |
* @param string $to The email address the payment notification is sent to. |
| 343 |
* @param array<string, mixed> $payment_confirmation_data The payment confirmation data. |
| 344 |
*/ |
| 345 |
$to = apply_filters( |
| 346 |
'simpay_email_payment_notification_to', |
| 347 |
$to, |
| 348 |
$payment_confirmation_data |
| 349 |
); |
| 350 |
|
| 351 |
$mailer->set_to( $to ); |
| 352 |
|
| 353 |
// ...then set the subject. |
| 354 |
$subject = $email->get_subject(); |
| 355 |
|
| 356 |
// Per-form subject override. |
| 357 |
if ( false !== $form ) { |
| 358 |
$form_subject = $form->get_email_notification_subject(); |
| 359 |
|
| 360 |
if ( ! empty( $form_subject ) ) { |
| 361 |
$subject = $form_subject; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
$mailer->set_subject( $subject ); |
| 366 |
|
| 367 |
$body = ''; |
| 368 |
|
| 369 |
// If there is a per-form custom mesasge, use it. |
| 370 |
if ( false !== $form ) { |
| 371 |
$notification_message = $form->get_email_notification_message(); |
| 372 |
|
| 373 |
if ( ! empty( $notification_message ) ) { |
| 374 |
$type = 'one_time'; |
| 375 |
|
| 376 |
if ( ! empty( $payment_confirmation_data['subscriptions'] ) ) { |
| 377 |
$type = 'subscription'; |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Filters the email notification message. |
| 382 |
* |
| 383 |
* @since 4.12.0 |
| 384 |
* |
| 385 |
* @param string $notification_message The email notification message. |
| 386 |
* @param int $form_id The form ID. |
| 387 |
* @param string $type The type of payment. |
| 388 |
*/ |
| 389 |
$body = apply_filters( |
| 390 |
'simpay_email_payment_notification_message', |
| 391 |
$notification_message, |
| 392 |
$form_id, |
| 393 |
$type |
| 394 |
); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
// Otherwise use the default message. |
| 399 |
if ( empty( $body ) ) { |
| 400 |
$body = $email->get_body(); |
| 401 |
} |
| 402 |
|
| 403 |
$mailer->set_body( $body ); |
| 404 |
|
| 405 |
// Finally, send the email. |
| 406 |
$mailer->send(); |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Mails the "Upcoming Invoice" email. |
| 411 |
* |
| 412 |
* @since 4.7.3 |
| 413 |
* |
| 414 |
* @param \SimplePay\Vendor\Stripe\Event $event Stripe Event object. |
| 415 |
* @param \SimplePay\Vendor\Stripe\Invoice $invoice Stripe Invoice object. |
| 416 |
* @param \SimplePay\Vendor\Stripe\Subscription $subscription Stripe Subscription object. |
| 417 |
* @return void |
| 418 |
*/ |
| 419 |
function upcoming_invoice( $event, $invoice, $subscription ) { |
| 420 |
/** @var \SimplePay\Core\Emails\Email\UpcomingInvoiceEmail $email */ |
| 421 |
$email = $this->emails['upcoming-invoice']; |
| 422 |
|
| 423 |
// If the email is not enabled, do nothing. |
| 424 |
if ( ! $email->is_enabled() ) { |
| 425 |
return; |
| 426 |
} |
| 427 |
|
| 428 |
$send_upcoming_invoice_email = true; |
| 429 |
|
| 430 |
/** |
| 431 |
* Determines if the "Upcoming Invoice" email should be sent. |
| 432 |
* |
| 433 |
* @since 3.9.0 |
| 434 |
* @since 4.0.0 Deprecated. Use email settings. |
| 435 |
* |
| 436 |
* @param bool $send_upcoming_invoice_email If the email should be sent. |
| 437 |
* @param \SimplePay\Vendor\Stripe\Event $event Stripe Event object. |
| 438 |
* @param \SimplePay\Vendor\Stripe\Invoice $invoice Stripe Invoice object. |
| 439 |
* @param \SimplePay\Vendor\Stripe\Subscription $subscription Stripe Subscription object. |
| 440 |
*/ |
| 441 |
$send_upcoming_invoice_email = apply_filters( |
| 442 |
'simpay_send_upcoming_invoice_email', |
| 443 |
$send_upcoming_invoice_email, |
| 444 |
$event, |
| 445 |
$invoice, |
| 446 |
$subscription |
| 447 |
); |
| 448 |
|
| 449 |
// If the email is disabled via legacy filter, do nothing. |
| 450 |
if ( false === $send_upcoming_invoice_email ) { |
| 451 |
return; |
| 452 |
} |
| 453 |
|
| 454 |
// If the Subscription was not created by WP Simple Pay, do nothing. |
| 455 |
if ( ! isset( $subscription->metadata->simpay_form_id ) ) { |
| 456 |
return; |
| 457 |
} |
| 458 |
|
| 459 |
// If Subscription was created before 3.7.0, or is missing a key, do nothing. |
| 460 |
if ( ! isset( $subscription->metadata->simpay_subscription_key ) ) { |
| 461 |
return; |
| 462 |
} |
| 463 |
|
| 464 |
// Retrieve the payment confirmation data. |
| 465 |
/** @var \SimplePay\Vendor\Stripe\Customer $customer */ |
| 466 |
$customer = $subscription->customer; |
| 467 |
|
| 468 |
// Ensure we have data before proceeding. |
| 469 |
$payment_confirmation_data = Payment_Confirmation\get_confirmation_data( |
| 470 |
$customer->id, |
| 471 |
false, |
| 472 |
$subscription->metadata->simpay_form_id |
| 473 |
); |
| 474 |
|
| 475 |
// @todo log why the email didn't send. |
| 476 |
if ( empty( $payment_confirmation_data ) ) { |
| 477 |
return; |
| 478 |
} |
| 479 |
|
| 480 |
$mailer = new Mailer( $email ); |
| 481 |
|
| 482 |
// ...hydrate with the payment confirmation data. |
| 483 |
$mailer->set_data( $payment_confirmation_data ); |
| 484 |
|
| 485 |
// ...then set the payment mode. |
| 486 |
$mailer->set_livemode( |
| 487 |
$payment_confirmation_data['form']->is_livemode() |
| 488 |
); |
| 489 |
|
| 490 |
// ...then set the address(es). |
| 491 |
/** @var string $to_address */ |
| 492 |
$to_address = $invoice->customer_email; |
| 493 |
$mailer->set_to( trim( $to_address ) ); |
| 494 |
|
| 495 |
// ...then set subject. |
| 496 |
$mailer->set_subject( $email->get_subject() ); |
| 497 |
|
| 498 |
// ...then parse and set the body. |
| 499 |
$mailer->set_body( $email->get_body() ); |
| 500 |
|
| 501 |
// Finally, send the email. |
| 502 |
$mailer->send(); |
| 503 |
} |
| 504 |
|
| 505 |
/** |
| 506 |
* Mails the "Invoice Confirmation" email. |
| 507 |
* |
| 508 |
* @since 4.7.3 |
| 509 |
* |
| 510 |
* @param \SimplePay\Vendor\Stripe\Event $event Stripe Event object. |
| 511 |
* @param \SimplePay\Vendor\Stripe\Invoice $invoice Stripe Invoice object. |
| 512 |
* @param \SimplePay\Vendor\Stripe\Subscription $subscription Stripe Subscription object. |
| 513 |
* @return void |
| 514 |
*/ |
| 515 |
function invoice_confirmation( $event, $invoice, $subscription ) { |
| 516 |
/** @var \SimplePay\Core\Emails\Email\InvoiceConfirmationEmail $email */ |
| 517 |
$email = $this->emails['invoice-confirmation']; |
| 518 |
|
| 519 |
// If the email is not enabled, do nothing. |
| 520 |
if ( ! $email->is_enabled() ) { |
| 521 |
return; |
| 522 |
} |
| 523 |
|
| 524 |
// If this is the first invoice of the Subscription, do nothing. |
| 525 |
if ( 'subscription_create' === $invoice->billing_reason ) { |
| 526 |
return; |
| 527 |
} |
| 528 |
|
| 529 |
// If the Subscription was not created by WP Simple Pay, do nothing. |
| 530 |
if ( ! isset( $subscription->metadata->simpay_form_id ) ) { |
| 531 |
return; |
| 532 |
} |
| 533 |
|
| 534 |
// If Subscription was created before 3.7.0, or is missing a key, do nothing. |
| 535 |
if ( ! isset( $subscription->metadata->simpay_subscription_key ) ) { |
| 536 |
return; |
| 537 |
} |
| 538 |
|
| 539 |
// Retrieve the payment confirmation data. |
| 540 |
/** @var \SimplePay\Vendor\Stripe\Customer $customer */ |
| 541 |
$customer = $subscription->customer; |
| 542 |
|
| 543 |
// Ensure we have data before proceeding. |
| 544 |
$payment_confirmation_data = Payment_Confirmation\get_confirmation_data( |
| 545 |
$customer->id, |
| 546 |
false, |
| 547 |
$subscription->metadata->simpay_form_id |
| 548 |
); |
| 549 |
|
| 550 |
// @todo log why the email didn't send. |
| 551 |
if ( empty( $payment_confirmation_data ) ) { |
| 552 |
return; |
| 553 |
} |
| 554 |
|
| 555 |
// Setup the mailer. |
| 556 |
$mailer = new Mailer( $email ); |
| 557 |
|
| 558 |
// ...hydrate with the payment confirmation data. |
| 559 |
$mailer->set_data( $payment_confirmation_data ); |
| 560 |
|
| 561 |
// ...then set the address(es). |
| 562 |
/** @var string $to_address */ |
| 563 |
$to_address = $invoice->customer_email; |
| 564 |
$mailer->set_to( trim( $to_address ) ); |
| 565 |
|
| 566 |
// ...then set the subject. |
| 567 |
$mailer->set_subject( $email->get_subject() ); |
| 568 |
|
| 569 |
// ...then parse and set the body. |
| 570 |
$mailer->set_body( $email->get_body() ); |
| 571 |
|
| 572 |
// Finally, send the email. |
| 573 |
$mailer->send(); |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 577 |
* Mails the "Summary Report" email. |
| 578 |
* |
| 579 |
* @since 4.7.3 |
| 580 |
* |
| 581 |
* @return void |
| 582 |
*/ |
| 583 |
public function summary_report() { |
| 584 |
/** @var \SimplePay\Core\Emails\Email\SummaryReportEmail $email */ |
| 585 |
$email = $this->emails['summary-report']; |
| 586 |
|
| 587 |
// If the email is not enabled, do nothing. |
| 588 |
if ( ! $email->is_enabled() ) { |
| 589 |
return; |
| 590 |
} |
| 591 |
|
| 592 |
add_filter( 'simpay_emails_autop', '__return_false' ); |
| 593 |
|
| 594 |
// Setup the mailer. |
| 595 |
$mailer = new Mailer( $email ); |
| 596 |
|
| 597 |
// ...then set the address(es). |
| 598 |
$mailer->set_to( $email->get_to() ); |
| 599 |
|
| 600 |
// ...then set the subject. |
| 601 |
$mailer->set_subject( $email->get_subject() ); |
| 602 |
|
| 603 |
// ...then parse and set the body. |
| 604 |
$mailer->set_body( $email->get_body() ); |
| 605 |
|
| 606 |
// Finally, send the email. |
| 607 |
$mailer->send(); |
| 608 |
|
| 609 |
add_filter( 'simpay_emails_autop', '__return_true' ); |
| 610 |
} |
| 611 |
|
| 612 |
/** |
| 613 |
* Mails the "Payment Refunded Confirmation" email. |
| 614 |
* |
| 615 |
* @since 4.10.0 |
| 616 |
* @param \SimplePay\Vendor\Stripe\Event $event Stripe Event object. |
| 617 |
* @param \SimplePay\Vendor\Stripe\Charge $charge Stripe Charge object. |
| 618 |
* @param \SimplePay\Core\Abstracts\Form $form Form object. |
| 619 |
* |
| 620 |
* @return void |
| 621 |
*/ |
| 622 |
public function payment_refunded_confirmation( $event, $charge, $form ) { |
| 623 |
/** @var \SimplePay\Core\Emails\Email\PaymentRefundedConfirmationEmail $email */ |
| 624 |
$email = $this->emails['payment-refunded-confirmation']; |
| 625 |
|
| 626 |
// If the email is not enabled, do nothing. |
| 627 |
if ( ! $email->is_enabled() ) { |
| 628 |
return; |
| 629 |
} |
| 630 |
|
| 631 |
if ( false === $form ) { |
| 632 |
return; |
| 633 |
} |
| 634 |
|
| 635 |
// Ensure we have data before proceeding. |
| 636 |
$payment_refund_data = Payment_Confirmation\get_confirmation_data( |
| 637 |
$charge->customer->id, // @phpstan-ignore-line |
| 638 |
false, |
| 639 |
$form->id |
| 640 |
); |
| 641 |
|
| 642 |
$payment_refund_data['charge'] = $charge; |
| 643 |
|
| 644 |
// Setup the mailer. |
| 645 |
$mailer = new Mailer( $email ); |
| 646 |
|
| 647 |
// Set data. |
| 648 |
|
| 649 |
$mailer->set_data( $payment_refund_data ); |
| 650 |
|
| 651 |
// Register smart tags. |
| 652 |
$email->register_smart_tags(); |
| 653 |
|
| 654 |
// ...then set the address(es). |
| 655 |
$mailer->set_to( $payment_refund_data['customer']->email ); |
| 656 |
|
| 657 |
// ...then set the subject. |
| 658 |
$mailer->set_subject( $email->get_subject() ); |
| 659 |
|
| 660 |
// ...then parse and set the body. |
| 661 |
$mailer->set_body( $email->get_body() ); |
| 662 |
|
| 663 |
// Finally, send the email. |
| 664 |
$mailer->send(); |
| 665 |
} |
| 666 |
|
| 667 |
/** |
| 668 |
* Mails the 'Subscription Cancel' email . |
| 669 |
* |
| 670 |
* @since 4.10.0 |
| 671 |
* |
| 672 |
* @param \SimplePay\Vendor\Stripe\Event $event Stripe Event object . |
| 673 |
* @param \SimplePay\Vendor\Stripe\Subscription $subscription Stripe Subscription object . |
| 674 |
* @return void |
| 675 |
*/ |
| 676 |
public function subscription_cancel( $event, $subscription ) { |
| 677 |
if ( null === $subscription->canceled_at ) { |
| 678 |
return; |
| 679 |
} |
| 680 |
|
| 681 |
if ( 'customer.subscription.deleted' === $event->type |
| 682 |
&& $subscription->cancel_at_period_end ) { |
| 683 |
return; |
| 684 |
} |
| 685 |
|
| 686 |
/** @var \SimplePay\Core\Emails\Email\SubscriptionCancellationNotification $subscription_cancel_confirmation_email */ |
| 687 |
$subscription_cancel_confirmation_email = $this->emails['subscription-cancel-confirmation']; |
| 688 |
|
| 689 |
/** @var \SimplePay\Core\Emails\Email\SubscriptionCancellationNotification $subscription_cancel_notification_email */ |
| 690 |
$subscription_cancel_notification_email = $this->emails['subscription-cancel-notification']; |
| 691 |
|
| 692 |
$notification_mailer = new Mailer( $subscription_cancel_notification_email ); |
| 693 |
$confirmation_mailer = new Mailer( $subscription_cancel_confirmation_email ); |
| 694 |
|
| 695 |
// Retrieve the payment confirmation data. |
| 696 |
/** @var \SimplePay\Vendor\Stripe\Customer $customer */ |
| 697 |
$customer = $subscription->customer; |
| 698 |
|
| 699 |
// Ensure we have data before proceeding. |
| 700 |
$payment_confirmation_data = Payment_Confirmation\get_confirmation_data( |
| 701 |
$customer->id, |
| 702 |
false, |
| 703 |
$subscription->metadata->simpay_form_id // @phpstan-ignore-line |
| 704 |
); |
| 705 |
|
| 706 |
// Set email for merchant. |
| 707 |
$notification_mailer->set_data( $payment_confirmation_data ); |
| 708 |
$notification_mailer->set_to( $subscription_cancel_notification_email->get_to() ); |
| 709 |
$notification_mailer->set_subject( $subscription_cancel_notification_email->get_subject() ); |
| 710 |
$notification_mailer->set_body( $subscription_cancel_notification_email->get_body() ); |
| 711 |
|
| 712 |
// Set email for customer. |
| 713 |
$confirmation_mailer->set_data( $payment_confirmation_data ); |
| 714 |
$confirmation_mailer->set_to( $subscription->customer->email ); /** @phpstan-ignore-line */ |
| 715 |
$confirmation_mailer->set_subject( $subscription_cancel_confirmation_email->get_subject() ); |
| 716 |
$confirmation_mailer->set_body( $subscription_cancel_confirmation_email->get_body() ); |
| 717 |
|
| 718 |
// Send emails. |
| 719 |
if ( $subscription_cancel_notification_email->is_enabled() ) { |
| 720 |
$notification_mailer->send(); |
| 721 |
} |
| 722 |
|
| 723 |
if ( $subscription_cancel_confirmation_email->is_enabled() ) { |
| 724 |
$confirmation_mailer->send(); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
/** |
| 729 |
* Mails the "Payment Processing" email. |
| 730 |
* |
| 731 |
* @since 4.10.0 |
| 732 |
* |
| 733 |
* @param \SimplePay\Vendor\Stripe\Event $event Stripe webhook event. |
| 734 |
* @param \SimplePay\Vendor\Stripe\PaymentIntent $payment_intent Stripe PaymentIntent. |
| 735 |
* @param \SimplePay\Core\Abstracts\Form $form Form object. |
| 736 |
* @return void |
| 737 |
*/ |
| 738 |
public function payment_processing( $event, $payment_intent, $form ) { |
| 739 |
if ( false === $form ) { |
| 740 |
return; |
| 741 |
} |
| 742 |
|
| 743 |
// Retrieve the payment confirmation data. |
| 744 |
/** @var \SimplePay\Vendor\Stripe\Customer $customer */ |
| 745 |
$customer = $payment_intent->customer; |
| 746 |
|
| 747 |
// Ensure we have data before proceeding. |
| 748 |
$payment_confirmation_data = Payment_Confirmation\get_confirmation_data( |
| 749 |
$customer->id, |
| 750 |
false, |
| 751 |
$form->id |
| 752 |
); |
| 753 |
|
| 754 |
// if there is no data, do nothing. |
| 755 |
if ( empty( $payment_confirmation_data ) ) { |
| 756 |
return; |
| 757 |
} |
| 758 |
|
| 759 |
/** @var \SimplePay\Core\Emails\Email\PaymentProcessingConfirmationEmail $payment_processing_confirmation_email */ |
| 760 |
$payment_processing_confirmation_email = $this->emails['payment-processing-confirmation']; |
| 761 |
|
| 762 |
/** @var \SimplePay\Core\Emails\Email\PaymentProcessingNotificationEmail $payment_processing_notification_email */ |
| 763 |
$payment_processing_notification_email = $this->emails['payment-processing-notification']; |
| 764 |
|
| 765 |
// Send confirmation email if enabled. |
| 766 |
if ( $payment_processing_confirmation_email->is_enabled() ) { |
| 767 |
$confirmation_mailer = new Mailer( $payment_processing_confirmation_email ); |
| 768 |
$confirmation_mailer->set_data( $payment_confirmation_data ); |
| 769 |
$confirmation_mailer->set_to( $customer->email ); // @phpstan-ignore-line |
| 770 |
$confirmation_mailer->set_subject( $payment_processing_confirmation_email->get_subject() ); |
| 771 |
$confirmation_mailer->set_body( $payment_processing_confirmation_email->get_body() ); |
| 772 |
$confirmation_mailer->send(); |
| 773 |
} |
| 774 |
|
| 775 |
// Send notification email if enabled. |
| 776 |
if ( $payment_processing_notification_email->is_enabled() ) { |
| 777 |
$notification_mailer = new Mailer( $payment_processing_notification_email ); |
| 778 |
$notification_mailer->set_data( $payment_confirmation_data ); |
| 779 |
$notification_mailer->set_to( $payment_processing_notification_email->get_to() ); |
| 780 |
$notification_mailer->set_subject( $payment_processing_notification_email->get_subject() ); |
| 781 |
$notification_mailer->set_body( $payment_processing_notification_email->get_body() ); |
| 782 |
$notification_mailer->send(); |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
|