From 6826b12f846b8991b30a16964847016fc9a0e831 Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Tue, 22 Jul 2014 16:41:28 -0400 Subject: [PATCH] Still work in progress --- .../community/Bitpay/Bitcoins/Model/Ipn.php | 67 ++++++++++++- .../Bitpay/Bitcoins/Model/Observer.php | 94 +++++++++++++++++++ .../community/Bitpay/Bitcoins/etc/config.xml | 21 +++++ 3 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 app/code/community/Bitpay/Bitcoins/Model/Observer.php diff --git a/app/code/community/Bitpay/Bitcoins/Model/Ipn.php b/app/code/community/Bitpay/Bitcoins/Model/Ipn.php index 37b61f2..b930eab 100644 --- a/app/code/community/Bitpay/Bitcoins/Model/Ipn.php +++ b/app/code/community/Bitpay/Bitcoins/Model/Ipn.php @@ -31,9 +31,8 @@ class Bitpay_Bitcoins_Model_Ipn extends Mage_Core_Model_Abstract */ function _construct() { + parent::_construct(); $this->_init('Bitcoins/ipn'); - - return parent::_construct(); } /** @@ -133,4 +132,68 @@ class Bitpay_Bitcoins_Model_Ipn extends Mage_Core_Model_Abstract { return $this->GetStatusReceived($quoteId, array('confirmed', 'complete')); } + + /** + * This method returns an array of orders in the database that have paid + * using bitcoins, but are still open and we need to query the invoice + * IDs at BitPay and see if they invoice has expired, is invalid, or is + * complete. + * + * @return array + */ + public function getOpenOrders() + { + $doneCollection = $this->getCollection(); + + /** + * Get all the IPNs that have been completed + * + * SELECT + * order_id + * FROM + * bitpay_ipns + * WHERE + * status IN ('completed','invalid','expired') AND order_id IS NOT NULL + * GROUP BY + * order_id + */ + $doneCollection + ->addFieldToFilter('status', array('in' => array('completed','invalid','expired'))) + ->getSelect() + //->where('status IN (?)', array('completed','invalid','expired')) + //->where('order_id IS NOT NULL') + ->group('order_id'); + Mage::log($doneCollection->toArray(), null, 'bitpay.log'); + Mage::log($doneCollection->getColumnValues('order_id'), null, 'bitpay.log'); + Mage::log((string) $doneCollection->getSelect(), null, 'bitpay.log'); + + return array(); + + $collection = $this->getCollection(); + + /** + * Get all the open orders that have not received a IPN that closes + * the invoice. + * + * SELECT + * * + * FROM + * bitpay_ipns + * JOIN + * 'sales/order' ON bitpay_ipns.order_id='sales/order'.increment_id + * WHERE + * order_id NOT IN (?) AND order_id IS NOT NULL + * GROUP BY + * order_id + */ + $collection + ->getSelect() + ->join( + array('order' => $this->getTable('sales/order')), + 'main_table.order_id = order.increment_id' + ) + ->where('main_table.order_id NOT IN (?)', $doneCollection->getColumnValues('order_id')); + + return $collection->toArray(); + } } diff --git a/app/code/community/Bitpay/Bitcoins/Model/Observer.php b/app/code/community/Bitpay/Bitcoins/Model/Observer.php new file mode 100644 index 0000000..7013406 --- /dev/null +++ b/app/code/community/Bitpay/Bitcoins/Model/Observer.php @@ -0,0 +1,94 @@ +getOpenOrders(); + + /** + * Get all orders that have been paid using bitpay and + * are not complete/closed/etc + */ + foreach ($orders as $order) { + /** + * Query BitPay with the invoice ID to get the status. We must take + * care not to anger the API limiting gods and disable our access + * to the API. + */ + $status = null; + + // Does the order need to be updated? + // Yes? Update Order Status + // No? continue + } + + Mage::log( + 'cronjob: end', + Zend_Log::DEBUG, + 'bitpay.log' + ); + } + + /** + * This is called to clean up old IPNs in the bitpay_ipn table + */ + public function cleanIpnTable() + { + // @TODO + } +} diff --git a/app/code/community/Bitpay/Bitcoins/etc/config.xml b/app/code/community/Bitpay/Bitcoins/etc/config.xml index 6ff80ec..d60d56f 100644 --- a/app/code/community/Bitpay/Bitcoins/etc/config.xml +++ b/app/code/community/Bitpay/Bitcoins/etc/config.xml @@ -96,6 +96,27 @@ + + + + + + + */1 * * * * + + + Bitcoins/Observer::updateOrderStates + + + + +