|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Plugin\jtl_wallee\Services; |
| 6 | + |
| 7 | +use JTL\Checkout\Bestellung; |
| 8 | +use JTL\Customer\Customer; |
| 9 | +use JTL\DB\DbInterface; |
| 10 | +use JTL\Mail\Mailer; |
| 11 | +use JTL\Mail\Mail\Mail; |
| 12 | +use Plugin\jtl_wallee\WalleeHelper; |
| 13 | + |
| 14 | +/** |
| 15 | + * Service for sending emails related to Wallee. |
| 16 | + */ |
| 17 | +class WalleeMailService { |
| 18 | + |
| 19 | + /** |
| 20 | + * @var Mailer $mailer |
| 21 | + */ |
| 22 | + protected $mailer; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var Mail $mail |
| 26 | + */ |
| 27 | + protected $mail; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var DbInterface $db |
| 31 | + */ |
| 32 | + protected $db; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var array $pluginConfig |
| 36 | + */ |
| 37 | + protected array $pluginConfig; |
| 38 | + |
| 39 | + protected $emailTemplates = [ |
| 40 | + 'authorization' => \MAILTEMPLATE_BESTELLBESTAETIGUNG, |
| 41 | + 'fulfill' => \MAILTEMPLATE_BESTELLUNG_BEZAHLT, |
| 42 | + ]; |
| 43 | + |
| 44 | + /** |
| 45 | + * Constructor for WalleeAbstractMailTemplate. |
| 46 | + * |
| 47 | + * @param Mailer $mailer The mailer instance. |
| 48 | + * @param Mail $mail The mail instance. |
| 49 | + * @param DbInterface $db The database instance. |
| 50 | + */ |
| 51 | + public function __construct(Mailer $mailer, Mail $mail, DbInterface $db, array $pluginConfig) { |
| 52 | + $this->mailer = $mailer; |
| 53 | + $this->mail = $mail; |
| 54 | + $this->db = $db; |
| 55 | + $this->pluginConfig = $pluginConfig; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Validates the template. |
| 60 | + * |
| 61 | + * @param string $template |
| 62 | + * The template to validate. Currently only 'authorization' and 'fulfill' values are supported. |
| 63 | + * @return void |
| 64 | + * @throws \Exception If the template is invalid. |
| 65 | + */ |
| 66 | + protected function validateTemplate(string $template) { |
| 67 | + if (!isset($this->emailTemplates[$template])) { |
| 68 | + throw new \Exception("Invalid template: $template"); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Checks if the email specified by the template has been sent for the given order. |
| 74 | + * |
| 75 | + * @param int $orderId |
| 76 | + * The ID of the order. |
| 77 | + * @param string $template |
| 78 | + * The template to use. Currently only 'authorization' and 'fulfill' values are supported. |
| 79 | + * |
| 80 | + * @return bool True if the email has been sent for this template, false otherwise. |
| 81 | + */ |
| 82 | + public function isEmailSent(int $orderId, string $template): bool { |
| 83 | + |
| 84 | + $this->validateTemplate($template); |
| 85 | + $column_name = "{$template}_email_sent"; |
| 86 | + |
| 87 | + $result = $this->db->select( |
| 88 | + 'wallee_transactions', |
| 89 | + 'order_id', |
| 90 | + $orderId |
| 91 | + ); |
| 92 | + return !empty($result) && $result->$column_name == 1; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Sends an email for the specified order. |
| 97 | + * |
| 98 | + * @param int $orderId The ID of the order. |
| 99 | + * @return void |
| 100 | + */ |
| 101 | + public function sendMail(int $orderId, string $template): void { |
| 102 | + try { |
| 103 | + $data = $this->prepareData($orderId); |
| 104 | + if ($this->canSendEmail($data, $template)) { |
| 105 | + $this->send($data, $template); |
| 106 | + $this->updateDatabase($orderId, $template); |
| 107 | + } |
| 108 | + } |
| 109 | + catch (Exception $e) { |
| 110 | + // Handle errors (logging, retries, etc.) |
| 111 | + error_log("Error sending mail for template : " . $template . " : " . $e->getMessage()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Updates the database for the specified order and template. |
| 117 | + * |
| 118 | + * @param int $orderId The ID of the order. |
| 119 | + * @param string $template |
| 120 | + * The template to use. Currently only 'authorization' and 'fulfill' values are supported. |
| 121 | + * @return void |
| 122 | + */ |
| 123 | + protected function updateDatabase(int $orderId, string $template): void { |
| 124 | + $this->validateTemplate($template); |
| 125 | + $column_name = "{$template}_email_sent"; |
| 126 | + |
| 127 | + $this->db->update( |
| 128 | + 'wallee_transactions', |
| 129 | + ['order_id'], |
| 130 | + [$orderId], |
| 131 | + (object)[ |
| 132 | + $column_name => 1, |
| 133 | + 'updated_at' => date('Y-m-d H:i:s'), |
| 134 | + ] |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Prepares the data for the email. |
| 140 | + * |
| 141 | + * @param int $orderId The ID of the order. |
| 142 | + * @return stdClass The data prepared for the email. |
| 143 | + */ |
| 144 | + protected function prepareData(int $orderId): \stdClass { |
| 145 | + $order = new Bestellung($orderId, FALSE, $this->db); |
| 146 | + $order->fuelleBestellung(false); |
| 147 | + $customer = new Customer($order->kKunde, null, $this->db); |
| 148 | + $data = new \stdClass(); |
| 149 | + $data->tkunde = $customer; |
| 150 | + $data->tbestellung = $order; |
| 151 | + |
| 152 | + return $data; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Sends the email using the prepared data. |
| 157 | + * |
| 158 | + * @param stdClass $data The data prepared for the email. |
| 159 | + * @param string $template |
| 160 | + * The template to use. Currently only 'authorization' and 'fulfill' values are supported. |
| 161 | + * @return void |
| 162 | + */ |
| 163 | + protected function send(\stdClass $data, string $template): void { |
| 164 | + $this->validateTemplate($template); |
| 165 | + $this->mailer->send($this->mail->createFromTemplateID($this->emailTemplates[$template], $data)); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Decides if an email can be sent for this template. |
| 170 | + * |
| 171 | + * @param stdClass $data |
| 172 | + * The data prepared for the email. |
| 173 | + * @param string $template |
| 174 | + * The template to use. Currently only 'authorization' and 'fulfill' values are supported. |
| 175 | + * @return bool True if the email can be sent, false otherwise. |
| 176 | + */ |
| 177 | + protected function canSendEmail(\stdClass $data, string $template): bool { |
| 178 | + if ($template == 'authorization') { |
| 179 | + $sendEmail = $this->pluginConfig[WalleeHelper::SEND_AUTHORIZATION_EMAIL] ?? null; |
| 180 | + |
| 181 | + return $sendEmail === 'YES' && !empty($data->tkunde->cMail); |
| 182 | + } |
| 183 | + elseif ($template == 'fulfill') { |
| 184 | + $sendEmail = $this->pluginConfig[WalleeHelper::SEND_FULFILL_EMAIL] ?? null; |
| 185 | + |
| 186 | + return $sendEmail === 'YES' && !empty($data->tkunde->cMail) |
| 187 | + && ($data->tbestellung->Zahlungsart->nMailSenden & \ZAHLUNGSART_MAIL_EINGANG); |
| 188 | + } |
| 189 | + |
| 190 | + return FALSE; |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments