init_hooks(); } /** * Initialize hooks. */ private function init_hooks() { // WooCommerce order emails. if ( class_exists( 'WooCommerce' ) ) { add_action( 'woocommerce_order_status_adp-pending', array( $this, 'send_pending_email' ), 10, 2 ); add_action( 'woocommerce_order_status_adp-proof', array( $this, 'send_proof_received_email' ), 10, 2 ); add_action( 'woocommerce_order_status_adp-approved', array( $this, 'send_approved_email' ), 10, 2 ); add_action( 'woocommerce_order_status_adp-rejected', array( $this, 'send_rejected_email' ), 10, 2 ); add_action( 'woocommerce_order_status_adp-expired', array( $this, 'send_expired_email' ), 10, 2 ); add_filter( 'woocommerce_email_classes', array( $this, 'register_email_classes' ) ); } // Standalone payment emails. add_action( 'afridirect_payment_approved', array( $this, 'send_standalone_approved_email' ), 10, 1 ); add_action( 'afridirect_payment_rejected', array( $this, 'send_standalone_rejected_email' ), 10, 2 ); add_action( 'afridirect_payment_proof_uploaded', array( $this, 'send_standalone_proof_received_email' ), 10, 1 ); } /** * Send pending payment email. * * @param int $order_id Order ID. * @param WC_Order $order Order object. */ public function send_pending_email( $order_id, $order ) { if ( 'afridirect_payment' !== $order->get_payment_method() ) { return; } $mailer = WC()->mailer(); $emails = $mailer->get_emails(); if ( isset( $emails['afridirect_pending_payment'] ) ) { $emails['afridirect_pending_payment']->trigger( $order_id, $order ); } } /** * Send proof received email. * * @param int $order_id Order ID. * @param WC_Order $order Order object. */ public function send_proof_received_email( $order_id, $order ) { if ( 'afridirect_payment' !== $order->get_payment_method() ) { return; } $mailer = WC()->mailer(); $emails = $mailer->get_emails(); if ( isset( $emails['afridirect_proof_received'] ) ) { $emails['afridirect_proof_received']->trigger( $order_id, $order ); } } /** * Send approved email. * * @param int $order_id Order ID. * @param WC_Order $order Order object. */ public function send_approved_email( $order_id, $order ) { if ( 'afridirect_payment' !== $order->get_payment_method() ) { return; } $mailer = WC()->mailer(); $emails = $mailer->get_emails(); if ( isset( $emails['afridirect_approved'] ) ) { $emails['afridirect_approved']->trigger( $order_id, $order ); } } /** * Send rejected email. * * @param int $order_id Order ID. * @param WC_Order $order Order object. */ public function send_rejected_email( $order_id, $order ) { if ( 'afridirect_payment' !== $order->get_payment_method() ) { return; } $mailer = WC()->mailer(); $emails = $mailer->get_emails(); if ( isset( $emails['afridirect_rejected'] ) ) { $emails['afridirect_rejected']->trigger( $order_id, $order ); } } /** * Send expired email. * * @param int $order_id Order ID. * @param WC_Order $order Order object. */ public function send_expired_email( $order_id, $order ) { if ( 'afridirect_payment' !== $order->get_payment_method() ) { return; } $mailer = WC()->mailer(); $emails = $mailer->get_emails(); if ( isset( $emails['afridirect_expired'] ) ) { $emails['afridirect_expired']->trigger( $order_id, $order ); } } /** * Register email classes. * * @param array $emails Existing email classes. * @return array */ public function register_email_classes( $emails ) { // WooCommerce isn't ready yet. if ( ! class_exists( 'WC_Email' ) ) { return $emails; } // Load our email classes only when WooCommerce email base is available. if ( ! class_exists( 'AfriDirect_Payment_Email_Pending' ) ) { require AFRIDIRECT_PAYMENT_PLUGIN_DIR . 'includes/class-emails.php'; } // If the file was included but classes are still missing, fail gracefully. if ( ! class_exists( 'AfriDirect_Payment_Email_Pending' ) ) { return $emails; } $pending_email = new AfriDirect_Payment_Email_Pending(); $proof_received_email = new AfriDirect_Payment_Email_Proof_Received(); $approved_email = new AfriDirect_Payment_Email_Approved(); $rejected_email = new AfriDirect_Payment_Email_Rejected(); $expired_email = new AfriDirect_Payment_Email_Expired(); $emails[ $pending_email->id ] = $pending_email; $emails[ $proof_received_email->id ] = $proof_received_email; $emails[ $approved_email->id ] = $approved_email; $emails[ $rejected_email->id ] = $rejected_email; $emails[ $expired_email->id ] = $expired_email; return $emails; } /** * Send approved email for standalone payment. * * @param int $payment_id Payment ID. */ public function send_standalone_approved_email( $payment_id ) { $payment = AfriDirect_Payments::get_payment( $payment_id ); if ( ! $payment || empty( $payment['payer_email'] ) ) { return; } /* translators: %s: Payment reference number */ $subject = sprintf( __( 'Payment Approved - %s', 'afridirect-payment' ), $payment['reference'] ); $message = $this->get_standalone_approved_email_content( $payment ); wp_mail( $payment['payer_email'], $subject, $message, array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . get_bloginfo( 'name' ) . ' <' . get_option( 'admin_email' ) . '>', ) ); } /** * Send rejected email for standalone payment. * * @param int $payment_id Payment ID. * @param string $reason Rejection reason. */ public function send_standalone_rejected_email( $payment_id, $reason = '' ) { $payment = AfriDirect_Payments::get_payment( $payment_id ); if ( ! $payment || empty( $payment['payer_email'] ) ) { return; } /* translators: %s: Payment reference number */ $subject = sprintf( __( 'Payment Rejected - %s', 'afridirect-payment' ), $payment['reference'] ); $message = $this->get_standalone_rejected_email_content( $payment, $reason ); wp_mail( $payment['payer_email'], $subject, $message, array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . get_bloginfo( 'name' ) . ' <' . get_option( 'admin_email' ) . '>', ) ); } /** * Send proof received email for standalone payment. * * @param int $payment_id Payment ID. */ public function send_standalone_proof_received_email( $payment_id ) { // This can notify admin that proof was uploaded. // For now, we'll just log it or send admin notification if needed. // Customer notification happens on approval/rejection. } /** * Get approved email content for standalone payment. * * @param array $payment Payment data. * @return string */ private function get_standalone_approved_email_content( $payment ) { $receipt_number = get_post_meta( $payment['id'], '_afridirect_receipt_no', true ); ob_start(); ?>

,


,