Skip to content
This repository was archived by the owner on Jan 22, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/SetCurlOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

//When running bitpay on your local server
$network = new Bitpay\Network\Customnet("alex.bp", 8088, true);

//Customize the curl options
$curl_options = array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
);

// If nothing is passed into the CurlAdapter
// then default values are used
$adapter = new Bitpay\Client\Adapter\CurlAdapter($curl_options);
74 changes: 57 additions & 17 deletions src/Bitpay/Client/Adapter/CurlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,45 @@
*/
class CurlAdapter implements AdapterInterface
{
/**
* @var array
*/
protected $curlOptions;

/**
* @param array $curlOptions
*/
public function __construct(array $curlOptions = array())
{
$this->curlOptions = $curlOptions;
}

/**
* Returns an array of curl settings to use
*
* @return array
*/
public function getCurlOptions()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc block comments

{
return $this->curlOptions;
}

/**
* @inheritdoc
*/
public function sendRequest(RequestInterface $request)
{
$curl = curl_init();
curl_setopt_array(
$curl,
array(
CURLOPT_URL => $request->getUri(),
CURLOPT_PORT => 443,
CURLOPT_CUSTOMREQUEST => $request->getMethod(),
CURLOPT_HTTPHEADER => $request->getHeaderFields(),
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => 1,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CAINFO => __DIR__ . '/ca-bundle.crt',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_HEADER => true,
)
);

$default_curl_options = $this->getCurlDefaultOptions($request);

foreach ($this->getCurlOptions() as $curl_option_key => $curl_option_value) {
if (!is_null($curl_option_value)) {
$default_curl_options[$curl_option_key] = $curl_option_value;
}
}

curl_setopt_array($curl, $default_curl_options);

if (RequestInterface::METHOD_POST == $request->getMethod()) {
curl_setopt_array(
Expand All @@ -68,4 +84,28 @@ public function sendRequest(RequestInterface $request)

return $response;
}

/**
* Returns an array of default curl settings to use
*
* @param RequestInterface $request
* @return array
*/
private function getCurlDefaultOptions(RequestInterface $request)
{
return array(
CURLOPT_URL => $request->getUri(),
CURLOPT_PORT => $request->getPort(),
CURLOPT_CUSTOMREQUEST => $request->getMethod(),
CURLOPT_HTTPHEADER => $request->getHeaderFields(),
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => 1,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CAINFO => __DIR__.'/ca-bundle.crt',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_HEADER => true,
);
}
}
11 changes: 10 additions & 1 deletion src/Bitpay/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,17 @@ protected function addSignatureHeader(RequestInterface $request)
throw new \Exception('Please set your Private Key');
}

if (isset($this->network->isPortRequiredInUrl)) {
if ($this->network->isPortRequiredInUrl === true) {
$url = $request->getUriWithPort();
}
} else {
$url = $request->getUri();
}

$message = sprintf(
'%s%s',
$request->getUri(),
$url,
$request->getBody()
);

Expand All @@ -608,6 +616,7 @@ protected function createNewRequest()
{
$request = new Request();
$request->setHost($this->network->getApiHost());
$request->setPort($this->network->getApiPort());
$this->prepareRequestHeaders($request);

return $request;
Expand Down
21 changes: 20 additions & 1 deletion src/Bitpay/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class Request implements RequestInterface
*/
protected $path;

/**
* Default is 443 but should be changed by whatever is passed in through the Adapter.
*
* @var integer
*/
protected $port;

/**
*/
public function __construct()
Expand All @@ -61,6 +68,8 @@ public function __construct()
'X-BitPay-Plugin-Info' => null,
);

$this->port = 443;

// Default method is POST
$this->method = self::METHOD_POST;
}
Expand Down Expand Up @@ -93,7 +102,17 @@ public function isMethod($method)
*/
public function getPort()
{
return 443;
return $this->port;
}

/**
* This is called in the Adapter
*
* @inheritdoc
*/
public function setPort($port)
{
$this->port = $port;
}

/**
Expand Down
47 changes: 47 additions & 0 deletions src/Bitpay/Network/Customnet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @license Copyright 2011-2014 BitPay Inc., MIT License
* see https://github.com/bitpay/php-bitpay-client/blob/master/LICENSE
*/

namespace Bitpay\Network;

/**
*
* @package Bitcore
*/
class Customnet implements NetworkInterface
{
protected $host_url;

protected $host_port;

public $isPortRequiredInUrl;

public function __construct($url, $port, $isPortRequiredInUrl = false)
{
$this->host_url = $url;
$this->host_port = $port;
$this->isPortRequiredInUrl = $isPortRequiredInUrl;
}

public function getName()
{
return 'Custom Network';
}

public function getAddressVersion()
{
return 0x00;
}

public function getApiHost()
{
return $this->host_url;
}

public function getApiPort()
{
return $this->host_port;
}
}
5 changes: 5 additions & 0 deletions src/Bitpay/Network/Livenet.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public function getApiHost()
{
return 'bitpay.com';
}

public function getApiPort()
{
return 443;
}
}
7 changes: 7 additions & 0 deletions src/Bitpay/Network/NetworkInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ public function getAddressVersion();
* @return string
*/
public function getApiHost();

/**
* The port of the host
*
* @return integer
*/
public function getApiPort();
}
5 changes: 5 additions & 0 deletions src/Bitpay/Network/Testnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public function getApiHost()
{
return 'test.bitpay.com';
}

public function getApiPort()
{
return 443;
}
}
62 changes: 62 additions & 0 deletions tests/Bitpay/Client/Adapter/CurlAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @license Copyright 2011-2014 BitPay Inc., MIT License
* see https://github.com/bitpay/php-bitpay-client/blob/master/LICENSE
*/

namespace Bitpay\Client\Adapter;

use Bitpay\Client\Request;

class CurlAdapterTest extends \PHPUnit_Framework_TestCase
{
protected $request;

public function setUp()
{
$this->request = new Request();
}

public function testConstruct()
{
$adapter = new CurlAdapter();
$this->assertNotNull($adapter->getCurlOptions());
}

public function testGetCurlOptions()
{
$adapter = new CurlAdapter();
$this->assertEquals(array(), $adapter->getCurlOptions());
}

public function testSendRequestWithException()
{
$http = $this->getMock('HttpRequest');
$this->setExpectedException('Exception');

$curl_options = array(
CURLOPT_URL => "www.example.com",
CURLOPT_SSL_VERIFYPEER => 1,
CURLOPT_SSL_VERIFYHOST => 2,
);

$adapter = new CurlAdapter($curl_options);
$response = $adapter->sendRequest($this->request);
}

public function testSendRequestWithoutException()
{
$http = $this->getMock('HttpRequest');

$curl_options = array(
CURLOPT_URL => "www.bitpay.com",
CURLOPT_SSL_VERIFYPEER => 1,
CURLOPT_SSL_VERIFYHOST => 2,
);

$adapter = new CurlAdapter($curl_options);
$response = $adapter->sendRequest($this->request);
$this->assertNotNull($response);
}

}
10 changes: 10 additions & 0 deletions tests/Bitpay/Client/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,21 @@ public function testIsMethodUnknown()
$this->assertFalse($this->request->isMethod('unknown method'));
}

/**
* @depends testGetPort
*/
public function testGetPort()
{
$this->assertSame(443, $this->request->getPort());
}

public function testSetPort()
{
$this->request->setPort(444);
$this->assertSame(444, $this->request->getPort());
}


public function testGetSchema()
{
$this->assertSame('https', $this->request->getSchema());
Expand Down