<?php
/**
* The core plugin class.
*
* The admin-specific functionality of the plugin.
*
*
* @since 1.0.0
* @package Wanderlust Web Design Mercado Pago Marketplace
* @subpackage wanderlust-meli/includes
* @author Conrado Galli <[email protected]>
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Wanderlust_Meli_Admin_Mp {
private $plugin_name;
private $version;
private $soap_url;
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
}
public function enqueue_styles() {
wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wanderlust-meli-admin.css', array(), $this->version, 'all' );
}
public function enqueue_scripts() {
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wanderlust-meli-admin.js', array( 'jquery' ), $this->version, false );
}
public function add_settings_tab( $settings_tabs ) {
$settings_tabs['wanderlust_meli_mp'] = __( 'Mercado Pago Marketplace Settings', 'wanderlust-meli' );
return $settings_tabs;
}
public static function settings_tab() {
woocommerce_admin_fields( self::get_settings() );
}
public static function update_settings() {
woocommerce_update_options( self::get_settings() );
}
public function wanderlust_meli_mp_add_metaboxes(){
//add_meta_box( 'wanderlust_meli_mp_metabox', __('Mercado Libre','wanderlust-meli'), array( $this, 'wanderlust_meli_mp_order_buttons' ), 'shop_order', 'side', 'core' );
}
public static function wanderlust_meli_mp_order_buttons(){
}
public static function get_settings() {
global $woocommerce, $wp_session;
session_start();
$callback = get_site_url();
$dates = get_option('fullupdate_meli');
$appid = get_option('wc_wanderlust_meli_mp_appid');
$secretkey = get_option('wc_wanderlust_meli_mp_secretkey');
$access_token = get_option('wanderlust_meli_mp_auth_access_token');
$refresh_token = get_option('wanderlust_meli_mp_auth_refresh_token');
$expires_in = get_option('wanderlust_meli_mp_auth_expires_in');
$meli = new Meli($appid, $secretkey, $access_token, $refresh_token);
if($_GET['code'] || $access_token) {
// If code exist and session is empty
if($_GET['code'] && !($access_token)) {
// If the code was in get parameter we authorize
$user = $meli->authorize($_GET['code'], $callback .'/wp-admin/admin.php?page=wc-settings&tab=wanderlust_meli_mp');
// Now we create the sessions with the authenticated user
$access_token = $user['body']->access_token;
$expires_in = time() + $user['body']->expires_in;
$refresh_token = $user['body']->refresh_token;
update_option( 'wanderlust_meli_mp_auth_code', $_GET['code'] );
update_option( 'wanderlust_meli_mp_auth_access_token', $user['body']->access_token );
update_option( 'wanderlust_meli_mp_auth_expires_in', time() + $user['body']->expires_in );
update_option( 'wanderlust_meli_mp_auth_refresh_token', $user['body']->refresh_token );
} else {
// We can check if the access token in invalid checking the time
if($expires_in < time()) {
try {
// Make the refresh proccess
$refresh = $meli->refreshAccessToken();
// Now we create the sessions with the new parameters
$access_token = $refresh['body']->access_token;
$expires_in = time() + $refresh['body']->expires_in;
$refresh_token = $refresh['body']->refresh_token;
update_option( 'wanderlust_meli_mp_auth_access_token', $access_token );
update_option( 'wanderlust_meli_mp_auth_expires_in', $expires_in );
update_option( 'wanderlust_meli_mp_auth_refresh_token', $refresh_token );
} catch (Exception $e) {
echo "Exception: ", $e->getMessage(), "\n";
}
}
$users = $meli->authorize($_GET['code'], $callback .'/wp-admin/admin.php?page=wc-settings&tab=wanderlust_meli_mp');
}
}
$params = array();
$result = $meli->get('users/me?access_token='.$access_token, $params);
$nickname = $result['body']->nickname;
$user_id = $result['body']->id;
if(!empty($nickname)){ ?>
<? } else { ?>
<? }
$settings = array(
'section_title' => array(
'name' => __( 'Mercado Pago Marketplace Settings', 'wanderlust-meli' ),
'type' => 'title',
'desc' => '',
'id' => 'wc_wanderlust_meli_mp_section_title'
),
'section_contactos' => array(
'name' => __( 'Instructions', 'wanderlust-meli' ),
'type' => 'title',
'desc' => '<a href="https://www.mercadopago.com.ar/developers/" target="_blank">1- Create APP on Mercado Pago</a> with this info:<br>
<strong>a.</strong> Redirect URI: <strong>https://ok.dashclick.net/dashboard/withdraw/</strong><br>
<strong>b.</strong> Scopes: offline_access , read write<br>
<strong>c.</strong> Tópicos: items, created orders, payments<br>
<strong>d.</strong> Notifications Callback URL: <strong>'.$callback .'/?wc-api=wanderlust_meli_mp </strong> <br><br>
2- Complete <strong>App ID y Secret Key</strong> provided by your APP.
',
'id' => 'wc_wanderlust_meli_mp_section_contactos'
),
'appid' => array(
'name' => __( 'App ID', 'wanderlust-meli' ),
'type' => 'text',
'desc' => __( 'App ID provided by Mercado Pago', 'wanderlust-meli' ),
'id' => 'wc_wanderlust_meli_mp_appid'
),
'secretkey' => array(
'name' => __( 'Secret Key', 'wanderlust-meli' ),
'type' => 'password',
'desc' => __( 'Secret Key provided by Mercado Pago', 'wanderlust-meli' ),
'id' => 'wc_wanderlust_meli_mp_secretkey'
),
'sectionend' => array(
'type' => 'sectionend',
'id' => 'wc_wanderlust_meli_mp_section_title'
),
'section_contactob' => array(
'name' => __( 'Authorization', 'wanderlust-meli' ),
'type' => 'title',
'desc' => '<div id="authorize_meli_info">
<strong>DATE..:</strong> '.$dates .' <br>
<strong>User.:</strong> '.$nickname .' <strong>User ID.:</strong> '.$user_id.'<br>
<a id="unauthorize_meli" href="'.$callback .'/wp-admin/admin.php?page=wc-settings&tab=wanderlust_meli_mp&unauthorize=yes">REMOVE AUTHORIZATION</a> <br><br>
</div><br><br>
',
'id' => 'wc_wanderlust_meli_mp_section_contactob'
),
'section_contacto' => array(
'name' => __( 'Soporte', 'wanderlust-meli' ),
'type' => 'title',
'desc' => 'Soporte: <a href="https://wanderlust-webdesign.com/contact">https://wanderlust-webdesign.com/contact</a> <br> ',
'id' => 'wc_wanderlust_meli_mp_section_contacto'
),
'sectionend_contacto' => array(
'type' => 'sectionend',
'id' => 'wc_wanderlust_meli_mp_section_contacto'
),
);
return apply_filters( 'wc_wanderlust_meli_mp', $settings );
}
public function woocommerce_melibox_box_add_box() {
//add_meta_box( 'woocommerce-melibox-box', __('Mercado Libre','wanderlust-meli'), array( $this, 'woocommerce_melibox_box_create_box_content' ), 'shop_order', 'side', 'core' );
}
public static function woocommerce_melibox_box_create_box_content() {
global $wpdb, $product, $post, $woocommerce, $user;
}
public function wanderlust_mp_account_menu_items( $items ) {
// Remove the logout menu item.
//$logout = $items['customer-logout'];
//unset( $items['customer-logout'] );
// Insert your custom endpoint.
//$items['mercadopago'] = 'Mercado Pago';
// Insert back the logout item.
//$items['customer-logout'] = $logout;
return $items;
}
public function my_account_mercadopago_endpoints() {
// add_rewrite_endpoint( 'mercadopago', EP_ROOT | EP_PAGES );
// flush_rewrite_rules();
}
public function mercadopago_endpoint_content() {
$callback = get_site_url();
$appid = get_option('wc_wanderlust_meli_mp_appid');
$secretkey = get_option('wc_wanderlust_meli_mp_secretkey');
$my_account_url = get_permalink( get_option('woocommerce_myaccount_page_id') );
$connect_url = 'https://auth.mercadopago.com.ar/authorization?client_id='.$appid.'&response_type=code&platform_id=mp&redirect_uri=https://ok.dashclick.net/dashboard/withdraw/';
$current_user = wp_get_current_user();
$mpdata = get_user_meta( $current_user->ID, 'mercado_pago_response', true);
$mpdata_origin = get_user_meta( $current_user->ID, 'mercado_pago_response_origin', true);
if(empty($mpdata)){
echo '<a href="'.$connect_url.'" style="padding: 15px 25px; height: 50px; line-height: 50px; background-color: #fcb800; transition: all 0.5s; color: #000; font-size: 16px; font-weight: 600; text-align: center; border: none;" > Autorizar Mercado Pago </a></br></br>';
} else {
$mpdata = json_decode($mpdata);
echo '<h3 style="background: #0099cc; color: white; padding: 15px 20px; width: 450px;">Tu cuenta fue vinculada con Mercado Pago - ID: '.$mpdata->user_id.'</h3>';
$dtF = new \DateTime('@0');
$dtT = new \DateTime("@$mpdata->expires_in");
$ac = $dtF->diff($dtT)->format('%a');
$date = date('Y-m-j');
$newdate = strtotime ( '+'.$ac.' day' , $mpdata_origin ) ;
$newdate = date ( 'j-m-Y' , $newdate );
$now = time(); // or your date as well
$your_date = strtotime($newdate);
$datediff = $your_date - $now ;
echo '<small>*Faltan '.round($datediff / (60 * 60 * 24)).' dias, para que expire la vinculación.</small> </br></br>';
echo '<a href="https://ok.dashclick.net/dashboard/withdraw/?remover=removemp" style="padding: 15px 25px; height: 50px; line-height: 50px; background-color: #fcb800; transition: all 0.5s; color: #000; font-size: 16px; font-weight: 600; text-align: center; border: none;" > Desvincular Mercado Pago </a></br></br>';
}
if($_GET['remover'] == 'removemp') {
update_user_meta( $current_user->ID, 'mercado_pago_response', '');
update_user_meta( $current_user->ID, 'mercado_pago_response_origin', '');
wp_safe_redirect( 'https://ok.dashclick.net/dashboard/withdraw/' );
}
if($_GET['code']) {
update_user_meta( $current_user->ID, 'mercado_pago_code', $_GET['code']);
$date = strtotime( date('Y-m-d') );
$access_token = $_GET['code'];
$data = array('site_id' => 'MLA');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL,'https://api.mercadopago.com/oauth/token?client_id='.$appid.'&client_secret='.$secretkey.'&grant_type=authorization_code&code='.$access_token.'&redirect_uri=https://ok.dashclick.net/dashboard/withdraw/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$response = curl_exec($ch);
$response_decoded = json_decode($response);
if($response_decoded->access_token){
update_option( 'wanderlust_meli_mp_'.$response_decoded->user_id , $response_decoded->access_token );
update_user_meta( $current_user->ID, 'mercado_pago_response', $response);
update_user_meta( $current_user->ID, 'mercado_pago_response_origin', $date);
wp_safe_redirect( 'https://ok.dashclick.net/dashboard/withdraw/' );
}
}
}
}