/** * @When /^the user creates an invoice for "([^"]*)" "([^"]*)"$/ */ public function theUserCreatesAnInvoiceFor($price, $currency) { try { // Load keys list($privateKey, $publicKey, $token_id) = loadKeys(); $network = $this->network; $client = createClient($network, $privateKey, $publicKey); $token = new \Bitpay\Token(); $token->setToken($token_id); $client->setToken($token); $invoice = new \Bitpay\Invoice(); $item = new \Bitpay\Item(); $item->setCode('skuNumber')->setDescription('General Description of Item')->setPrice($price); $invoice->setItem($item); $invoice->setCurrency(new \Bitpay\Currency($currency)); $client->createInvoice($invoice); $this->response = $client->getResponse(); $body = $this->response->getBody(); $json = json_decode($body, true); $this->invoiceId = $json['data']['id']; } catch (\Exception $e) { $this->error = $e; } finally { return true; } }
/** * * 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); } }
); echo 'Token Obtained "'.$token->getToken().'"'.PHP_EOL; //exit(0); */ /** * If you already have a token, uncomment this code * */ $token = new \Bitpay\Token(); $token->setToken($tokenString); /** * Code that makes the invoice */ $invoice = new \Bitpay\Invoice(); $item = new \Bitpay\Item(); $item->setCode('skuNumber')->setDescription('General Description of Item')->setPrice('1.99'); $invoice->setCurrency(new \Bitpay\Currency('USD')); $invoice->setItem($item); $client = $bitpay->get('client'); $client->setToken($token); try { $client->createInvoice($invoice); } catch (\Exception $e) { $request = $client->getRequest(); $response = $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;