Skip to content

Commit d81ef13

Browse files
committed
chore: add new v3 zendesk endpoints
1 parent b31346d commit d81ef13

File tree

6 files changed

+612
-3
lines changed

6 files changed

+612
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
**/.DS_Store
2+
13
/vendor

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The latest stable version of the extension can be installed via the [Magento Con
1414

1515
### General Notes
1616

17-
The extension provides its own custom RESTful API, which is intended to be used by the [Magento Zendesk App](https://github.com/zendesk/magento_app). The custom API allows for a consistent interface across all Magento versions, regardless of whether they support XML-RPC, SOAP or REST interfaces, and provides exactly the data that the app requires.
17+
The extension provides its own custom RESTful API, which is intended to be used by the [agnoStack app](https://www.zendesk.com/apps/support/agnostack-commerce---by-particular/). The custom API allows for a consistent interface across all Magento versions, regardless of whether they support XML-RPC, SOAP or REST interfaces, and provides exactly the data that the app requires.
1818

1919
The base URL of the API is `http://your_site_base_url/zendesk/api/`.
2020

src/app/code/community/Zendesk/Zendesk/Helper/Data.php

Lines changed: 376 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,380 @@ public function isAdmin()
442442
(Mage::app()->getStore()->isAdmin() || Mage::getDesign()->getArea() == 'adminhtml')
443443
);
444444
}
445+
446+
#region version 3.0 or later
447+
448+
protected function formatPrice($price, $currency)
449+
{
450+
return array(
451+
'amount' => $price * 100,
452+
'currency' => $currency
453+
);
454+
}
455+
456+
protected function formatCustomer($order)
457+
{
458+
$isGuest = (bool)$order->getCustomerIsGuest();
459+
$id = $order->getCustomerId();
460+
$email = $order->getCustomerEmail();
461+
$customer = array();
462+
463+
if ($isGuest){
464+
$customer['type'] = 'guest';
465+
} else {
466+
$customer['type'] = 'customer';
467+
}
468+
469+
if (!empty($id)) {
470+
$customer['id'] = $id;
471+
}
472+
if (!empty($email)) {
473+
$customer['email'] = $email;
474+
}
475+
// TODO: can we get customer URL or timestamps??
476+
477+
return $customer;
478+
}
479+
480+
protected function formatAddress($address)
481+
{
482+
$addressData = array(
483+
'type' => 'address',
484+
'first_name' => $address->getFirstname(),
485+
'last_name' => $address->getLastname(),
486+
'city' => $address->getCity(),
487+
'county' => $address->getRegion(),
488+
'postcode' => $address->getPostcode(),
489+
'country' => $address->getCountryId(),
490+
'phone' => $address->getTelephone()
491+
);
492+
493+
$entityId = $address->getEntityId();
494+
$addressId = $address->getCustomerAddressId();
495+
$addressData['id'] = $addressId ?: $entityId;
496+
497+
$street = $address->getStreet();
498+
$addressData['line_1'] = $street[0] ?: '';
499+
$addressData['line_2'] = $street[1] ?: '';
500+
501+
return $addressData;
502+
}
503+
504+
public function getShipments($order)
505+
{
506+
$shipments = array();
507+
$orderStatus = $order->getStatus();
508+
509+
foreach($order->getShipmentsCollection() as $shipment) {
510+
$shipmentId = $shipment->getEntityId();
511+
$shippingAddress = $shipment->getShippingAddress();
512+
$serviceCode = $order->getShippingDescription();
513+
}
514+
515+
if ($shipmentId) {
516+
$tracks = $order->getTracksCollection();
517+
if (count($tracks) > 0) {
518+
foreach($tracks as $track) {
519+
if ($shipmentId == $track->getParentId()) {
520+
$shipments[] = array(
521+
'id' => $track->getEntityId(),
522+
'carrier' => $track->getTitle(),
523+
'carrier_code' => $track->getCarrierCode(),
524+
'service_code' => $serviceCode,
525+
'shipping_description' => $track->getDescription() ?: '',
526+
'created_at' => $track->getCreatedAt(),
527+
'updated_at' => $track->getUpdatedAt(),
528+
'tracking_number' => $track->getTrackNumber(),
529+
'shipping_address' => $this->formatAddress($shippingAddress),
530+
'order_status' => $orderStatus,
531+
);
532+
}
533+
}
534+
} else {
535+
$shipments[] = array(
536+
'service_code' => $serviceCode,
537+
'carrier_code' => $order->getShippingMethod(),
538+
'shipping_address' => $this->formatAddress($shippingAddress),
539+
'order_status' => $orderStatus,
540+
);
541+
}
542+
}
543+
544+
return $shipments;
545+
}
546+
547+
public function getOrderDetailBasic($order)
548+
{
549+
// if the admin site has a custom URL, use it
550+
$urlModel = Mage::getModel('adminhtml/url')->setStore('admin');
551+
552+
$currency = $order->getOrderCurrencyCode();
553+
$shippingAddress = $order->getShippingAddress();
554+
$shippingWithTax = $order->getShippingInclTax();
555+
556+
$orderInfo = array(
557+
'id' => $order->getIncrementId(),
558+
'url' => $urlModel->getUrl('adminhtml/sales_order/view', array('order_id' => $order->getId())),
559+
'transaction_id' => $order->getIncrementId(),
560+
'status' => $order->getStatus(),
561+
'billing_address' => $this->formatAddress($order->getBillingAddress()),
562+
'meta' => array(
563+
'store_info' => array(
564+
'type' => 'store_info',
565+
'name' => $order->getStoreName()
566+
),
567+
'display_price' => array(
568+
'with_tax' => $this->formatPrice($order->getGrandTotal(), $currency),
569+
'without_tax' => $this->formatPrice($order->getGrandTotal() - $order->getTaxAmount(), $currency), // TODO: get without tax
570+
'tax' => $this->formatPrice($order->getTaxAmount(), $currency) // TODO: get tax
571+
),
572+
'timestamps' => array(
573+
'created_at' => $order->getCreatedAt(),
574+
'updated_at' => $order->getUpdatedAt(),
575+
)
576+
),
577+
'relationships' => array(
578+
'customer' => array(
579+
'data' => $this->formatCustomer($order)
580+
),
581+
'items' => array(
582+
'data' => array()
583+
),
584+
),
585+
'shipments' => array(),
586+
);
587+
588+
foreach($order->getItemsCollection(array(), true) as $item) {
589+
$itemWithTax = $item->getRowTotal();
590+
$itemTax = $item->getTaxAmount();
591+
$orderInfo['relationships']['items']['data'][] = array(
592+
'type' => 'order_item',
593+
'id' => $item->getItemId(),
594+
'product_id' => $item->getProductId(),
595+
'name' => $item->getName(),
596+
'sku' => $item->getSku(),
597+
'quantity' => intval($item->getQtyOrdered()),
598+
'refunded' => intval($item->getQtyRefunded()),
599+
'meta' => array(
600+
'display_price' => array(
601+
'with_tax' => $this->formatPrice($itemWithTax, $currency),
602+
'without_tax' => $this->formatPrice($itemWithTax - $itemTax, $currency),
603+
'tax' => $this->formatPrice($iitemTax, $currency)
604+
),
605+
'timestamps' => array(
606+
'created_at' => $item->getCreatedAt(),
607+
'updated_at' => $item->getUpdatedAt(),
608+
)
609+
)
610+
);
611+
}
612+
613+
if ($shippingWithTax) {
614+
$shippingTax = $order->getShippingTaxAmount();
615+
$shippingItem = array(
616+
'type' => 'custom_item',
617+
'id' => 'shipping--'.$order->getEntityId(),
618+
'product_id' => $order->getEntityId(),
619+
'name' => 'shipping--'.$order->getShippingDescription(),
620+
'sku' => $order->getShippingMethod(),
621+
'quantity' => 1,
622+
'refunded' => 0,
623+
'meta' => array(
624+
'display_price' => array(
625+
'with_tax' => $this->formatPrice($shippingWithTax, $currency),
626+
'without_tax' => $this->formatPrice($shippingWithTax - $shippingTax, $currency),
627+
'tax' => $this->formatPrice($shippingTax, $currency)
628+
),
629+
'timestamps' => array(
630+
'created_at' => $order->getCreatedAt(),
631+
'updated_at' => $order->getUpdatedAt(),
632+
)
633+
)
634+
);
635+
array_push($orderInfo['relationships']['items']['data'], $shippingItem);
636+
}
637+
638+
$orderInfo['shipments'] = $this->getShipments($order);
639+
640+
return $orderInfo;
641+
}
642+
643+
public function getOrderDetailExtended($order)
644+
{
645+
// if the admin site has a custom URL, use it
646+
$urlModel = Mage::getModel('adminhtml/url')->setStore('admin');
647+
648+
$currency = $order->getOrderCurrencyCode();
649+
650+
$orderInfo = $this->getOrderDetailBasic($order);
651+
652+
foreach($order->getPaymentsCollection() as $payment) {
653+
$transactionAmount = $payment->getAmountAuthorized() ?: $payment->getAmountOrdered();
654+
$gateway = $payment->getMethod();
655+
656+
$lastTransId = $payment->getLastTransId();
657+
658+
if (!$lastTransId && $gateway == 'authorizenet') {
659+
$additionalInformation = $payment->getAdditionalInformation();
660+
$authorizeCards = $additionalInformation['authorize_cards'];
661+
$authorizeCardKeys = array_keys($authorizeCards);
662+
$authorizeCard = $authorizeCards[$authorizeCardKeys[0]];
663+
$lastTransId = $authorizeCard['last_trans_id'];
664+
}
665+
666+
if (null != $lastTransId) {
667+
$transaction = $payment->lookupTransaction($lastTransId, 'capture'); // TODO grab authorization as well
668+
}
669+
670+
if (!empty($transaction)) {
671+
$transactionData = $transaction->getData();
672+
}
673+
674+
$orderInfo['relationships']['transactions']['data'][] = array(
675+
// 'DATA' => $payment->getData(), // TEMP
676+
// 'METHODS' => get_class_methods($payment), // TEMP
677+
'id' => $transactionData['transaction_id'], //TODO validate this is the correct value
678+
'type' => $transactionData['txn_type'], //TODO is this only always payment? or can this be refund?
679+
'reference' => $transactionData['txn_id'], //TODO validate this is the correct value
680+
'gateway' => $gateway, //TODO validate this is the correct value
681+
'status' => $payment->getCcCidStatus(), //TODO validate this is the correct value
682+
'meta' => array(
683+
'display_price' => $this->formatPrice($transactionAmount, $currency),
684+
'timestamps' => array(
685+
'created_at' => $order->getCreatedAt(),
686+
'updated_at' => $order->getUpdatedAt(),
687+
)
688+
),
689+
'relationships' => array(
690+
'charges' => array(
691+
'data' => []
692+
)
693+
)
694+
);
695+
}
696+
697+
// NOTE this is invoices
698+
// $orderInfo['invoices'] = array();
699+
// foreach($order->getInvoiceCollection() as $invoice) {
700+
// $orderInfo['invoices'][] = $invoice->getData();
701+
// }
702+
703+
// NOTE this is refunds
704+
// $orderInfo['creditmemos'] = array();
705+
// foreach($order->getCreditmemosCollection() as $creditmemo) {
706+
// $orderInfo['creditmemos'][] = $creditmemo->getData();
707+
// }
708+
709+
return $orderInfo;
710+
}
711+
712+
public function getOrderNotes($order)
713+
{
714+
$notesInfo = array();
715+
716+
foreach($order->getStatusHistoryCollection() as $note) {
717+
$noteInfo = array(
718+
'id' => $note->getEntityId(),
719+
'type' => 'order_note',
720+
'source' => 'magento',
721+
'status' => $note->getStatus() ?: '',
722+
'created_at' => $note->getCreatedAt(),
723+
'entity_name' => $note->getEntityName()
724+
);
725+
726+
$comment = $note->getComment();
727+
if ($comment) {
728+
$noteInfo['messages'][] = $comment;
729+
}
730+
731+
$notesInfo[] = $noteInfo;
732+
}
733+
734+
return $notesInfo;
735+
}
736+
737+
public function getCustomer($customer)
738+
{
739+
$customerId = $customer->getEntityId();
740+
$urlModel = Mage::getModel('adminhtml/url')->setStore('admin');
741+
$adminUrl = $urlModel->getUrl('adminhtml/zendesk/redirect', array('id' => $customerId, 'type' => 'customer'));
742+
743+
return array(
744+
'id' => $customerId,
745+
'type' => 'customer',
746+
'url' => $adminUrl,
747+
'email' => $customer->getEmail(),
748+
'first_name' => $customer->getFirstname(),
749+
'last_name' => $customer->getLastname(),
750+
'created_at' => $customer->getCreatedAt(),
751+
'updated_at' => $customer->getUpdatedAt(),
752+
);
753+
}
754+
755+
public function getFilteredOrders($customerFilters, $generalFilters)
756+
{
757+
// Get a list of all orders for the given email address
758+
// This is used to determine if a missing customer is a guest or if they really aren't a customer at all
759+
$orderCollection = Mage::getModel('sales/order')->getCollection();
760+
761+
foreach($customerFilters as $customerKey => $customerValue) {
762+
$orderCollection->addFieldToFilter('customer_'.$customerKey, $customerValue);
763+
}
764+
765+
foreach($generalFilters as $generalKey => $generalValue) {
766+
$orderCollection->addFieldToFilter($generalKey, $generalValue);
767+
}
768+
769+
$orders = array();
770+
771+
if($orderCollection->getSize()) {
772+
foreach($orderCollection as $order) {
773+
$orders[] = $this->getOrderDetailBasic($order);
774+
}
775+
}
776+
777+
return $orders;
778+
}
779+
780+
public function getFilteredOrdersByProduct($customerFilters, $productFilters)
781+
{
782+
$emailKey = 'email';
783+
$email = $customerFilters->$emailKey;
784+
785+
$orderItemCollection = Mage::getResourceModel('sales/order_item_collection');
786+
787+
foreach($productFilters as $key => $value) {
788+
$orderItemCollection->addAttributeToFilter($key, $value);
789+
}
790+
$orderItemCollection->load();
791+
792+
$ordersData = array();
793+
794+
foreach($orderItemCollection as $orderItem) {
795+
$orderId = $orderItem->getOrderId();
796+
$ordersData[$orderId] = array(
797+
'email' => $orderItem->getOrder()->getCustomerEmail(),
798+
'order' => $orderItem->getOrder()
799+
);
800+
}
801+
802+
if($email) {
803+
$filteredOrdersData = array_filter(array_values($ordersData), function ($orderData) use ($email) {
804+
return ($orderData['email'] == $email);
805+
});
806+
} else {
807+
$filteredOrdersData = $ordersData;
808+
}
809+
810+
$orders = array();
811+
812+
foreach($filteredOrdersData as $filteredData) {
813+
$orders[] = $this->getOrderDetailBasic($filteredData['order']);
814+
}
815+
816+
return $orders;
817+
}
818+
819+
#endregion version 3.0 or later
445820
}
821+

0 commit comments

Comments
 (0)