Skip to content

Commit 4c74ee5

Browse files
committed
order confirmation email rendering workflow (WIP)
Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent 372eab7 commit 4c74ee5

13 files changed

Lines changed: 518 additions & 50 deletions

File tree

app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitOrdersApiController.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,4 +1172,20 @@ public function deActivateTicket($summit_id, $order_id, $ticket_id)
11721172
));
11731173
});
11741174
}
1175+
1176+
/**
1177+
* @param $summit_id
1178+
* @param $order_id
1179+
* @return \Illuminate\Http\JsonResponse|mixed
1180+
*/
1181+
public function getOrderConfirmationEmailPDF($summit_id, $order_id)
1182+
{
1183+
return $this->processRequest(function () use ($summit_id, $order_id) {
1184+
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->getResourceServerContext())->find($summit_id);
1185+
if (is_null($summit)) return $this->error404();
1186+
1187+
$content = $this->service->renderOrderConfirmationEmail($summit, intval($order_id));
1188+
return $this->pdf("order_{$order_id}_confirmation_email.pdf", $content);
1189+
});
1190+
}
11751191
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php namespace App\Http\Renderers;
2+
/**
3+
* Copyright 2023 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
15+
use TCPDF;
16+
/**
17+
* Class HTML2PDFRenderer
18+
* @package App\Http\Renderers
19+
*/
20+
final class HTML2PDFRenderer implements IRenderer
21+
{
22+
/**
23+
* @var string
24+
*/
25+
private $html;
26+
27+
/**
28+
* HTML2PDFRenderer constructor.
29+
* @param string $html
30+
*/
31+
public function __construct(string $html)
32+
{
33+
$this->html = $html;
34+
}
35+
36+
public function render(): string
37+
{
38+
// create new PDF document
39+
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
40+
41+
// set document information
42+
$pdf->SetCreator(PDF_CREATOR);
43+
$pdf->SetTitle('');
44+
45+
// remove default header/footer
46+
$pdf->setPrintHeader(false);
47+
$pdf->setPrintFooter(false);
48+
49+
// set header and footer fonts
50+
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
51+
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
52+
53+
// set default monospaced font
54+
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
55+
56+
// set margins
57+
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
58+
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
59+
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
60+
61+
// set auto page breaks
62+
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
63+
64+
// set image scale factor
65+
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
66+
67+
// set font
68+
$pdf->setFont('helvetica', '', 10);
69+
70+
// add a page
71+
$pdf->AddPage();
72+
73+
$pdf->writeHTML($this->html, true, false, true, false, '');
74+
75+
//Close and output PDF document
76+
return $pdf->Output('', 'S');
77+
}
78+
}

app/ModelSerializers/SerializerRegistry.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ final class SerializerRegistry
151151
const SerializerType_Admin_Voteable_CSV = "ADMIN_VOTEABLE_CSV";
152152
const SerializerType_CSV = 'CSV';
153153
const SerializerType_Admin_Registration_Stats = 'ADMIN_REG_STATS';
154+
const SerializerType_Admin_Email_Preview = 'ADMIN_EMAIL_PREVIEW';
154155

