Added extra error handling and debugging
This commit is contained in:
parent
409eb02c94
commit
c29c10c599
|
@ -12,6 +12,7 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
|
|||
protected $_code = 'bitpay';
|
||||
protected $_formBlockType = 'bitpay/form_bitpay';
|
||||
protected $_infoBlockType = 'bitpay/info';
|
||||
|
||||
protected $_isGateway = true;
|
||||
protected $_canAuthorize = true;
|
||||
protected $_canCapture = false;
|
||||
|
@ -19,12 +20,14 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
|
|||
protected $_isInitializeNeeded = false;
|
||||
protected $_canFetchTransactionInfo = false;
|
||||
protected $_canManagerRecurringProfiles = false;
|
||||
//protected $_canUseCheckout = true;
|
||||
//protected $_canUseForMultishipping = true;
|
||||
//protected $_canCapturePartial = false;
|
||||
//protected $_canRefund = false;
|
||||
//protected $_canVoid = false;
|
||||
protected $_canUseCheckout = true;
|
||||
protected $_canUseForMultishipping = true;
|
||||
protected $_canCapturePartial = false;
|
||||
protected $_canRefund = false;
|
||||
protected $_canVoid = false;
|
||||
|
||||
protected $_debugReplacePrivateDataKeys = array();
|
||||
|
||||
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)
|
||||
{
|
||||
$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
|
||||
$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);
|
||||
|
||||
try {
|
||||
$bitpayInvoice = Mage::helper('bitpay')->getBitpayClient()->createInvoice($invoice);
|
||||
} catch (Exception $e) {
|
||||
$this->debugData($e->getMessage());
|
||||
$this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::authorize(): ' . $e->getMessage());
|
||||
$this->debugData(
|
||||
array(
|
||||
Mage::helper('bitpay')->getBitpayClient()->getRequest()->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();
|
||||
|
||||
$this->debugData(
|
||||
array(
|
||||
'BitPay Invoice created',
|
||||
'[INFO] BitPay Invoice created',
|
||||
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())
|
||||
->save();
|
||||
|
||||
$this->debugData($bitpayInvoice->getId());
|
||||
$this->debugData('[INFO] Leaving Bitpay_Core_Model_Method_Bitcoin::authorize(): invoice id ' . $bitpayInvoice->getId());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -83,16 +98,18 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
|
|||
{
|
||||
$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
|
||||
* 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;
|
||||
}
|
||||
|
||||
$this->debugData('[INFO] Leaving Bitpay_Core_Model_Method_Bitcoin::canUseCheckout(): token obtained from storage successfully.');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -104,11 +121,33 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
|
|||
*/
|
||||
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();
|
||||
|
||||
$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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -121,9 +160,12 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
|
|||
*/
|
||||
public function extractAddress($address)
|
||||
{
|
||||
$this->debugData(
|
||||
sprintf('Extracting addess')
|
||||
);
|
||||
if (false === isset($address) || true === empty($address)) {
|
||||
$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['buyerName'] = $address->getName();
|
||||
|
@ -145,7 +187,10 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
|
|||
|
||||
// trim to fit API specs
|
||||
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;
|
||||
|
@ -158,9 +203,7 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
|
|||
*/
|
||||
public function getOrderPlaceRedirectUrl()
|
||||
{
|
||||
$this->debugData(
|
||||
'Customer wants to place the order. Create invoice and redirect user to invoice'
|
||||
);
|
||||
$this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::getOrderPlaceRedirectUrl(): $_redirectUrl is ' . 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();
|
||||
|
||||
$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->setTransactionSpeed(Mage::getStoreConfig('payment/bitpay/speed'));
|
||||
$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)
|
||||
{
|
||||
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->setPosData(
|
||||
json_encode(
|
||||
array(
|
||||
'id' => $payment->getOrder()->getIncrementId(),
|
||||
)
|
||||
)
|
||||
);
|
||||
$invoice->setPosData(json_encode(array('id' => $payment->getOrder()->getIncrementId())));
|
||||
|
||||
$invoice = $this->addCurrencyInfo($invoice, $payment->getOrder());
|
||||
$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)
|
||||
{
|
||||
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();
|
||||
|
||||
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->setLastName($order->getCustomerLastname());
|
||||
$invoice->setBuyer($buyer);
|
||||
|
@ -237,7 +302,20 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
|
|||
*/
|
||||
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();
|
||||
|
||||
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());
|
||||
$invoice->setCurrency($currency);
|
||||
|
||||
|
@ -253,7 +331,20 @@ class Bitpay_Core_Model_Method_Bitcoin extends Mage_Payment_Model_Method_Abstrac
|
|||
*/
|
||||
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();
|
||||
|
||||
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);
|
||||
$invoice->setItem($item);
|
||||
|
||||
|
|
Loading…
Reference in New Issue