| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The api functionality of the plugin. |
| 5 |
* |
| 6 |
* @link https://makecommerce.net/ |
| 7 |
* @since 3.0.0 |
| 8 |
* |
| 9 |
* @package Makecommerce |
| 10 |
* @subpackage Makecommerce/api |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace MakeCommerce; |
| 14 |
|
| 15 |
/** |
| 16 |
* The payment functionality of the plugin. |
| 17 |
* |
| 18 |
* Defines the plugin name, version, and two examples hooks for how to |
| 19 |
* enqueue the admin-specific stylesheet and JavaScript. |
| 20 |
* |
| 21 |
* @package Makecommerce |
| 22 |
* @subpackage Makecommerce/api |
| 23 |
* @author Maksekeskus AS <support@maksekeskus.ee> |
| 24 |
*/ |
| 25 |
class API { |
| 26 |
|
| 27 |
/** |
| 28 |
* The ID of this plugin. |
| 29 |
* |
| 30 |
* @since 3.0.0 |
| 31 |
* @access private |
| 32 |
* @var string $plugin_name The ID of this plugin. |
| 33 |
*/ |
| 34 |
private $plugin_name; |
| 35 |
|
| 36 |
/** |
| 37 |
* The version of this plugin. |
| 38 |
* |
| 39 |
* @since 3.0.0 |
| 40 |
* @access private |
| 41 |
* @var string $version The current version of this plugin. |
| 42 |
*/ |
| 43 |
private $version; |
| 44 |
|
| 45 |
private Loader $loader; |
| 46 |
|
| 47 |
/** |
| 48 |
* Initialize the class and set its properties. |
| 49 |
* |
| 50 |
* @since 3.0.0 |
| 51 |
* @param string $plugin_name The name of this plugin. |
| 52 |
* @param string $version The version of this plugin. |
| 53 |
*/ |
| 54 |
public function __construct( $plugin_name, $version, Loader $loader ) { |
| 55 |
|
| 56 |
$this->plugin_name = $plugin_name; |
| 57 |
$this->version = $version; |
| 58 |
$this->loader = $loader; |
| 59 |
} |
| 60 |
|
| 61 |
public function label_formats() { |
| 62 |
|
| 63 |
$MK = \MakeCommerce::get_api(); |
| 64 |
|
| 65 |
if ( !$MK ) { |
| 66 |
return array( 'A4' => 'A4' ); |
| 67 |
} |
| 68 |
|
| 69 |
$optionsArray = array(); |
| 70 |
|
| 71 |
try { |
| 72 |
|
| 73 |
$formats = $MK->getLabelFormats(); |
| 74 |
|
| 75 |
foreach ( $formats as $format ) { |
| 76 |
$optionsArray[$format] = $format; |
| 77 |
} |
| 78 |
} catch ( \Exception $e ) { |
| 79 |
//do nothing, something is wrong with the credentials or the shop is disabled |
| 80 |
} |
| 81 |
|
| 82 |
return $optionsArray; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Gives error in admin if curl is not loaded |
| 87 |
*/ |
| 88 |
public function check_if_curl_is_loaded() { |
| 89 |
|
| 90 |
if ( !extension_loaded('curl') ) { |
| 91 |
|
| 92 |
$class = 'notice notice-error'; |
| 93 |
$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' ); |
| 94 |
|
| 95 |
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Add menu item to WooCommerce advanced section |
| 101 |
* |
| 102 |
* @since 3.0.0 |
| 103 |
*/ |
| 104 |
public function add_woo_menu( $sections ) { |
| 105 |
|
| 106 |
$sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain'); |
| 107 |
|
| 108 |
return $sections; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Adds new column (payment method) in order view |
| 113 |
* |
| 114 |
* @since 3.0.0 |
| 115 |
*/ |
| 116 |
public function add_ordersview_paymentmethod_column( $columns ) { |
| 117 |
|
| 118 |
$columns['makecommerce_payment_method'] = __('Payment method (MC)'); |
| 119 |
|
| 120 |
return $columns; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Makes payment method column in order view sortable |
| 125 |
* |
| 126 |
* @since 3.0.0 |
| 127 |
*/ |
| 128 |
public function make_ordersview_paymentmethod_column_sortable( $columns ) { |
| 129 |
|
| 130 |
return wp_parse_args (array( 'makecommerce_payment_method' => 'makecommerce_payment_method' ), $columns ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Inserts content for payment method column in order view |
| 135 |
* |
| 136 |
* @since 3.0.0 |
| 137 |
*/ |
| 138 |
public function fill_ordersview_paymentmethod_column( $column, $post_id ) { |
| 139 |
|
| 140 |
if ( 'makecommerce_payment_method' === $column ) { |
| 141 |
$order = wc_get_order( $post_id ); |
| 142 |
echo $order->get_meta( '_makecommerce_preselected_method', true ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Update banklinks when an admin logs in |
| 148 |
* |
| 149 |
* @since 3.0.0 |
| 150 |
*/ |
| 151 |
public function admin_login( $user_login, $user ) { |
| 152 |
|
| 153 |
if ( user_can( $user, 'administrator' ) ) { |
| 154 |
Payment::update_banklinks(); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Adds settings link to plugins page |
| 160 |
* |
| 161 |
* @since 3.0.0 |
| 162 |
*/ |
| 163 |
public function add_plugin_settings_link( $links ) { |
| 164 |
|
| 165 |
$settings_link = '<a href="admin.php?page=wc-settings&tab=api§ion=mk_api">API '.__('Settings').'</a>'; |
| 166 |
|
| 167 |
if ( \MakeCommerce::new_woo_version() ) { |
| 168 |
$settings_link = '<a href="admin.php?page=wc-settings&tab=advanced§ion=mk_api">API '.__('Settings').'</a>'; |
| 169 |
} |
| 170 |
|
| 171 |
array_unshift( $links, $settings_link ); |
| 172 |
|
| 173 |
return $links; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Updates banklinks if current section is not in mk_api |
| 178 |
* |
| 179 |
* @since 3.0.0 |
| 180 |
*/ |
| 181 |
public function save_settings() { |
| 182 |
|
| 183 |
global $current_section; |
| 184 |
|
| 185 |
if ( $current_section !== 'mk_api' ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
Payment::update_banklinks(); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Test and live switching in API settings (jQuery) |
| 194 |
* |
| 195 |
* @since 3.0.0 |
| 196 |
*/ |
| 197 |
public function api_javascript_ui( $data ) { |
| 198 |
|
| 199 |
wp_enqueue_script( $this->plugin_name . "api-admin", plugin_dir_url(__FILE__) . 'js/api.js', array( 'jquery' ), $this->version ); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Add admin settings for api in woocommerce->settings->advanced->Makcommerce API |
| 204 |
* |
| 205 |
* @since 3.0.0 |
| 206 |
*/ |
| 207 |
public function add_woo_admin_settings( $settings ) { |
| 208 |
|
| 209 |
/** |
| 210 |
* Prevent the settings from being loaded if we are not at actually the page |
| 211 |
*/ |
| 212 |
global $current_section; |
| 213 |
|
| 214 |
if ( $current_section !== 'mk_api' ) { |
| 215 |
return $settings; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Return array with fields for settings |
| 220 |
*/ |
| 221 |
return [ |
| 222 |
[ 'type' => 'title', 'desc' => \MakeCommerce::get_logo_html() ], |
| 223 |
[ |
| 224 |
'type' => 'title', |
| 225 |
'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'), |
| 226 |
'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain'). |
| 227 |
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§ion=makecommerce'), |
| 228 |
'id' => 'mk_api_settings' |
| 229 |
], |
| 230 |
[ |
| 231 |
'type' => 'select', |
| 232 |
'title' => __('Current environment', 'wc_makecommerce_domain'), |
| 233 |
'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'), |
| 234 |
'default' => 'live', |
| 235 |
'options' => [ |
| 236 |
'live' => __('Live', 'wc_makecommerce_domain'), |
| 237 |
'test' => __('Test', 'wc_makecommerce_domain'), |
| 238 |
], |
| 239 |
'id' => 'mk_api_type' |
| 240 |
], |
| 241 |
[ |
| 242 |
'id' => 'mk_shop_id', |
| 243 |
'type' => 'text', |
| 244 |
'title' => __('Shop ID (live)', 'wc_makecommerce_domain'), |
| 245 |
'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'), |
| 246 |
'class' => 'input-text regular-input', |
| 247 |
], |
| 248 |
[ |
| 249 |
'id' => 'mk_private_key', |
| 250 |
'type' => 'text', |
| 251 |
'title' => __('Secret key (live)', 'wc_makecommerce_domain'), |
| 252 |
'class' => 'input-text regular-input', |
| 253 |
], |
| 254 |
[ |
| 255 |
'id' => 'mk_public_key', |
| 256 |
'type' => 'text', |
| 257 |
'title' => __('Publishable key (live)', 'wc_makecommerce_domain'), |
| 258 |
'class' => 'input-text regular-input', |
| 259 |
], |
| 260 |
[ |
| 261 |
'id' => 'mk_test_shop_id', |
| 262 |
'type' => 'text', |
| 263 |
'title' => __('Shop ID (test)', 'wc_makecommerce_domain'), |
| 264 |
'class' => 'input-text regular-input', |
| 265 |
'default' => 'f64b4f20-5ef9-4b7b-a6fa-d623d87f0b9c', |
| 266 |
'desc' => __('Get it from <a href="https://merchant.test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'), |
| 267 |
], |
| 268 |
[ |
| 269 |
'id' => 'mk_test_private_key', |
| 270 |
'type' => 'text', |
| 271 |
'title' => __('Secret key (test)', 'wc_makecommerce_domain'), |
| 272 |
'default' => 'MPjcVMoRZPAucsTuGK5ZukOlV7BlzgSvXOowJUkk9IFSQPVooRwcVOxzz3mhEpgM', |
| 273 |
'class' => 'input-text regular-input', |
| 274 |
], |
| 275 |
[ |
| 276 |
'id' => 'mk_test_public_key', |
| 277 |
'type' => 'text', |
| 278 |
'title' => __('Publishable key (test)', 'wc_makecommerce_domain'), |
| 279 |
'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m', |
| 280 |
'class' => 'input-text regular-input', |
| 281 |
], |
| 282 |
[ |
| 283 |
'type' => 'select', |
| 284 |
'title' => __('Label print format', 'wc_makecommerce_domain'), |
| 285 |
'desc' => __('In which format should shipping labels be printed. (*make sure you have API access to see more options)', 'wc_makecommerce_domain'), |
| 286 |
'default' => 'A4', |
| 287 |
'options' => $this->label_formats(), |
| 288 |
'id' => 'mk_label_format' |
| 289 |
], |
| 290 |
['type' => 'sectionend', 'id' => 'mk_api_settings'], |
| 291 |
[ |
| 292 |
'type' => 'title', |
| 293 |
'desc' => '<br><hr><br>', |
| 294 |
], |
| 295 |
[ |
| 296 |
'title' => __( 'Parcel Machine Map Settings', 'wc_makecommerce_domain' ), |
| 297 |
'type' => 'title', |
| 298 |
'desc' => __( 'Before using, please read more about the functionalities, benefits and security measures of our parcel machine map from', 'wc_makecommerce_domain' ) . |
| 299 |
' ' . sprintf('<a target="_blank" href="%s">' . |
| 300 |
__( 'our plugin instructions', 'wc_makecommerce_domain' ) . |
| 301 |
'</a>', __( 'https://makecommerce.net/integration-modules/makecommerce-plugin-for-woocommerce/#map-view-for-the-selection-of-parcel-machines', 'wc_makecommerce_domain' ) ), |
| 302 |
'id' => 'mc_map_settings', |
| 303 |
], |
| 304 |
[ |
| 305 |
'title' => __( 'Enable map selection for parcel machines', 'wc_makecommerce_domain' ), |
| 306 |
'label' => __( 'Allow customers to select their desired parcel machine from a map view', 'wc_makecommerce_domain' ), |
| 307 |
'type' => 'checkbox', |
| 308 |
'default' => 'no', |
| 309 |
'id' => 'mc_parcel_machine_map', |
| 310 |
], |
| 311 |
[ |
| 312 |
'title' => __( 'Define Google Javascript API key', 'wc_makecommerce_domain' ), |
| 313 |
'type' => 'text', |
| 314 |
'desc_tip' => __( 'In order to enable the map feature for parcel machine seletion, a Google Javascript API key needs to be obtained', 'wc_makecommerce_domain' ), |
| 315 |
'class' => 'ui-map-identifier', |
| 316 |
'id' => 'mc_google_api_key', |
| 317 |
], |
| 318 |
[ |
| 319 |
'title' => __( 'Use Google Geocoding', 'wc_makecommerce_domain' ), |
| 320 |
'label' => __( 'Geocoding allows the map to centralize on the shipping address', 'wc_makecommerce_domain' ), |
| 321 |
'type' => 'checkbox', |
| 322 |
'default' => 'no', |
| 323 |
'id' => 'mc_map_geocoding', |
| 324 |
], |
| 325 |
[ |
| 326 |
'title' => __( 'Define a Google Geocoding API key', 'wc_makecommerce_domain' ), |
| 327 |
'type' => 'text', |
| 328 |
'desc_tip' => __( 'A separate API key is recommended to be defined for Geocoding due to security reasons', 'wc_makecommerce_domain' ), |
| 329 |
'class' => 'ui-map-identifier', |
| 330 |
'id' => 'mc_map_geocoding_api_key', |
| 331 |
], |
| 332 |
[ |
| 333 |
'type' => 'sectionend', |
| 334 |
'id' => 'mc_map_settings', |
| 335 |
], |
| 336 |
[ |
| 337 |
'type' => 'title', |
| 338 |
'desc' => '<br><hr><br>', |
| 339 |
], |
| 340 |
[ |
| 341 |
'id' => 'mk_module_title', |
| 342 |
'type' => 'title', |
| 343 |
'title' => __('Makecommerce modules', 'wc_makecommerce_domain'), |
| 344 |
'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'), |
| 345 |
'class' => '', |
| 346 |
], |
| 347 |
[ |
| 348 |
'id' => 'mk_transport_apt_omniva', |
| 349 |
'type' => 'checkbox', |
| 350 |
'default' => 'yes', |
| 351 |
'title' => __('Omniva Parcel Machine', 'wc_makecommerce_domain'), |
| 352 |
'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§ion=parcelmachine_omniva')).')', |
| 353 |
'class' => '', |
| 354 |
], |
| 355 |
[ |
| 356 |
'id' => 'mk_transport_apt_smartpost', |
| 357 |
'type' => 'checkbox', |
| 358 |
'default' => 'yes', |
| 359 |
'title' => __('Smartpost Parcel Machine', 'wc_makecommerce_domain'), |
| 360 |
'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§ion=parcelmachine_smartpost')).')', |
| 361 |
'class' => '', |
| 362 |
], |
| 363 |
[ |
| 364 |
'id' => 'mk_transport_apt_dpd', |
| 365 |
'type' => 'checkbox', |
| 366 |
'default' => 'no', |
| 367 |
'title' => __('DPD', 'wc_makecommerce_domain') . ' ' . __('parcel machine', 'wc_makecommerce_domain'), |
| 368 |
'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§ion=parcelmachine_dpd')).')', |
| 369 |
'class' => '', |
| 370 |
], |
| 371 |
[ |
| 372 |
'id' => 'mk_transport_apt_lp_express_lt', |
| 373 |
'type' => 'checkbox', |
| 374 |
'default' => 'no', |
| 375 |
'title' => __('LP Express', 'wc_makecommerce_domain') . ' ' . __('parcel machine', 'wc_makecommerce_domain'), |
| 376 |
'desc' => __('enable LP Express 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§ion=parcelmachine_lp_express_lt')).')', |
| 377 |
'class' => '', |
| 378 |
], |
| 379 |
[ |
| 380 |
'id' => 'mk_transport_courier_omniva', |
| 381 |
'type' => 'checkbox', |
| 382 |
'default' => 'yes', |
| 383 |
'title' => __('Omniva Courier', 'wc_makecommerce_domain'), |
| 384 |
'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§ion=courier_omniva')).')', |
| 385 |
'class' => '', |
| 386 |
], |
| 387 |
[ |
| 388 |
'id' => 'mk_transport_courier_smartpost', |
| 389 |
'type' => 'checkbox', |
| 390 |
'default' => 'yes', |
| 391 |
'title' => __('Smartpost Courier', 'wc_makecommerce_domain'), |
| 392 |
'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§ion=courier_smartpost')).')', |
| 393 |
'class' => '', |
| 394 |
], |
| 395 |
[ |
| 396 |
'id' => 'mk_transport_courier_dpd', |
| 397 |
'type' => 'checkbox', |
| 398 |
'default' => 'yes', |
| 399 |
'title' => __('DPD Courier', 'wc_makecommerce_domain'), |
| 400 |
'desc' => __('enable DPD 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§ion=courier_dpd')).')', |
| 401 |
'class' => '', |
| 402 |
], |
| 403 |
[ |
| 404 |
'id' => 'mk_javascript_ui', |
| 405 |
'type' => 'api_javascript_ui', |
| 406 |
], |
| 407 |
['type' => 'sectionend', 'id' => 'mk_api_settings'] |
| 408 |
]; |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Function for resetting machines and payment methods when API type is changed |
| 413 |
* |
| 414 |
* @since 3.2.1 |
| 415 |
*/ |
| 416 |
public function mk_delete_api_cache() { |
| 417 |
|
| 418 |
global $wpdb; |
| 419 |
$tableName = $wpdb->prefix . MAKECOMMERCE_TABLENAME; |
| 420 |
|
| 421 |
// Delete machines' cache and timer |
| 422 |
delete_option( 'mk_machines_expires' ); |
| 423 |
delete_option( 'mk_machines_cache' ); |
| 424 |
|
| 425 |
// Delete payment methods |
| 426 |
$wpdb->query( 'TRUNCATE TABLE `'.$tableName.'`' ); |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Notice for missing API information |
| 431 |
* |
| 432 |
* @since 3.0.0 |
| 433 |
*/ |
| 434 |
public function api_info_missing() { |
| 435 |
|
| 436 |
?> |
| 437 |
<div class="notice notice-error is-dismissible"> |
| 438 |
<p> |
| 439 |
<?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' ); ?> |
| 440 |
<?php if ( \MakeCommerce::new_woo_version() ) { ?> |
| 441 |
<a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=mk_api' ); ?>"><?php echo __( 'Click here to enter them', 'wc_makecommerce_domain' ); ?></a> |
| 442 |
<?php } else { ?> |
| 443 |
<a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=api§ion=mk_api' ); ?>"><?php echo __( 'Click here to enter them', 'wc_makecommerce_domain' ); ?></a> |
| 444 |
<?php } ?> |
| 445 |
</p> |
| 446 |
</div> |
| 447 |
<?php |
| 448 |
} |
| 449 |
} |