155156
private function __clone()
156157
{
@@ -429,6 +430,7 @@ private function __construct()
429430

430431
$this->registry['SummitOrder'] = [
431432
self::SerializerType_Public => SummitOrderBaseSerializer::class,
433+
self::SerializerType_Admin_Email_Preview => SummitOrderConfirmationEmailPreviewSerializer::class,
432434
ISummitOrderSerializerTypes::CheckOutType => SummitOrderBaseSerializer::class,
433435
ISummitOrderSerializerTypes::ReservationType => SummitOrderReservationSerializer::class,
434436
ISummitOrderSerializerTypes::AdminType => SummitOrderAdminSerializer::class,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php namespace ModelSerializers;
2+
use Libs\ModelSerializers\AbstractSerializer;
3+
use models\summit\SummitOrder;
4+
5+
/**
6+
* Copyright 2023 OpenStack Foundation
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
**/
17+
18+
19+
/**
20+
* Class SummitOrderConfirmationEmailPreviewSerializer
21+
* @package App\ModelSerializers
22+
*/
23+
final class SummitOrderConfirmationEmailPreviewSerializer extends AbstractSerializer
24+
{
25+
protected static $array_mappings = [
26+
'CreditCardType' => 'order_credit_card_type:json_string',
27+
'CreditCard4Number' => 'order_credit_card_4number:json_string',
28+
'Currency' => 'order_currency:json_string',
29+
'CurrencySymbol' => 'order_currency_symbol:json_string',
30+
'Number' => 'order_number:json_string',
31+
'FinalAmountAdjusted' => 'order_amount:json_float',
32+
'OwnerFullName' => 'owner_full_name:json_string',
33+
'OwnerCompanyName' => 'owner_company:json_string',
34+
];
35+
36+
protected static $allowed_relations = [
37+
'member',
38+
'tickets',
39+
];
40+
41+
/**
42+
* @param null $expand
43+
* @param array $fields
44+
* @param array $relations
45+
* @param array $params
46+
* @return array
47+
*/
48+
public function serialize($expand = null, array $fields = array(), array $relations = array(), array $params = array())
49+
{
50+
$order = $this->object;
51+
if (!$order instanceof SummitOrder) return [];
52+
$values = parent::serialize($expand, $fields, $relations, $params);
53+
54+
$values["summit_name"] = $order->getSummit()->getName();
55+
56+
if (!count($relations)) $relations = $this->getAllowedRelations();
57+
58+
if (in_array('tickets', $relations)) {
59+
$tickets = [];
60+
foreach ($order->getTickets() as $ticket) {
61+
$ticket_dic = [
62+
"currency" => $ticket->getCurrency(),
63+
"currency_symbol" => $ticket->getCurrencySymbol(),
64+
"has_owner" => $ticket->hasOwner(),
65+
"need_details" => false,
66+
"ticket_type_name" => $ticket->getTicketTypeName(),
67+
"owner_email" => $ticket->getOwnerEmail(),
68+
"price" => $ticket->getFinalAmount()
69+
];
70+
71+
$promo_code = $ticket->getPromoCode();
72+
if (!is_null($promo_code)) {
73+
$ticket_dic["promo_code"] = ["code" => $promo_code->getCode()];
74+
}
75+
76+
$tickets[] = $ticket_dic;
77+
}
78+
$values['tickets'] = $tickets;
79+
}
80+
81+
return $values;
82+
}
83+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php namespace App\Services\Apis;
2+
/**
3+
* Copyright 2023 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use GuzzleHttp\Exception\RequestException;
15+
use models\exceptions\ValidationException;
16+
use libs\utils\ICacheService;
17+
use GuzzleHttp\Client;
18+
use GuzzleHttp\HandlerStack;
19+
use GuzzleRetry\GuzzleRetryMiddleware;
20+
use GuzzleHttp\RequestOptions;
21+
use Illuminate\Support\Facades\Config;
22+
use Illuminate\Support\Facades\Log;
23+
use Exception;
24+
/**
25+
* Class EmailTemplatesApi
26+
* @package App\Services
27+
*/
28+
final class EmailTemplatesApi extends AbstractOAuth2Api
29+
implements IEmailTemplatesApi
30+
{
31+
const AppName = 'EMAIL_TEMPLATES_SERVICE';
32+
33+
/**
34+
* @var Client
35+
*/
36+
private $client;
37+
38+
/**
39+
* @var string
40+
*/
41+
private $scopes;
42+
43+
/**
44+
* ExternalUserApi constructor.
45+
* @param ICacheService $cacheService
46+
*/
47+
public function __construct(ICacheService $cacheService)
48+
{
49+
parent::__construct($cacheService);
50+
$stack = HandlerStack::create();
51+
$stack->push(GuzzleRetryMiddleware::factory());
52+
53+
$this->client = new Client([
54+
'handler' => $stack,
55+
'base_uri' => Config::get('mail.service_base_url') ?? '',
56+
'timeout' => Config::get('curl.timeout', 60),
57+
'allow_redirects' => Config::get('curl.allow_redirects', false),
58+
'verify' => Config::get('curl.verify_ssl_cert', true),
59+
]);
60+
}
61+
62+
/**
63+
* @return string
64+
*/
65+
public function getAppName(): string
66+
{
67+
return self::AppName;
68+
}
69+
70+
/**
71+
* @return array
72+
*/
73+
public function getAppConfig(): array
74+
{
75+
return [
76+
'client_id' => Config::get("mail.service_client_id"),
77+
'client_secret' => Config::get("mail.service_client_secret"),
78+
'scopes' => Config::get("mail.service_client_scopes")
79+
];
80+
}
81+
82+
/**
83+
* @param string $template_id
84+
* @return mixed
85+
* @throws \GuzzleHttp\Exception\GuzzleException
86+
* @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException
87+
*/
88+
public function getEmailTemplate(string $template_id) {
89+
Log::debug("EmailTemplatesApi::getEmailTemplate");
90+
91+
try {
92+
$query = [
93+
'access_token' => $this->getAccessToken()
94+
];
95+
96+
$response = $this->client->get("/api/v1/mail-templates/{$template_id}", [
97+
'query' => $query,
98+
]
99+
);
100+
return json_decode($response->getBody()->getContents(), true);
101+
}
102+
catch (Exception $ex) {
103+
$this->cleanAccessToken();
104+
Log::error($ex);
105+
throw $ex;
106+
}
107+
}
108+
109+
/**
110+
* @param array $payload
111+
* @param string $html_template
112+
* @return mixed
113+
* @throws \GuzzleHttp\Exception\GuzzleException|\League\OAuth2\Client\Provider\Exception\IdentityProviderException
114+
*/
115+
public function getEmailPreview(array $payload, string $html_template)
116+
{
117+
Log::debug("EmailTemplatesApi::getEmailPreview");
118+
119+
try {
120+
$query = [
121+
'access_token' => $this->getAccessToken()
122+
];
123+
124+
$response = $this->client->put('/api/v1/mail-templates/all/render', [
125+
'query' => $query,
126+
RequestOptions::JSON => [
127+
"html" => $html_template,
128+
"payload" => $payload
129+
]
130+
]
131+
);
132+
return json_decode($response->getBody()->getContents(), true);
133+
}
134+
catch (Exception $ex) {
135+
$this->cleanAccessToken();
136+
Log::error($ex);
137+
throw $ex;
138+
}
139+
}
140+
}
141+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php namespace App\Services\Apis;
2+
/**
3+
* Copyright 2023 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use Exception;
15+
/**
16+
* Interface IEmailTemplatesApi
17+
* @package App\Services\Apis
18+
*/
19+
interface IEmailTemplatesApi
20+
{
21+
/**
22+
* @param string $template_id
23+
* @return null|mixed
24+
* @throws Exception
25+
*/
26+
public function getEmailTemplate(string $template_id);
27+
28+
/**
29+
* @param array $payload
30+
* @param string $html_template
31+
* @return null|mixed
32+
* @throws Exception
33+
*/
34+
public function getEmailPreview(array $payload, string $html_template);
35+
}

0 commit comments

Comments
 (0)