Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
added magento2 aftership module
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmonsters committed Dec 5, 2017
1 parent 9743b58 commit a6e5070
Show file tree
Hide file tree
Showing 18 changed files with 1,116 additions and 0 deletions.
118 changes: 118 additions & 0 deletions Api/Data/TrackInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Created by PhpStorm.
* User: alvin
* Date: 04/12/2017
* Time: 5:44 PM
*/

namespace Mrmonsters\Aftership\Api\Data;

interface TrackInterface {

const TRACK_ID = 'track_id';
const TRACKING_NUMBER = 'tracking_number';
const SHIP_COMP_CODE = 'ship_comp_code';
const EMAIL = 'email';
const TELEPHONE = 'telephone';
const TITLE = 'title';
const POSTED = 'posted';
const ORDER_ID = 'order_id';

/**
* @return int|null
*/
public function getTrackId();

/**
* @return string|null
*/
public function getTrackingNumber();

/**
* @return string|null
*/
public function getShipCompCode();

/**
* @return string|null
*/
public function getEmail();

/**
* @return string|null
*/
public function getTelephone();

/**
* @return string|null
*/
public function getTitle();

/**
* @return int|null
*/
public function getPosted();

/**
* @return string|null
*/
public function getOrderId();

/**
* @param string $trackId
*
* @return TrackInterface
*/
public function setTrackId($trackId);

/**
* @param string $trackNo
*
* @return TrackInterface
*/
public function setTrackingNumber($trackNo);

/**
* @param string $code
*
* @return TrackInterface
*/
public function setShipCompCode($code);

/**
* @param string $email
*
* @return TrackInterface
*/
public function setEmail($email);

/**
* @param string $telephone
*
* @return TrackInterface
*/
public function setTelephone($telephone);

/**
* @param string $title
*
* @return TrackInterface
*/
public function setTitle($title);

/**
* @param int $posted
*
* @return TrackInterface
*/
public function setPosted($posted);

/**
* @param string $orderId
*
* @return TrackInterface
*/
public function setOrderId($orderId);

}
109 changes: 109 additions & 0 deletions Helper/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Created by PhpStorm.
* User: alvin
* Date: 05/12/2017
* Time: 9:38 AM
*/

namespace Mrmonsters\Aftership\Helper;

use Magento\Config\Model\Config\Factory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

class Config extends AbstractHelper {

const XML_PATH_MESSAGES_API_KEY = 'aftership_options/messages/api_key';
const XML_PATH_MESSAGES_STATUS = 'aftership_options/messages/status';
const XML_PATH_MESSAGES_CRON_JOB_ENABLE = 'aftership_options/messages/cron_job_enable';
const XML_PATH_MESSAGES_LAST_UPDATE = 'aftership_options/messages/last_update';

protected $_configFactory;
protected $_storeManager;

/**
* @inheritDoc
*/
public function __construct(Factory $factory, StoreManagerInterface $storeManager, Context $context)
{
$this->_configFactory = $factory;
$this->_storeManager = $storeManager;

parent::__construct($context);
}

public function getExtensionApiKey($websiteId = null)
{
return $this->scopeConfig->getValue(self::XML_PATH_MESSAGES_API_KEY, ScopeInterface::SCOPE_WEBSITES, $websiteId);
}

public function getExtensionEnabled($websiteId = null)
{
return $this->scopeConfig->getValue(self::XML_PATH_MESSAGES_STATUS, ScopeInterface::SCOPE_WEBSITES, $websiteId);
}

public function getExtensionCronJobEnabled($websiteId = null)
{
return $this->scopeConfig->getValue(self::XML_PATH_MESSAGES_CRON_JOB_ENABLE, ScopeInterface::SCOPE_WEBSITES, $websiteId);
}

public function getExtensionLastUpdate($websiteId = null)
{
$scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;

if ($websiteId) {

$scopeType = ScopeInterface::SCOPE_WEBSITES;
}

return $this->scopeConfig->getValue(self::XML_PATH_MESSAGES_LAST_UPDATE, $scopeType, $websiteId);
}

public function setExtensionCronLastUpdate($time)
{
$defaultConfig = $this->_configFactory->create([
'data' => [
'section' => 'aftership_options',
'website' => null,
'store' => null,
'groups' => [
'messages' => [
'fields' => [
'last_update' => [
'value' => $time,
],
],
],
],
],
]);

$defaultConfig->save();

foreach ($this->_storeManager->getWebsites() as $website) {

$config = $this->_configFactory->create([
'data' => [
'section' => 'aftership_options',
'website' => $website->getId(),
'store' => null,
'groups' => [
'messages' => [
'fields' => [
'last_update' => [
'value' => $time,
],
],
],
],
],
]);

$config->save();
}
}
}
150 changes: 150 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
/**
* Created by PhpStorm.
* User: alvin
* Date: 04/12/2017
* Time: 5:01 PM
*/

