Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #12 from pmclain/feature/webhooks
Browse files Browse the repository at this point in the history
Add Webhook Support
  • Loading branch information
pmclain authored Oct 1, 2017
2 parents 67cc340 + fabf4e1 commit d104de4
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 71 deletions.
10 changes: 9 additions & 1 deletion Api/Data/LogInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface LogInterface
{
public function getId();

public function getSid();

public function getEntityId();

public function getEntityTypeId();
Expand All @@ -31,10 +33,14 @@ public function getIsError();

public function getResult();

public function getTimestamp();
public function getCreatedAt();

public function getUpdatedAt();

public function setId($id);

public function setSid($sid);

public function setEntityId($entityId);

public function setEntityTypeId($entityTypeId);
Expand All @@ -44,4 +50,6 @@ public function setRecipientPhone($recipientPhone);
public function setIsError($isError);

public function setResult($result);

public function setUpdatedAt($updatedAt);
}
14 changes: 14 additions & 0 deletions Api/LogRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,26 @@

namespace Pmclain\Twilio\Api;

use Magento\Framework\Exception\NoSuchEntityException;

interface LogRepositoryInterface
{
public function save(\Pmclain\Twilio\Api\Data\LogInterface $log);

/**
* @param string|int $logId
* @throws NoSuchEntityException
* @return Data\LogInterface
*/
public function getById($logId);

/**
* @param string $sid
* @throws NoSuchEntityException
* @return Data\LogInterface
*/
public function getBySid($sid);

public function getList(
\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
);
Expand Down
65 changes: 65 additions & 0 deletions Controller/Webhook/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Pmclain\Twilio\Controller\Webhook;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Pmclain\Twilio\Api\LogRepositoryInterface;
use Magento\Framework\Stdlib\DateTime\DateTimeFactory;
use Magento\Framework\Controller\ResultFactory;

class Index extends Action
{
/**
* @var LogRepositoryInterface
*/
protected $logRepository;

/**
* @var DateTimeFactory
*/
protected $dateTimeFactory;

/**
* Index constructor.
* @param Context $context
* @param LogRepositoryInterface $logRepository
* @param DateTimeFactory $dateTimeFactory
*/
public function __construct(
Context $context,
LogRepositoryInterface $logRepository,
DateTimeFactory $dateTimeFactory
) {
parent::__construct($context);
$this->logRepository = $logRepository;
$this->dateTimeFactory = $dateTimeFactory;
}

/**
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
if (!$this->getRequest()->isPost()) {
$resultRedirect = $this->resultRedirectFactory->create();

return $resultRedirect->setUrl($this->_redirect->getRefererUrl());
}

$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);

try {
$log = $this->logRepository->getBySid($this->getRequest()->getParam('SmsSid'));
$log->setResult($this->getRequest()->getParam('SmsStatus'));
$log->setUpdatedAt($this->dateTimeFactory->create()->timestamp());
$this->logRepository->save($log);

$resultJson->setData(['message' => 'success']);
} catch (\Exception $e) {
$resultJson->setData(['message' => $e->getMessage()]);
}

return $resultJson;
}
}
87 changes: 62 additions & 25 deletions Model/Adapter/AdapterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Psr\Log\LoggerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Pmclain\Twilio\Model\LogFactory;
use Magento\Framework\UrlInterface;

abstract class AdapterAbstract
{
Expand Down Expand Up @@ -58,20 +59,34 @@ abstract class AdapterAbstract
protected $_messageTemplateParser;

/**
* @var \Twilio\Rest\Api\V2010\Account\MessageInstance
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_smsStatus;
protected $_storeManager;

/**
* @var \Magento\Store\Model\StoreManagerInterface;
* @var \Pmclain\Twilio\Model\LogRepository
*/
protected $_storeManager;

protected $_twilioLogRepository;

/**
* @var LogFactory
*/
protected $_twilioLogFactory;

protected $_hasError;
/**
* @var int
*/
protected $entityTypeId = 0;

/**
* @var int
*/
protected $entityId;

/**
* @var UrlInterface
*/
protected $urlBuilder;

/**
* AdapterAbstract constructor.
Expand All @@ -80,6 +95,7 @@ abstract class AdapterAbstract
* @param LoggerInterface $logger
* @param MessageTemplateParser $messageTemplateParser
* @param StoreManagerInterface $storeManager
* @param UrlInterface $url
*/
public function __construct(
Helper $helper,
Expand All @@ -88,7 +104,8 @@ public function __construct(
MessageTemplateParser $messageTemplateParser,
StoreManagerInterface $storeManager,
\Pmclain\Twilio\Model\LogRepository $logRepository,
\Pmclain\Twilio\Model\LogFactory $logFactory
\Pmclain\Twilio\Model\LogFactory $logFactory,
UrlInterface $url
) {
$this->_helper = $helper;
$this->_twilioClientFactory = $twilioClientFactory;
Expand All @@ -97,10 +114,11 @@ public function __construct(
$this->_storeManager = $storeManager;
$this->_twilioLogRepository = $logRepository;
$this->_twilioLogFactory = $logFactory;
$this->urlBuilder = $url;
}

/**
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
* @return $this
*/
protected function _sendSms()
{
Expand All @@ -109,40 +127,59 @@ protected function _sendSms()
'password' => $this->_helper->getAccountAuthToken()
]);

return $client->messages->create(
$this->_recipientPhone,
[
'from' => $this->_helper->getTwilioPhone(),
'body' => $this->_message
]
);
try {
$result = $client->messages->create(
$this->_recipientPhone,
[
'from' => $this->_helper->getTwilioPhone(),
'body' => $this->_message,
'statusCallback' => $this->urlBuilder->getUrl('twilio/webhook'),
]
);

$this->logSuccess($result);
} catch (\Exception $e) {
$this->logError($e);
}

return $this;
}

/**
* @param \Twilio\Rest\Api\V2010\Account\MessageInstance $result
*/
protected function logSuccess($result)
{
$this->_logResult($result->status, $result->sid);
}

/**
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
* @param \Exception $exception
*/
public function getSmsStatus()
protected function logError($exception)
{
return $this->_smsStatus;
$this->_logResult($exception->getMessage(), null, true);
}

/**
* @param int $entityId
* @param int $entityTypeId
* @param string $status
* @param null|string $sid
* @param bool $error
*/
protected function _logResult($entityId, $entityTypeId)
protected function _logResult($status, $sid = null, $error = false)
{
if (!$this->_helper->isLogEnabled()) {
return;
}

$log = $this->_twilioLogFactory->create();

$log->setEntityId($entityId);
$log->setEntityTypeId($entityTypeId);
$log->setEntityId($this->entityId);
$log->setEntityTypeId($this->entityTypeId);
$log->setRecipientPhone($this->_recipientPhone);
$log->setIsError($this->_hasError);
$log->setResult($this->_smsStatus);
$log->setIsError($error);
$log->setResult($status);
$log->setSid($sid);

$this->_twilioLogRepository->save($log);
}
Expand Down
18 changes: 6 additions & 12 deletions Model/Adapter/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

class Order extends AdapterAbstract
{
const ENTITY_TYPE_ID = 1;
/**
* @var int
*/
protected $entityTypeId = 1;

/**
* @param \Magento\Sales\Model\Order $order
Expand All @@ -43,17 +46,8 @@ public function sendOrderSms(SalesOrder $order)
// and add country code
$this->_recipientPhone = '+1' . $order->getBillingAddress()->getTelephone();

try {
$result = $this->_sendSms();
$this->_smsStatus = $result->status;
$this->_hasError = false;
} catch (\Exception $e) {
$this->_logger->addCritical($e->getMessage());
$this->_smsStatus = $e->getMessage();
$this->_hasError = true;
}

$this->_logResult($order->getId(), self::ENTITY_TYPE_ID);
$this->entityId = $order->getId();
$this->_sendSms();

return $this;
}
Expand Down
18 changes: 6 additions & 12 deletions Model/Adapter/Order/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

class Invoice extends AdapterAbstract
{
const ENTITY_TYPE_ID = 2;
/**
* @var int
*/
protected $entityTypeId = 2;

/**
* @param \Magento\Sales\Model\Order\Invoice $invoice
Expand All @@ -45,17 +48,8 @@ public function sendOrderSms(SalesInvoice $invoice)
// and add country code
$this->_recipientPhone = '+1' . $order->getBillingAddress()->getTelephone();

try {
$result = $this->_sendSms();
$this->_smsStatus = $result->status;
$this->_hasError = false;
} catch (\Exception $e) {
$this->_logger->addCritical($e->getMessage());
$this->_smsStatus = $e->getMessage();
$this->_hasError = true;
}

$this->_logResult($order->getId(), self::ENTITY_TYPE_ID);
$this->entityId = $invoice->getId();
$this->_sendSms();

return $this;
}
Expand Down
18 changes: 6 additions & 12 deletions Model/Adapter/Order/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@

class Shipment extends AdapterAbstract
{
const ENTITY_TYPE_ID = 3;
/**
* @var int
*/
protected $entityTypeId = 3;

/** @var CarrierFactory */
protected $carrierFactory;
Expand Down Expand Up @@ -76,17 +79,8 @@ public function sendOrderSms(SalesShipment $shipment)
// and add country code
$this->_recipientPhone = '+1' . $order->getShippingAddress()->getTelephone();

try {
$result = $this->_sendSms();
$this->_smsStatus = $result->status;
$this->_hasError = false;
} catch (\Exception $e) {
$this->_logger->addCritical($e->getMessage());
$this->_smsStatus = $e->getMessage();
$this->_hasError = true;
}

$this->_logResult($order->getId(), self::ENTITY_TYPE_ID);
$this->entityId = $shipment->getId();
$this->_sendSms();

return $this;
}
Expand Down
Loading

0 comments on commit d104de4

Please sign in to comment.