Merge pull request #26 from ionux/master

Fix for order not marked complete issue
This commit is contained in:
Ryan X. Charles 2014-03-13 16:21:10 -04:00
commit 82ad4204ac
2 changed files with 59 additions and 26 deletions

View File

@ -53,7 +53,7 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
/** /**
* Can capture funds online? * Can capture funds online?
*/ */
protected $_canCapture = false; protected $_canCapture = true;
/** /**
* Can capture partial amounts online? * Can capture partial amounts online?
@ -93,7 +93,7 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
//protected $_formBlockType = 'bitcoins/form'; //protected $_formBlockType = 'bitcoins/form';
//protected $_infoBlockType = 'bitcoins/info'; //protected $_infoBlockType = 'bitcoins/info';
function canUseForCurrency($currencyCode) { function canUseForCurrency($currencyCode) {
$currencies = Mage::getStoreConfig('payment/Bitcoins/currencies'); $currencies = Mage::getStoreConfig('payment/Bitcoins/currencies');
$currencies = array_map('trim', explode(',', $currencies)); $currencies = array_map('trim', explode(',', $currencies));
return array_search($currencyCode, $currencies) !== false; return array_search($currencyCode, $currencies) !== false;
@ -144,32 +144,61 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save(); $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
if (!count($order->getInvoiceCollection())) { if (!count($order->getInvoiceCollection())) {
$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();
$order->addStatusToHistory(Mage_Sales_Model_Order::STATE_COMPLETE);
try { try {
$order->sendNewOrderEmail(); $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) { } catch (Exception $e) {
Mage::logException($e); Mage::logException($e);
} }
} else {
Mage::log('Count of InvoiceCollection was zero! Order not invoiced.', null, 'bitpay.log');
} }
} }
function MarkOrderComplete($order) {
if ($order->hasInvoices()) {
foreach ($order->getInvoiceCollection() as $_eachInvoice) {
try {
$_eachInvoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$_eachInvoice->capture()->save();
} catch (Exception $e) {
Mage::logException($e);
}
}
}
$shipment = $order->prepareShipment();
if($shipment) {
$shipment->register();
$order->setIsInProcess(true);
$transaction_save = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();
}
try {
$order->setState('Complete', 'complete', 'Completed by BitPay payments.', false);
$order->save();
} catch (Exception $e) {
Mage::logException($e);
}
}
function MarkOrderCancelled($order) { function MarkOrderCancelled($order) {
$order->setState(Mage_Sales_Model_Order::STATE_CANCELLED, true)->save(); $order->setState(Mage_Sales_Model_Order::STATE_CANCELLED, true)->save();
} }
// given Mage_Core_Model_Abstract, return api-friendly address // given Mage_Core_Model_Abstract, return api-friendly address
function ExtractAddress($address) { function ExtractAddress($address) {
$options = array(); $options = array();
@ -178,7 +207,7 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
if ($address->getCompany()) if ($address->getCompany())
$options['buyerName'] = $options['buyerName'].' c/o '.$address->getCompany(); $options['buyerName'] = $options['buyerName'].' c/o '.$address->getCompany();
$options['buyerAddress1'] = $address->getStreet1(); $options['buyerAddress1'] = $address->getStreet1();
$options['buyerAddress2'] = $address->getStreet2(); $options['buyerAddress2'] = $address->getStreet2();
$options['buyerAddress3'] = $address->getStreet3(); $options['buyerAddress3'] = $address->getStreet3();
$options['buyerAddress4'] = $address->getStreet4(); $options['buyerAddress4'] = $address->getStreet4();
@ -193,11 +222,11 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
foreach(array('buyerName', 'buyerAddress1', 'buyerAddress2', 'buyerAddress3', 'buyerAddress4', 'buyerCity', 'buyerState', 'buyerZip', 'buyerCountry', 'buyerEmail', 'buyerPhone') as $f) foreach(array('buyerName', 'buyerAddress1', 'buyerAddress2', 'buyerAddress3', 'buyerAddress4', 'buyerCity', 'buyerState', 'buyerZip', 'buyerCountry', 'buyerEmail', 'buyerPhone') as $f)
$options[$f] = substr($options[$f], 0, 100); $options[$f] = substr($options[$f], 0, 100);
return $options; return $options;
} }
function CreateInvoiceAndRedirect($payment, $amount) { function CreateInvoiceAndRedirect($payment, $amount) {
include Mage::getBaseDir('lib').'/bitpay/bp_lib.php'; include Mage::getBaseDir('lib').'/bitpay/bp_lib.php';
$apiKey = Mage::getStoreConfig('payment/Bitcoins/api_key'); $apiKey = Mage::getStoreConfig('payment/Bitcoins/api_key');
$speed = Mage::getStoreConfig('payment/Bitcoins/speed'); $speed = Mage::getStoreConfig('payment/Bitcoins/speed');
@ -206,7 +235,7 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
$options = array( $options = array(
'currency' => $order->getBaseCurrencyCode(), 'currency' => $order->getBaseCurrencyCode(),
'buyerName' => $order->getCustomerFirstname().' '.$order->getCustomerLastname(), 'buyerName' => $order->getCustomerFirstname().' '.$order->getCustomerLastname(),
'fullNotifications' => 'true', 'fullNotifications' => 'true',
'notificationURL' => Mage::getUrl('bitpay_callback'), 'notificationURL' => Mage::getUrl('bitpay_callback'),
'redirectURL' => Mage::getUrl('checkout/onepage/success'), 'redirectURL' => Mage::getUrl('checkout/onepage/success'),
@ -214,7 +243,7 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
'apiKey' => $apiKey, 'apiKey' => $apiKey,
); );
$options += $this->ExtractAddress($order->getShippingAddress()); $options += $this->ExtractAddress($order->getShippingAddress());
$invoice = bpCreateInvoice($orderId, $amount, array('orderId' => $orderId), $options); $invoice = bpCreateInvoice($orderId, $amount, array('orderId' => $orderId), $options);
$payment->setIsTransactionPending(true); // status will be PAYMENT_REVIEW instead of PROCESSING $payment->setIsTransactionPending(true); // status will be PAYMENT_REVIEW instead of PROCESSING

View File

@ -31,7 +31,7 @@ class Bitpay_Bitcoins_IndexController extends Mage_Core_Controller_Front_Action
} }
// bitpay's IPN lands here // bitpay's IPN lands here
public function indexAction() { public function indexAction() {
require Mage::getBaseDir('lib').'/bitpay/bp_lib.php'; require Mage::getBaseDir('lib').'/bitpay/bp_lib.php';
Mage::log(file_get_contents('php://input'), null, 'bitpay.log'); Mage::log(file_get_contents('php://input'), null, 'bitpay.log');
$apiKey = Mage::getStoreConfig('payment/Bitcoins/api_key'); $apiKey = Mage::getStoreConfig('payment/Bitcoins/api_key');
@ -55,10 +55,14 @@ class Bitpay_Bitcoins_IndexController extends Mage_Core_Controller_Front_Action
// update the order if it exists already // update the order if it exists already
if ($order->getId()) if ($order->getId())
switch($invoice['status']) { switch($invoice['status']) {
case 'paid':
$method = Mage::getModel('Bitcoins/paymentMethod');
$method->MarkOrderPaid($order);
break;
case 'confirmed': case 'confirmed':
case 'complete': case 'complete':
$method = Mage::getModel('Bitcoins/paymentMethod'); $method = Mage::getModel('Bitcoins/paymentMethod');
$method->MarkOrderPaid($order); $method->MarkOrderComplete($order);
break; break;
case 'invalid': case 'invalid':
$method = Mage::getModel('Bitcoins/paymentMethod'); $method = Mage::getModel('Bitcoins/paymentMethod');