Skip to content

Instantly share code, notes, and snippets.

@JoshuaEstes
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshuaEstes/b599eeaa7df8d4db3e44 to your computer and use it in GitHub Desktop.
Save JoshuaEstes/b599eeaa7df8d4db3e44 to your computer and use it in GitHub Desktop.
<?php
class BitPayClient
{
private $_client;
private $_network;
private $_adapter;
private $_storageEngine;
public function __construct($setup = false)
{
$this->_client = new Client();
if (\App::environment() == "production") {
$this->_network = new Livenet();
} else {
$this->_network = new Testnet();
}
$this->_adapter = new CurlAdapter();
$this->_storageEngine = new EncryptedFilesystemStorage(\Config::get('app.key'));
if ($setup == false) {
/**
* Assume keys have already been generated and persisted.
*/
list($privateKey, $publicKey) = $this->fetchKeys();
$this->_client->setPrivateKey($privateKey);
$this->_client->setPublicKey($publicKey);
$this->_client->setNetwork($this->_network);
$this->_client->setAdapter($this->_adapter);
// This may be the casue of most of your issues. You should
// first start with only one token (in this case BITPAY_KEY)
// at any given time.
$bitPayKey = \AppConfig::whereKey('BITPAY-KEY')->first();
if ($bitPayKey) {
$token = new Token();
$token->setToken($bitPayKey->value);
$this->_client->setToken($token);
} else {
throw new \Exception("BitPay Key not setup");
}
}
}
public function generateKeysAndPersist()
{
$privateKey = PrivateKey::create(storage_path('bitpay/private.key'))->generate();
$publicKey = new PublicKey(storage_path('bitpay/public.key'));
// Inject the private key into the public key
$publicKey->setPrivateKey($privateKey);
// Generate the public key
$publicKey->generate();
/**
* app.key is the password used to encrypt/decrypt keys. If this value changes,
* you will not be able to decrypt the keys that were generated.
*/
$this->_storageEngine->persist($privateKey);
$this->_storageEngine->persist($publicKey);
return array(
$privateKey,
$publicKey,
);
}
public function fetchKeys()
{
$privateKey = $this->_storageEngine->load(storage_path('bitpay/private.key'));
$publicKey = $this->_storageEngine->load(storage_path('bitpay/public.key'));
return array(
$privateKey,
$publicKey,
);
}
/**
* Run only once.
*/
public function createToken($pairing_code)
{
/**
* This might be causing issues since a token (in this case named BITPAY-KEY) can
* only be paired to one set of keys. If there is already a token set, keys will
* not be generated for a new token
*/
//$bitPayKey = \AppConfig::whereKey('BITPAY-KEY')->first();
//if (is_null($bitPayKey)) {
list($privateKey, $publicKey) = $this->fetchKeys();
$this->_client->setPrivateKey($privateKey);
$this->_client->setPublicKey($publicKey);
$this->_client->setNetwork($this->_network);
$this->_client->setAdapter($this->_adapter);
$sin = new SinKey();
$sin = $sin->setPublicKey($publicKey)->generate();
$token = $this->_client->createToken(
[
'id' => (string) $sin,
'pairingCode' => $pairing_code,
'label' => 'App Pairing Token',
]
);
\AppConfig::create([
'key' => 'BITPAY-KEY',
'value' => $token->getToken()
]);
//}
}
public function createInvoice()
{
$invoice = new Invoice();
$item = new Item();
$item->setCode('skuNumber');
$item->setDescription('General Description of Item');
$item->setPrice('1.99');
$invoice->setCurrency(new Currency('USD'));
try {
$this->_client->createInvoice($invoice);
} catch (\Exception $e) {
$request = $this->_client->getRequest();
$response = $this->_client->getResponse();
echo (string)$request . PHP_EOL . PHP_EOL . PHP_EOL;
echo (string)$response . PHP_EOL . PHP_EOL;
exit(1); // We do not want to continue if something went wrong
}
echo 'Invoice "' . $invoice->getId() . '" created, see ' . $invoice->getUrl() . PHP_EOL;
}
}
/**
* Need to generate and persist keys
*/
$c = new BitPayClient();
// Keys generated and persisted.
$c->generateKeysAndPersist();
/**
* Need to pair keys to a token
*/
$c->createToken('pairingCode');
/**
* Assume that everything above worked.
*/
$c = new BitPayClient(true);
$c->createInvoice();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment