Skip to content

Feature/order confirmation email pdf #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1183,4 +1183,20 @@ public function deActivateTicket($summit_id, $order_id, $ticket_id)
));
});
}

/**
* @param $summit_id
* @param $order_id
* @return \Illuminate\Http\JsonResponse|mixed
*/
public function getOrderConfirmationEmailPDF($summit_id, $order_id)
{
return $this->processRequest(function () use ($summit_id, $order_id) {
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->getResourceServerContext())->find($summit_id);
if (is_null($summit)) return $this->error404();

$content = $this->service->renderOrderConfirmationEmail($summit, intval($order_id));
return $this->pdf("order_{$order_id}_confirmation_email.pdf", $content);
});
}
}
78 changes: 78 additions & 0 deletions app/Http/Renderers/HTML2PDFRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php namespace App\Http\Renderers;
/**
* Copyright 2023 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use TCPDF;
/**
* Class HTML2PDFRenderer
* @package App\Http\Renderers
*/
final class HTML2PDFRenderer implements IRenderer
{
/**
* @var string
*/
private $html;

/**
* HTML2PDFRenderer constructor.
* @param string $html
*/
public function __construct(string $html)
{
$this->html = $html;
}

public function render(): string
{
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetTitle('');

// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set font
$pdf->setFont('helvetica', '', 10);

// add a page
$pdf->AddPage();

$pdf->writeHTML($this->html, true, false, true, false, '');

//Close and output PDF document
return $pdf->Output('', 'S');
}
}
2 changes: 2 additions & 0 deletions app/ModelSerializers/SerializerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ final class SerializerRegistry
const SerializerType_Admin_Voteable_CSV = "ADMIN_VOTEABLE_CSV";
const SerializerType_CSV = 'CSV';
const SerializerType_Admin_Registration_Stats = 'ADMIN_REG_STATS';
const SerializerType_Admin_Email_Preview = 'ADMIN_EMAIL_PREVIEW';

