function ajax_bitpay_pair_code()
 {
     $nonce = $_POST['pairNonce'];
     if (!wp_verify_nonce($nonce, 'bitpay-pair-nonce')) {
         die('Unauthorized!');
     }
     if (current_user_can('manage_options')) {
         if (true === isset($_POST['pairing_code']) && trim($_POST['pairing_code']) !== '') {
             // Validate the Pairing Code
             $pairing_code = trim($_POST['pairing_code']);
         } else {
             wp_send_json_error("Pairing Code is required");
             return;
         }
         if (!preg_match('/^[a-zA-Z0-9]{7}$/', $pairing_code)) {
             wp_send_json_error("Invalid Pairing Code");
             return;
         }
         // Validate the Network
         $network = $_POST['network'] === 'livenet' ? 'livenet' : 'testnet';
         // Generate Private Key
         $key = new \Bitpay\PrivateKey();
         if (true === empty($key)) {
             throw new \Exception('The Bitpay payment plugin was called to process a pairing code but could not instantiate a PrivateKey object. Cannot continue!');
         }
         $key->generate();
         // Generate Public Key
         $pub = new \Bitpay\PublicKey();
         if (true === empty($pub)) {
             throw new \Exception('The Bitpay payment plugin was called to process a pairing code but could not instantiate a PublicKey object. Cannot continue!');
         }
         $pub->setPrivateKey($key);
         $pub->generate();
         // Get SIN Format
         $sin = new \Bitpay\SinKey();
         if (true === empty($sin)) {
             throw new \Exception('The Bitpay payment plugin was called to process a pairing code but could not instantiate a SinKey object. Cannot continue!');
         }
         $sin->setPublicKey($pub);
         $sin->generate();
         // Create an API Client
         $client = new \Bitpay\Client\Client();
         if (true === empty($client)) {
             throw new \Exception('The Bitpay payment plugin was called to process a pairing code but could not instantiate a Client object. Cannot continue!');
         }
         if ($network === 'livenet') {
             $client->setNetwork(new \Bitpay\Network\Livenet());
         } else {
             $client->setNetwork(new \Bitpay\Network\Testnet());
         }
         $curlAdapter = new \Bitpay\Client\Adapter\CurlAdapter();
         if (true === empty($curlAdapter)) {
             throw new \Exception('The Bitpay payment plugin was called to process a pairing code but could not instantiate a CurlAdapter object. Cannot continue!');
         }
         $client->setAdapter($curlAdapter);
         $client->setPrivateKey($key);
         $client->setPublicKey($pub);
         // Sanitize label
         $label = preg_replace('/[^a-zA-Z0-9 \\-\\_\\.]/', '', get_bloginfo());
         $label = substr('WooCommerce - ' . $label, 0, 59);
         try {
             $token = $client->createToken(array('id' => (string) $sin, 'pairingCode' => $pairing_code, 'label' => $label));
         } catch (\Exception $e) {
             wp_send_json_error($e->getMessage());
             return;
         }
         update_option('woocommerce_bitpay_key', bitpay_encrypt($key));
         update_option('woocommerce_bitpay_pub', bitpay_encrypt($pub));
         update_option('woocommerce_bitpay_sin', (string) $sin);
         update_option('woocommerce_bitpay_token', bitpay_encrypt($token));
         update_option('woocommerce_bitpay_label', $label);
         update_option('woocommerce_bitpay_network', $network);
         wp_send_json(array('sin' => (string) $sin, 'label' => $label, 'network' => $network));
     }
     exit;
 }
 /**
  *
  *  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);
     }
 }
Example #3
0
$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
     * wrong, but for right now, we will just var_dump them.
     */
    echo (string) $request . PHP_EOL . PHP_EOL . PHP_EOL;