/** * @return SinKey */ public function getSin() { if (empty($this->hex)) { $this->generate(); } if (null === $this->sin) { $this->sin = new SinKey(); $this->sin->setPublicKey($this); $this->sin->generate(); } return $this->sin; }
public function pairTokens($pairingCode) { $storageEngine = new Bitpay\Storage\FilesystemStorage(); if (_BIT_PAY_PRODUCTION_) { $privateKey = $storageEngine->load('/tmp/bitpay.pri'); $publicKey = $storageEngine->load('/tmp/bitpay.pub'); } else { $privateKey = $storageEngine->load('/tmp/bitpaydev.pri'); $publicKey = $storageEngine->load('/tmp/bitpaydev.pub'); } $sin = Bitpay\SinKey::create()->setPublicKey($publicKey)->generate(); $client = new Bitpay\Client\Client(); if (_BIT_PAY_PRODUCTION_) { $network = new Bitpay\Network\Livenet(); } else { $network = new Bitpay\Network\Testnet(); } $adapter = new Bitpay\Client\Adapter\CurlAdapter(); $client->setPrivateKey($privateKey); $client->setPublicKey($publicKey); $client->setNetwork($network); $client->setAdapter($adapter); $token = $client->createToken(array('pairingCode' => $pairingCode, 'label' => 'Auto-CMS', 'id' => (string) $sin)); $persistThisValue = $token->getToken(); return array('Token' => $persistThisValue); }
/** * * Method used by payment gateway. * * If this method return a \Thelia\Core\HttpFoundation\Response instance, this response is send to the * browser. * * In many cases, it's necessary to send a form to the payment gateway. On your response you can return this form already * completed, ready to be sent * * @param \Thelia\Model\Order $order processed order * @return null|\Thelia\Core\HttpFoundation\Response */ public function pay(Order $order) { $this->loadBitpayKeys(); $client = new \Bitpay\Client\Client(); $adapter = new \Bitpay\Client\Adapter\CurlAdapter(); $config = new BitpayPaymentsConfig(); $config->pushValues(); if ($config->getSandbox()) { $pairingKey = $config->getPairingKeySandbox(); $apiKey = $config->getApiKeySandbox(); $network = new \Bitpay\Network\Testnet(); $environment = "Sandbox"; } else { $pairingKey = $config->getPairingKey(); $apiKey = $config->getApiKey(); $network = new \Bitpay\Network\Livenet(); $environment = "Live"; } $client->setPrivateKey($this->privateKey); $client->setPublicKey($this->publicKey); $client->setNetwork($network); $client->setAdapter($adapter); if (!isset($apiKey) || $apiKey == '') { // must create API key if (!isset($pairingKey) || $pairingKey == '') { // error: no pairing key $error = "Thelia BitpayPayments error: No API key or pairing key for environment {$environment} provided."; Tlog::getInstance()->error($error); throw new \Exception($error); } else { // pairing key available, now trying to get an API key $sin = \Bitpay\SinKey::create()->setPublicKey($this->publicKey)->generate(); try { $token = $client->createToken(array('pairingCode' => $pairingKey, 'label' => 'Thelia BitpayPayments', 'id' => (string) $sin)); } catch (\Exception $e) { $request = $client->getRequest(); $response = $client->getResponse(); $error = 'Thelia BitpayPayments error:' . PHP_EOL . PHP_EOL . $request . PHP_EOL . PHP_EOL . $response . PHP_EOL . PHP_EOL; Tlog::getInstance()->error($error); throw new \Exception($error); } $config->setApiKeyCurrentEnvironment($token->getToken()); $config->setPairingKeyCurrentEnvironment(''); } } // token should be available now $token = new \Bitpay\Token(); $token->setToken($config->getApiKeyCurrentEnvironment()); $client->setToken($token); $invoice = new \Bitpay\Invoice(); $item = new \Bitpay\Item(); $item->setCode('testCode'); $item->setDescription('Purchase'); $item->setPrice($order->getTotalAmount()); $invoice->setItem($item); $invoice->setCurrency(new \Bitpay\Currency($order->getCurrency()->getCode())); try { $client->createInvoice($invoice); } catch (\Exception $e) { $request = $client->getRequest(); $response = $client->getResponse(); $error = 'Thelia BitpayPayments error:' . PHP_EOL . PHP_EOL . $request . PHP_EOL . PHP_EOL . $response . PHP_EOL . PHP_EOL; Tlog::getInstance()->error($error); throw new \Exception($error); } }
* Now all the objects are created and we can inject them into the client */ $client->setPrivateKey($privateKey); $client->setPublicKey($publicKey); $client->setNetwork($network); $client->setAdapter($adapter); /** * Visit https://test.bitpay.com/api-tokens and create a new pairing code. Pairing * codes can only be used once and the generated code is valid for only 24 hours. */ $pairingCode = 'InsertPairingCodeHere'; /** * Currently this part is required, however future versions of the PHP SDK will * be refactor and this part may become obsolete. */ $sin = \Bitpay\SinKey::create()->setPublicKey($publicKey)->generate(); /**** end ****/ try { $token = $client->createToken(array('pairingCode' => $pairingCode, 'label' => 'You can insert a label here', 'id' => (string) $sin)); } catch (\Exception $e) { /** * The code will throw an exception if anything goes wrong, if you did not * change the $pairingCode value or if you are trying to use a pairing * code that has already been used, you will get an exception. It was * decided that it makes more sense to allow your application to handle * this exception since each app is different and has different requirements. */ $request = $client->getRequest(); $response = $client->getResponse(); /** * You can use the entire request/response to help figure out what went