Added extra error handling and debugging

This commit is contained in:
Rich Morgan 2014-12-17 17:02:59 -05:00
parent 409eb02c94
commit c29c10c599
1 changed files with 117 additions and 26 deletions

View File

@ -12,6 +12,7 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
protected $_code = 'bitpay'; protected $_code = 'bitpay';
protected $_formBlockType = 'bitpay/form_bitpay'; protected $_formBlockType = 'bitpay/form_bitpay';
protected $_infoBlockType = 'bitpay/info'; protected $_infoBlockType = 'bitpay/info';
protected $_isGateway = true; protected $_isGateway = true;
protected $_canAuthorize = true; protected $_canAuthorize = true;
protected $_canCapture = false; protected $_canCapture = false;
@ -19,12 +20,14 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
protected $_isInitializeNeeded = false; protected $_isInitializeNeeded = false;
protected $_canFetchTransactionInfo = false; protected $_canFetchTransactionInfo = false;
protected $_canManagerRecurringProfiles = false; protected $_canManagerRecurringProfiles = false;
//protected $_canUseCheckout = true; protected $_canUseCheckout = true;
//protected $_canUseForMultishipping = true; protected $_canUseForMultishipping = true;
//protected $_canCapturePartial = false; protected $_canCapturePartial = false;
//protected $_canRefund = false; protected $_canRefund = false;
//protected $_canVoid = false; protected $_canVoid = false;
protected $_debugReplacePrivateDataKeys = array(); protected $_debugReplacePrivateDataKeys = array();
protected static $_redirectUrl; protected static $_redirectUrl;
/** /**
@ -34,29 +37,41 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
*/ */
public function authorize(Varien_Object $payment, $amount) public function authorize(Varien_Object $payment, $amount)
{ {
$this->debugData('authorizing new order'); if (false === isset($payment) || false === isset($amount) || true === empty($payment) || true === empty($amount)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::authorize(): missing payment or amount parameters.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::authorize(): missing payment or amount parameters.');
}
$this->debugData('[INFO] Bitpay_Core_Model_Method_Bitcoin::authorize(): authorizing new order.');
// Create BitPay Invoice // Create BitPay Invoice
$invoice = $this->initializeInvoice(); $invoice = $this->initializeInvoice();
if (false === isset($invoice) || true === empty($invoice)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::authorize(): could not initialize invoice.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::authorize(): could not initialize invoice.');
}
$invoice = $this->prepareInvoice($invoice, $payment, $amount); $invoice = $this->prepareInvoice($invoice, $payment, $amount);
try { try {
$bitpayInvoice = Mage::helper('bitpay')->getBitpayClient()->createInvoice($invoice); $bitpayInvoice = Mage::helper('bitpay')->getBitpayClient()->createInvoice($invoice);
} catch (Exception $e) { } catch (Exception $e) {
$this->debugData($e->getMessage()); $this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::authorize(): ' . $e->getMessage());
$this->debugData( $this->debugData(
array( array(
Mage::helper('bitpay')->getBitpayClient()->getRequest()->getBody(), Mage::helper('bitpay')->getBitpayClient()->getRequest()->getBody(),
Mage::helper('bitpay')->getBitpayClient()->getResponse()->getBody(), Mage::helper('bitpay')->getBitpayClient()->getResponse()->getBody(),
) )
); );
Mage::throwException('Could not authorize transaction.'); Mage::throwException('In Bitpay_Core_Model_Method_Bitcoin::authorize(): Could not authorize transaction.');
} }
self::$_redirectUrl = $bitpayInvoice->getUrl(); self::$_redirectUrl = $bitpayInvoice->getUrl();
$this->debugData( $this->debugData(
array( array(
'BitPay Invoice created', '[INFO] BitPay Invoice created',
sprintf('Invoice URL: "%s"', $bitpayInvoice->getUrl()), sprintf('Invoice URL: "%s"', $bitpayInvoice->getUrl()),
) )
); );
@ -67,7 +82,7 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
->prepateWithOrder($payment->getOrder()) ->prepateWithOrder($payment->getOrder())
->save(); ->save();
$this->debugData($bitpayInvoice->getId()); $this->debugData('[INFO] Leaving Bitpay_Core_Model_Method_Bitcoin::authorize(): invoice id ' . $bitpayInvoice->getId());
return $this; return $this;
} }
@ -83,16 +98,18 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
{ {
$token = Mage::getStoreConfig('payment/bitpay/token'); $token = Mage::getStoreConfig('payment/bitpay/token');
if (empty($token)) { if (false === isset($token) || true === empty($token)) {
/** /**
* Merchant must goto their account and create a pairing code to * Merchant must goto their account and create a pairing code to
* enter in. * enter in.
*/ */
$this->debugData('Magento store does not have a BitPay token.'); $this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::canUseCheckout(): There was an error retrieving the token store param from the database or this Magento store does not have a BitPay token.');
return false; return false;
} }
$this->debugData('[INFO] Leaving Bitpay_Core_Model_Method_Bitcoin::canUseCheckout(): token obtained from storage successfully.');
return true; return true;
} }
@ -104,11 +121,33 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
*/ */
public function fetchInvoice($id) public function fetchInvoice($id)
{ {
if (false === isset($id) || true === empty($id)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::fetchInvoice(): missing or invalid id parameter.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::fetchInvoice(): missing or invalid id parameter.');
} else {
$this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::fetchInvoice(): function called with id ' . $id);
}
Mage::helper('bitpay')->registerAutoloader(); Mage::helper('bitpay')->registerAutoloader();
$client = Mage::helper('bitpay')->getBitpayClient(); $client = Mage::helper('bitpay')->getBitpayClient();
if (false === isset($client) || true === empty($client)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::fetchInvoice(): could not obtain BitPay client.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::fetchInvoice(): could not obtain BitPay client.');
} else {
$this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::fetchInvoice(): obtained BitPay client successfully.');
}
$invoice = $client->getInvoice($id); $invoice = $client->getInvoice($id);
if (false === isset($invoice) || true === empty($invoice)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::fetchInvoice(): could not retrieve invoice from BitPay.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::fetchInvoice(): could not retrieve invoice from BitPay.');
} else {
$this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::fetchInvoice(): successfully retrieved invoice id ' . $id . ' from BitPay.');
}
return $invoice; return $invoice;
} }
@ -121,9 +160,12 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
*/ */
public function extractAddress($address) public function extractAddress($address)
{ {
$this->debugData( if (false === isset($address) || true === empty($address)) {
sprintf('Extracting addess') $this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::extractAddress(): missing or invalid address parameter.');
); throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::extractAddress(): missing or invalid address parameter.');
} else {
$this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::extractAddress(): called with good address parameter, extracting now.');
}
$options = array(); $options = array();
$options['buyerName'] = $address->getName(); $options['buyerName'] = $address->getName();
@ -145,7 +187,10 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
// trim to fit API specs // trim to fit API specs
foreach (array('buyerName', 'buyerAddress1', 'buyerAddress2', 'buyerAddress3', 'buyerAddress4', 'buyerCity', 'buyerState', 'buyerZip', 'buyerCountry', 'buyerEmail', 'buyerPhone') as $f) { foreach (array('buyerName', 'buyerAddress1', 'buyerAddress2', 'buyerAddress3', 'buyerAddress4', 'buyerCity', 'buyerState', 'buyerZip', 'buyerCountry', 'buyerEmail', 'buyerPhone') as $f) {
$options[$f] = substr($options[$f], 0, 100); if (true === isset($options[$f]) && strlen($options[$f]) > 100) {
$this->debugData('[WARNING] In Bitpay_Core_Model_Method_Bitcoin::extractAddress(): the ' . $f . ' parameter was greater than 100 characters, trimming.');
$options[$f] = substr($options[$f], 0, 100);
}
} }
return $options; return $options;
@ -158,9 +203,7 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
*/ */
public function getOrderPlaceRedirectUrl() public function getOrderPlaceRedirectUrl()
{ {
$this->debugData( $this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::getOrderPlaceRedirectUrl(): $_redirectUrl is ' . self::$_redirectUrl);
'Customer wants to place the order. Create invoice and redirect user to invoice'
);
return self::$_redirectUrl; return self::$_redirectUrl;
} }
@ -176,6 +219,14 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
Mage::helper('bitpay')->registerAutoloader(); Mage::helper('bitpay')->registerAutoloader();
$invoice = new Bitpay\Invoice(); $invoice = new Bitpay\Invoice();
if (false === isset($invoice) || true === empty($invoice)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::initializeInvoice(): could not construct new BitPay invoice object.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::initializeInvoice(): could not construct new BitPay invoice object.');
} else {
$this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::initializeInvoice(): constructed new BitPay invoice object successfully.');
}
$invoice->setFullNotifications(true); $invoice->setFullNotifications(true);
$invoice->setTransactionSpeed(Mage::getStoreConfig('payment/bitpay/speed')); $invoice->setTransactionSpeed(Mage::getStoreConfig('payment/bitpay/speed'));
$invoice->setNotificationUrl(Mage::getUrl(Mage::getStoreConfig('payment/bitpay/notification_url'))); $invoice->setNotificationUrl(Mage::getUrl(Mage::getStoreConfig('payment/bitpay/notification_url')));
@ -195,14 +246,15 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
*/ */
private function prepareInvoice($invoice, $payment, $amount) private function prepareInvoice($invoice, $payment, $amount)
{ {
if (false === isset($invoice) || true === empty($invoice) || false === isset($payment) || true === empty($payment) || false === isset($amount) || true === empty($amount)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::prepareInvoice(): missing or invalid invoice, payment or amount parameter.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::prepareInvoice(): missing or invalid invoice, payment or amount parameter.');
} else {
$this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::prepareInvoice(): entered function with good invoice, payment and amount parameters.');
}
$invoice->setOrderId($payment->getOrder()->getIncrementId()); $invoice->setOrderId($payment->getOrder()->getIncrementId());
$invoice->setPosData( $invoice->setPosData(json_encode(array('id' => $payment->getOrder()->getIncrementId())));
json_encode(
array(
'id' => $payment->getOrder()->getIncrementId(),
)
)
);
$invoice = $this->addCurrencyInfo($invoice, $payment->getOrder()); $invoice = $this->addCurrencyInfo($invoice, $payment->getOrder());
$invoice = $this->addPriceInfo($invoice, $amount); $invoice = $this->addPriceInfo($invoice, $amount);
@ -220,7 +272,20 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
*/ */
private function addBuyerInfo($invoice, $order) private function addBuyerInfo($invoice, $order)
{ {
if (false === isset($invoice) || true === empty($invoice) || false === isset($order) || true === empty($order)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::addBuyerInfo(): missing or invalid invoice or order parameter.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::addBuyerInfo(): missing or invalid invoice or order parameter.');
} else {
$this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::addBuyerInfo(): function called with good invoice and order parameters.');
}
$buyer = new Bitpay\Buyer(); $buyer = new Bitpay\Buyer();
if (false === isset($buyer) || true === empty($buyer)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::addBuyerInfo(): could not construct new BitPay buyer object.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::addBuyerInfo(): could not construct new BitPay buyer object.');
}
$buyer->setFirstName($order->getCustomerFirstname()); $buyer->setFirstName($order->getCustomerFirstname());
$buyer->setLastName($order->getCustomerLastname()); $buyer->setLastName($order->getCustomerLastname());
$invoice->setBuyer($buyer); $invoice->setBuyer($buyer);
@ -237,7 +302,20 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
*/ */
private function addCurrencyInfo($invoice, $order) private function addCurrencyInfo($invoice, $order)
{ {
if (false === isset($invoice) || true === empty($invoice) || false === isset($order) || true === empty($order)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::addCurrencyInfo(): missing or invalid invoice or order parameter.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::addCurrencyInfo(): missing or invalid invoice or order parameter.');
} else {
$this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::addCurrencyInfo(): function called with good invoice and order parameters.');
}
$currency = new Bitpay\Currency(); $currency = new Bitpay\Currency();
if (false === isset($currency) || true === empty($currency)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::addCurrencyInfo(): could not construct new BitPay currency object.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::addCurrencyInfo(): could not construct new BitPay currency object.');
}
$currency->setCode($order->getBaseCurrencyCode()); $currency->setCode($order->getBaseCurrencyCode());
$invoice->setCurrency($currency); $invoice->setCurrency($currency);
@ -253,7 +331,20 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
*/ */
private function addPriceInfo($invoice, $amount) private function addPriceInfo($invoice, $amount)
{ {
if (false === isset($invoice) || true === empty($invoice) || false === isset($amount) || true === empty($amount)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::addPriceInfo(): missing or invalid invoice or amount parameter.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::addPriceInfo(): missing or invalid invoice or amount parameter.');
} else {
$this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::addPriceInfo(): function called with good invoice and amount parameters.');
}
$item = new \Bitpay\Item(); $item = new \Bitpay\Item();
if (false === isset($item) || true === empty($item)) {
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::addPriceInfo(): could not construct new BitPay item object.');
throw new Exception('In Bitpay_Core_Model_Method_Bitcoin::addPriceInfo(): could not construct new BitPay item object.');
}
$item->setPrice($amount); $item->setPrice($amount);
$invoice->setItem($item); $invoice->setItem($item);