<commit_before><?php
/*
©2011 BIT-PAY LLC.
Permission is hereby granted to any person obtaining a copy of this software
and associated documentation for use and/or modification in association with
the bit-pay.com service.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

class ControllerPaymentBitpay extends Controller {

    // below is the url that can take you do the order information
    // http://127.0.0.1/~spair/store/index.php?route=account/order/info&order_id=35

    private $payment_module_name  = 'bitpay';
	protected function index() {
        $this->language->load('payment/'.$this->payment_module_name);
    	$this->data['button_bitpay_confirm'] = $this->language->get('button_bitpay_confirm');

		$this->data['continue'] = $this->url->link('checkout/success');
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/bitpay.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/payment/bitpay.tpl';
		} else {
			$this->template = 'default/template/payment/bitpay.tpl';
		}	
		
		$this->render();
	}
	
	function log($contents){
		$file = DIR_APPLICATION.'../bitpay/log.txt';
		file_put_contents($file, date('m-d H:i:s').": \n", FILE_APPEND);
		if (is_array($contents))
			foreach($contents as $k => $v)
				file_put_contents($file, $k.': '.$v."\n", FILE_APPEND);
		else
			file_put_contents($file, $contents."\n", FILE_APPEND);
	}

    public function send() {
		require DIR_APPLICATION.'../bitpay/bp_lib.php';
		
        $this->load->model('checkout/order');
        $order = $this->model_checkout_order->getOrder($this->session->data['order_id']);
        $price = $this->currency->format($order['total'], $order['currency_code'], $order['currency_value'], false);
        $posData = $order['order_id'];

        $options = array(
			'apiKey' => $this->config->get($this->payment_module_name.'_api_key'),
            'notificationURL' => $this->url->link('payment/bitpay/callback'),
            'redirectURL' => $this->url->link('account/order/info&order_id=' . $order['order_id']),
            'currency' => $order['currency_code'],
            'physical' => 'true',
            'fullNotifications' => 'true',
            'transactionSpeed' => $this->config->get($this->payment_module_name.'_transaction_speed')
        );
        $response = bpCreateInvoice($order['order_id'], $price, $posData, $options);
		
        if(array_key_exists('error', $response)) {
            $this->log("communication error");
			$this->log($response['error']);
            echo "{\"error\": \"Error: Problem communicating with payment provider.\\nPlease try again later.\"}";
        } else {
            echo "{\"url\": \"" . $response["url"] . "\"}";<commit_msg>Fix to use server error handling<commit_after><?php
/*
©2011 BIT-PAY LLC.
Permission is hereby granted to any person obtaining a copy of this software
and associated documentation for use and/or modification in association with
the bit-pay.com service.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

class ControllerPaymentBitpay extends Controller {

    // below is the url that can take you do the order information
    // http://127.0.0.1/~spair/store/index.php?route=account/order/info&order_id=35

    private $payment_module_name  = 'bitpay';
	protected function index() {
        $this->language->load('payment/'.$this->payment_module_name);
    	$this->data['button_bitpay_confirm'] = $this->language->get('button_bitpay_confirm');

		$this->data['continue'] = $this->url->link('checkout/success');
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/bitpay.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/payment/bitpay.tpl';
		} else {
			$this->template = 'default/template/payment/bitpay.tpl';
		}	
		
		$this->render();
	}
	
	function log($contents){
		error_log($contents);
	}

    public function send() {
		require DIR_APPLICATION.'../bitpay/bp_lib.php';
		
        $this->load->model('checkout/order');
        $order = $this->model_checkout_order->getOrder($this->session->data['order_id']);
        $price = $this->currency->format($order['total'], $order['currency_code'], $order['currency_value'], false);
        $posData = $order['order_id'];

        $options = array(
			'apiKey' => $this->config->get($this->payment_module_name.'_api_key'),
            'notificationURL' => $this->url->link('payment/bitpay/callback'),
            'redirectURL' => $this->url->link('account/order/info&order_id=' . $order['order_id']),
            'currency' => $order['currency_code'],
            'physical' => 'true',
            'fullNotifications' => 'true',
            'transactionSpeed' => $this->config->get($this->payment_module_name.'_transaction_speed')
        );
        $response = bpCreateInvoice($order['order_id'], $price, $posData, $options);
		
        if(array_key_exists('error', $response)) {
            $this->log("communication error");
			$this->log($response['error']);
            echo "{\"error\": \"Error: Problem communicating with payment provider.\\nPlease try again later.\"}";
        } else {
            echo "{\"url\": \"" . $response["url"] . "\"}";
{'fraction_non_alphanumeric': 0.16095947063688998, 'fraction_numerical': 0.003970223325062035, 'mean_word_length': 3.6904577191621413, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6335864', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1773, 'n_words': 435}