add( self::METHOD_ID, $msg ); } public static function configure_aplazame_profile( $sandbox, $private_key, $redirect_id ) { $client = new Aplazame_Sdk_Api_Client( getenv( 'APLAZAME_API_BASE_URI' ) ? getenv( 'APLAZAME_API_BASE_URI' ) : 'https://api.aplazame.com', ($sandbox ? Aplazame_Sdk_Api_Client::ENVIRONMENT_SANDBOX : Aplazame_Sdk_Api_Client::ENVIRONMENT_PRODUCTION), $private_key ); $response = $client->patch( '/me', array( 'confirmation_url' => add_query_arg( array( 'action' => 'aplazame_api', 'path' => '/confirm/', ), get_permalink( $redirect_id ) ), ) ); return $response; } /** * @var array */ public $settings; /** * @var null|bool Null when the plugin is not configured yet. */ public $enabled; /** * @var bool */ public $sandbox; /** * @var string */ public $apiBaseUri; /** * @var Aplazame_Redirect */ public $redirect; /** * @param string $apiBaseUri */ public function __construct( $apiBaseUri ) { // Dependencies include_once( 'classes/lib/Helpers.php' ); include_once( 'classes/lib/Redirect.php' ); register_uninstall_hook( __FILE__, 'WC_Aplazame_Install::uninstall' ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) ); add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway' ) ); // i18n load_plugin_textdomain( 'aplazame', false, dirname( plugin_basename( __FILE__ ) ) . '/i18n/languages' ); // Settings register_activation_hook( __FILE__, 'WC_Aplazame_Install::resetSettings' ); $this->settings = get_option( 'woocommerce_aplazame_settings' ); if ( ! $this->settings ) { $this->settings = WC_Aplazame_Install::resetSettings(); } else { $this->settings = array_merge( WC_Aplazame_Install::$defaultSettings, $this->settings ); } $this->enabled = $this->settings['enabled'] === 'yes'; $this->sandbox = $this->settings['sandbox'] === 'yes'; $this->apiBaseUri = $apiBaseUri; $this->private_api_key = $this->settings['private_api_key']; // Aplazame JS add_action( 'wp_head', array( $this, 'aplazameJs' ), 999999 ); // Redirect $this->redirect = new Aplazame_Redirect(); register_activation_hook( __FILE__, array( $this->redirect, 'addRedirectPage' ) ); register_deactivation_hook( __FILE__, array( $this->redirect, 'removeRedirectPage' ) ); add_action( 'init', array( 'WC_Aplazame_Install', 'upgrade' ), 5 ); register_activation_hook( __FILE__, 'WC_Aplazame_Install::upgrade' ); add_action( 'wp_footer', array( $this->redirect, 'checkout' ) ); // TODO: Redirect nav // add_filter('wp_nav_menu_objects', '?'); // Router to action add_filter( 'template_include', array( $this, 'router' ) ); // Widgets add_action( 'woocommerce_single_product_summary', array( $this, 'product_widget', ), 100 ); add_action( 'woocommerce_after_cart_totals', array( $this, 'cart_widget', ), 100 ); // Handlers add_action( 'woocommerce_order_status_cancelled', array( $this, 'order_cancelled', ) ); add_action( 'woocommerce_order_status_refunded', array( $this, 'order_cancelled', ) ); add_filter( 'woocommerce_product_data_tabs', array( $this, 'aplazame_campaigns_tab' ) ); add_action( 'woocommerce_product_data_panels', array( $this, 'product_campaigns' ) ); } public function aplazame_campaigns_tab( $tabs ) { $tabs['aplazame_campaigns'] = array( 'label' => __( 'Aplazame Campaigns', 'aplazame' ), 'target' => 'aplazame_campaigns_tab', ); return $tabs; } public function product_campaigns() { Aplazame_Helpers::render_to_template( 'product/campaigns.php' ); } /** * Add relevant links to plugins page * * @param array $links * @return array */ public function plugin_action_links( $links ) { $plugin_links = array( '' . __( 'Settings', 'aplazame' ) . '', ); return array_merge( $plugin_links, $links ); } /** * @return Aplazame_Client */ public function get_client() { include_once( 'classes/sdk/Client.php' ); return new Aplazame_Client( $this->apiBaseUri, $this->sandbox, $this->private_api_key ); } /** * @param int|object|WC_Order $order_id . * @param string $msg */ public function add_order_note( $order_id, $msg ) { $order = new WC_Order( $order_id ); $order->add_order_note( $msg ); } // Hooks /** * @param array $methods * * @return array|void */ public function add_gateway( $methods ) { if ( ! class_exists( 'WC_Payment_Gateway' ) ) { return; } include_once( 'classes/wc-aplazame-gateway.php' ); $methods[] = 'WC_Aplazame_Gateway'; return $methods; } // Controllers /** * @param string $template * * @return null|string */ public function router( $template ) { if ( ! isset( $_GET['action'] ) || ! $this->redirect->isRedirect( get_the_ID() ) ) { return $template; } switch ( $_GET['action'] ) { case 'aplazame_api': $path = isset( $_GET['path'] ) ? $_GET['path'] : ''; $pathArguments = isset( $_GET['path_arguments'] ) ? json_decode( stripslashes_deep( $_GET['path_arguments'] ), true ) : array(); $queryArguments = isset( $_GET['query_arguments'] ) ? json_decode( stripslashes_deep( $_GET['query_arguments'] ), true ) : array(); $payload = json_decode( file_get_contents( 'php://input' ), true ); include_once( 'classes/api/Aplazame_Api_Router.php' ); $api = new Aplazame_Api_Router( $this->private_api_key, $this->sandbox ); $api->process( $path, $pathArguments, $queryArguments, $payload ); // die break; case 'history': include_once( 'classes/Aplazame_History.php' ); $api = new Aplazame_History( $this->private_api_key ); $api->process( $_GET['order_id'] ); // die } return $template; } public function aplazameJs() { Aplazame_Helpers::render_to_template( 'layout/header.php' ); } // Widgets public function product_widget() { Aplazame_Helpers::render_to_template( 'widgets/product.php' ); } public function cart_widget() { Aplazame_Helpers::render_to_template( 'widgets/cart.php' ); } // Handlers (no return) /** * @param int $order_id */ public function order_cancelled( $order_id ) { if ( ! static::is_aplazame_order( $order_id ) ) { return; } $client = $this->get_client(); try { $client->cancel( $order_id ); } catch ( Exception $e ) { $this->add_order_note( $order_id, sprintf( __( '%s ERROR: Order #%s cannot be cancelled. Reason: %s', 'aplazame' ), self::METHOD_TITLE, $order_id, $e->getMessage() ) ); return; } $this->add_order_note( $order_id, sprintf( __( 'Order #%s has been successful cancelled by %s.', 'aplazame' ), $order_id, self::METHOD_TITLE ) ); } // Static /** * @param int $order_id * * @return bool */ protected static function is_aplazame_order( $order_id ) { return Aplazame_Helpers::get_payment_method( $order_id ) === self::METHOD_ID; } } class WC_Aplazame_Install { public static $defaultSettings = array( 'enabled' => null, 'sandbox' => 'yes', 'button' => '#payment ul li:has(input#payment_method_aplazame)', 'quantity_selector' => '', 'price_product_selector' => '', 'price_variable_product_selector' => '#main [itemtype="http://schema.org/Product"] .single_variation_wrap .amount', 'public_api_key' => '', 'private_api_key' => '', ); public static function upgrade() { if ( version_compare( get_option( 'aplazame_version' ), WC_Aplazame::VERSION, '<' ) ) { self::set_aplazame_profile(); self::update_aplazame_version(); } } public static function uninstall() { self::removeSettings(); } /** * @return array */ public static function resetSettings() { add_option( 'woocommerce_aplazame_settings', self::$defaultSettings ); return self::$defaultSettings; } public static function removeSettings() { delete_option( 'woocommerce_aplazame_settings' ); } private static function update_aplazame_version() { delete_option( 'aplazame_version' ); add_option( 'aplazame_version', WC_Aplazame::VERSION ); } private static function set_aplazame_profile() { /** @var WC_Aplazame $aplazame */ global $aplazame; if ( ! $aplazame->private_api_key ) { return; } try { WC_Aplazame::configure_aplazame_profile( $aplazame->settings['sandbox'], $aplazame->private_api_key, $aplazame->redirect->id ); } catch (Exception $e) { $aplazame->private_api_key = null; $aplazame->settings['private_api_key'] = null; } } } $GLOBALS['aplazame'] = new WC_Aplazame( defined( 'APLAZAME_API_BASE_URI' ) ? APLAZAME_API_BASE_URI : 'https://api.aplazame.com' ); include_once( 'classes/wc-aplazame-proxy.php' );