Merge pull request #20 from ionux/master

Corrected company name, license and formatting
This commit is contained in:
Ryan X. Charles 2014-01-29 09:59:41 -08:00
commit 70ee93bbff
11 changed files with 728 additions and 556 deletions

View File

@ -1,4 +1,5 @@
©2011 BIT-PAY LLC.
©2011-2014 BITPAY, INC.
Permission is hereby granted to any person obtaining a copy of this software
and associated documentation for use and/or modification in association with
the bitpay.com service.

View File

@ -1,24 +1,38 @@
<?php
class Bitpay_Bitcoins_Block_Iframe extends Mage_Checkout_Block_Onepage_Payment
{
protected function _construct()
{
/**
* ©2011,2012,2013,2014 BITPAY, INC.
*
* Permission is hereby granted to any person obtaining a copy of this software
* and associated documentation for use and/or modification in association with
* the bitpay.com service.
*
* 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.
*
* Bitcoin payment plugin using the bitpay.com service.
*
*/
class Bitpay_Bitcoins_Block_Iframe extends Mage_Checkout_Block_Onepage_Payment {
protected function _construct() {
$this->setTemplate('bitcoins/iframe.phtml');
parent::_construct();
}
public function GetQuoteId()
{
public function GetQuoteId() {
$quote = $this->getQuote();
$quoteId = $quote->getId();
return $quoteId;
}
// create an invoice and return the url so that iframe.phtml can display it
public function GetIframeUrl()
{
public function GetIframeUrl() {
// are they using bitpay?
if (!($quote = Mage::getSingleton('checkout/session')->getQuote())
or !($payment = $quote->getPayment())
@ -37,9 +51,11 @@ class Bitpay_Bitcoins_Block_Iframe extends Mage_Checkout_Block_Onepage_Payment
$quote = $this->getQuote();
$quoteId = $quote->getId();
if (Mage::getModel('Bitcoins/ipn')->GetQuotePaid($quoteId))
return 'paid'; // quote's already paid, so don't show the iframe
$options = array(
'currency' => $quote->getQuoteCurrencyCode(),
'fullNotifications' => 'true',
@ -60,11 +76,12 @@ class Bitpay_Bitcoins_Block_Iframe extends Mage_Checkout_Block_Onepage_Payment
$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))
{
if (array_key_exists('error', $invoice)) {
Mage::log('Error creating bitpay invoice', null, 'bitpay.log');
Mage::log($invoice['error'], null, 'bitpay.log');
Mage::throwException("Error creating bit-pay invoice. Please try again or use another payment option.");
@ -73,4 +90,5 @@ class Bitpay_Bitcoins_Block_Iframe extends Mage_Checkout_Block_Onepage_Payment
return $invoice['url'].'&view=iframe';
}
}

View File

@ -1,15 +1,32 @@
<?php
class Bitpay_Bitcoins_Model_Ipn extends Mage_Core_Model_Abstract
{
function _construct()
{
/**
* ©2011,2012,2013,2014 BITPAY, INC.
*
* Permission is hereby granted to any person obtaining a copy of this software
* and associated documentation for use and/or modification in association with
* the bitpay.com service.
*
* 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.
*
* Bitcoin payment plugin using the bitpay.com service.
*
*/
class Bitpay_Bitcoins_Model_Ipn extends Mage_Core_Model_Abstract {
function _construct() {
$this->_init('Bitcoins/ipn');
return parent::_construct();
}
function Record($invoice)
{
function Record($invoice) {
return $this
->setQuoteId(isset($invoice['posData']['quoteId']) ? $invoice['posData']['quoteId'] : NULL)
->setOrderId(isset($invoice['posData']['orderId']) ? $invoice['posData']['orderId'] : NULL)
@ -26,32 +43,31 @@ class Bitpay_Bitcoins_Model_Ipn extends Mage_Core_Model_Abstract
->save();
}
function GetStatusReceived($quoteId, $statuses)
{
function GetStatusReceived($quoteId, $statuses) {
if (!$quoteId)
return false;
$quote = Mage::getModel('sales/quote')->load($quoteId, 'entity_id');
if (!$quote)
{
if (!$quote) {
Mage::log('quote not found', NULL, 'bitpay.log');
return false;
}
$quoteHash = Mage::getModel('Bitcoins/paymentMethod')->getQuoteHash($quoteId);
if (!$quoteHash)
{
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))
{
foreach($collection as $i) {
if (in_array($i->getStatus(), $statuses)) {
// check that quote data was not updated after IPN sent
$posData = json_decode($i->getPosData());
if (!$posData)
continue;
@ -63,18 +79,14 @@ class Bitpay_Bitcoins_Model_Ipn extends Mage_Core_Model_Abstract
return false;
}
function GetQuotePaid($quoteId)
{
function GetQuotePaid($quoteId) {
return $this->GetStatusReceived($quoteId, array('paid', 'confirmed', 'complete'));
}
function GetQuoteComplete($quoteId)
{
function GetQuoteComplete($quoteId) {
return $this->GetStatusReceived($quoteId, array('confirmed', 'complete'));
}
}
?>

View File

@ -1,10 +1,28 @@
<?php
/**
* Our test CC module adapter
*/
class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract
{
* ©2011,2012,2013,2014 BITPAY, INC.
*
* Permission is hereby granted to any person obtaining a copy of this software
* and associated documentation for use and/or modification in association with
* the bitpay.com service.
*
* 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.
*
* Bitcoin payment plugin using the bitpay.com service.
*
*/
/**
* Our test CC module adapter
*/
class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract {
/**
* unique internal payment method identifier
*
@ -75,26 +93,23 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
//protected $_formBlockType = 'bitcoins/form';
//protected $_infoBlockType = 'bitcoins/info';
function canUseForCurrency($currencyCode)
{
function canUseForCurrency($currencyCode) {
$currencies = Mage::getStoreConfig('payment/Bitcoins/currencies');
$currencies = array_map('trim', explode(',', $currencies));
return array_search($currencyCode, $currencies) !== false;
}
public function canUseCheckout()
{
public function canUseCheckout() {
$secret = Mage::getStoreConfig('payment/Bitcoins/api_key');
if (!$secret or !strlen($secret))
{
if (!$secret or !strlen($secret)) {
Mage::log('Bitpay/Bitcoins: API key not entered', null, 'bitpay.log');
return false;
}
$speed = Mage::getStoreConfig('payment/Bitcoins/speed');
if (!$speed or !strlen($speed))
{
if (!$speed or !strlen($speed)) {
Mage::log('Bitpay/Bitcoins: Transaction Speed invalid', null, 'bitpay.log');
return false;
}
@ -102,41 +117,33 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
return $this->_canUseCheckout;
}
public function authorize(Varien_Object $payment, $amount)
{
public function authorize(Varien_Object $payment, $amount) {
if (!Mage::getStoreConfig('payment/Bitcoins/fullscreen'))
return $this->CheckForPayment($payment);
else
return $this->CreateInvoiceAndRedirect($payment, $amount);
}
function CheckForPayment($payment)
{
function CheckForPayment($payment) {
$quoteId = $payment->getOrder()->getQuoteId();
$ipn = Mage::getModel('Bitcoins/ipn');
if (!$ipn->GetQuotePaid($quoteId))
{
if (!$ipn->GetQuotePaid($quoteId)) {
Mage::throwException("Order not paid for. Please pay first and then Place your Order.");
}
else if (!$ipn->GetQuoteComplete($quoteId))
{
} else if (!$ipn->GetQuoteComplete($quoteId)) {
// order status will be PAYMENT_REVIEW instead of PROCESSING
$payment->setIsTransactionPending(true);
}
else
{
} else {
$this->MarkOrderPaid($payment->getOrder());
}
return $this;
}
function MarkOrderPaid($order)
{
function MarkOrderPaid($order) {
$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')
@ -146,17 +153,19 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
}
}
// given Mage_Core_Model_Abstract, return api-friendly address
function ExtractAddress($address)
{
function ExtractAddress($address) {
$options = array();
$options['buyerName'] = $address->getName();
if ($address->getCompany())
$options['buyerName'] = $options['buyerName'].' c/o '.$address->getCompany();
$options['buyerAddress1'] = $address->getStreet1();
$options['buyerAddress2'] = $address->getStreet2();
$options['buyerAddress3'] = $address->getStreet3();
@ -167,21 +176,22 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
$options['buyerCountry'] = $address->getCountry();
$options['buyerEmail'] = $address->getEmail();
$options['buyerPhone'] = $address->getTelephone();
// trim to fit API specs
foreach(array('buyerName', 'buyerAddress1', 'buyerAddress2', 'buyerAddress3', 'buyerAddress4', 'buyerCity', 'buyerState', 'buyerZip', 'buyerCountry', 'buyerEmail', 'buyerPhone') as $f)
$options[$f] = substr($options[$f], 0, 100);
return $options;
}
function CreateInvoiceAndRedirect($payment, $amount)
{
function CreateInvoiceAndRedirect($payment, $amount) {
include Mage::getBaseDir('lib').'/bitpay/bp_lib.php';
$apiKey = Mage::getStoreConfig('payment/Bitcoins/api_key');
$speed = Mage::getStoreConfig('payment/Bitcoins/speed');
$order = $payment->getOrder();
$orderId = $order->getIncrementId();
$options = array(
'currency' => $order->getBaseCurrencyCode(),
'buyerName' => $order->getCustomerFirstname().' '.$order->getCustomerLastname(),
@ -191,20 +201,16 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
'transactionSpeed' => $speed,
'apiKey' => $apiKey,
);
$options += $this->ExtractAddress($order->getShippingAddress());
$invoice = bpCreateInvoice($orderId, $amount, array('orderId' => $orderId), $options);
$payment->setIsTransactionPending(true); // status will be PAYMENT_REVIEW instead of PROCESSING
if (array_key_exists('error', $invoice))
{
if (array_key_exists('error', $invoice)) {
Mage::log('Error creating bitpay invoice', null, 'bitpay.log');
Mage::log($invoice['error'], null, 'bitpay.log');
Mage::throwException("Error creating bit-pay invoice. Please try again or use another payment option.");
}
else
{
} else {
$invoiceId = Mage::getModel('sales/order_invoice_api')->create($orderId, array());
Mage::getSingleton('customer/session')->setRedirectUrl($invoice['url']);
}
@ -212,33 +218,29 @@ class Bitpay_Bitcoins_Model_PaymentMethod extends Mage_Payment_Model_Method_Abst
return $this;
}
public function getOrderPlaceRedirectUrl()
{
public function getOrderPlaceRedirectUrl() {
if (Mage::getStoreConfig('payment/Bitcoins/fullscreen'))
return Mage::getSingleton('customer/session')->getRedirectUrl();
else
return '';
}
# computes a unique hash determined by the contents of the cart
public function getQuoteHash($quoteId)
{
// 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)
{
if (!$quote) {
Mage::log('getQuoteTimestamp: quote not found', NULL, 'bitpay.log');
return false;
}
#encode items
// 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
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));

View File

@ -1,9 +1,26 @@
<?php
class Bitpay_Bitcoins_Model_Resource_Ipn extends Mage_Core_Model_Resource_Db_Abstract
{
protected function _construct()
{
/**
* ©2011,2012,2013,2014 BITPAY, INC.
*
* Permission is hereby granted to any person obtaining a copy of this software
* and associated documentation for use and/or modification in association with
* the bitpay.com service.
*
* 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.
*
* Bitcoin payment plugin using the bitpay.com service.
*
*/
class Bitpay_Bitcoins_Model_Resource_Ipn extends Mage_Core_Model_Resource_Db_Abstract {
protected function _construct() {
$this->_init('Bitcoins/ipn', 'id');
}
}

View File

@ -1,9 +1,26 @@
<?php
class Bitpay_Bitcoins_Model_Resource_Ipn_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
protected function _construct()
{
/**
* ©2011,2012,2013,2014 BITPAY, INC.
*
* Permission is hereby granted to any person obtaining a copy of this software
* and associated documentation for use and/or modification in association with
* the bitpay.com service.
*
* 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.
*
* Bitcoin payment plugin using the bitpay.com service.
*
*/
class Bitpay_Bitcoins_Model_Resource_Ipn_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
protected function _construct() {
$this->_init('Bitcoins/ipn');
}
}

View File

@ -1,10 +1,26 @@
<?php
class Bitpay_Bitcoins_Model_Source_Speed
{
public function toOptionArray()
{
/**
* ©2011,2012,2013,2014 BITPAY, INC.
*
* Permission is hereby granted to any person obtaining a copy of this software
* and associated documentation for use and/or modification in association with
* the bitpay.com service.
*
* 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.
*
* Bitcoin payment plugin using the bitpay.com service.
*
*/
class Bitpay_Bitcoins_Model_Source_Speed {
public function toOptionArray() {
return array(
array(
'value' => 'low',

View File

@ -1,10 +1,28 @@
<?php
/**
* ©2011,2012,2013,2014 BITPAY, INC.
*
* Permission is hereby granted to any person obtaining a copy of this software
* and associated documentation for use and/or modification in association with
* the bitpay.com service.
*
* 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.
*
* Bitcoin payment plugin using the bitpay.com service.
*
*/
// callback controller
class Bitpay_Bitcoins_IndexController extends Mage_Core_Controller_Front_Action {
public function checkForPaymentAction()
{
public function checkForPaymentAction() {
$params = $this->getRequest()->getParams();
$quoteId = $params['quote'];
$paid = Mage::getModel('Bitcoins/ipn')->GetQuotePaid($quoteId);
@ -15,9 +33,7 @@ class Bitpay_Bitcoins_IndexController extends Mage_Core_Controller_Front_Action
// bitpay's IPN lands here
public function indexAction() {
require Mage::getBaseDir('lib').'/bitpay/bp_lib.php';
Mage::log(file_get_contents('php://input'), null, 'bitpay.log');
$apiKey = Mage::getStoreConfig('payment/Bitcoins/api_key');
$invoice = bpVerifyNotification($apiKey);
@ -28,8 +44,7 @@ class Bitpay_Bitcoins_IndexController extends Mage_Core_Controller_Front_Action
if (isset($invoice['posData']['quoteId'])) {
$quoteId = $invoice['posData']['quoteId'];
$order = Mage::getModel('sales/order')->load($quoteId, 'quote_id');
}
else {
} else {
$orderId = $invoice['posData']['orderId'];
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
}
@ -44,10 +59,11 @@ class Bitpay_Bitcoins_IndexController extends Mage_Core_Controller_Front_Action
case 'complete':
$method = Mage::getModel('Bitcoins/paymentMethod');
$method->MarkOrderPaid($order);
break;
}
}
}
}

View File

@ -1,15 +1,40 @@
<?php
/**
* ©2011,2012,2013,2014 BITPAY, INC.
*
* Permission is hereby granted to any person obtaining a copy of this software
* and associated documentation for use and/or modification in association with
* the bitpay.com service.
*
* 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.
*
* Bitcoin payment plugin using the bitpay.com service.
*
*/
global $bpconfig;
$bpconfig['host']="bitpay.com";
$bpconfig['port']=443;
$bpconfig['hostAndPort']=$bpconfig['host'];
if ($bpconfig['port']!=443)
$bpconfig['hostAndPort'].=":".$bpconfig['host'];
$bpconfig['ssl_verifypeer']=1;
$bpconfig['ssl_verifyhost']=2;
//include custom config overrides if it exists
try {
include 'bp_config.php';
} catch (Exception $e) {
// do nothing
}
?>

View File

@ -1,5 +1,24 @@
<?php
/**
* ©2011,2012,2013,2014 BITPAY, INC.
*
* Permission is hereby granted to any person obtaining a copy of this software
* and associated documentation for use and/or modification in association with
* the bitpay.com service.
*
* 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.
*
* Bitcoin payment plugin using the bitpay.com service.
*
*/
require_once 'bp_config_default.php';
require_once 'bp_options.php';
@ -8,8 +27,8 @@ function bpCurl($url, $apiKey, $post = false) {
$curl = curl_init($url);
$length = 0;
if ($post)
{
if ($post) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
$length = strlen($post);
@ -18,8 +37,8 @@ function bpCurl($url, $apiKey, $post = false) {
$uname = base64_encode($apiKey);
$header = array(
'Content-Type: application/json',
"Content-Length: $length",
"Authorization: Basic $uname",
'Content-Length: ' . $length,
'Authorization: Basic ' . $uname,
);
curl_setopt($curl, CURLOPT_PORT, $bpconfig['port']);
@ -41,9 +60,11 @@ function bpCurl($url, $apiKey, $post = false) {
if (!$response)
$response = array('error' => 'invalid json: '.$responseString);
}
curl_close($curl);
return $response;
}
// $orderId: Used to display an orderID to the buyer. In the account summary view, this value is used to
// identify a ledger entry if present.
//
@ -64,21 +85,23 @@ function bpCreateInvoice($orderId, $price, $posData, $options = array()) {
global $bpOptions, $bpconfig;
$options = array_merge($bpOptions, $options); // $options override any options found in bp_options.php
$pos = array('posData' => $posData);
if ($bpOptions['verifyPos'])
$pos['hash'] = crypt(serialize($posData), $options['apiKey']);
$options['posData'] = json_encode($pos);
$options['posData'] = json_encode($pos);
$options['orderID'] = $orderId;
$options['price'] = $price;
$postOptions = array('orderID', 'itemDesc', 'itemCode', 'notificationEmail', 'notificationURL', 'redirectURL',
'posData', 'price', 'currency', 'physical', 'fullNotifications', 'transactionSpeed', 'buyerName',
'buyerAddress1', 'buyerAddress2', 'buyerCity', 'buyerState', 'buyerZip', 'buyerEmail', 'buyerPhone');
foreach($postOptions as $o)
if (array_key_exists($o, $options))
$post[$o] = $options[$o];
$post = json_encode($post);
$response = bpCurl('https://'.$bpconfig['hostAndPort'].'/api/invoice/', $options['apiKey'], $post);
@ -89,10 +112,12 @@ function bpCreateInvoice($orderId, $price, $posData, $options = array()) {
// Call from your notification handler to convert $_POST data to an object containing invoice data
function bpVerifyNotification($apiKey = false) {
global $bpOptions, $bpconfig;
if (!$apiKey)
$apiKey = $bpOptions['apiKey'];
$post = file_get_contents("php://input");
if (!$post)
return 'No post data';
@ -105,8 +130,10 @@ function bpVerifyNotification($apiKey = false) {
return 'no posData';
$posData = json_decode($json['posData'], true);
if($bpOptions['verifyPos'] and $posData['hash'] != crypt(serialize($posData['posData']), $apiKey))
return 'authentication failed (bad hash)';
$json['posData'] = $posData['posData'];
return $json;
@ -115,17 +142,19 @@ function bpVerifyNotification($apiKey = false) {
// $options can include ('apiKey')
function bpGetInvoice($invoiceId, $apiKey=false) {
global $bpOptions, $bpconfig;
if (!$apiKey)
$apiKey = $bpOptions['apiKey'];
$response = bpCurl('https://'.$bpconfig['hostAndPort'].'/api/invoice/'.$invoiceId, $apiKey);
if (is_string($response))
return $response; // error
$response['posData'] = json_decode($response['posData'], true);
$response['posData'] = $response['posData']['posData'];
return $response;
}
?>

View File

@ -1,5 +1,24 @@
<?php
/**
* ©2011,2012,2013,2014 BITPAY, INC.
*
* Permission is hereby granted to any person obtaining a copy of this software
* and associated documentation for use and/or modification in association with
* the bitpay.com service.
*
* 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.
*
* Bitcoin payment plugin using the bitpay.com service.
*
*/
global $bpOptions;
// do not edit this file