/** * Process the payment and return the result * * @param int $order_id * @return array */ public function process_payment($order_id) { $this->log(' [Info] Entered process_payment() with order_id = ' . $order_id . '...'); if (true === empty($order_id)) { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but the order_id was missing.'); throw new \Exception('The Bitpay payment plugin was called to process a payment but the order_id was missing. Cannot continue!'); } $order = wc_get_order($order_id); if (false === $order) { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but could not retrieve the order details for order_id ' . $order_id); throw new \Exception('The Bitpay payment plugin was called to process a payment but could not retrieve the order details for order_id ' . $order_id . '. Cannot continue!'); } $this->log(' [Info] Generating payment form for order ' . $order->get_order_number() . '. Notify URL: ' . $this->notification_url); // Mark new order according to user settings (we're awaiting the payment) $new_order_states = $this->get_option('order_states'); $new_order_status = $new_order_states['new']; $order->update_status($new_order_status, 'Awaiting payment notification from BitPay.'); $thanks_link = $this->get_return_url($order); $this->log(' [Info] The variable thanks_link = ' . $thanks_link . '...'); // Redirect URL & Notification URL $redirect_url = $this->get_option('redirect_url', $thanks_link); $this->log(' [Info] The variable redirect_url = ' . $redirect_url . '...'); $notification_url = $this->get_option('notification_url', WC()->api_request_url('WC_Gateway_Bitpay')); $this->log(' [Info] Notification URL is now set to: ' . $notification_url . '...'); // Setup the currency $currency_code = get_woocommerce_currency(); $this->log(' [Info] The variable currency_code = ' . $currency_code . '...'); $currency = new \Bitpay\Currency($currency_code); if (false === isset($currency) && true === empty($currency)) { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but could not instantiate a Currency object.'); throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate a Currency object. Cannot continue!'); } // Get a BitPay Client to prepare for invoice creation $client = new \Bitpay\Client\Client(); if (false === isset($client) && true === empty($client)) { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but could not instantiate a client object.'); throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate a client object. Cannot continue!'); } if ('livenet' === $this->api_network) { $client->setNetwork(new \Bitpay\Network\Livenet()); $this->log(' [Info] Set network to Livenet...'); } else { $client->setNetwork(new \Bitpay\Network\Testnet()); $this->log(' [Info] Set network to Testnet...'); } $curlAdapter = new \Bitpay\Client\Adapter\CurlAdapter(); if (false === isset($curlAdapter) || true === empty($curlAdapter)) { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but could not instantiate a CurlAdapter object.'); throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate a CurlAdapter object. Cannot continue!'); } $client->setAdapter($curlAdapter); if (false === empty($this->api_key)) { $client->setPrivateKey($this->api_key); } else { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but could not set client->setPrivateKey to this->api_key. The empty() check failed!'); throw new \Exception(' The Bitpay payment plugin was called to process a payment but could not set client->setPrivateKey to this->api_key. The empty() check failed!'); } if (false === empty($this->api_pub)) { $client->setPublicKey($this->api_pub); } else { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but could not set client->setPublicKey to this->api_pub. The empty() check failed!'); throw new \Exception(' The Bitpay payment plugin was called to process a payment but could not set client->setPublicKey to this->api_pub. The empty() check failed!'); } if (false === empty($this->api_token)) { $client->setToken($this->api_token); } else { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but could not set client->setToken to this->api_token. The empty() check failed!'); throw new \Exception(' The Bitpay payment plugin was called to process a payment but could not set client->setToken to this->api_token. The empty() check failed!'); } $this->log(' [Info] Key and token empty checks passed. Parameters in client set accordingly...'); // Setup the Invoice $invoice = new \Bitpay\Invoice(); if (false === isset($invoice) || true === empty($invoice)) { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but could not instantiate an Invoice object.'); throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate an Invoice object. Cannot continue!'); } else { $this->log(' [Info] Invoice object created successfully...'); } $order_number = $order->get_order_number(); $invoice->setOrderId((string) $order_number); $invoice->setCurrency($currency); $invoice->setFullNotifications(true); // Add a priced item to the invoice $item = new \Bitpay\Item(); if (false === isset($item) || true === empty($item)) { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but could not instantiate an item object.'); throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate an item object. Cannot continue!'); } else { $this->log(' [Info] Item object created successfully...'); } if (true === isset($order->order_total) && false === empty($order->order_total)) { $item->setPrice($order->order_total); } else { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but could not set item->setPrice to order->order_total. The empty() check failed!'); throw new \Exception('The Bitpay payment plugin was called to process a payment but could not set item->setPrice to order->order_total. The empty() check failed!'); } $invoice->setItem($item); // Add the Redirect and Notification URLs $invoice->setRedirectUrl($redirect_url); $invoice->setNotificationUrl($notification_url); $invoice->setTransactionSpeed($this->transaction_speed); try { $this->log(' [Info] Attempting to generate invoice for ' . $order->get_order_number() . '...'); $invoice = $client->createInvoice($invoice); if (false === isset($invoice) || true === empty($invoice)) { $this->log(' [Error] The Bitpay payment plugin was called to process a payment but could not instantiate an invoice object.'); throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate an invoice object. Cannot continue!'); } else { $this->log(' [Info] Call to generate invoice was successful: ' . $client->getResponse()->getBody()); } } catch (\Exception $e) { $this->log(' [Error] Error generating invoice for ' . $order->get_order_number() . ', "' . $e->getMessage() . '"'); error_log($e->getMessage()); return array('result' => 'success', 'messages' => 'Sorry, but Bitcoin checkout with BitPay does not appear to be working.'); } // Reduce stock levels $order->reduce_order_stock(); // Remove cart WC()->cart->empty_cart(); $this->log(' [Info] Leaving process_payment()...'); // Redirect the customer to the BitPay invoice return array('result' => 'success', 'redirect' => $invoice->getUrl()); }
/** * * 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); } }
* 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; echo (string) $response . PHP_EOL . PHP_EOL; /** * NOTE: The `(string)` is include so that the objects are converted to a * user friendly string. */ exit(1); // We do not want to continue if something went wrong } /** * You will need to persist the token somewhere, by the time you get to this