Skip to content

Commit

Permalink
Merge pull request #178 from agnostack/master
Browse files Browse the repository at this point in the history
Release 3.0.1 > develop
  • Loading branch information
agrohs authored Oct 20, 2020
2 parents 307a58e + d27c4f9 commit f213e09
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 185 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The new 3.0 version fixes several core issues (incorrect/missing order address,
* Enable direct Order Search by ID
* Access Order Messages and Notes

Dowload the latest version of the extension at: https://github.com/agnostack/magento_extension/releases/tag/v3.0.0.
Dowload the latest version of the extension at: https://github.com/agnostack/magento_extension/releases/tag/v3.0.1.

Additional detailed instructions on configuring agnoStack for the new version can be found at: https://agnostack.com/faqs/setup-onboarding/configuring-magento-1.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,48 +37,11 @@ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
$originalData = $element->getOriginalData();
$this->addData(array(
'button_label' => Mage::helper('zendesk')->__($originalData['button_label']),
'button_url' => Mage::helper('zendesk')->__($originalData['button_url']),
'html_id' => $element->getHtmlId(),
'url' => Mage::getSingleton('adminhtml/url')->getUrl('*/setup/start')
));

return $this->_toHtml();
}

public function getPostUrl()
{
return Mage::helper('zendesk')->getProvisionUrl();
}

public function getPostInfo()
{
$websiteCode = Mage::app()->getRequest()->getParam('website');
if ($websiteCode) {
$website = Mage::getModel('core/website')->load($websiteCode);
} else {
$website = Mage::getModel('core/website')->getCollection()
->addFieldToFilter('is_default', 1)
->getFirstItem();
}

$storeCode = Mage::app()->getRequest()->getParam('store');
if ($storeCode) {
$store = Mage::getModel('core/store')->load($storeCode);
} else {
$store = $website->getDefaultStore();
}

$info = array(
'magento_domain' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB),
'magento_current_user_id' => Mage::getSingleton('admin/session')->getUser()->getUserId(),
'magento_user_count' => Mage::getModel('admin/user')->getCollection()->getSize(),
'magento_auth_token' => Mage::helper('zendesk')->getProvisionToken(true),
'magento_callback' => Mage::helper('adminhtml')->getUrl('adminhtml/zendesk/redirect', array('type' => 'settings', 'id' => 'zendesk')),
'magento_locale' => Mage::getStoreConfig('general/locale/code'),
'magento_timezone' => Mage::getStoreConfig('general/locale/timezone'),
'magento_api_url' => Mage::getUrl('zendesk/api', array('_store' => $store->getCode())),
'magento_store_name' => $website->getName(),
);

return $info;
}
}
167 changes: 71 additions & 96 deletions src/app/code/community/Zendesk/Zendesk/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,46 +154,6 @@ public function setApiToken($token = null)
return $token;
}

/**
* Returns the provisioning endpoint for new setups.
*
* This uses the config/zendesk/provision_url XML path to retrieve the setting, with a default value set in
* the extension config.xml file. This can be overridden in your website's local.xml file.
* @return null|string URL or null on failure
*/
public function getProvisionUrl()
{
$config = Mage::getConfig();
$data = $config->getNode('zendesk/provision_url');
if(!$data) {
return null;
}
return (string)$data;
}

public function getProvisionToken($generate = false)
{
$token = Mage::getStoreConfig('zendesk/hidden/provision_token', 0);

if( (!$token || strlen(trim($token)) == 0) && $generate) {
$token = $this->setProvisionToken();
}

return $token;
}

public function setProvisionToken($token = null)
{
if(!$token) {
$token = hash('sha256', Mage::helper('oauth')->generateToken());
}

Mage::getModel('core/config')->saveConfig('zendesk/hidden/provision_token', $token, 'default');
Mage::getConfig()->removeCache();

return $token;
}

