Skip to content

Commit 8acdaca

Browse files
committed
order confirmation email rendering workflow
Signed-off-by: romanetar <roman_ag@hotmail.com>
2 parents 23f4af2 + 4c74ee5 commit 8acdaca

5 files changed

Lines changed: 133 additions & 208 deletions

File tree

app/Services/Apis/EmailTemplatesApi.php

Lines changed: 0 additions & 141 deletions
This file was deleted.
Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
use Exception;
1415
use GuzzleHttp\Client;
1516
use GuzzleHttp\Exception\ClientException;
1617
use GuzzleHttp\Exception\RequestException;
@@ -22,10 +23,10 @@
2223
use libs\utils\ICacheService;
2324
use models\exceptions\ValidationException;
2425
/**
25-
* Class MailApi
26+
* Class MailService
2627
* @package App\Services\Apis
2728
*/
28-
final class MailApi extends AbstractOAuth2Api implements IMailApi
29+
final class MailService extends AbstractOAuth2Api implements IMailApi, IEmailTemplatesApi
2930
{
3031

3132
/**
@@ -107,7 +108,7 @@ public function sendEmail
107108
(
108109
sprintf
109110
(
110-
"MailApi::sendEmail template_identifier %s to_email %s payload %s",
111+
"MailService::sendEmail template_identifier %s to_email %s payload %s",
111112
$template_identifier,
112113
$to_email,
113114
json_encode($payload)
@@ -132,12 +133,12 @@ public function sendEmail
132133
];
133134

134135
if(!empty($cc_email)){
135-
Log::debug(sprintf("MailApi::sendEmail setting cc_email %s", $cc_email));
136+
Log::debug(sprintf("MailService::sendEmail setting cc_email %s", $cc_email));
136137
$payload['cc_email'] = trim($cc_email);
137138
}
138139

139140
if(!empty($bcc_email)){
140-
Log::debug(sprintf("MailApi::sendEmail setting bcc_email %s", $bcc_email));
141+
Log::debug(sprintf("MailService::sendEmail setting bcc_email %s", $bcc_email));
141142
$payload['bcc_email'] = trim($bcc_email);
142143
}
143144

@@ -152,7 +153,7 @@ public function sendEmail
152153
(
153154
sprintf
154155
(
155-
"MailApi::sendEmail template_identifier %s to_email %s body %s",
156+
"MailService::sendEmail template_identifier %s to_email %s body %s",
156157
$template_identifier,
157158
$to_email,
158159
$body
@@ -173,7 +174,7 @@ public function sendEmail
173174
(
174175
sprintf
175176
(
176-
"MailApi::sendEmail template_identifier %s to_email %s payload %s error %s",
177+
"MailService::sendEmail template_identifier %s to_email %s payload %s error %s",
177178
$template_identifier,
178179
$to_email,
179180
json_encode($payload),
@@ -188,4 +189,63 @@ public function sendEmail
188189
throw $ex;
189190
}
190191
}
192+
193+
/**
194+
* @param string $template_id
195+
* @return mixed
196+
* @throws \GuzzleHttp\Exception\GuzzleException
197+
* @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException
198+
*/
199+
public function getEmailTemplate(string $template_id) {
200+
Log::debug("MailService::getEmailTemplate");
201+
202+
try {
203+
$query = [
204+
'access_token' => $this->getAccessToken()
205+
];
206+
207+
$response = $this->client->get("/api/v1/mail-templates/{$template_id}", [
208+
'query' => $query,
209+
]
210+
);
211+
return json_decode($response->getBody()->getContents(), true);
212+
}
213+
catch (Exception $ex) {
214+
$this->cleanAccessToken();
215+
Log::error($ex);
216+
throw $ex;
217+
}
218+
}
219+
220+
/**
221+
* @param array $payload
222+
* @param string $html_template
223+
* @return mixed
224+
* @throws \GuzzleHttp\Exception\GuzzleException|\League\OAuth2\Client\Provider\Exception\IdentityProviderException
225+
*/
226+
public function getEmailPreview(array $payload, string $html_template)
227+
{
228+
Log::debug("MailService::getEmailPreview");
229+
230+
try {
231+
$query = [
232+
'access_token' => $this->getAccessToken()
233+
];
234+
235+
$response = $this->client->put('/api/v1/mail-templates/all/render', [
236+
'query' => $query,
237+
RequestOptions::JSON => [
238+
"html" => $html_template,
239+
"payload" => $payload
240+
]
241+
]
242+
);
243+
return json_decode($response->getBody()->getContents(), true);
244+
}
245+
catch (Exception $ex) {
246+
$this->cleanAccessToken();
247+
Log::error($ex);
248+
throw $ex;
249+
}
250+
}
191251
}

