Update for invalid input stream from IPN post #74

* Performs check to ensure the file_get_contents() function is not disabled and attempts to enable.
* Prior to PHP 5.6, php://input couldn't be reliably re-read.  This patch reads it once and works with that data via the raw_post_data variable.
* https://github.com/bitpay/magento-plugin/issues/74
This commit is contained in:
Rich Morgan 2014-11-18 18:16:17 -05:00
parent 482027cde1
commit cc63905509
1 changed files with 12 additions and 2 deletions

View File

@ -19,17 +19,27 @@ class Bitpay_Core_IpnController extends Mage_Core_Controller_Front_Action
*/ */
public function indexAction() public function indexAction()
{ {
if (!ini_get('allow_url_fopen')) {
ini_set('allow_url_fopen', true);
}
$raw_post_data = file_get_contents('php://input');
if ($raw_post_data === false) {
throw new Exception('Could not read from the php://input stream or invalid Bitpay IPN received.');
}
Mage::helper('bitpay')->registerAutoloader(); Mage::helper('bitpay')->registerAutoloader();
Mage::helper('bitpay')->debugData( Mage::helper('bitpay')->debugData(
array( array(
sprintf('Incoming IPN from bitpay'), sprintf('Incoming IPN from bitpay'),
getallheaders(), getallheaders(),
file_get_contents('php://input'), $raw_post_data,
) )
); );
// Magento doesn't seem to have a way to get the Request body // Magento doesn't seem to have a way to get the Request body
$ipn = json_decode(file_get_contents('php://input')); $ipn = json_decode($raw_post_data);
$ipn->posData = is_string($ipn->posData) ? json_decode($ipn->posData) : $ipn->posData; $ipn->posData = is_string($ipn->posData) ? json_decode($ipn->posData) : $ipn->posData;
$ipn->buyerFields = isset($ipn->buyerFields) ? $ipn->buyerFields : new stdClass(); $ipn->buyerFields = isset($ipn->buyerFields) ? $ipn->buyerFields : new stdClass();
Mage::helper('bitpay')->debugData($ipn); Mage::helper('bitpay')->debugData($ipn);