public function getOrderDetail($order)
{
// if the admin site has a custom URL, use it
Expand Down Expand Up @@ -479,24 +439,26 @@ protected function formatCustomer($order)

protected function formatAddress($address)
{
$addressData = array(
'type' => 'address',
'first_name' => $address->getFirstname(),
'last_name' => $address->getLastname(),
'city' => $address->getCity(),
'county' => $address->getRegion(),
'postcode' => $address->getPostcode(),
'country' => $address->getCountryId(),
'phone' => $address->getTelephone()
);

$entityId = $address->getEntityId();
$addressId = $address->getCustomerAddressId();
$addressData['id'] = $addressId ?: $entityId;

$street = $address->getStreet();
$addressData['line_1'] = $street[0] ?: '';
$addressData['line_2'] = $street[1] ?: '';
if ($address) {
$addressData = array(
'type' => 'address',
'first_name' => $address->getFirstname(),
'last_name' => $address->getLastname(),
'city' => $address->getCity(),
'county' => $address->getRegion(),
'postcode' => $address->getPostcode(),
'country' => $address->getCountryId(),
'phone' => $address->getTelephone()
);

$entityId = $address->getEntityId();
$addressId = $address->getCustomerAddressId();
$addressData['id'] = $addressId ?: $entityId;

$street = $address->getStreet();
$addressData['line_1'] = $street[0] ?: '';
$addressData['line_2'] = $street[1] ?: '';
}

return $addressData;
}
Expand All @@ -505,44 +467,52 @@ public function getShipments($order)
{
$shipments = array();
$orderStatus = $order->getStatus();
$serviceCode = $order->getShippingDescription();
$tracks = $order->getTracksCollection();
$shippingMethod = $order->getShippingMethod();
$orderShippingAddress = $order->getShippingAddress();

foreach($order->getShipmentsCollection() as $shipment) {
$shipmentId = $shipment->getEntityId();
$shippingAddress = $shipment->getShippingAddress();
$serviceCode = $order->getShippingDescription();
}

if ($shipmentId) {
$tracks = $order->getTracksCollection();
if (count($tracks) > 0) {
foreach($tracks as $track) {
if ($shipmentId == $track->getParentId()) {
$shipments[] = array(
'id' => $track->getEntityId(),
'carrier' => $track->getTitle(),
'carrier_code' => $track->getCarrierCode(),
'service_code' => $serviceCode,
'shipping_description' => $track->getDescription() ?: '',
'created_at' => $track->getCreatedAt(),
'updated_at' => $track->getUpdatedAt(),
'tracking_number' => $track->getTrackNumber(),
'shipping_address' => $this->formatAddress($shippingAddress),
'order_status' => $orderStatus,
);
if ($shipmentId) {
if (count($tracks) > 0) {
foreach($tracks as $track) {
if ($shipmentId == $track->getParentId()) {
$shipment = array(
'id' => $track->getEntityId(),
'carrier' => $track->getTitle(),
'carrier_code' => $track->getCarrierCode(),
'service_code' => $serviceCode,
'shipping_description' => $track->getDescription() ?: '',
'created_at' => $track->getCreatedAt(),
'updated_at' => $track->getUpdatedAt(),
'tracking_number' => $track->getTrackNumber(),
'order_status' => $orderStatus,
);
if ($shippingAddress) {
$shipment['shipping_address'] = $this->formatAddress($shippingAddress);
}
$shipments[] = $shipment;
}
}
} else {
$shipment = array(
'service_code' => $serviceCode,
'carrier_code' => $shippingMethod,
'order_status' => $orderStatus,
);
if ($shippingAddress) {
$shipment['shipping_address'] = $this->formatAddress($shippingAddress);
}
$shipments[] = $shipment;
}
} else {
$shipments[] = array(
'service_code' => $serviceCode,
'carrier_code' => $order->getShippingMethod(),
'shipping_address' => $this->formatAddress($shippingAddress),
'order_status' => $orderStatus,
);
}
} else {
$shippingAddress = $order->getShippingAddress();
}

if (empty($shipments) && $orderShippingAddress) {
$shipments[] = array(
'shipping_address' => $this->formatAddress($shippingAddress),
'shipping_address' => $this->formatAddress($orderShippingAddress),
);
}

Expand All @@ -558,22 +528,22 @@ public function getOrderDetailBasic($order)
$shippingAddress = $order->getShippingAddress();
$shippingWithTax = $order->getShippingInclTax();
$shippingMethod = $order->getShippingMethod();
$billingAddress = $order->getBillingAddress();

$orderInfo = array(
'id' => $order->getIncrementId(),
'url' => $urlModel->getUrl('adminhtml/sales_order/view', array('order_id' => $order->getId())),
'transaction_id' => $order->getIncrementId(),
'status' => $order->getStatus(),
'billing_address' => $this->formatAddress($order->getBillingAddress()),
'meta' => array(
'store_info' => array(
'type' => 'store_info',
'name' => $order->getStoreName()
),
'display_price' => array(
'with_tax' => $this->formatPrice($order->getGrandTotal(), $currency),
'without_tax' => $this->formatPrice($order->getGrandTotal() - $order->getTaxAmount(), $currency), // TODO: get without tax
'tax' => $this->formatPrice($order->getTaxAmount(), $currency) // TODO: get tax
'without_tax' => $this->formatPrice($order->getGrandTotal() - $order->getTaxAmount(), $currency),
'tax' => $this->formatPrice($order->getTaxAmount(), $currency)
),
'timestamps' => array(
'created_at' => $order->getCreatedAt(),
Expand All @@ -590,9 +560,12 @@ public function getOrderDetailBasic($order)
),
'shipments' => array(),
);
if ($billingAddress) {
$orderInfo['billing_address'] = $this->formatAddress($billingAddress);
}

foreach($order->getItemsCollection(array(), true) as $item) {
$itemWithTax = $item->getRowTotal();
$itemWithoutTax = $item->getRowTotal();
$itemTax = $item->getTaxAmount();

$productId = $item->getProductId();
Expand All @@ -610,8 +583,8 @@ public function getOrderDetailBasic($order)
'refunded' => intval($item->getQtyRefunded()),
'meta' => array(
'display_price' => array(
'with_tax' => $this->formatPrice($itemWithTax, $currency),
'without_tax' => $this->formatPrice($itemWithTax - $itemTax, $currency),
'with_tax' => $this->formatPrice($itemWithoutTax + $itemTax, $currency),
'without_tax' => $this->formatPrice($itemWithoutTax, $currency),
'tax' => $this->formatPrice($iitemTax, $currency)
),
'timestamps' => array(
Expand Down Expand Up @@ -772,7 +745,9 @@ public function getCustomer($customer)
);

foreach($customer->getAddressesCollection() as $address) {
$info['addresses'][] = $this->formatAddress($address);
if ($address) {
$info['addresses'][] = $this->formatAddress($address);
}
}

return $info;
Expand Down Expand Up @@ -825,7 +800,7 @@ public function getFilteredOrdersByProduct($customerFilters, $productFilters)
);
}

if($email) {
if ($email) {
$filteredOrdersData = array_filter(array_values($ordersData), function ($orderData) use ($email) {
return ($orderData['email'] == $email);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function preDispatch() {
// TODO!!!!!!!: read version from config.xml
// $configSettings = Mage::getSingleton('Zendesk_Zendesk/config');
// Mage::log(json_encode($configSettings), null, 'zendesk.log');
$this->getResponse()->setHeader('X-Extension-Version', '3.0.0');
$this->getResponse()->setHeader('X-Extension-Version', '3.0.1');
return $this;
}

Expand Down Expand Up @@ -67,45 +67,39 @@ public function _authorise()
}

$apiToken = Mage::helper('zendesk')->getApiToken(false);
$provisionToken = Mage::helper('zendesk')->getProvisionToken(false);

// Provisioning tokens are always accepted, hence why they are deleted after the initial process
if(!$provisionToken || $token != $provisionToken) {
// Use of the provisioning token "overrides" the configuration for the API, so we check this after
// confirming the provisioning token has not been sent
if(!Mage::getStoreConfig('zendesk/api/enabled')) {
$this->getResponse()
->setBody(json_encode(array('success' => false, 'message' => 'API access disabled')))
->setHttpResponseCode(403)
->setHeader('Content-type', 'application/json', true);

if(!Mage::getStoreConfig('zendesk/api/enabled')) {
$this->getResponse()
->setBody(json_encode(array('success' => false, 'message' => 'API access disabled')))
->setHttpResponseCode(403)
->setHeader('Content-type', 'application/json', true);

Mage::log('API access disabled.', null, 'zendesk.log');
Mage::log('API access disabled.', null, 'zendesk.log');

return false;
}
return false;
}

// If the API is enabled then check the token
if(!$token) {
$this->getResponse()
->setBody(json_encode(array('success' => false, 'message' => 'No authorisation token provided')))
->setHttpResponseCode(401)
->setHeader('Content-type', 'application/json', true);
// If the API is enabled then check the token
if(!$token) {
$this->getResponse()
->setBody(json_encode(array('success' => false, 'message' => 'No authorisation token provided')))
->setHttpResponseCode(401)
->setHeader('Content-type', 'application/json', true);

Mage::log('No authorisation token provided.', null, 'zendesk.log');
Mage::log('No authorisation token provided.', null, 'zendesk.log');

return false;
}
return false;
}

if($token != $apiToken) {
$this->getResponse()
->setBody(json_encode(array('success' => false, 'message' => 'Not authorised')))
->setHttpResponseCode(401)
->setHeader('Content-type', 'application/json', true);
if($token != $apiToken) {
$this->getResponse()
->setBody(json_encode(array('success' => false, 'message' => 'Not authorised')))
->setHttpResponseCode(401)
->setHeader('Content-type', 'application/json', true);

Mage::log('Not authorised.', null, 'zendesk.log');
Mage::log('Not authorised.', null, 'zendesk.log');

return false;
}
return false;
}

return true;
Expand Down
5 changes: 1 addition & 4 deletions src/app/code/community/Zendesk/Zendesk/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
<config>
<modules>
<Zendesk_Zendesk>
<version>3.0.0</version>
<version>3.0.1</version>
</Zendesk_Zendesk>
</modules>
<zendesk>
<provision_url>https://signup.zendesk.com/provisioning/magento/welcome</provision_url>
</zendesk>
<global>
<models>
<zendesk>
Expand Down
Loading

0 comments on commit f213e09

Please sign in to comment.