Still work in progress

This commit is contained in:
Joshua Estes 2014-07-22 16:41:28 -04:00
parent 16731c0509
commit 6826b12f84
3 changed files with 180 additions and 2 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1,94 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2011-2014 BitPay LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class Bitpay_Bitcoins_Model_Observer
{
/**
* Queries BitPay to update the order states in magento to make sure that
* open orders are closed/canceled if the BitPay invoice expires or becomes
* invalid.
*/
public function updateOrderStates()
{
Mage::log(
'cronjob: started',
Zend_Log::DEBUG,
'bitpay.log'
);
$apiKey = Mage::getStoreConfig('payment/Bitcoins/api_key');
if (empty($apiKey)) {
Mage::log(
'cronjob: Api Key not set.',
Zend_Log::ERR,
'bitpay.log'
);
return; // Api Key needs to be set
}
/**
* Get all of the orders that are open and have not received an IPN for
* complete, expired, or invalid.
*
* If anyone knows of a better way to do this, please let me know
*/
$orders = Mage::getModel('Bitcoins/ipn')->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
}
}

View File

@ -96,6 +96,27 @@
</resources>
</global>
<crontab>
<jobs>
<bitpay_bitcoins>
<schedule>
<!--
Everyday at midnight
<cron_expr>0 0 * * *</cron_expr>
-->
<!--
Debug
Every 5 Minutes
-->
<cron_expr>*/1 * * * *</cron_expr>
</schedule>
<run>
<model>Bitcoins/Observer::updateOrderStates</model>
</run>
</bitpay_bitcoins>
</jobs>
</crontab>
<default>
<payment>
<Bitcoins>