Skip to content

Commit

Permalink
Merge pull request #52 from paynl/feature/paylink_bugfix
Browse files Browse the repository at this point in the history
Bugfix Paylink
  • Loading branch information
woutse authored Jul 13, 2022
2 parents 5786a16 + 60f72ef commit ec94318
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 80 deletions.
178 changes: 99 additions & 79 deletions Model/Paymentmethod/Paylink.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Magento\Sales\Model\Order;
use Magento\Framework\DataObject;
use Paynl\Payment\Helper\PayHelper;

class Paylink extends PaymentMethod
{
Expand Down Expand Up @@ -54,93 +55,112 @@ public function initialize($paymentAction, $stateObject)
$send_paylink_email = $this->_scopeConfig->getValue('payment/paynl_payment_paylink/send_paylink_email', 'store', $storeId);

if ($send_paylink_email == 0) {
$paylinktext = __('PAY.: Here is your ');
$postText = __('Open or copy the link to share.');
$order->addStatusHistoryComment($paylinktext . '<A href="' . $url . '">PAY. Link</a>. ' . $postText, $status);
$this->addPaylinkComment($order, $url, $status);
} else {
$paylinktext = __('A PAY. Paylink has been send to');
$order->addStatusHistoryComment($paylinktext . ' ' . $order->getCustomerEmail() . '.', $status);

$supportEmail = $this->_scopeConfig->getValue('trans_email/ident_support/email', 'store', $storeId);
$senderName = $this->_scopeConfig->getValue('trans_email/ident_sales/name', 'store', $storeId);
$senderEmail = $this->_scopeConfig->getValue('trans_email/ident_sales/email', 'store', $storeId);

$sender = [
'name' => $senderName,
'email' => $senderEmail,
];

$customerEmail = [$order->getCustomerEmail()];

$paymentHelper = $objectManager->create(\Magento\Payment\Helper\Data::class);

$orderHTML = $paymentHelper->getInfoBlockHtml(
$order->getPayment(),
$storeId
);

$addressRenderer = $objectManager->create(\Magento\Sales\Model\Order\Address\Renderer::class);

$show_order_in_mail = $this->_scopeConfig->getValue('payment/paynl_payment_paylink/show_order_in_mail', 'store', $storeId);
if ($show_order_in_mail) {
$show_order_in_mail = 1;
} else {
$show_order_in_mail = 0;
try {
$customerEmail = [$order->getCustomerEmail()];

if (empty($customerEmail)) {
# Can't send email without customer email so add paylink as a comment instead.
$order->addStatusHistoryComment(__('PAY.: Customer E-mail is empty cannot send E-mail.'), $status)->save();
$this->addPaylinkComment($order, $url, $status);
return false;
}

$supportEmail = $this->_scopeConfig->getValue('trans_email/ident_support/email', 'store', $storeId);
$senderName = $this->_scopeConfig->getValue('trans_email/ident_sales/name', 'store', $storeId);
$senderEmail = $this->_scopeConfig->getValue('trans_email/ident_sales/email', 'store', $storeId);

$sender = [
'name' => $senderName,
'email' => $senderEmail,
];

$paymentHelper = $objectManager->create(\Magento\Payment\Helper\Data::class);

$orderHTML = $paymentHelper->getInfoBlockHtml(
$order->getPayment(),
$storeId
);

$addressRenderer = $objectManager->create(\Magento\Sales\Model\Order\Address\Renderer::class);

$show_order_in_mail = $this->_scopeConfig->getValue('payment/paynl_payment_paylink/show_order_in_mail', 'store', $storeId);
if ($show_order_in_mail) {
$show_order_in_mail = 1;
} else {
$show_order_in_mail = 0;
}

$subject = $this->_scopeConfig->getValue('payment/paynl_payment_paylink/paylink_subject', 'store', $storeId);
$subject = str_replace('((paylink))', '<a href="' . $url . '">' . __('PAY. paylink') . '</a>', $subject);
$subject = str_replace('((customer_name))', $order->getCustomerName(), $subject);
$subject = str_replace('((store_name))', $order->getStore()->getName(), $subject);
$subject = str_replace('((support_email))', '<a href="mailto:' . $supportEmail . '">' . $supportEmail . '</a>', $subject);
$subject = str_replace('((order_id))', $order->getIncrementId(), $subject);

$body = $this->_scopeConfig->getValue('payment/paynl_payment_paylink/paylink_body', 'store', $storeId);
$body = nl2br($body);
$body = str_replace('((paylink))', '<a href="' . $url . '">' . __('PAY. paylink') . '</a>', $body);
$body = str_replace('((customer_name))', $order->getCustomerName(), $body);
$body = str_replace('((store_name))', $order->getStore()->getName(), $body);
$body = str_replace('((support_email))', '<a href="mailto:' . $supportEmail . '">' . $supportEmail . '</a>', $body);
$body = str_replace('((order_id))', $order->getIncrementId(), $body);

$templateVars = [
'subject' => $subject,
'body' => $body,
'order' => $order,
'store' => $store,
'customer_name' => $order->getCustomerName(),
'paylink' => $url,
'support_email' => $supportEmail,
'current_language' => $lang,
'order_id' => $order->getIncrementId(),
'billing' => $order->getBillingAddress(),
'payment_html' => $orderHTML,
'formattedShippingAddress' => $order->getIsVirtual() ? null : $addressRenderer->format($order->getShippingAddress(), 'html'),
'formattedBillingAddress' => $addressRenderer->format($order->getBillingAddress(), 'html'),
'created_at_formatted' => $order->getCreatedAtFormatted(1),
'customer_name' => $order->getCustomerName(),
'is_not_virtual' => $order->getIsNotVirtual(),
'email_customer_note' => $order->getEmailCustomerNote(),
'frontend_status_label' => $order->getFrontendStatusLabel(),
'show_order_in_mail' => $show_order_in_mail,
];

$transportBuilder = $objectManager->create(\Magento\Framework\Mail\Template\TransportBuilder::class);

payHelper::logDebug('Sending Paylink E-mail with the following user data: ', array("sender" => $sender, "customer_email" => $customerEmail, "support_email" => $supportEmail));
$transport = $transportBuilder->setTemplateIdentifier('paylink_email_template')
->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId])
->setTemplateVars($templateVars)
->setFrom($sender)
->addTo($customerEmail)
->setReplyTo($supportEmail)
->getTransport();
$transport->sendMessage();

$paylinktext = __('A PAY. Paylink has been send to');
$order->addStatusHistoryComment($paylinktext . ' ' . $order->getCustomerEmail() . '.', $status)->save();
} catch (\Exception $e) {
payHelper::logDebug('Paylink exception: ' . $e->getMessage());
$order->addStatusHistoryComment(__('PAY.: Unable to send E-mail'), $status)->save();
$this->addPaylinkComment($order, $url, $status);
}

$subject = $this->_scopeConfig->getValue('payment/paynl_payment_paylink/paylink_subject', 'store', $storeId);
$subject = str_replace('((paylink))', '<a href="' . $url . '">' . __('PAY. paylink') . '</a>', $subject);
$subject = str_replace('((customer_name))', $order->getCustomerName(), $subject);
$subject = str_replace('((store_name))', $order->getStore()->getName(), $subject);
$subject = str_replace('((support_email))', '<a href="mailto:' . $supportEmail . '">' . $supportEmail . '</a>', $subject);
$subject = str_replace('((order_id))', $order->getIncrementId(), $subject);

$body = $this->_scopeConfig->getValue('payment/paynl_payment_paylink/paylink_body', 'store', $storeId);
$body = nl2br($body);
$body = str_replace('((paylink))', '<a href="' . $url . '">' . __('PAY. paylink') . '</a>', $body);
$body = str_replace('((customer_name))', $order->getCustomerName(), $body);
$body = str_replace('((store_name))', $order->getStore()->getName(), $body);
$body = str_replace('((support_email))', '<a href="mailto:' . $supportEmail . '">' . $supportEmail . '</a>', $body);
$body = str_replace('((order_id))', $order->getIncrementId(), $body);

$templateVars = [
'subject' => $subject,
'body' => $body,
'order' => $order,
'store' => $store,
'customer_name' => $order->getCustomerName(),
'paylink' => $url,
'support_email' => $supportEmail,
'current_language' => $lang,
'order_id' => $order->getIncrementId(),
'billing' => $order->getBillingAddress(),
'payment_html' => $orderHTML,
'formattedShippingAddress' => $order->getIsVirtual() ? null : $addressRenderer->format($order->getShippingAddress(), 'html'),
'formattedBillingAddress' => $addressRenderer->format($order->getBillingAddress(), 'html'),
'created_at_formatted' => $order->getCreatedAtFormatted(1),
'customer_name' => $order->getCustomerName(),
'is_not_virtual' => $order->getIsNotVirtual(),
'email_customer_note' => $order->getEmailCustomerNote(),
'frontend_status_label' => $order->getFrontendStatusLabel(),
'show_order_in_mail' => $show_order_in_mail,
];

$transportBuilder = $objectManager->create(\Magento\Framework\Mail\Template\TransportBuilder::class);

$transport = $transportBuilder->setTemplateIdentifier('paylink_email_template')
->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId])
->setTemplateVars($templateVars)
->setFrom($sender)
->addTo($customerEmail)
->setReplyTo($supportEmail)
->getTransport();
$transport->sendMessage();
}