private function __clone()
{
Expand Down Expand Up @@ -429,6 +430,7 @@ private function __construct()

$this->registry['SummitOrder'] = [
self::SerializerType_Public => SummitOrderBaseSerializer::class,
self::SerializerType_Admin_Email_Preview => SummitOrderConfirmationEmailPreviewSerializer::class,
ISummitOrderSerializerTypes::CheckOutType => SummitOrderBaseSerializer::class,
ISummitOrderSerializerTypes::ReservationType => SummitOrderReservationSerializer::class,
ISummitOrderSerializerTypes::AdminType => SummitOrderAdminSerializer::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php namespace ModelSerializers;
use Libs\ModelSerializers\AbstractSerializer;
use models\summit\SummitOrder;

/**
* Copyright 2023 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/


/**
* Class SummitOrderConfirmationEmailPreviewSerializer
* @package App\ModelSerializers
*/
final class SummitOrderConfirmationEmailPreviewSerializer extends AbstractSerializer
{
protected static $array_mappings = [
'CreditCardType' => 'order_credit_card_type:json_string',
'CreditCard4Number' => 'order_credit_card_4number:json_string',
'Currency' => 'order_currency:json_string',
'CurrencySymbol' => 'order_currency_symbol:json_string',
'Number' => 'order_number:json_string',
'FinalAmountAdjusted' => 'order_amount:json_float',
'OwnerFullName' => 'owner_full_name:json_string',
'OwnerCompanyName' => 'owner_company:json_string',
];

protected static $allowed_relations = [
'member',
'tickets',
];

/**
* @param null $expand
* @param array $fields
* @param array $relations
* @param array $params
* @return array
*/
public function serialize($expand = null, array $fields = array(), array $relations = array(), array $params = array())
{
$order = $this->object;
if (!$order instanceof SummitOrder) return [];
$values = parent::serialize($expand, $fields, $relations, $params);

$values["summit_name"] = $order->getSummit()->getName();

if (!count($relations)) $relations = $this->getAllowedRelations();

if (in_array('tickets', $relations)) {
$tickets = [];
foreach ($order->getTickets() as $ticket) {
$ticket_dic = [
"currency" => $ticket->getCurrency(),
"currency_symbol" => $ticket->getCurrencySymbol(),
"has_owner" => $ticket->hasOwner(),
"need_details" => false,
"ticket_type_name" => $ticket->getTicketTypeName(),
"owner_email" => $ticket->getOwnerEmail(),
"price" => $ticket->getFinalAmount()
];

$promo_code = $ticket->getPromoCode();
if (!is_null($promo_code)) {
$ticket_dic["promo_code"] = ["code" => $promo_code->getCode()];
}

$tickets[] = $ticket_dic;
}
$values['tickets'] = $tickets;
}

return $values;
}
}
35 changes: 35 additions & 0 deletions app/Services/Apis/IEmailTemplatesApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php namespace App\Services\Apis;
/**
* Copyright 2023 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Exception;
/**
* Interface IEmailTemplatesApi
* @package App\Services\Apis
*/
interface IEmailTemplatesApi
{
/**
* @param string $template_id
* @return null|mixed
* @throws Exception
*/
public function getEmailTemplate(string $template_id);

/**
* @param array $payload
* @param string $html_template
* @return null|mixed
* @throws Exception
*/
public function getEmailPreview(array $payload, string $html_template);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
Expand All @@ -22,10 +23,10 @@
use libs\utils\ICacheService;
use models\exceptions\ValidationException;
/**
* Class MailApi
* Class MailService
* @package App\Services\Apis
*/
final class MailApi extends AbstractOAuth2Api implements IMailApi
final class MailService extends AbstractOAuth2Api implements IMailApi, IEmailTemplatesApi
{

/**
Expand Down Expand Up @@ -107,7 +108,7 @@ public function sendEmail
(
sprintf
(
"MailApi::sendEmail template_identifier %s to_email %s payload %s",
"MailService::sendEmail template_identifier %s to_email %s payload %s",
$template_identifier,
$to_email,
json_encode($payload)
Expand All @@ -132,12 +133,12 @@ public function sendEmail
];

if(!empty($cc_email)){
Log::debug(sprintf("MailApi::sendEmail setting cc_email %s", $cc_email));
Log::debug(sprintf("MailService::sendEmail setting cc_email %s", $cc_email));
$payload['cc_email'] = trim($cc_email);
}

if(!empty($bcc_email)){
Log::debug(sprintf("MailApi::sendEmail setting bcc_email %s", $bcc_email));
Log::debug(sprintf("MailService::sendEmail setting bcc_email %s", $bcc_email));
$payload['bcc_email'] = trim($bcc_email);
}

Expand All @@ -152,7 +153,7 @@ public function sendEmail
(
sprintf
(
"MailApi::sendEmail template_identifier %s to_email %s body %s",
"MailService::sendEmail template_identifier %s to_email %s body %s",
$template_identifier,
$to_email,
$body
Expand All @@ -173,7 +174,7 @@ public function sendEmail
(
sprintf
(
"MailApi::sendEmail template_identifier %s to_email %s payload %s error %s",
"MailService::sendEmail template_identifier %s to_email %s payload %s error %s",
$template_identifier,
$to_email,
json_encode($payload),
Expand All @@ -188,4 +189,63 @@ public function sendEmail
throw $ex;
}
}

/**
* @param string $template_id
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException
*/
public function getEmailTemplate(string $template_id) {
Log::debug("MailService::getEmailTemplate");

try {
$query = [
'access_token' => $this->getAccessToken()
];

$response = $this->client->get("/api/v1/mail-templates/{$template_id}", [
'query' => $query,
]
);
return json_decode($response->getBody()->getContents(), true);
}
catch (Exception $ex) {
$this->cleanAccessToken();
Log::error($ex);
throw $ex;
}
}

/**
* @param array $payload
* @param string $html_template
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException|\League\OAuth2\Client\Provider\Exception\IdentityProviderException
*/
public function getEmailPreview(array $payload, string $html_template)
{
Log::debug("MailService::getEmailPreview");

try {
$query = [
'access_token' => $this->getAccessToken()
];

$response = $this->client->put('/api/v1/mail-templates/all/render', [
'query' => $query,
RequestOptions::JSON => [
"html" => $html_template,
"payload" => $payload
]
]
);
return json_decode($response->getBody()->getContents(), true);
}
catch (Exception $ex) {
$this->cleanAccessToken();
Log::error($ex);
throw $ex;
}
}
}
Loading