startSession();
// Load order
$order = new WC_Order ($order_id);
$txnid = $order->get_transaction_id();
if (! $txnid) {
return;
}
// Login to Altapay gateway:
$api = new PensioMerchantAPI (esc_attr (get_option('altapay_gateway_url')), esc_attr (get_option('altapay_username')), esc_attr (get_option('altapay_password')), null);
try {
$api->login();
}
catch (Exception $e) {
$_SESSION['altapay_login_error'] = $e->getMessage();
return;
}
$payment = $api->getPayment($txnid);
if (! $payment) {
return;
}
$payments = $payment->getPayments();
$pay = $payments[0];
if ($pay->getCapturedAmount() == 0) { // Order wasn't captured and must be captured now
$amount = $pay->getReservedAmount(); // Amount to capture
$salesTax = $order->get_total_tax();
//TODO: add orderLines, if is set to parse them
$orderLines = array(array());
$capture_result = $api->captureReservation($txnid, $amount, $orderLines, $salesTax);
if(! $capture_result->wasSuccessful()) {
$order->add_order_note (__('Capture failed: ' . $capture_result->getMerchantErrorMessage(), 'Altapay')); // log to history
$this->save_capture_failed_message ('Capture failed for order ' . $order_id . ': ' . $capture_result->getMerchantErrorMessage());
return;
}
else {
update_post_meta ($order_id, '_captured', true);
$order->add_order_note (__('Order captured: amount: ' . $amount, 'Altapay'));
}
}
else {
$this->save_capture_warning ('This order was already fully or partially captured: ' . $order_id);
}
}
function save_capture_warning ($newMessage) {
if (isset($_SESSION ['altapay_capture_warning'])) {
$message = $_SESSION ['altapay_capture_warning'] . "
";
}
else {
$message = "";
}
$_SESSION ['altapay_capture_warning'] = $message . $newMessage;
}
function save_capture_failed_message ($newMessage) {
if (isset($_SESSION ['altapay_capture_failed'])) {
$message = $_SESSION ['altapay_capture_failed'] . "
";
} else {
$message = "";
}
$_SESSION ['altapay_capture_failed'] = $message . $newMessage;
}
function show_user_message ($field, $type, $message = '') {
$this->startSession();
if (! isset($_SESSION [$field])) {
return;
}
echo "
$message $_SESSION[$field]
'.__('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 $active_currency = get_woocommerce_currency(); if ($active_currency != $this->currency) { return false; } 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 ) && $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' ); // 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_capture', 'altapay_capture_callback' ); add_action( 'wp_ajax_altapay_refund', 'altapay_refund_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; if ($post->post_type != 'shop_order') { return true; } // 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', 'normal' ); } return true; } function altapay_meta_box( $post ) { global $woocommerce; // Load order $order = new WC_Order($post->ID); $orderItems = $order->get_items(); $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()); } $payment = $api->getPayment($txnid); if ($payment) { $payments = $payment->getPayments(); foreach ($payments as $pay) { $reserved = $pay->getReservedAmount(); $captured = $pay->getCapturedAmount(); $refunded = $pay->getRefundedAmount(); $status = $pay->getCurrentStatus(); if ($status == 'released') { echo '| Product name | Price with tax | Price without tax | Ordered | Quantity | Total amount |
| ' . $itemData->get_product()->get_name() . ' | '; echo '' . $incTax . ' | '; echo '' . $excTax . ' | '; echo '' . $qty . ' | '; echo ''; echo ' | ' . $totalIncTax . ' | '; echo '
| ' . $order->get_shipping_method(). ' | '; echo '' . $totalIncTax . ' | '; echo '' . $excTax . ' | '; echo '' . 1 . ' | '; echo ''; echo ' | ' . $totalIncTax . ' | '; echo '
'.$order_note.'
'.$orderNote.'