parent::initialize($paymentAction, $stateObject);
}
}

public function addPaylinkComment($order, $url, $status)
{
$paylinktext = __('PAY.: Here is your ');
$postText = __('Open or copy the link to share.');
$order->addStatusHistoryComment($paylinktext . '<A href="' . $url . '">PAY. Link</a>. ' . $postText, $status)->save();
}

public function assignData(\Magento\Framework\DataObject $data)
{
$this->getInfoInstance()->setAdditionalInformation('valid_days', $data->getData('additional_data')['valid_days']);
Expand Down
4 changes: 3 additions & 1 deletion i18n/nl_NL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,6 @@
"Set PAY. loglevel","Geef aan tot welk niveau logregels moeten worden opgeslagen"
"Select whether you want PAY. style applied to your checkout.","Geef aan of u PAY. styling wilt toepassen in uw checkout(Deze is vereist voor de optie `Icoon Grootte`)."
"Select whether you want to use classic icons, or the newest up-to-date icons.","Geef aan of u de oude stijl betaaliconen wilt gebruiken, of liever de meest up-to-date set afbeeldingen."
"Payment has been canceled","Betaling is geannuleerd"
"Payment has been canceled","Betaling is geannuleerd"
"PAY.: Unable to send E-mail", "PAY.: Kan geen e-mail verzenden"
"PAY.: Customer E-mail is empty cannot send E-mail.", "PAY.: E-mail klant is leeg kan geen e-mail verzenden."

0 comments on commit ec94318

Please sign in to comment.