Merge pull request #60 from JoshuaEstes/master

Refactoring and bug fix
This commit is contained in:
Rich Morgan 2014-07-08 17:57:58 -04:00
commit d2221326e1
3 changed files with 116 additions and 92 deletions

View File

@ -65,7 +65,8 @@ class Bitpay_Bitcoins_Model_Ipn extends Mage_Core_Model_Abstract
*
* @return boolean
*/
function GetStatusReceived($quoteId, $statuses) {
function GetStatusReceived($quoteId, $statuses)
{
if (!$quoteId)
{
return false;

View File

@ -24,9 +24,6 @@
* THE SOFTWARE.
*/
/**
* Our test CC module adapter
*/
class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract
{
@ -123,18 +120,14 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
*/
public function canUseCheckout()
{
$secret = Mage::getStoreConfig('payment/Bitcoins/api_key');
if (!$secret or !strlen($secret))
if (!$this->isApiKeyConfigured())
{
Mage::log('Bitpay/Bitcoins: API key not entered', Zend_Log::ERR, 'bitpay.log');
return false;
}
$speed = Mage::getStoreConfig('payment/Bitcoins/speed');
if (!$speed or !strlen($speed))
if (!$this->isTransactionSpeedConfigured())
{
Mage::log('Bitpay/Bitcoins: Transaction Speed invalid', Zend_Log::ERR, 'bitpay.log');
@ -145,10 +138,36 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
}
/**
* @param Varien_Object $payment
* @param string $amounf
* Returns true if the merchant has set their api key
*
* @return
* @return boolean
*/
public function isApiKeyConfigured()
{
$key = Mage::getStoreConfig('payment/Bitcoins/api_key');
return !empty($key);
}
/**
* Returns true if Transaction Speed has been configured
*
* @return boolean
*/
public function isTransactionSpeedConfigured()
{
$speed = Mage::getStoreConfig('payment/Bitcoins/speed');
return !empty($speed);
}
/**
* Authorize payment method
*
* @param Varien_Object $payment
* @param float $amount
*
* @return Bitpay_Bitcoins_Model_PaymentMethod
*/
public function authorize(Varien_Object $payment, $amount)
{
@ -163,7 +182,7 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
}
/**
* @param $payment
* @param Varien_Object $payment
*
* @return Bitpay_Bitcoins_Model_PaymentMethod
*/
@ -190,18 +209,17 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
}
/**
* @param $order
* @param Varien_Object $order
*/
public function MarkOrderPaid($order)
{
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
if ($order->getTotalDue() > 0)
{
if (!count($order->getInvoiceCollection()))
public function invoiceOrder($order)
{
try
{
if (!$order->canInvoice())
{
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
}
$invoice = $order->prepareInvoice()
->setTransactionId(1)
->addComment('Invoiced automatically by Bitpay/Bitcoins/controllers/IndexController.php')
@ -220,6 +238,20 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
Mage::logException($e);
}
}
/**
* @param $order
*/
public function MarkOrderPaid($order)
{
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
if ($order->getTotalDue() > 0)
{
if (!count($order->getInvoiceCollection()))
{
$this->invoiceOrder($order);
}
}
else
{
@ -232,11 +264,7 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
*/
public function MarkOrderComplete($order)
{
/**
* The order has already been invoiced and has already been paid, this
* code leads to having payments applied multiple times.
*
if ($order->getTotalDue() <= 0)
if ($order->getTotalDue() >= 0 && $order->canInvoice())
{
if ($order->hasInvoices())
{
@ -255,11 +283,6 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
}
}
}
else
{
Mage::log('MarkOrderComplete called but order '. $order->getId() .' has an outstanding balance that has not been paid.', Zend_Log::WARN, 'bitpay.log');
}
*/
// If the $_bpCreateShipment option is set to true above, this code will
// programmatically create a shipment for you. By design, this will mark

View File

@ -50,63 +50,63 @@ class Bitpay_Bitcoins_IndexController extends Mage_Core_Controller_Front_Action
if (is_string($invoice))
{
Mage::log("bitpay callback error: $invoice", Zend_Log::ERR, 'bitpay.log');
throw new Exception('Bitpay callback error:' . $invoice);
}
else
{
// get the order
if (isset($invoice['posData']['quoteId']))
{
$quoteId = $invoice['posData']['quoteId'];
$order = Mage::getModel('sales/order')->load($quoteId, 'quote_id');
}
else
elseif (isset($invoice['posData']['orderId']))
{
$orderId = $invoice['posData']['orderId'];
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
}
else
{
Mage::log('Invalid posData, does not contain quoteId or orderId.', Zend_Log::ERR, 'bitpay.log');
throw new Exception('Invalid Bitpay IPN received.');
}
// save the ipn so that we can find it when the user clicks "Place Order"
Mage::getModel('Bitcoins/ipn')->Record($invoice);
// update the order if it exists already
if ($order->getId())
if (!$order->getId())
{
Mage::log('Order object does not contain an ID', Zend_Log::ERR, 'bitpay.log');
throw new Exception('Order object does not contain an ID');
}
// update the order if it exists already
// BitPay Statuses
// new, paid, confirmed, complete, expired, invalid
Mage::log('Received IPN with "' . $invoice['status'] . '" status', Zend_Log::DEBUG, 'bitpay.log');
switch($invoice['status'])
{
// Map to Magento state Processing
case 'paid':
// Mark paid if there is an outstanding total
if ($order->getTotalDue() > 0)
{
$method = Mage::getModel('Bitcoins/paymentMethod');
$method->MarkOrderPaid($order);
}
else
{
Mage::log('Received a PAID notification from BitPay but there is nothing due on this invoice. Ignoring this IPN.', null, 'bitpay.log');
}
break;
// Map to Magento status Complete
case 'confirmed':
case 'complete':
// Mark confirmed/complete if the order has been paid
if ($order->getTotalDue() <= 0)
{
$method = Mage::getModel('Bitcoins/paymentMethod');
$method->MarkOrderComplete($order);
}
else
{
Mage::log('Received a ' . $invoice['status'] . ' notification from BitPay but this order is not paid yet. Possible internal error with Magento. Check order status to confirm.', Zend_Log::ERR, 'bitpay.log');
}
//Mage::log('Received a ' . $invoice['status'] . ' notification from BitPay but this order is not paid yet. Possible internal error with Magento. Check order status to confirm.', Zend_Log::ERR, 'bitpay.log');
break;
// Map to Magento State Closed
case 'invalid':
$method = Mage::getModel('Bitcoins/paymentMethod');
$method->MarkOrderCancelled($order);
break;
}
}
}
}
}