commit
d2221326e1
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
@ -189,6 +208,37 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Varien_Object $order
|
||||
*/
|
||||
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')
|
||||
->register()
|
||||
->pay();
|
||||
|
||||
$transactionSave = Mage::getModel('core/resource_transaction')
|
||||
->addObject($invoice)
|
||||
->addObject($invoice->getOrder());
|
||||
|
||||
$transactionSave->save();
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
Mage::log($e->getMessage(), Zend_Log::EMERG, 'bitpay.log');
|
||||
Mage::logException($e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $order
|
||||
*/
|
||||
|
@ -200,25 +250,7 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
|
|||
{
|
||||
if (!count($order->getInvoiceCollection()))
|
||||
{
|
||||
try
|
||||
{
|
||||
$invoice = $order->prepareInvoice()
|
||||
->setTransactionId(1)
|
||||
->addComment('Invoiced automatically by Bitpay/Bitcoins/controllers/IndexController.php')
|
||||
->register()
|
||||
->pay();
|
||||
|
||||
$transactionSave = Mage::getModel('core/resource_transaction')
|
||||
->addObject($invoice)
|
||||
->addObject($invoice->getOrder());
|
||||
|
||||
$transactionSave->save();
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
Mage::log($e->getMessage(), Zend_Log::EMERG, 'bitpay.log');
|
||||
Mage::logException($e);
|
||||
}
|
||||
$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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
// get the order
|
||||
if (isset($invoice['posData']['quoteId']))
|
||||
{
|
||||
$quoteId = $invoice['posData']['quoteId'];
|
||||
$order = Mage::getModel('sales/order')->load($quoteId, 'quote_id');
|
||||
}
|
||||
elseif (isset($invoice['posData']['orderId']))
|
||||
{
|
||||
$orderId = $invoice['posData']['orderId'];
|
||||
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
|
||||
}
|
||||
else
|
||||
{
|
||||
// get the order
|
||||
if (isset($invoice['posData']['quoteId']))
|
||||
{
|
||||
$quoteId = $invoice['posData']['quoteId'];
|
||||
$order = Mage::getModel('sales/order')->load($quoteId, 'quote_id');
|
||||
}
|
||||
else
|
||||
{
|
||||
$orderId = $invoice['posData']['orderId'];
|
||||
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
|
||||
}
|
||||
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);
|
||||
// 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())
|
||||
{
|
||||
switch($invoice['status'])
|
||||
{
|
||||
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');
|
||||
}
|
||||
|
||||
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;
|
||||
// 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'])
|
||||
{
|
||||
|
||||
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');
|
||||
}
|
||||
break;
|
||||
// Map to Magento state Processing
|
||||
case 'paid':
|
||||
// Mark paid if there is an outstanding total
|
||||
$method = Mage::getModel('Bitcoins/paymentMethod');
|
||||
$method->MarkOrderPaid($order);
|
||||
break;
|
||||
|
||||
case 'invalid':
|
||||
$method = Mage::getModel('Bitcoins/paymentMethod');
|
||||
$method->MarkOrderCancelled($order);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Map to Magento status Complete
|
||||
case 'confirmed':
|
||||
case 'complete':
|
||||
// Mark confirmed/complete if the order has been paid
|
||||
$method = Mage::getModel('Bitcoins/paymentMethod');
|
||||
$method->MarkOrderComplete($order);
|
||||
//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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue