Settings', ); return array_merge( $links, $newlink ); } public function altapay_localization_init() { load_plugin_textdomain('altapay', false, dirname(plugin_basename(__FILE__)) . '/languages/'); } public function altapay_settings_menu() { add_submenu_page( 'woocommerce', 'Altapay Settings', 'Altapay Settings', 'manage_options', $this->plugin_options_key, array( $this, 'altapay_settings' ) ); } public function altapay_register_settings() { register_setting( 'altapay-settings-group', 'altapay_gateway_url' ); register_setting( 'altapay-settings-group', 'altapay_username' ); register_setting( 'altapay-settings-group', 'altapay_password' ); register_setting( 'altapay-settings-group', 'altapay_payment_page' ); register_setting( 'altapay-settings-group', 'altapay_terminals_enabled', 'json_encode' ); } public function altapay_settings() { $gateway_url = esc_attr( get_option('altapay_gateway_url') ); $username = esc_attr( get_option('altapay_username') ); $password = esc_attr( get_option('altapay_password') ); $payment_page = esc_attr( get_option('altapay_payment_page') ); $terminals = json_decode( get_option('altapay_terminals') ); $terminals_enabled = json_decode( get_option('altapay_terminals_enabled') ); if (!$terminals_enabled || $terminals_enabled == 'null') { $terminals_enabled = array(); } ?>

ID == $payment_page) { $exists = true; $page_title = $page->post_title; $page_id = $page->ID; } } if ($exists) { ?> :

name; ?> key, $terminals_enabled)) { ?>checked="checked" />
altapay_refresh_connection_form(); } } ?>

login(); if (!$response->wasSuccessful()) { echo '

'.__('Could not connect to Altapay', 'altapay').'

'; } else { echo '

'.__('Connection OK', 'altapay').'

'; ?> getTerminals(); $terminals = array(); $terms = $terminal_info->getTerminals(); foreach ($terms as $terminal) { $terminals[] = array( 'key' => str_replace(' ','_', $terminal->getTitle()), 'name' => $terminal->getTitle(), ); } update_option('altapay_terminals', json_encode($terminals)); } } } } // Init altapay settings and gateway function init_altapay_settings() { // Make sure WooCommerce and WooCommerce gateway is enabled and loaded if (!class_exists( 'WC_Payment_Gateway' )) { return; } $settings = new Altapay_settings(); // Add Gateway to WooCommerce if enabled if ( json_decode( get_option('altapay_terminals_enabled') ) ) { add_filter( 'woocommerce_payment_gateways', 'altapay_add_gateway' ); } // Define default functions using traits trait AltapayMaster { public function init_form_fields() { // Define form setting fields $this->form_fields = array( 'enabled' => array( 'title' => __( 'Enable/Disable', 'woocommerce' ), 'type' => 'checkbox', 'label' => __( 'Enable AltaPay', 'altapay' ), 'default' => 'yes' ), 'title' => array( 'title' => __( 'Title', 'woocommerce' ), 'type' => 'text', 'description' => __( 'Title to show during checkout.', 'altapay' ), 'default' => __( 'AltaPay', 'woocommerce' ), 'desc_tip' => true, ), 'description' => array( 'title' => __( 'Message', 'woocommerce' ), 'description' => __( 'Message to show during checkout.', 'altapay' ), 'type' => 'textarea', 'default' => '', ), 'payment_action' => array( 'title' => __( 'Payment action', 'woocommerce' ), 'description' => __( 'Make payment authorized or authorized and captured', 'altapay' ), 'type' => 'select', 'options' => array( 'authorize' => __('Authorize Only', 'altapay'), 'authorize_capture' => __('Authorize and Capture', 'altapay'), ), 'default' => '', ), 'currency' => array( 'title' => __( 'Currency', 'altapay' ), 'type' => 'select', 'description' => __( 'Select the currency does this terminal work with' ), 'options' => get_woocommerce_currencies(), 'default' => $this->default_currency, ), ); } public function process_payment( $order_id ) { global $woocommerce; $order = new WC_Order( $order_id ); // Return goto payment url return array( 'result' => 'success', 'redirect' => $order->get_checkout_payment_url( true ), ); } public function api_login() { $api = new PensioMerchantAPI(esc_attr( get_option('altapay_gateway_url') ), esc_attr( get_option('altapay_username') ), esc_attr( get_option('altapay_password') ), null); $response = $api->login(); if(!$response->wasSuccessful()) { return new WP_Error( 'error', "Could not login to the Merchant API: ".$response->getErrorMessage()); } return $api; } public function is_available() { // Check if payment method is enabled if ( 'yes' !== $this->enabled ) { return false; } //Check on payment currency $currency = $this->currency; $active_currency = get_woocommerce_currency(); if ($active_currency != $this->currency) { return false; } return true; } public function process_refund( $order_id, $amount = null, $reason = '' ) { $order = new WC_Order( $order_id ); $txnid = $order->get_transaction_id(); // Check if order has transaction ID if (!$order->get_transaction_id()) { return new WP_Error( 'error', __( 'Refund Failed: No transaction ID', 'altapay' ) ); } if ($txnid) { // Validate if transactions exists in Altapay $api = $this->api_login(); // Check if order have been captured before, because we cannot do a release on a already captured order if (get_post_meta($order_id, '_captured', true) || get_post_meta($order_id, '_refunded', true)) { $result = $api->refundCapturedReservation($txnid, $amount); if($result->wasSuccessful()) { update_post_meta( $order_id, '_refunded', true ); } } else { if ($order->get_remaining_refund_amount() == 0) { $result = $api->releaseReservation($txnid); } else { $result = $api->refundCapturedReservation($txnid, $amount); if($result->wasSuccessful()) { update_post_meta( $order_id, '_refunded', true ); } } } if(!$result->wasSuccessful()) { $order->add_order_note( __('Refund failed: ' . $result->getmerchantErrorMessage(), 'altapay') ); return new WP_Error( 'error', __( 'Refund Failed: '.$result->getmerchantErrorMessage(), 'altapay' ) ); } else { $order->add_order_note( __('Order refunded: amount '.$amount, 'altapay') ); } } return true; } public function scheduled_subscription_payment( $amount, $renewal_order ) { try { if( $amount == 0 ) { $renewal_order->payment_complete(); return; } $transaction_id = ''; if ( wcs_order_contains_renewal( $renewal_order->id ) ) { //$parent_order = wcs_get_subscriptions_for_renewal_order( $renewal_order->id ); $parent_order_id = WC_Subscriptions_Renewal_Order::get_parent_order_id( $renewal_order->id ); } $orderinfo = new WC_Order($parent_order_id); $transaction_id = $orderinfo->get_transaction_id(); if( !$transaction_id ) { // Set subscription payment as failure $renewal_order->update_status( 'failed', __( 'Altapay could not locate transaction ID', 'altapay' ) ); return false; } $api = $this->api_login(); $result = $api->chargeSubscription( $transaction_id, $amount ); if ($result->wasSuccessful()) { $renewal_order->payment_complete(); } else { $renewal_order->update_status( 'failed', sprintf( __( 'Altapay payment declined: %s', 'altapay' ), $result->getErrorMessage() ) ); } } catch (Exception $e) { $renewal_order->update_status( 'failed', sprintf( __( 'Altapay payment declined: %s', 'altapay' ), $e->getMessage() ) ); } } public function altapay_get_currency_code( $number ) { $codes = array( '004' => 'AFA','012' => 'DZD','020' => 'ADP','031' => 'AZM','032' => 'ARS','036' => 'AUD','044' => 'BSD','048' => 'BHD', '050' => 'BDT','051' => 'AMD','052' => 'BBD','060' => 'BMD','064' => 'BTN','068' => 'BOB','072' => 'BWP','084' => 'BZD', '096' => 'BND','100' => 'BGL','108' => 'BIF','124' => 'CAD','132' => 'CVE','152' => 'CLP','156' => 'CNY','170' => 'COP', '188' => 'CRC','191' => 'HRK','192' => 'CUP','196' => 'CYP','203' => 'CZK','208' => 'DKK','214' => 'DOP','218' => 'ECS', '230' => 'ETB','232' => 'ERN','233' => 'EEK','238' => 'FKP','242' => 'FJD','262' => 'DJF','270' => 'GMD','288' => 'GHC', '292' => 'GIP','320' => 'GTQ','324' => 'GNF','328' => 'GYD','340' => 'HNL','344' => 'HKD','532' => 'ANG','533' => 'AWG', '578' => 'NOK','624' => 'GWP','752' => 'SEK','756' => 'CHF','784' => 'AED','818' => 'EGP','826' => 'GBP','840' => 'USD', '973' => 'AOA','974' => 'BYR','975' => 'BGN','976' => 'CDF','977' => 'BAM','978' => 'EUR','981' => 'GEL','983' => 'ECV', '984' => 'BOV','986' => 'BRL','990' => 'CLF' ); return $codes[$number]; } } // Add gateways to WooCommerce. function altapay_add_gateway($methods) { // Get enabled terminals $terminals = json_decode( get_option('altapay_terminals_enabled') ); if ($terminals) { foreach ($terminals as $terminal) { // Load Terminal information $terminal_info = json_decode( get_option('altapay_terminals') ); $terminal_name = $terminal; foreach ($terminal_info as $term) { if ($term->key == $terminal) { $terminal_name = $term->name; } } // Check if file exists $dir = plugin_dir_path( __FILE__ ); $tmpdir = sys_get_temp_dir(); $path = $dir.'terminals/'.$terminal.'.class.php'; $tmppath = $tmpdir.'/'.$terminal.'.class.php'; if (file_exists($path)) { require_once ($path); $methods[$terminal] = 'WC_Gateway_'.$terminal; } elseif (file_exists($tmppath)) { require_once ($tmppath); $methods[$terminal] = 'WC_Gateway_'.$terminal; } else { // Create file $template = file_get_contents($dir.'templates/payment_class.tpl'); // Replace patterns $content = str_replace(array('{key}','{name}'),array($terminal,$terminal_name), $template); $terminal_dir = $dir.'terminals'; $filename = $dir.'terminals/'.$terminal.'.class.php'; // Check if terminals folder is writable or use tmp as fallback if (is_writable($terminal_dir)) { file_put_contents($filename, $content); } else { $filename = $tmpdir.'/'.$terminal.'.class.php'; file_put_contents($filename, $content); } } } } return $methods; } /// Make sure payment page form loads payment template add_filter( 'template_include', 'altapay_page_template', 99 ); function altapay_page_template( $template ) { // Get payment form page id $payment_form_page_id = esc_attr( get_option('altapay_payment_page') ); if ( is_page( $payment_form_page_id ) ) { // Make sure the template is only loaded from Altapay //if ($_SERVER['REMOTE_ADDR'] == '77.66.40.133') { // Load template override $template = locate_template( 'altapay-payment-form.php', true, true ); // If no template override load template from plugin if ( !$template ) { $template = dirname(__FILE__) . '/templates/altapay-payment-form.php'; } //} } return $template; } // Capture function add_action( 'add_meta_boxes', 'altapay_add_meta_boxes' ); add_action( 'wp_ajax_altapay_action', 'altapay_action_callback' ); add_action( 'admin_footer', 'altapay_action_javascript' ); // Create payment page function add_action( 'wp_ajax_create_altapay_payment_page', 'create_altapay_payment_page_callback' ); function altapay_add_meta_boxes() { global $post, $woocommerce; // Load order $order = new WC_Order($post->ID); $payment_method = $order->payment_method; // Only show on altapay orders if (strpos($payment_method,'altapay') !== false) { add_meta_box( 'altapay-actions', __( 'AltaPay actions', 'altapay' ), 'altapay_meta_box', 'shop_order', 'side', 'default' ); } return true; } function altapay_meta_box( $post ) { global $woocommerce; // Load order $order = new WC_Order($post->ID); // Validate if payment method is Altapay $payment_method = $order->payment_method; $txnid = $order->get_transaction_id(); //TODO Insert description here... if ($txnid) { // Validate is transactions exists in Altapay $api = new PensioMerchantAPI(esc_attr( get_option('altapay_gateway_url') ), esc_attr( get_option('altapay_username') ), esc_attr( get_option('altapay_password') ), null); $response = $api->login(); if(!$response->wasSuccessful()) { return new WP_Error( 'error', "Could not login to the Merchant API: ".$response->getErrorMessage()); } $payment = $api->getPayment($txnid); if ($payment) { $payments = $payment->getpayments(); foreach ($payments as $pay) { $reserved = $pay->getreservedAmount(); $captured = $pay->getcapturedAmount() - $pay->getrefundedAmount(); $refunded = $pay->getrefundedAmount(); $status = $pay->getCurrentStatus(); if ($status == 'released') { echo '
'.__('Payment released', 'altapay').''; } else { $charge = $reserved - $captured - $refunded; if ($charge <= 0) $charge = 0.00; echo '
'; echo '
Payment reserved: '.number_format($reserved, 2).' '.$order->get_order_currency().'
'; echo '
Payment captured: '.number_format($captured, 2).' '.$order->get_order_currency().'
'; echo '
Payment refunded: '.number_format($refunded, 2).' '.$order->get_order_currency().'
'; echo '
Payment chargeable: '.number_format($charge, 2).' '.$order->get_order_currency().'
'; if ($charge <= 0) { if ($reserved == $refunded) { echo '
'.__('Order refunded', 'altapay').''; } else { echo '
'.__('Order captured', 'altapay').''; } } else { echo '
Amount: '; echo 'Capture'; echo ''; } } } echo ''; } } else { echo __('Order got no transaction', 'altapay'); } } function altapay_action_javascript() { global $post; if(isset($post->ID)) { // Check if woocommerce order if ($post->post_type == 'shop_order') { $order = new WC_Order($post->ID); $payment_method = $order->payment_method; if (strpos($payment_method,'altapay') !== false) { ?> 'page', 'post_content' => '', 'post_parent' => 0, 'post_author' => $user_ID, 'post_status' => 'publish', 'post_title' => 'Altapay payment form', ); // Create page $page_id = wp_insert_post($page); if ($page_id == 0) { echo json_encode( array('status' => 'error', 'message' => __('Error creating page, try again','altapay')) ); } else { echo json_encode( array('status' => 'ok', 'message' => __('Payment page created','altapay'), 'page_id' => $page_id) ); } wp_die(); } function altapay_action_callback() { global $wpdb; $order_id = sanitize_text_field($_POST['order_id']); $amount = floatval($_POST['amount']); if (!$order_id || !$amount) { echo json_encode(array('status' => 'error', 'message' => 'error')); wp_die(); } // Load order $order = new WC_Order($order_id); $txnid = $order->get_transaction_id(); if ($txnid) { // Validate is transactions exists in Altapay $api = new PensioMerchantAPI(esc_attr( get_option('altapay_gateway_url') ), esc_attr( get_option('altapay_username') ), esc_attr( get_option('altapay_password') ), null); $response = $api->login(); if(!$response->wasSuccessful()) { return new WP_Error( 'error', "Could not login to the Merchant API: ".$response->getErrorMessage()); } // Capture amount $capture_result = $api->captureReservation($txnid, $amount); if(!$capture_result->wasSuccessful()) { // log to history $order->add_order_note( __('Capture failed: ' . $capture_result->getmerchantErrorMessage(), 'Altapay') ); echo json_encode( array('status' => 'error', 'message' => $capture_result->getmerchantErrorMessage()) ); } else { // Get payment data $payment = $api->getPayment($txnid); if ($payment) { $payments = $payment->getpayments(); foreach ($payments as $pay) { $reserved = $pay->getreservedAmount(); $captured = $pay->getcapturedAmount() - $pay->getrefundedAmount(); $refunded = $pay->getrefundedAmount(); $charge = $reserved - $captured - $refunded; if ($charge <= 0) $charge = 0.00; } } update_post_meta( $order_id, '_captured', true ); $order_note = __('Order captured: amount: '.$amount, 'Altapay'); $order->add_order_note( $order_note ); $note_html = '
  • '.$order_note.'

    '.sprintf( __( 'added on %1$s at %2$s', 'woocommerce' ), date_i18n( wc_date_format(), time() ), date_i18n( wc_time_format(), time() ) ).'

  • '; echo json_encode( array('status' => 'ok', 'captured' => number_format($captured,2), 'reserved' => number_format($reserved,2), 'refunded' => number_format($refunded,2), 'chargeable' => number_format($charge,2), 'message' => $capture_result, 'note' => $note_html) ); } } wp_die(); } } // Make sure plugins are loaded before running gateway add_action( 'plugins_loaded', 'init_altapay_settings', 0 );