<commit_before><?php

/*
Plugin Name: WooCommerce Payment Gateway - CoinGate
Plugin URI: https://coingate.com
Description: Accept Bitcoin via CoinGate in your WooCommerce store
Version: 1.1.0
Author: CoinGate
Author URI: https://coingate.com
License: MIT License
License URI: https://github.com/coingate/woocommerce-plugin/blob/master/LICENSE
Github Plugin URI: https://github.com/coingate/woocommerce-plugin
*/

add_action('plugins_loaded', 'coingate_init');

define('COINGATE_WOOCOMMERCE_VERSION', '1.1.0');

function coingate_init()
{
  if (!class_exists('WC_Payment_Gateway')) {
    return;
  };

  define('PLUGIN_DIR', plugins_url(basename(plugin_dir_path(__FILE__)), basename(__FILE__)).'/');

  require_once(__DIR__.'/lib/coingate/init.php');

  class WC_Gateway_Coingate extends WC_Payment_Gateway
  {
    public function __construct()
    {
      global $woocommerce;

      $this->id = 'coingate';
      $this->has_fields = false;
      $this->method_title = 'CoinGate';
      $this->icon = apply_filters('woocommerce_coingate_icon', PLUGIN_DIR.'assets/bitcoin.png');

      $this->init_form_fields();
      $this->init_settings();

      $this->title = $this->get_option('title');
      $this->description = $this->get_option('description');
      $this->app_id = $this->get_option('app_id');
      $this->api_key = $this->get_option('api_key');
      $this->api_secret = $this->get_option('api_secret');
      $this->receive_currency = $this->get_option('receive_currency');
      $this->order_statuses = $this->get_option('order_statuses');
      $this->test = ('yes' === $this->get_option('test', 'no'));

      add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
      add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'save_order_statuses'));
      add_action('woocommerce_thankyou_coingate', array($this, 'thankyou'));
      add_action('woocommerce_api_wc_gateway_coingate', array($this, 'payment_callback'));
    }

    public function admin_options()
    {
      ?>
      <h3><?php _e('CoinGate', 'woothemes'); ?></h3>
      <p><?php _e('Accept Bitcoin through the CoinGate.com and receive payments in euros and US dollars.<br>
        <a href="https://developer.coingate.com/docs/issues" target="_blank">Not working? Common issues</a> &middot; <a href="mailto:<EMAIL>"><EMAIL></a>', 'woothemes'); ?></p>
      <table class="form-table">
        <?php $this->generate_settings_html(); ?>
      </table>
      <?php

    }

    public function init_form_fields()
    {
      $this->form_fields = array(
        'enabled' => array(
          'title' => __('Enable CoinGate', 'woocommerce'),
          'label' => __('Enable Bitcoin payment via CoinGate', 'woocommerce'),
          'type' => 'checkbox',
          'description' => '',
          'default' => 'no',
        ),
        'description' => array(
          'title' => __('Description', 'woocommerce'),
          'type' => 'textarea',
          'description' => __('This controls the description which the user sees during checkout.', 'woocommerce'),
          'default' => __('Pay with Bitcoin via Coingate'),
        ),
        'title' => array(
          'title' => __('Title', 'woocommerce'),
          'type' => 'text',
          'description' => __('Payment method title that the customer will see on your website.', 'woocommerce'),
          'default' => __('Bitcoin', 'woocommerce'),
        ),
        'app_id' => array(
          'title' => __('App ID', 'woocommerce'),
          'type' => 'text',
          'description' => __('CoinGate App ID', 'woocommerce'),
          'default' => '',
        ),
        'api_key' => array(
          'title' => __('API Key', 'woocommerce'),
          'type' => 'text',
          'description' => __('CoinGate API Key', 'woocommerce'),
          'default' => __('', 'woocommerce'),
        ),
        'api_secret' => array(
          'title' => __('API Secret', 'woocommerce'),
          'type' => 'text',
          'default' => '',
          'description' => __('CoinGate API Secret', 'woocommerce'),
        ),
        'receive_currency' => array(
          'title' => __('Receive Currency', 'woocommerce'),
          'type' => 'select',
          'options' => array(
            'BTC' => __('Bitcoin (฿)', 'woocommerce'),
            'EUR' => __('Euros (€)', 'woocommerce'),
            'USD' => __('US Dollars ($)', 'woocommerce')
          ),
          'description' => __('Currency you want to receive when making withdrawal at CoinGate. Please take a note what if you choose EUR or USD you will be asked to verify your business before making a withdrawal at CoinGate.', 'woocomerce'),
          'default' => 'BTC',
        ),
        'order_statuses' => array(
          'type' => 'order_statuses'
        ),
        'test' => array(
          'title' => __('Test', 'woocommerce'),
          'type' => 'checkbox',
          'label' => __('Enable test mode', 'woocommerce'),
          'default' => 'no',
          'description' => __('Enable "Test" to test on <a href="https://sandbox.coingate.com" target="_blank">sandbox.coingate.com</a>. <a href="http://support.coingate.com/knowledge_base/topics/how-can-i-test-your-service-without-signing-up" target="_blank">Read more about testing</a>.<br>
            Please note, that for "Test" mode you <b>must</b> generate separate API credentials on <a href="https://sandbox.coingate.com" target="_blank">sandbox.coingate.com</a>. API credentials generated on <a href="https://coingate.com" target="_blank">coingate.com</a> will <b>not</b> work for "Test" mode.', 'woocommerce'),
        ),
      );
    }

    public function thankyou()
    {
      if ($description = $this->get_description()) {
        echo wpautop(wptexturize($description));
      }
    }

    public function process_payment($order_id)
    {
      global $woocommerce, $page, $paged;
      $order = new WC_Order($order_id);

      $this->init_coingate();

      $description = array();
      foreach ($order->get_items('line_item') as $item) {
        $description[] = $item['qty'].' × '.$item['name'];
      }

      $token = get_post_meta($order->id, 'coingate_order_token', true);

      if ($token == '') {
        $token = substr(md5(rand()), 0, 32);

        update_post_meta($order_id, 'coingate_order_token', $token);
      }

      $wcOrder = wc_get_order($order_id);

      $order = \CoinGate\Merchant\Order::create(array(
        'order_id' => $order->id,
        'price' => number_format($order->get_total(), 2, '.', ''),
        'currency' => get_woocommerce_currency(),
        'receive_currency' => $this->receive_currency,
        'cancel_url' => $order->get_cancel_order_url(),
        'callback_url' => trailingslashit(get_bloginfo('wpurl')).'?wc-api=wc_gateway_coingate&token='.$token,
        'success_url' => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, $this->get_return_url($wcOrder))),
        'title' => get_bloginfo('name', 'raw').' Order #'.$order->id,
        'description' => implode($description, ', '),
      ));

      if ($order && $order->payment_url) {
        return array(
          'result' => 'success',
          'redirect' => $order->payment_url,
        );
      } else {
        return array(
          'result' => 'fail',
        );
      }
    }

    public function payment_callback()
    {
      $request = $_REQUEST;<commit_msg>Update decimal places of order<commit_after><?php

/*
Plugin Name: WooCommerce Payment Gateway - CoinGate
Plugin URI: https://coingate.com
Description: Accept Bitcoin via CoinGate in your WooCommerce store
Version: 1.1.0
Author: CoinGate
Author URI: https://coingate.com
License: MIT License
License URI: https://github.com/coingate/woocommerce-plugin/blob/master/LICENSE
Github Plugin URI: https://github.com/coingate/woocommerce-plugin
*/

add_action('plugins_loaded', 'coingate_init');

define('COINGATE_WOOCOMMERCE_VERSION', '1.1.0');

function coingate_init()
{
  if (!class_exists('WC_Payment_Gateway')) {
    return;
  };

  define('PLUGIN_DIR', plugins_url(basename(plugin_dir_path(__FILE__)), basename(__FILE__)).'/');

  require_once(__DIR__.'/lib/coingate/init.php');

  class WC_Gateway_Coingate extends WC_Payment_Gateway
  {
    public function __construct()
    {
      global $woocommerce;

      $this->id = 'coingate';
      $this->has_fields = false;
      $this->method_title = 'CoinGate';
      $this->icon = apply_filters('woocommerce_coingate_icon', PLUGIN_DIR.'assets/bitcoin.png');

      $this->init_form_fields();
      $this->init_settings();

      $this->title = $this->get_option('title');
      $this->description = $this->get_option('description');
      $this->app_id = $this->get_option('app_id');
      $this->api_key = $this->get_option('api_key');
      $this->api_secret = $this->get_option('api_secret');
      $this->receive_currency = $this->get_option('receive_currency');
      $this->order_statuses = $this->get_option('order_statuses');
      $this->test = ('yes' === $this->get_option('test', 'no'));

      add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
      add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'save_order_statuses'));
      add_action('woocommerce_thankyou_coingate', array($this, 'thankyou'));
      add_action('woocommerce_api_wc_gateway_coingate', array($this, 'payment_callback'));
    }

    public function admin_options()
    {
      ?>
      <h3><?php _e('CoinGate', 'woothemes'); ?></h3>
      <p><?php _e('Accept Bitcoin through the CoinGate.com and receive payments in euros and US dollars.<br>
        <a href="https://developer.coingate.com/docs/issues" target="_blank">Not working? Common issues</a> &middot; <a href="mailto:<EMAIL>"><EMAIL></a>', 'woothemes'); ?></p>
      <table class="form-table">
        <?php $this->generate_settings_html(); ?>
      </table>
      <?php

    }

    public function init_form_fields()
    {
      $this->form_fields = array(
        'enabled' => array(
          'title' => __('Enable CoinGate', 'woocommerce'),
          'label' => __('Enable Bitcoin payment via CoinGate', 'woocommerce'),
          'type' => 'checkbox',
          'description' => '',
          'default' => 'no',
        ),
        'description' => array(
          'title' => __('Description', 'woocommerce'),
          'type' => 'textarea',
          'description' => __('This controls the description which the user sees during checkout.', 'woocommerce'),
          'default' => __('Pay with Bitcoin via Coingate'),
        ),
        'title' => array(
          'title' => __('Title', 'woocommerce'),
          'type' => 'text',
          'description' => __('Payment method title that the customer will see on your website.', 'woocommerce'),
          'default' => __('Bitcoin', 'woocommerce'),
        ),
        'app_id' => array(
          'title' => __('App ID', 'woocommerce'),
          'type' => 'text',
          'description' => __('CoinGate App ID', 'woocommerce'),
          'default' => '',
        ),
        'api_key' => array(
          'title' => __('API Key', 'woocommerce'),
          'type' => 'text',
          'description' => __('CoinGate API Key', 'woocommerce'),
          'default' => __('', 'woocommerce'),
        ),
        'api_secret' => array(
          'title' => __('API Secret', 'woocommerce'),
          'type' => 'text',
          'default' => '',
          'description' => __('CoinGate API Secret', 'woocommerce'),
        ),
        'receive_currency' => array(
          'title' => __('Receive Currency', 'woocommerce'),
          'type' => 'select',
          'options' => array(
            'BTC' => __('Bitcoin (฿)', 'woocommerce'),
            'EUR' => __('Euros (€)', 'woocommerce'),
            'USD' => __('US Dollars ($)', 'woocommerce')
          ),
          'description' => __('Currency you want to receive when making withdrawal at CoinGate. Please take a note what if you choose EUR or USD you will be asked to verify your business before making a withdrawal at CoinGate.', 'woocomerce'),
          'default' => 'BTC',
        ),
        'order_statuses' => array(
          'type' => 'order_statuses'
        ),
        'test' => array(
          'title' => __('Test', 'woocommerce'),
          'type' => 'checkbox',
          'label' => __('Enable test mode', 'woocommerce'),
          'default' => 'no',
          'description' => __('Enable "Test" to test on <a href="https://sandbox.coingate.com" target="_blank">sandbox.coingate.com</a>. <a href="http://support.coingate.com/knowledge_base/topics/how-can-i-test-your-service-without-signing-up" target="_blank">Read more about testing</a>.<br>
            Please note, that for "Test" mode you <b>must</b> generate separate API credentials on <a href="https://sandbox.coingate.com" target="_blank">sandbox.coingate.com</a>. API credentials generated on <a href="https://coingate.com" target="_blank">coingate.com</a> will <b>not</b> work for "Test" mode.', 'woocommerce'),
        ),
      );
    }

    public function thankyou()
    {
      if ($description = $this->get_description()) {
        echo wpautop(wptexturize($description));
      }
    }

    public function process_payment($order_id)
    {
      global $woocommerce, $page, $paged;
      $order = new WC_Order($order_id);

      $this->init_coingate();

      $description = array();
      foreach ($order->get_items('line_item') as $item) {
        $description[] = $item['qty'].' × '.$item['name'];
      }

      $token = get_post_meta($order->id, 'coingate_order_token', true);

      if ($token == '') {
        $token = substr(md5(rand()), 0, 32);

        update_post_meta($order_id, 'coingate_order_token', $token);
      }

      $wcOrder = wc_get_order($order_id);

      $order = \CoinGate\Merchant\Order::create(array(
        'order_id' => $order->id,
        'price' => number_format($order->get_total(), 8, '.', ''),
        'currency' => get_woocommerce_currency(),
        'receive_currency' => $this->receive_currency,
        'cancel_url' => $order->get_cancel_order_url(),
        'callback_url' => trailingslashit(get_bloginfo('wpurl')).'?wc-api=wc_gateway_coingate&token='.$token,
        'success_url' => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, $this->get_return_url($wcOrder))),
        'title' => get_bloginfo('name', 'raw').' Order #'.$order->id,
        'description' => implode($description, ', '),
      ));

      if ($order && $order->payment_url) {
        return array(
          'result' => 'success',
          'redirect' => $order->payment_url,
        );
      } else {
        return array(
          'result' => 'fail',
        );
      }
    }

    public function payment_callback()
    {
      $request = $_REQUEST;
{'fraction_non_alphanumeric': 0.17642718186087072, 'fraction_numerical': 0.0017441470450124102, 'mean_word_length': 2.957525882665251, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 263, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1141033', 'n_tokens_mistral': 4751, 'n_tokens_neox': 4699, 'n_words': 939}