namespace Mrmonsters\Aftership\Helper;

use AfterShip\AfterShipException;
use AfterShip\Trackings;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\OrderFactory;
use Magento\Shipping\Model\Order\Track;
use Mrmonsters\Aftership\Model\TrackFactory;

class Data extends AbstractHelper {

const ENDPOINT_AUTHENTICATE = 'https://api.aftership.com/v4/couriers';
const POSTED_NOT_YET = 0;
const POSTED_DONE = 1;
const POSTED_DISABLED = 2;

protected $_storeManager;
protected $_trackFactory;
protected $_orderFactory;
protected $_configHelper;

/**
* @inheritDoc
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
TrackFactory $trackFactory,
OrderFactory $orderFactory,
Config $configHelper,
Context $context
)
{
$this->_storeManager = $storeManager;
$this->_trackFactory = $trackFactory;
$this->_orderFactory = $orderFactory;
$this->_configHelper = $configHelper;

parent::__construct($context);
}

public function callApiAuthenticate($apiKey)
{
$headers = [
'aftership-api-key: ' . $apiKey,
'Content-Type: application/json',
];
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, self::ENDPOINT_AUTHENTICATE);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

//handle SSL certificate problem: unable to get local issuer certificate issue
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //the SSL is not correct
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //the SSL is not correct
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

return $httpStatus;
}

public function saveTrack(Track $magentoTrack)
{
/* @var Order $order */
$order = $magentoTrack->getShipment()->getOrder();
/* @var Order\Address $address */
$address = $order->getShippingAddress();
/* @var \Mrmonsters\Aftership\Model\Track $track */
$track = $this->_trackFactory->create();

$track->setTrackingNumber($magentoTrack->getTrackNumber());
$track->setShipCompCode($magentoTrack->getCarrierCode());
$track->setTitle($order->getIncrementId());
$track->setOrderId($order->getIncrementId());

if ($order->getCustomerEmail()) {

$track->setEmail($order->getCustomerEmail());
}

if ($address->getTelephone()) {

$track->setTelephone($address->getTelephone());
}

$enabled = $this->_configHelper->getExtensionEnabled($order->getStore()->getWebsiteId());

if ($enabled) {

$track->setPosted(self::POSTED_NOT_YET);
} else {

$track->setPosted(self::POSTED_DISABLED);
}

$track->save();

return $track;
}

public function sendTrack(\Mrmonsters\Aftership\Model\Track $track)
{
if ($track->getPosted() != self::POSTED_NOT_YET) {

return false;
}

/* @var Order $order */
$order = $this->_orderFactory->create()->loadByIncrementId($track->getOrderId());
$shippingAddress = $order->getShippingAddress();
$apiKey = $this->_configHelper->getExtensionApiKey($order->getStore()->getWebsiteId());
$tracking = new Trackings($apiKey);

try {

$response = $tracking->create($track->getTrackingNumber(), [
'destination_country_iso3' => $shippingAddress->getCountryId(),
'smses' => $shippingAddress->getTelephone(),
'emails' => $order->getCustomerEmail(),
'title' => $track->getOrderId(),
'order_id' => $track->getOrderId(),
'customer_name' => $shippingAddress->getFirstname() . ' ' . $shippingAddress->getLastname(),
]);

$track->setPosted(self::POSTED_DONE)->save();
} catch (AfterShipException $e) {


}
}

}
Loading

0 comments on commit a6e5070

Please sign in to comment.