app/Services/BaseServicesProvider.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
**/
1414
use App\Permissions\IPermissionsManager;
1515
use App\Permissions\PermissionsManager;
16-
use App\Services\Apis\EmailTemplatesApi;
1716
use App\Services\Apis\ExternalUserApi;
1817
use App\Services\Apis\GoogleGeoCodingAPI;
1918
use App\Services\Apis\IEmailTemplatesApi;
@@ -22,7 +21,7 @@
2221
use App\Services\Apis\IMailApi;
2322
use App\Services\Apis\IMUXApi;
2423
use App\Services\Apis\IPasswordlessAPI;
25-
use App\Services\Apis\MailApi;
24+
use App\Services\Apis\MailService;
2625
use App\Services\Apis\MUXApi;
2726
use App\Services\Apis\MuxCredentials;
2827
use App\Services\Apis\PasswordlessAPI;
@@ -130,7 +129,12 @@ public function register()
130129

131130
App::singleton(
132131
IMailApi::class,
133-
MailApi::class
132+
MailService::class
133+
);
134+
135+
App::singleton(
136+
IEmailTemplatesApi::class,
137+
MailService::class
134138
);
135139

136140
App::singleton(
@@ -160,11 +164,6 @@ function(){
160164
return new MUXApi();
161165
}
162166
);
163-
164-
App::singleton(
165-
IEmailTemplatesApi::class,
166-
EmailTemplatesApi::class
167-
);
168167
}
169168

170169
/**

app/Services/Model/Imp/SummitOrderService.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4361,7 +4361,7 @@ public function renderOrderConfirmationEmail(Summit $summit, int $order_id): str
43614361
$order = $summit->getOrderById($order_id);
43624362

43634363
if (!$order instanceof SummitOrder)
4364-
throw new EntityNotFoundException("order not found");
4364+
throw new EntityNotFoundException("Order not found.");
43654365

43664366
$payload = SerializerRegistry::getInstance()
43674367
->getSerializer($order, SerializerRegistry::SerializerType_Admin_Email_Preview)
@@ -4370,16 +4370,23 @@ public function renderOrderConfirmationEmail(Summit $summit, int $order_id): str
43704370
$template_id = $summit->getEmailIdentifierPerEmailEventFlowSlug(RegisteredMemberOrderPaidMail::EVENT_SLUG);
43714371

43724372
if (is_null($template_id))
4373-
throw new EntityNotFoundException("order confirmation email template not found");
4373+
throw new EntityNotFoundException("Order confirmation email template not found.");
43744374

43754375
$template = $this->email_templates_api->getEmailTemplate($template_id);
43764376

4377+
if (is_null($template))
4378+
throw new EntityNotFoundException("Could not retrieve the order confirmation email template.");
4379+
43774380
$preview = $this->email_templates_api->getEmailPreview($payload, $template['html_content']);
43784381

4382+
//Only the HTML body content is needed to create the PDF
43794383
preg_match("/<body[^>]*>(.*?)<\/body>/is", $preview['html_content'], $matches);
43804384

4381-
$renderer = new HTML2PDFRenderer(trim($matches[1]));
4382-
return $renderer->render();
4385+
if (!is_null($matches) && count($matches) > 1) {
4386+
$renderer = new HTML2PDFRenderer(trim($matches[1]));
4387+
return $renderer->render();
4388+
}
4389+
return "";
43834390
} catch (\Exception $ex) {
43844391
Log::warning($ex);
43854392
throw $ex;

0 commit comments

Comments
 (0)