# makecommerce/3.0.5/api/api.php

MakeCommerce for WooCommerce, version 3.0.5. 402 lines.

- Page: https://pluginprobe.com/plugins/makecommerce/3.0.5/code/api/api.php
- Raw: https://pluginprobe.com/plugins/makecommerce/3.0.5/raw/api/api.php
- Modified: 2021-06-17T08:47:30+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/makecommerce/3.0.5/code/api/api.php#L10-L20`.

```php
<?php

/**
 * The api functionality of the plugin.
 *
 * @link       https://makecommerce.net/
 * @since      3.0.0
 *
 * @package    Makecommerce
 * @subpackage Makecommerce/api
 */

namespace MakeCommerce;

/**
 * The payment functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Makecommerce
 * @subpackage Makecommerce/api
 * @author     Maksekeskus AS <support@maksekeskus.ee>
 */
class API {

	/**
	 * The ID of this plugin.
	 *
	 * @since    3.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    3.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	private Loader $loader;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    3.0.0
	 * @param    string    $plugin_name       The name of this plugin.
	 * @param    string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version, Loader $loader ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;
		$this->loader = $loader;
	}

	public function label_formats() {

		$MK = \MakeCommerce::get_api();
		
		if ( !$MK ) {
			return array( 'A4' => 'A4' );
		}

		$formats = $MK->getLabelFormats();
		$optionsArray = array();
		foreach ( $formats as $format ) {
			$optionsArray[$format] = $format;
		}

		return $optionsArray;
	}
	
	/**
	 * Gives error in admin if curl is not loaded
	 */
	public function check_if_curl_is_loaded() {

		if ( !extension_loaded('curl') ) {

			$class = 'notice notice-error';
			$message = __( 'You have enabled MakeCommerce module but it seems that you don\'t have CURL enabled. This way MakeCommerce unfortunately does not work!', 'wc_makecommerce_domain' );
			
			printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
		}
	}

	/**
	 * Add menu item to WooCommerce advanced section
	 * 
	 * @since 3.0.0
	 */
	public function add_woo_menu( $sections ) { 

		$sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain');

		return $sections;
	}

	/**
	 * Adds new column (payment method) in order view
	 * 
	 * @since 3.0.0
	 */
	public function add_ordersview_paymentmethod_column( $columns ) {
		
		$columns['makecommerce_payment_method'] = __('Payment method (MC)');

		return $columns;
	}

	/**
	 * Makes payment method column in order view sortable
	 * 
	 * @since 3.0.0
	 */
	public function make_ordersview_paymentmethod_column_sortable( $columns ) {

		return wp_parse_args (array( 'makecommerce_payment_method' => 'makecommerce_payment_method' ), $columns );
	}
		
	/**
	 * Inserts content for payment method column in order view
	 * 
	 * @since 3.0.0
	 */
	public function fill_ordersview_paymentmethod_column( $column, $post_id ) {

		if ( 'makecommerce_payment_method' === $column ) {
			echo get_post_meta( $post_id, '_makecommerce_preselected_method', true );
		}
	}

	/**
	 * Update banklinks when an admin logs in
	 * 
	 * @since 3.0.0
	 */
	public function admin_login( $user_login, $user ) {

	    if ( user_can( $user, 'administrator' ) ) {
	    	Payment::update_banklinks();
	    }
	}

	/**
	 * Adds settings link to plugins page
	 * 
	 * @since 3.0.0
	 */
	public function add_plugin_settings_link( $links ) {

		$settings_link = '<a href="admin.php?page=wc-settings&tab=api&section=mk_api">API '.__('Settings').'</a>';

		if ( \MakeCommerce::new_woo_version() ) {
			$settings_link = '<a href="admin.php?page=wc-settings&tab=advanced&section=mk_api">API '.__('Settings').'</a>';
		}

		array_unshift( $links, $settings_link );

		return $links;
	}

	/**
	 * Updates banklinks if current section is not in mk_api
	 * 
	 * @since 3.0.0
	 */
	public function save_settings() {
		
		global $current_section;

		if ( $current_section !== 'mk_api' ) {
			return;
		}

		Payment::update_banklinks();
	}

	/**
	 * Test and live switching in API settings (jQuery)
	 * 
	 * @since 3.0.0
	 */
	public function api_javascript_ui( $data ) {
		
		?>
		<script type="text/javascript">
		jQuery(document).ready(function($) {

			var env_select = $('#mk_api_type');
			var test_fields = ['mk_test_shop_id', 'mk_test_private_key', 'mk_test_public_key'];
			var live_fields = ['mk_shop_id', 'mk_private_key', 'mk_public_key'];

			env_select.on('change', function(){
				hideFields($(this).val());
			});

			hideFields(env_select.val());

			function hideFields(type) {
				var hide = type == 'live' ? test_fields : live_fields;
				var show = type == 'live' ? live_fields : test_fields;
				$.each(hide, function(i, elem) {
					$('#'+elem).closest('tr').hide();
				});
				$.each(show, function(i, elem) {
					$('#'+elem).closest('tr').show();
				});
			}
		});
		</script>
		<?php
    }

	/**
	 * Add admin settings for api in woocommerce->settings->advanced->Makcommerce API
	 * 
	 * @since 3.0.0
	 */
	public function add_woo_admin_settings( $settings ) {

		/**
		 * Prevent the settings from being loaded if we are not at actually the page
		 */
		global $current_section;

		if ( $current_section !== 'mk_api' ) {
			return $settings;
		}
		
		/**
		 * Return array with fields for settings
		 */
		return array(
			array( 'type' => 'title', 'desc' => \MakeCommerce::get_logo_html() ),
			array(
				'type' => 'title', 
				'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'), 
				'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain').
				sprintf( __('To further configure the Payment methods please go to <a href=."%s">MakeCommerce Checkout Options</a>, links to settings of our Shipment methods are listed below', 'wc_makecommerce_domain'), 'admin.php?page=wc-settings&tab=checkout&section=makecommerce'),
				'id' => 'mk_api_settings'
			),
			array(
			        'type' => 'select',
			        'title' => __('Current environment', 'wc_makecommerce_domain'),
			        'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'),
			        'default' => 'live',
			        'options' => array(
					        'live' => __('Live', 'wc_makecommerce_domain'),
					        'test' => __('Test', 'wc_makecommerce_domain'),
		        			),
				'id' => 'mk_api_type'
			),
		      	array(
			        'id' => 'mk_shop_id',
			        'type' => 'text',
			        'title' => __('Shop ID (live)', 'wc_makecommerce_domain'),
			        'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'), 
			        'class' => 'input-text regular-input',
		      	),
		      	array(
			        'id' => 'mk_private_key',
			        'type' => 'text',
			        'title' => __('Secret key (live)', 'wc_makecommerce_domain'),
			        'class' => 'input-text regular-input',
		      	),		      	
		      	array(
			        'id' => 'mk_public_key',
			        'type' => 'text',
			        'title' => __('Publishable key (live)', 'wc_makecommerce_domain'),
			        'class' => 'input-text regular-input',
		      	),
		      	array(
			        'id' => 'mk_test_shop_id',
			        'type' => 'text',
			        'title' => __('Shop ID (test)', 'wc_makecommerce_domain'),
			        'class' => 'input-text regular-input',
			        'default' => 'f64b4f20-5ef9-4b7b-a6fa-d623d87f0b9c',
					'desc' => __('Get it from <a href="https://merchant.test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'),
		      	),		      	
		      	array(
			        'id' => 'mk_test_private_key',
			        'type' => 'text',
			        'title' => __('Secret key (test)', 'wc_makecommerce_domain'),
			        'default' => 'MPjcVMoRZPAucsTuGK5ZukOlV7BlzgSvXOowJUkk9IFSQPVooRwcVOxzz3mhEpgM',
			        'class' => 'input-text regular-input',
		      	),
		      	array(
			        'id' => 'mk_test_public_key',
			        'type' => 'text',
			        'title' => __('Publishable key (test)', 'wc_makecommerce_domain'),
			        'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m',
			        'class' => 'input-text regular-input',
		      	),
				array(
			        'type' => 'select',
			        'title' => __('Label print format', 'wc_makecommerce_domain'),
			        'desc' => __('In which format should shipping labels be printed. (*make sure you have API access to see more options)', 'wc_makecommerce_domain'),
			        'default' => 'A4',
			        'options' => $this->label_formats(),
					'id' => 'mk_label_format'
				),
				array('type' => 'sectionend', 'id' => 'mk_api_settings'),
				array(
					'type' => 'title',
					'desc' => '<br><hr><br>',
				),	
				array(
					'id' => 'mk_module_title',
					'type' => 'title',
					'title' => __('Makecommerce modules', 'wc_makecommerce_domain'),
					'desc' => __('Our plugin adds several modules to your shop. Here you can switch off modules that are not important for you, they will disappear from Woocommerce settings menus.<br> Each active module have their own settings dialog, where they must also be Enabled before use.', 'wc_makecommerce_domain'),
					'class' => '',
				),
		      	array(
			        'id' => 'mk_transport_apt_omniva',
			        'type' => 'checkbox',
					'default' => 'yes',
			        'title' => __('Omniva Parcel Machine', 'wc_makecommerce_domain'),
			        'desc' => __('enable Omniva parcel machines shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=parcelmachine_omniva')).')',
			        'class' => '',
		      	),
		      	array(
			        'id' => 'mk_transport_apt_smartpost',
			        'type' => 'checkbox',
					'default' => 'yes',
			        'title' => __('Smartpost Parcel Machine', 'wc_makecommerce_domain'),
			        'desc' => __('enable Smartpost parcel machines shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=parcelmachine_smartpost')).')',
			        'class' => '',
		      	),
		      	array(
			        'id' => 'mk_transport_apt_dpd',
			        'type' => 'checkbox',
					'default' => 'no',
			        'title' => __('DPD', 'wc_makecommerce_domain') . ' ' . __('parcel machine', 'wc_makecommerce_domain'),
			        'desc' => __('enable DPD parcel machine shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=parcelmachine_dpd')).')',
			        'class' => '',
		      	),
		      	array(
			        'id' => 'mk_transport_courier_omniva',
			        'type' => 'checkbox',
					'default' => 'yes',
			        'title' => __('Omniva Courier', 'wc_makecommerce_domain'),
			        'desc' => __('enable Omniva courier shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=courier_omniva')).')',			        
			        'class' => '',
		      	),
		      	array(
			        'id' => 'mk_transport_courier_smartpost',
			        'type' => 'checkbox',
					'default' => 'yes',
			        'title' => __('Smartpost Courier', 'wc_makecommerce_domain'),
			        'desc' => __('enable Smartpost courier shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=courier_smartpost')).')',			        
			        'class' => '',
		      	),
		      	array(
			        'id' => 'mk_checkout_sco',
			        'type' => 'checkbox',
			        'title' => __('SimpleCheckout', 'wc_makecommerce_domain'),
			        'desc' => __('enable SimpleCheckout&trade; Checkout method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=checkout&section=makecommerce_sc')).')',			        
			        'class' => '',
		      	),
		      	array(
			        'id' => 'mk_product_slice',
			        'type' => 'checkbox',
			        'title' => __('Indivy Slice 3 information in product view', 'wc_makecommerce_domain'),
			        'desc' => __('enable Indivy Slice 3 in product view', 'wc_makecommerce_domain'),			        
			        'class' => '',
		      	),
		      	array(
			        'id' => 'mk_javascript_ui',
			        'type' => 'api_javascript_ui',
		      	),
			array('type' => 'sectionend', 'id' => 'mk_api_settings')
		);
	}

	/**
     * Notice for missing API information
     * 
     * @since 3.0.0
     */
    public function api_info_missing() {

        ?>
        <div class="notice notice-error is-dismissible">
            <p>
                <?php echo __( 'You have not entered the Shop ID and keys for the MakeCommerce payment module. The module will not work without them.', 'wc_makecommerce_domain' ); ?>
                <?php if ( \MakeCommerce::new_woo_version() ) { ?>
                <a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=advanced&section=mk_api' ); ?>"><?php echo __( 'Click here to enter them', 'wc_makecommerce_domain' ); ?></a>
                <?php } else { ?>
                <a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=api&section=mk_api' ); ?>"><?php echo __( 'Click here to enter them', 'wc_makecommerce_domain' ); ?></a>
                <?php } ?>
            </p>
        </div>
        <?php
    }
}
```
