Merge pull request #8 from jaafit/master

added sort order to options
This commit is contained in:
Kevin McInturff 2013-05-24 10:59:47 -07:00
commit c15f609c57
7 changed files with 85 additions and 6 deletions

View File

@ -52,8 +52,16 @@ class Bitpay_Bitcoins_Block_Iframe extends Mage_Checkout_Block_Onepage_Payment
// customer data
$method = Mage::getModel('Bitcoins/paymentMethod');
$options += $method->ExtractAddress($quote->getShippingAddress());
$invoice = bpCreateInvoice($quoteId, $quote->getGrandTotal(), array('quoteId' => $quoteId), $options);
// Mage doesn't round the total until saving and it can have more precision at this point which would be bad for later comparing records w/ bitpay. So round here to match what the price will be saved as:
$price = round($quote->getGrandTotal(),4);
//serialize info about the quote to detect changes
$hash = $method->getQuoteHash($quoteId);
Mage::log('invoicing for '.$price.' '.$quote->getQuoteCurrencyCode(), NULL, 'bitpay.log');
$invoice = bpCreateInvoice($quoteId, $price, array('quoteId' => $quoteId, 'quoteHash' => $hash), $options);
Mage::log($invoice, NULL, 'bitpay.log');
if (array_key_exists('error', $invoice))
{

View File

@ -13,6 +13,7 @@ class Bitpay_Bitcoins_Model_Ipn extends Mage_Core_Model_Abstract
return $this
->setQuoteId(isset($invoice['posData']['quoteId']) ? $invoice['posData']['quoteId'] : NULL)
->setOrderId(isset($invoice['posData']['orderId']) ? $invoice['posData']['orderId'] : NULL)
->setPosData(json_encode($invoice['posData']))
->setInvoiceId($invoice['id'])
->setUrl($invoice['url'])
->setStatus($invoice['status'])
@ -29,11 +30,35 @@ class Bitpay_Bitcoins_Model_Ipn extends Mage_Core_Model_Abstract
{
if (!$quoteId)
return false;
$quote = Mage::getModel('sales/quote')->load($quoteId, 'entity_id');
if (!$quote)
{
Mage::log('quote not found', NULL, 'bitpay.log');
return false;
}
$quoteHash = Mage::getModel('Bitcoins/paymentMethod')->getQuoteHash($quoteId);
if (!$quoteHash)
{
Mage::log('Could not find quote hash for quote '.$quoteId, NULL, 'bitpay.log');
return false;
}
$collection = $this->getCollection()->AddFilter('quote_id', $quoteId);
foreach($collection as $i)
{
if (in_array($i->getStatus(), $statuses))
return true;
{
// check that quote data was not updated after IPN sent
$posData = json_decode($i->getPosData());
if (!$posData)
continue;
if ($quoteHash == $posData->quoteHash)
return true;
}
}
return false;
}

View File

@ -220,5 +220,34 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
return '';
}
# computes a unique hash determined by the contents of the cart
public function getQuoteHash($quoteId)
{
$quote = Mage::getModel('sales/quote')->load($quoteId, 'entity_id');
if (!$quote)
{
Mage::log('getQuoteTimestamp: quote not found', NULL, 'bitpay.log');
return false;
}
#encode items
$items = $quote->getAllItems();
$latest = NULL;
$description = '';
foreach($items as $i)
{
$description.= 'i'.$i->getItemId().'q'.$i->getQty();
# could encode $i->getOptions() here but item ids are incremented if options are changed
}
$hash = base64_encode(hash_hmac('sha256', $description, $quoteId));
$hash = substr($hash, 0, 30); // fit it in posData maxlen
Mage::log("quote $quoteId descr $description hash $hash", NULL, 'bitpay.log');
return $hash;
}
}
?>

View File

@ -22,7 +22,7 @@
<modules>
<Bitpay_Bitcoins>
<!-- for database updates -->
<version>1.0.0</version>
<version>1.1.0</version>
</Bitpay_Bitcoins>
</modules>

View File

@ -67,6 +67,15 @@
<show_in_store>0</show_in_store>
</currencies>
<sort_order translate="label">
<label>Sort Order</label>
<frontend_type>text</frontend_type>
<sort_order>7</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</sort_order>
</fields>
</Bitcoins>
</groups>

View File

@ -0,0 +1,8 @@
<?php
$installer = $this;
$installer->startSetup();
$installer->run("ALTER TABLE `bitpay_ipns` ADD `pos_data` VARCHAR( 256 ) NOT NULL AFTER `url`;");
$installer->endSetup();

View File

@ -14,7 +14,7 @@ switch($url){
$quoteId = $this->GetQuoteId();
?>
<?
<?php
$request = Mage::app()->getRequest();
$url = Mage::getUrl('bitpay_callback/index/checkForPayment/');
if ($request->getScheme() == 'https')
@ -22,7 +22,7 @@ if ($request->getScheme() == 'https')
?>
<script type="text/javascript">
//<![CDATA[
new PeriodicalExecuter(function() {new Ajax.Request("<? echo $url; ?>?quote=<? echo $quoteId; ?>", {asynchronous:true, evalScripts:true, onComplete:function(request, json) {
new PeriodicalExecuter(function() {new Ajax.Request("<?php echo $url; ?>?quote=<?php echo $quoteId; ?>", {asynchronous:true, evalScripts:true, onComplete:function(request, json) {
data = request.responseText.evalJSON();
if (data.paid) {
buttons = $$("button.btn-checkout");