Added extra error handling and debugging

This commit is contained in:
Rich Morgan 2014-12-17 18:11:47 -05:00
parent 89a9807736
commit 701fc2e833
1 changed files with 186 additions and 51 deletions

View File

@ -21,7 +21,9 @@ class Bitpay_Core_Helper_Data extends Mage_Core_Helper_Abstract
*/ */
public function debugData($debugData) public function debugData($debugData)
{ {
Mage::getModel('bitpay/method_bitcoin')->debugData($debugData); if (true === isset($debugData) && false === empty($debugData)) {
Mage::getModel('bitpay/method_bitcoin')->debugData($debugData);
}
} }
/** /**
@ -70,11 +72,19 @@ class Bitpay_Core_Helper_Data extends Mage_Core_Helper_Abstract
*/ */
public function registerAutoloader() public function registerAutoloader()
{ {
if (null === $this->_autoloaderRegistered) { if (true === empty($this->_autoloaderRegistered)) {
require_once Mage::getBaseDir('lib').'/Bitpay/Autoloader.php'; $autoloader_filename = Mage::getBaseDir('lib').'/Bitpay/Autoloader.php';
\Bitpay\Autoloader::register();
$this->_autoloaderRegistered = true; if (true === is_file($autoloader_filename) && true === is_readable($autoloader_filename)) {
$this->debugData('BitPay Autoloader has been registered'); require_once $autoloader_filename;
\Bitpay\Autoloader::register();
$this->_autoloaderRegistered = true;
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::registerAutoloader(): autoloader file was found and has been registered.');
} else {
$this->_autoloaderRegistered = false;
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::registerAutoloader(): autoloader file was not found or is not readable. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::registerAutoloader(): autoloader file was not found or is not readable. Cannot continue!');
}
} }
} }
@ -84,21 +94,36 @@ class Bitpay_Core_Helper_Data extends Mage_Core_Helper_Abstract
*/ */
public function generateAndSaveKeys() public function generateAndSaveKeys()
{ {
$this->debugData('Generating Keys'); $this->debugData('[INFO] In Bitpay_Core_Helper_Data::generateAndSaveKeys(): attempting to generate new keypair and save to database.');
$this->registerAutoloader();
if (true === empty($this->_autoloaderRegistered)) {
$this->registerAutoloader();
}
$this->_privateKey = new Bitpay\PrivateKey('payment/bitpay/private_key'); $this->_privateKey = new Bitpay\PrivateKey('payment/bitpay/private_key');
$this->_privateKey->generate();
if (false === isset($this->_privateKey) || true === empty($this->_privateKey)) {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::generateAndSaveKeys(): could not create new Bitpay private key object. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::generateAndSaveKeys(): could not create new Bitpay private key object. Cannot continue!');
} else {
$this->_privateKey->generate();
}
$this->_publicKey = new Bitpay\PublicKey('payment/bitpay/public_key'); $this->_publicKey = new Bitpay\PublicKey('payment/bitpay/public_key');
$this->_publicKey
->setPrivateKey($this->_privateKey) if (false === isset($this->_publicKey) || true === empty($this->_publicKey)) {
->generate(); $this->debugData('[ERROR] In Bitpay_Core_Helper_Data::generateAndSaveKeys(): could not create new Bitpay public key object. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::generateAndSaveKeys(): could not create new Bitpay public key object. Cannot continue!');
} else {
$this->_publicKey
->setPrivateKey($this->_privateKey)
->generate();
}
$this->getKeyManager()->persist($this->_publicKey); $this->getKeyManager()->persist($this->_publicKey);
$this->getKeyManager()->persist($this->_privateKey); $this->getKeyManager()->persist($this->_privateKey);
$this->debugData('Keys persisted to database'); $this->debugData('[INFO] In Bitpay_Core_Helper_Data::generateAndSaveKeys(): key manager called to persist keypair to database.');
} }
/** /**
@ -106,36 +131,62 @@ class Bitpay_Core_Helper_Data extends Mage_Core_Helper_Abstract
*/ */
public function sendPairingRequest($pairingCode) public function sendPairingRequest($pairingCode)
{ {
$this->debugData( if (false === isset($pairingCode) || true === empty($pairingCode)) {
sprintf('Sending Paring Request with pairing code "%s"', $pairingCode) $this->debugData('[ERROR] In Bitpay_Core_Helper_Data::sendPairingRequest(): missing or invalid pairingCode parameter.');
); throw new Exception('In Bitpay_Core_Helper_Data::sendPairingRequest(): missing or invalid pairingCode parameter.');
} else {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::sendPairingRequest(): function called with the pairingCode parameter: ' . $pairingCode);
}
if (true === empty($this->_autoloaderRegistered)) {
$this->registerAutoloader();
}
// Generate/Regenerate keys // Generate/Regenerate keys
$this->generateAndSaveKeys(); $this->generateAndSaveKeys();
$sin = $this->getSinKey(); $sin = $this->getSinKey();
$this->debugData( if (false === isset($sin) || true === empty($sin)) {
sprintf('Sending Pairing Request for SIN "%s"', (string) $sin) $this->debugData('[ERROR] In Bitpay_Core_Helper_Data::sendPairingRequest(): could not retrieve the SIN parameter. Cannot continue!');
); throw new Exception('In Bitpay_Core_Helper_Data::sendPairingRequest(): could not retrieve the SIN parameter. Cannot continue!');
} else {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::sendPairingRequest(): attempting to pair with the SIN parameter: ' . $sin);
}
// Sanitize label // Sanitize label
$label = preg_replace('/[^a-zA-Z0-9 \-\_\.]/', '', Mage::app()->getStore()->getName()); $label = preg_replace('/[^a-zA-Z0-9 ]/', '', Mage::app()->getStore()->getName());
$label = substr('Magento - '.$label, 0, 59); $label = substr('Magento ' . $label, 0, 59);
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::sendPairingRequest(): using the label "' . $label . '".');
$token = $this->getBitpayClient()->createToken( $token = $this->getBitpayClient()->createToken(
array( array(
'id' => (string) $sin, 'id' => (string) $sin,
'pairingCode' => $pairingCode, 'pairingCode' => (string) $pairingCode,
'label' => $label, 'label' => (string) $label,
) )
); );
$this->debugData('Token Obtained'); if (false === isset($token) || true === empty($token)) {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::sendPairingRequest(): could not obtain the token from the pairing process. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::sendPairingRequest(): could not obtain the token from the pairing process. Cannot continue!');
} else {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::sendPairingRequest(): token successfully obtained.');
}
$config = new \Mage_Core_Model_Config(); $config = new \Mage_Core_Model_Config();
$config->saveConfig('payment/bitpay/token', $token->getToken());
$this->debugData('Token Persisted persisted to database'); if (false === isset($config) || true === empty($config)) {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::sendPairingRequest(): could not create new Mage_Core_Model_Config object. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::sendPairingRequest(): could not create new Mage_Core_Model_Config object. Cannot continue!');
}
if($config->saveConfig('payment/bitpay/token', $token->getToken())) {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::sendPairingRequest(): token saved to database.');
} else {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::sendPairingRequest(): token could not be saved to database.');
throw new Exception('In Bitpay_Core_Helper_Data::sendPairingRequest(): token could not be saved to database.');
}
} }
/** /**
@ -143,53 +194,97 @@ class Bitpay_Core_Helper_Data extends Mage_Core_Helper_Abstract
*/ */
public function getSinKey() public function getSinKey()
{ {
if (null !== $this->_sin) { if (false === empty($this->_sin)) {
return $this->_sin; return $this->_sin;
} }
$this->debugData('Getting SIN Key'); $this->debugData('[INFO] In Bitpay_Core_Helper_Data::getSinKey(): attempting to get the SIN parameter.');
if (true === empty($this->_autoloaderRegistered)) {
$this->registerAutoloader();
}
$this->registerAutoloader();
$this->_sin = new Bitpay\SinKey(); $this->_sin = new Bitpay\SinKey();
if (false === isset($this->_sin) || true === empty($this->_sin)) {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::getSinKey(): could not create new BitPay SinKey object. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::getSinKey(): could not create new BitPay SinKey object. Cannot continue!');
}
$this->_sin $this->_sin
->setPublicKey($this->getPublicKey()) ->setPublicKey($this->getPublicKey())
->generate(); ->generate();
if (false === isset($this->_sin) || true === empty($this->_sin)) {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::getSinKey(): could not generate a new SIN from the public key. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::getSinKey(): could not generate a new SIN from the public key. Cannot continue!');
}
return $this->_sin; return $this->_sin;
} }
public function getPublicKey() public function getPublicKey()
{ {
if (null !== $this->_publicKey) { if (true === isset($this->_publicKey) && false === empty($this->_publicKey)) {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getPublicKey(): found an existing public key, returning that.');
return $this->_publicKey; return $this->_publicKey;
} }
$this->debugData('Getting Public Key'); if (true === empty($this->_autoloaderRegistered)) {
$this->registerAutoloader();
}
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getPublicKey(): did not find an existing public key, attempting to load one from the key manager.');
$this->_publicKey = $this->getKeyManager()->load('payment/bitpay/public_key'); $this->_publicKey = $this->getKeyManager()->load('payment/bitpay/public_key');
if (!$this->_publicKey) { if (true === empty($this->_publicKey)) {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getPublicKey(): could not load a public key from the key manager, generating a new one.');
$this->generateAndSaveKeys(); $this->generateAndSaveKeys();
} else {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getPublicKey(): successfully loaded public key from the key manager, returning that.');
return $this->_publicKey;
} }
return $this->_publicKey; if (false === empty($this->_publicKey)) {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getPublicKey(): successfully generated a new public key.');
return $this->_publicKey;
} else {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::getPublicKey(): could not load or generate a new public key. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::getPublicKey(): could not load or generate a new public key. Cannot continue!');
}
} }
public function getPrivateKey() public function getPrivateKey()
{ {
if (null !== $this->_privateKey) { if (false === empty($this->_privateKey)) {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getPrivateKey(): found an existing private key, returning that.');
return $this->_privateKey; return $this->_privateKey;
} }
$this->debugData('Getting Private Key'); if (true === empty($this->_autoloaderRegistered)) {
$this->registerAutoloader();
}
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getPrivateKey(): did not find an existing private key, attempting to load one from the key manager.');
$this->_privateKey = $this->getKeyManager()->load('payment/bitpay/private_key'); $this->_privateKey = $this->getKeyManager()->load('payment/bitpay/private_key');
if (!$this->_publicKey) { if (true === empty($this->_privateKey)) {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getPrivateKey(): could not load a private key from the key manager, generating a new one.');
$this->generateAndSaveKeys(); $this->generateAndSaveKeys();
} else {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getPrivateKey(): successfully loaded private key from the key manager, returning that.');
return $this->_privateKey;
} }
return $this->_privateKey; if (false === empty($this->_privateKey)) {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getPrivateKey(): successfully generated a new private key.');
return $this->_privateKey;
} else {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::getPrivateKey(): could not load or generate a new private key. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::getPrivateKey(): could not load or generate a new private key. Cannot continue!');
}
} }
/** /**
@ -197,10 +292,19 @@ class Bitpay_Core_Helper_Data extends Mage_Core_Helper_Abstract
*/ */
public function getKeyManager() public function getKeyManager()
{ {
if (null == $this->_keyManager) { if (true === empty($this->_keyManager)) {
$this->registerAutoloader(); if (true === empty($this->_autoloaderRegistered)) {
$this->debugData('Creating instance of KeyManager'); $this->registerAutoloader();
}
$this->_keyManager = new Bitpay\KeyManager(new Bitpay\Storage\MagentoStorage()); $this->_keyManager = new Bitpay\KeyManager(new Bitpay\Storage\MagentoStorage());
if (false === isset($this->_keyManager) || true === empty($this->_keyManager)) {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::getKeyManager(): could not create new BitPay KeyManager object. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::getKeyManager(): could not create new BitPay KeyManager object. Cannot continue!');
} else {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getKeyManager(): successfully created new BitPay KeyManager object.');
}
} }
return $this->_keyManager; return $this->_keyManager;
@ -214,9 +318,19 @@ class Bitpay_Core_Helper_Data extends Mage_Core_Helper_Abstract
*/ */
public function getBitpay() public function getBitpay()
{ {
if (null === $this->_bitpay) { if (true === empty($this->_bitpay)) {
$this->registerAutoloader(); if (true === empty($this->_autoloaderRegistered)) {
$this->registerAutoloader();
}
$this->_bitpay = new Bitpay\Bitpay(array('bitpay' => $this->getBitpayConfig())); $this->_bitpay = new Bitpay\Bitpay(array('bitpay' => $this->getBitpayConfig()));
if (false === isset($this->_bitpay) || true === empty($this->_bitpay)) {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::getBitpay(): could not create new BitPay object. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::getBitpay(): could not create new BitPay object. Cannot continue!');
} else {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getBitpay(): successfully created new BitPay object.');
}
} }
return $this->_bitpay; return $this->_bitpay;
@ -242,13 +356,23 @@ class Bitpay_Core_Helper_Data extends Mage_Core_Helper_Abstract
*/ */
public function getBitpayClient() public function getBitpayClient()
{ {
if (null !== $this->_client) { if (false === empty($this->_client)) {
return $this->_client; return $this->_client;
} }
$this->registerAutoloader(); if (true === empty($this->_autoloaderRegistered)) {
$this->registerAutoloader();
}
$this->_client = new Bitpay\Client\Client(); $this->_client = new Bitpay\Client\Client();
if (false === isset($this->_client) || true === empty($this->_client)) {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::getBitpayClient(): could not create new BitPay Client object. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::getBitpayClient(): could not create new BitPay Client object. Cannot continue!');
} else {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getBitpayClient(): successfully created new BitPay Client object.');
}
$this->_client->setPublicKey($this->getPublicKey()); $this->_client->setPublicKey($this->getPublicKey());
$this->_client->setPrivateKey($this->getPrivateKey()); $this->_client->setPrivateKey($this->getPrivateKey());
$this->_client->setNetwork($this->getBitpay()->get('network')); $this->_client->setNetwork($this->getBitpay()->get('network'));
@ -260,8 +384,19 @@ class Bitpay_Core_Helper_Data extends Mage_Core_Helper_Abstract
public function getToken() public function getToken()
{ {
$this->registerAutoloader(); if (true === empty($this->_autoloaderRegistered)) {
$this->registerAutoloader();
}
$token = new Bitpay\Token(); $token = new Bitpay\Token();
if (false === isset($token) || true === empty($token)) {
$this->debugData('[ERROR] In Bitpay_Core_Helper_Data::getToken(): could not create new BitPay Token object. Cannot continue!');
throw new Exception('In Bitpay_Core_Helper_Data::getToken(): could not create new BitPay Token object. Cannot continue!');
} else {
$this->debugData('[INFO] In Bitpay_Core_Helper_Data::getToken(): successfully created new BitPay Token object.');
}
$token->setToken(Mage::getStoreConfig('payment/bitpay/token')); $token->setToken(Mage::getStoreConfig('payment/bitpay/token'));
return $token; return $token;