Skip to content

Commit

Permalink
Merge pull request #37 from Riskified/cancelation-email-when-order-is…
Browse files Browse the repository at this point in the history
…-declined

Cancelation email when order is declined
  • Loading branch information
Nimrod Ram authored Sep 12, 2017
2 parents 430b086 + 66e8bcd commit cfdefc0
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 0 deletions.
69 changes: 69 additions & 0 deletions app/code/community/Riskified/Full/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,73 @@ public function getDateTime($time = 'now')

return $dateTime->format($dateTime::ATOM);
}


/**
* Retrieve configuration of decline notification
*
* @return bool
*/
public function isDeclineNotificationEnabled()
{
return Mage::getStoreConfig('fullsection/decline_notification/enable', $this->getStoreId());
}

/**
* Retrieve declination email sender configuration
*
* @return string
*/
public function getDeclineNotificationSender()
{
return Mage::getStoreConfig('fullsection/decline_notification/email_identity', $this->getStoreId());

}

/**
* Retrieve declination email subject set in admin panel
*
* @return string
*/
public function getDeclineNotificationSubject()
{
return Mage::getStoreConfig('fullsection/decline_notification/title', $this->getStoreId());
}

/**
* Retrieve declination email content set in admin panel
*
* @return string
*/
public function getDeclineNotificationContent()
{
return Mage::getStoreConfig('fullsection/decline_notification/content', $this->getStoreId());
}

/**
* Retrieve declination email sender email based on configuration in admin panel
*
* @return string
*/
public function getDeclineNotificationSenderEmail()
{

return Mage::getStoreConfig(
'trans_email/ident_' . $this->getDeclineNotificationSender() . '/email',
$this->getStoreId()
);
}

/**
* Retrieve declination email sender name based on configuration in admin panel
*
* @return string
*/
public function getDeclineNotificationSenderName()
{
return Mage::getStoreConfig(
'trans_email/ident_' . $this->getDeclineNotificationSender() . '/name',
$this->getStoreId()
);
}
}
1 change: 1 addition & 0 deletions app/code/community/Riskified/Full/Helper/Order/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public function getPaymentDetails($order)
);
break;

case 'gene_braintree_creditcard':
case 'braintree':
case 'braintreevzero':
$cvvResultCode = $payment
Expand Down
131 changes: 131 additions & 0 deletions app/code/community/Riskified/Full/Model/Observer/Order/Decline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

class Riskified_Full_Model_Observer_Order_Decline
{
private $order;
public function handleOrderDecline(
Varien_Event_Observer $observer
) {
$order = $observer->getOrder();
$this->order = $order;
/**
* @var Riskified_Full_Helper_Data $dataHelper
*/
$dataHelper = Mage::helper("full");

if (!$dataHelper->isDeclineNotificationEnabled()) {
return $this;
}
if (Mage::registry("decline-email-sent")) {
return $this;
}

Mage::register("decline-email-sent", true);

$emailTemplate = Mage::getModel('core/email_template')
->loadDefault('riskified_order_declined');

$emailTemplate->setSenderEmail(
$dataHelper->getDeclineNotificationSenderEmail()
);

$emailTemplate->setSenderName(
$dataHelper->getDeclineNotificationSenderName()
);

$subject = $dataHelper->getDeclineNotificationSubject();
$content = $dataHelper->getDeclineNotificationContent();

$shortCodes = array(
"{{customer_name}}",
"{{customer_firstname}}",
"{{order_increment_id}}",
"{{order_view_url}}",
"{{products}}",
"{{store_name}}",
);
$formattedPayload = $this->getFormattedData();

foreach ($shortCodes as $key => $value) {
$subject = str_replace($value, $formattedPayload[$key], $subject);
$content = str_replace($value, $formattedPayload[$key], $content);
}

try {
if ($content == "") {
throw new Exception("Email content is empty");
}

if ($subject == "") {
throw new Exception("Email subject is empty");
}

$wasSent = $emailTemplate->send(
$order->getCustomerEmail(),
$order->getCustomerName(),
array(
'store' => Mage::app()->getStore(),
'subject' => $subject,
'order' => $order,
'content' => $content
)
);

if ($wasSent === true) {
$fileLog = $dataHelper->__(
"Decline email was sent to customer %s (%s) for order #%s",
$order->getCustomerName(),
$order->getCustomerEmail(),
$order->getIncrementId()
);

$orderComment = $dataHelper->__(
"Decline email was sent to customer %s (%s)",
$order->getCustomerName(),
$order->getCustomerEmail()
);
} else {
$fileLog = $dataHelper->__(
"Decline email was not sent to customer %s (%s) for order #%s - server internal error",
$order->getCustomerName(),
$order->getCustomerEmail(),
$order->getIncrementId()
);
$orderComment = $dataHelper->__(
"Decline email was not sent to customer %s (%s) - server internal error",
$order->getCustomerName(),
$order->getCustomerEmail()
);
}

Mage::helper('full/log')->log($fileLog);

$order
->addStatusHistoryComment($orderComment)
->setIsCustomerNotified(true);
$order->save();
} catch (Exception $e) {
Mage::logException($e);
}
}

private function getFormattedData()
{
$products = array();

foreach ($this->order->getAllItems() as $item) {
$products[] = $item->getName();
}

$data = array(
$this->order->getCustomerName(),
$this->order->getCustomerFirstname(),
$this->order->getIncrementId(),
Mage::getUrl('sales/order/view', array('order_id' => $this->order->getId())),
join(', ', $products),
Mage::app()->getStore()->getFrontendName()
);

return $data;
}
}
23 changes: 23 additions & 0 deletions app/code/community/Riskified/Full/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,25 @@
</riskified_full_handle_quote_submit>
</observers>
</sales_model_service_quote_submit_before>
<riskified_full_order_update_declined>
<observers>
<riskified_full_order_update_declined>
<type>singleton</type>
<class>full/observer_order_decline</class>
<method>handleOrderDecline</method>
</riskified_full_order_update_declined>
</observers>
</riskified_full_order_update_declined>
</events>
<template>
<email>
<riskified_order_declined module="full">
<label>Notification to customer when order was declined by Riskified</label>
<file>full/order/declined.html</file>
<type>html</type>
</riskified_order_declined>
</email>
</template>
</global>

