Updated so there is a command line task that can be used to cancel magento orders that have expired IPNs
This commit is contained in:
parent
f4ddb824a4
commit
952827f255
|
@ -158,16 +158,21 @@ class Bitpay_Bitcoins_Model_Ipn extends Mage_Core_Model_Abstract
|
||||||
* order_id
|
* order_id
|
||||||
*/
|
*/
|
||||||
$doneCollection
|
$doneCollection
|
||||||
->addFieldToFilter('status', array('in' => array('completed','invalid','expired')))
|
->addFieldToSelect('order_id')
|
||||||
|
->addFieldToFilter(
|
||||||
|
'status',
|
||||||
|
array(
|
||||||
|
'in' => array(
|
||||||
|
'complete',
|
||||||
|
'invalid',
|
||||||
|
'expired',
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$doneCollection
|
||||||
->getSelect()
|
->getSelect()
|
||||||
//->where('status IN (?)', array('completed','invalid','expired'))
|
->where('order_id IS NOT NULL')
|
||||||
//->where('order_id IS NOT NULL')
|
|
||||||
->group('order_id');
|
->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();
|
$collection = $this->getCollection();
|
||||||
|
|
||||||
|
@ -186,14 +191,64 @@ class Bitpay_Bitcoins_Model_Ipn extends Mage_Core_Model_Abstract
|
||||||
* GROUP BY
|
* GROUP BY
|
||||||
* order_id
|
* order_id
|
||||||
*/
|
*/
|
||||||
|
if (0 < $doneCollection->count()) {
|
||||||
|
$collection
|
||||||
|
->addFieldToFilter(
|
||||||
|
'status',
|
||||||
|
array(
|
||||||
|
'in' => $doneCollection->getColumnValues('order_id')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$collection
|
$collection
|
||||||
->getSelect()
|
->getSelect()
|
||||||
->join(
|
->where('order_id IS NOT NULL')
|
||||||
array('order' => $this->getTable('sales/order')),
|
->group('order_id');
|
||||||
'main_table.order_id = order.increment_id'
|
|
||||||
)
|
|
||||||
->where('main_table.order_id NOT IN (?)', $doneCollection->getColumnValues('order_id'));
|
|
||||||
|
|
||||||
return $collection->toArray();
|
return $collection->getItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all records that have expired
|
||||||
|
*/
|
||||||
|
public function getExpired()
|
||||||
|
{
|
||||||
|
$collection = $this->getCollection();
|
||||||
|
$now = new DateTime('now', new DateTimezone('UTC'));
|
||||||
|
|
||||||
|
$collection
|
||||||
|
->removeFieldFromSelect('status')
|
||||||
|
->addFieldToFilter(
|
||||||
|
'expiration_time',
|
||||||
|
array(
|
||||||
|
'lteq' => $now->getTimestamp()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$collection
|
||||||
|
->getSelect()
|
||||||
|
->group('order_id');
|
||||||
|
|
||||||
|
return $collection->getItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This will delete all records that match the order id (order id is also
|
||||||
|
* the increment id of the magento order)
|
||||||
|
*
|
||||||
|
* @see Bitpay_Bitcoins_Model_Resource_Ipn_Collection::delete()
|
||||||
|
*
|
||||||
|
* @param string $orderId
|
||||||
|
*/
|
||||||
|
public function deleteByOrderId($orderId)
|
||||||
|
{
|
||||||
|
$collection = Mage::getModel('Bitcoins/ipn')
|
||||||
|
->getCollection();
|
||||||
|
$collection
|
||||||
|
->getSelect()
|
||||||
|
->where('order_id = ?', $orderId);
|
||||||
|
|
||||||
|
$collection->delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
/**
|
/**
|
||||||
* The MIT License (MIT)
|
* The MIT License (MIT)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2011-2014 BitPay LLC
|
* Copyright (c) 2011-2014 BitPay, Inc.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -24,7 +24,7 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Bitpay_Bitcoins_Model_Resource_Ipn extends Mage_Core_Model_Resource_Db_Abstract
|
class Bitpay_Bitcoins_Model_Resource_Ipn extends Mage_Core_Model_Mysql4_Abstract
|
||||||
{
|
{
|
||||||
protected function _construct()
|
protected function _construct()
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
/**
|
/**
|
||||||
* The MIT License (MIT)
|
* The MIT License (MIT)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2011-2014 BitPay LLC
|
* Copyright (c) 2011-2014 BitPay, Inc.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -24,13 +24,21 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Bitpay_Bitcoins_Model_Resource_Ipn_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
|
class Bitpay_Bitcoins_Model_Resource_Ipn_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
protected function _construct()
|
protected function _construct()
|
||||||
{
|
{
|
||||||
|
parent::_construct();
|
||||||
$this->_init('Bitcoins/ipn');
|
$this->_init('Bitcoins/ipn');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function delete()
|
||||||
|
{
|
||||||
|
foreach ($this->getItems() as $item) {
|
||||||
|
$item->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
1
modman
1
modman
|
@ -25,3 +25,4 @@ app/design/frontend/base/default/layout/bitcoins.xml app/design/frontend/base/de
|
||||||
app/design/frontend/base/default/template/bitcoins app/design/frontend/base/default/template/bitcoins
|
app/design/frontend/base/default/template/bitcoins app/design/frontend/base/default/template/bitcoins
|
||||||
app/etc/modules/Bitpay_Bitcoins.xml app/etc/modules/Bitpay_Bitcoins.xml
|
app/etc/modules/Bitpay_Bitcoins.xml app/etc/modules/Bitpay_Bitcoins.xml
|
||||||
lib/bitpay lib/bitpay
|
lib/bitpay lib/bitpay
|
||||||
|
shell/bitpay.php shell/bitpay.php
|
||||||
|
|
|
@ -0,0 +1,155 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2011-2014 BitPay, Inc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once 'abstract.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is used to work with the bitpay api via the command line and to
|
||||||
|
* debug issues
|
||||||
|
*/
|
||||||
|
class Bitpay_Shell_Bitpay extends Mage_Shell_Abstract
|
||||||
|
{
|
||||||
|
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
if ($clean = $this->getArg('clean')) {
|
||||||
|
switch ($clean) {
|
||||||
|
case 'expired':
|
||||||
|
$this->cleanExpired();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
echo $this->usageHelp();
|
||||||
|
return 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->getArg('expired')) {
|
||||||
|
$expiredIpns = Mage::getModel('Bitcoins/ipn')->getExpired();
|
||||||
|
echo "\n";
|
||||||
|
foreach ($expiredIpns as $ipn) {
|
||||||
|
var_dump($ipn->toArray());
|
||||||
|
printf(
|
||||||
|
'Order ID: %s',
|
||||||
|
$ipn->getOrderId()
|
||||||
|
);
|
||||||
|
|
||||||
|
echo "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$orders = Mage::getModel('Bitcoins/ipn')->getOpenOrders();
|
||||||
|
foreach ($orders as $order) {
|
||||||
|
var_dump(get_class($order));
|
||||||
|
}
|
||||||
|
echo $this->usageHelp();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes expired IPNs from database and updates order if they are
|
||||||
|
* open
|
||||||
|
*/
|
||||||
|
public function cleanExpired()
|
||||||
|
{
|
||||||
|
$expiredRecords = Mage::getModel('Bitcoins/ipn')->getExpired();
|
||||||
|
|
||||||
|
// Parse each record
|
||||||
|
foreach ($expiredRecords as $ipn) {
|
||||||
|
$incrementId = $ipn->getOrderId();
|
||||||
|
if (empty($incrementId)) {
|
||||||
|
printf("Error processing IPN record\n");
|
||||||
|
Mage::log($ipn->toJson(), Zend_Log::DEBUG, 'bitpay.log');
|
||||||
|
/**
|
||||||
|
* We have no way to tie this to any magento order so it needs
|
||||||
|
* to be deleted
|
||||||
|
*/
|
||||||
|
$ipn->delete();
|
||||||
|
printf("IPN Record Deleted\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Parsing IPN for increment id x
|
||||||
|
printf("Prasing '%s'\n", $ipn->getOrderId());
|
||||||
|
// Cancel the order in the system
|
||||||
|
$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
|
||||||
|
$orderState = $order->getState();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the order is complete, we do not want to cancel it
|
||||||
|
*/
|
||||||
|
$statesWeDontCareAbout = array(
|
||||||
|
Mage_Sales_Model_Order::STATE_CANCELED,
|
||||||
|
Mage_Sales_Model_Order::STATE_CLOSED,
|
||||||
|
Mage_Sales_Model_Order::STATE_COMPLETE,
|
||||||
|
);
|
||||||
|
if (!in_array($orderState, $statesWeDontCareAbout)) {
|
||||||
|
$order->setState(
|
||||||
|
Mage_Sales_Model_Order::STATE_CANCELED,
|
||||||
|
true,
|
||||||
|
'BitPay Invoice has expired', // comment
|
||||||
|
false // notifiy customer?
|
||||||
|
)->save();
|
||||||
|
printf("Order has been canceled\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete all IPN records for order id
|
||||||
|
Mage::getModel('Bitcoins/ipn')
|
||||||
|
->deleteByOrderId($ipn->getOrderId());
|
||||||
|
|
||||||
|
printf("IPN Record Deleted\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Complete\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display help on how to use this bad boy
|
||||||
|
*/
|
||||||
|
public function usageHelp()
|
||||||
|
{
|
||||||
|
return <<<USAGE
|
||||||
|
Usage: php -f bitpay.php
|
||||||
|
|
||||||
|
--clean <status> Delete all IPN records based on <status>
|
||||||
|
|
||||||
|
List of Statuses:
|
||||||
|
|
||||||
|
new
|
||||||
|
paid
|
||||||
|
confirmed
|
||||||
|
complete
|
||||||
|
expired
|
||||||
|
invalid
|
||||||
|
|
||||||
|
USAGE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$shell = new Bitpay_Shell_Bitpay();
|
||||||
|
$shell->run();
|
Loading…
Reference in New Issue