| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup Xero Xero
- * @ingroup UnaModules
- *
- * @{
- */
- define('BX_XERO_ACCOUNT_CODE_SALES', 200);
- define('BX_XERO_ACCOUNT_CODE_PURCHASES', 300);
- define('BX_XERO_ACCOUNT_CODE_ADVERTIXING', 400);
- class BxXeroModule extends BxDolModule
- {
- protected $_oApi;
- protected $_oLog;
- function __construct($aModule)
- {
- parent::__construct($aModule);
- $this->_oConfig->init($this->_oDb);
- bx_import('Api', $aModule);
- $this->_oApi = new BxXeroApi($this);
- $this->_oLog = $this->_oConfig->getObjectLog();
- }
- /**
- * ACTION METHODS
- */
- public function actionAuthorize()
- {
- header('Location: ' . $this->_oApi->authorize());
- exit;
- }
- public function actionCallback()
- {
- $sCode = bx_process_input(bx_get('code'));
- if($sCode === false) {
- header('Location: ' . $this->_oConfig->getAuthorizeUrl(1));
- exit();
- }
- if(($sState = bx_get('state')) === false || $sState != $this->_oConfig->getState()) {
- $this->_oConfig->unsetState();
- header('Location: ' . $this->_oConfig->getAuthorizeUrl(2));
- exit();
- }
- $sUrl = $this->_oApi->callback($sCode);
- if($sUrl === false)
- $sUrl = $this->_oConfig->getAuthorizeUrl(3);
- header('Location: ' . $sUrl);
- exit();
- }
- public function actionWebhook()
- {
- $sResponse = file_get_contents('php://input');
- $sSignatureComputed = base64_encode(hash_hmac('sha256', $sResponse, $this->_oConfig->getWebhookKey(), true));
- $sSignatureReceived = isset($_SERVER['HTTP_X_XERO_SIGNATURE']) ? $_SERVER['HTTP_X_XERO_SIGNATURE'] : '';
- if(!hash_equals($sSignatureComputed, $sSignatureReceived)) {
- @$this->_oLog->write('Get Webhook: Wrong Signature', $sSignatureComputed, $sSignatureReceived);
- http_response_code(401);
- return;
- }
- http_response_code(200);
- $aResponce = json_decode($sResponse, true);
- if(empty($aResponce) || !is_array($aResponce))
- return;
- foreach($aResponce['events'] as $aEvent) {
- $sMethod = '_process' . bx_gen_method_name(strtolower($aEvent['eventType']) . '_' . strtolower($aEvent['eventCategory']));
- if(!method_exists($this, $sMethod))
- continue;
- $this->$sMethod($aEvent['resourceId']);
- }
- }
- public function actionTest()
- {
- $mixedResult = $this->_oApi->actionTest();
- if($mixedResult !== false)
- $mixedResult = _t('_bx_xero_txt_organization', $mixedResult);
- else
- $mixedResult = _t('_bx_xero_txt_err_unknown');
- echo $mixedResult;
- }
- /**
- * SERVICE METHODS
- */
- public function serviceIncludeCssJs()
- {
- return $this->_oTemplate->getIncludeCssJs();
- }
- public function serviceIsContact($iProfileId, $bForceCheckApi = false)
- {
- $oProfile = BxDolProfile::getInstance($iProfileId);
- if(!$oProfile)
- return false;
- $sProfileEmail = $oProfile->getAccountObject()->getEmail();
- return $this->isContact($sProfileEmail, $bForceCheckApi);
- }
- public function serviceAddContact($iProfileId)
- {
- $oProfile = BxDolProfile::getInstance($iProfileId);
- if(!$oProfile)
- return false;
- $sProfileName = $oProfile->getDisplayName();
- $sProfileEmail = $oProfile->getAccountObject()->getEmail();
- return $this->_oApi->actionAddContact($iProfileId, $sProfileName, $sProfileEmail);
- }
- public function serviceAddInvoce($iProfileId, $sName, $fAmount, $iQuantity = 1, $sDueDate = false)
- {
- $oProfile = BxDolProfile::getInstance($iProfileId);
- if(!$oProfile)
- return false;
- $sProfileEmail = $oProfile->getAccountObject()->getEmail();
- return $this->_oApi->actionAddInvoice($sProfileEmail, $sName, $fAmount, $iQuantity, $sDueDate);
- }
- public function serviceAddInvoceAuto($iProfileId, $sName, $fAmount, $iQuantity = 1, $mixedDueDate = false)
- {
- $oProfile = BxDolProfile::getInstance($iProfileId);
- if(!$oProfile)
- return false;
- $sProfileEmail = $oProfile->getAccountObject()->getEmail();
- if(!$this->isContact($sProfileEmail)) {
- $sProfileName = $oProfile->getDisplayName();
- if($this->_oApi->actionAddContact($iProfileId, $sProfileName, $sProfileEmail) === false)
- return false;
- }
- return $this->_oApi->actionAddInvoice($sProfileEmail, $sName, $fAmount, $iQuantity, $mixedDueDate);
- }
- /*
- * COMMON METHODS
- */
- public function getApi()
- {
- return $this->_oApi;
- }
- public function isContact($sProfileEmail, $bForceCheckApi = false)
- {
- if(!$bForceCheckApi && $this->_oDb->isContact($sProfileEmail))
- return true;
- return $this->_oApi->actionIsContact($sProfileEmail);
- }
- /*
- * INTERNAL METHODS
- */
- protected function _processCreateInvoice($sId)
- {
- $oInvoice = $this->_oApi->actionGetInvoice($sId);
- $aInvoice = $this->_getInvoiceData($oInvoice);
- $this->_oLog->write('Create Invoice: ' . $sId, $aInvoice);
- bx_alert($this->_oConfig->getName(), 'invoice_created', 0, 0, array(
- 'id' => $sId,
- 'invoice' => $aInvoice,
- ));
- }
- protected function _processUpdateInvoice($sId)
- {
- $oInvoice = $this->_oApi->actionGetInvoice($sId);
- $aInvoice = $this->_getInvoiceData($oInvoice);
- $this->_oLog->write('Update Invoice: ' . $sId, $aInvoice);
- bx_alert($this->_oConfig->getName(), 'invoice_updated', 0, 0, array(
- 'id' => $sId,
- 'invoice' => $aInvoice,
- ));
- }
- protected function _getInvoiceData($oInvoice)
- {
- return [
- 'id' => $oInvoice->getInvoiceId(),
- 'number' => $oInvoice->getInvoiceNumber(),
- 'reference' => $oInvoice->getReference(),
- 'currency_code' => $oInvoice->getCurrencyCode(),
- 'sub_total' => $oInvoice->getSubTotal(),
- 'total_tax' => $oInvoice->getTotalTax(),
- 'total' => $oInvoice->getTotal(),
- 'amount_due' => $oInvoice->getAmountDue(),
- 'amount_paid' => $oInvoice->getAmountPaid(),
- 'status' => $oInvoice->getStatus(),
- ];
- }
- }
- /** @} */
|