<frontend>
Expand Down Expand Up @@ -325,6 +343,11 @@
<auto_invoice_capture_case>online</auto_invoice_capture_case>
<debug_logs>0</debug_logs>
</full>
<decline_notification>
<email_identity>general</email_identity>
<title>{{store_name}}: Order # {{order_increment_id}} has been declined</title>
<content><![CDATA[<p>We regret to inform you that your recent order #{{order_increment_id}} for {{products}} 
has been declined.</p>]]></content>
</decline_notification>
</fullsection>
</default>
</config>
77 changes: 77 additions & 0 deletions app/code/community/Riskified/Full/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,83 @@
</debug_logs>
</fields>
</full>
<decline_notification translate="label">
<label>Decline notification settings</label>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<enable translate="label comment">
<label>Enabled</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>
<![CDATA[Customer will be notified when his order was declined by Riskified side.]]>
</comment>
</enable>
<email_identity translate="label">
<label>Email sender</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_identity</source_model>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<depends>
<enable>1</enable>
</depends>
</email_identity>
<title translate="label comment">
<label>Email title</label>
<frontend_type>text</frontend_type>
<sort_order>30</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<depends>
<enable>1</enable>
</depends>
<comment>
<![CDATA[
Available shortcodes: <br />
{{products}} - Product names comma separated <br />
{{customer_name}} - Customer full name <br />
{{customer_firstname}} - Customer first name <br />
{{order_increment_id}} - Order Number <br />
{{order_view_url}} - Order url to order detail page in customer dashboard <br />
{{store_name}} - Store name <br />
]]>
</comment>
</title>
<content translate="label comment">
<label>Email content</label>
<frontend_type>textarea</frontend_type>
<sort_order>40</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<depends>
<enable>1</enable>
</depends>
<comment>
<![CDATA[
Available shortcodes: <br />
{{products}} - Product names comma separated <br />
{{customer_name}} - Customer full name <br />
{{customer_firstname}} - Customer first name <br />
{{order_increment_id}} - Order Number <br />
{{order_view_url}} - Order url to order detail page in customer dashboard <br />
{{store_name}} - Store name <br />
]]>
</comment>
</content>
</fields>
</decline_notification>
</groups>
</fullsection>
</sections>
Expand Down
33 changes: 33 additions & 0 deletions app/locale/en_US/template/email/full/order/declined.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--@subject {{var subject}} @-->
<!--@vars
{"store url=\"\"":"Store Url",
"var logo_url":"Email Logo Image Url",
"var logo_alt":"Email Logo Image Alt",
"htmlescape var=$order.getCustomerName()":"Customer Name",
"var store.getFrontendName()":"Store Name",
"store url=\"customer/account/\"":"Customer Account Url",
"var order.increment_id":"Order Id",
"var order.getCreatedAtFormated('long')":"Order Created At (datetime)",
"var order.getBillingAddress().format('html')":"Billing Address",
"var payment_html":"Payment Details",
"var order.getShippingAddress().format('html')":"Shipping Address",
"var order.getShippingDescription()":"Shipping Description",
"layout handle=\"sales_email_order_items\" order=$order":"Order Items Grid",
"var order.getEmailCustomerNote()":"Email Order Note"}
@-->
<!--@styles
@-->

{{template config_path="design/email/header"}}
{{inlinecss file="email-inline.css"}}

<table cellpadding="0" cellspacing="0" border="0">

<tr class="order-information">
<td>
{{var content}}
</td>
</tr>
</table>

{{template config_path="design/email/footer"}}
1 change: 1 addition & 0 deletions modman
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ app/design/adminhtml/default/default/template/full/ app/design/adminhtml/def
app/design/frontend/base/default/layout/full.xml app/design/frontend/base/default/layout/full.xml
app/design/frontend/base/default/template/full/ app/design/frontend/base/default/template/full/
app/etc/modules/Riskified_Full.xml app/etc/modules/Riskified_Full.xml
app/locale/en_US/template/email/full/order/declined.html app/locale/en_US/template/email/full/order/declined.html
lib/riskified_php_sdk/ lib/riskified_php_sdk/
lib/riskified_scripts/ lib/riskified_scripts/
skin/adminhtml/default/default/images/riskified/ skin/adminhtml/default/default/images/riskified/
Expand Down

0 comments on commit cfdefc0

Please sign in to comment.