id = 'afterpay_fi_installments'; $this->method_title = __( 'AfterPay Finland Installments', 'afterpay' ); $this->has_fields = true; // Setup field to load installmentPlans in. $this->available_installment_plans = array(); // Load the form fields. $this->init_form_fields(); $this->show_bankaccount = false; $this->order_type = 'B2C'; // Load the settings. $this->init_settings(); // Define user set variables for basic settings. $this->enabled = ( isset( $this->settings['enabled'] ) ) ? $this->settings['enabled'] : ''; $this->title = ( isset( $this->settings['title'] ) ) ? $this->settings['title'] : ''; $this->extra_information = ( isset( $this->settings['extra_information'] ) ) ? $this->settings['extra_information'] : ''; $this->merchantid = ( isset( $this->settings['merchantid'] ) ) ? $this->settings['merchantid'] : ''; $this->lower_threshold = ( isset( $this->settings['lower_threshold'] ) ) ? $this->settings['lower_threshold'] : ''; $this->upper_threshold = ( isset( $this->settings['upper_threshold'] ) ) ? $this->settings['upper_threshold'] : ''; $this->testmode = ( isset( $this->settings['testmode'] ) ) ? $this->settings['testmode'] : ''; $this->debug_mail = ( isset( $this->settings['debug_mail'] ) ) ? $this->settings['debug_mail'] : ''; $this->ip_restriction = ( isset( $this->settings['ip_restriction'] ) ) ? $this->settings['ip_restriction'] : ''; $this->show_advanced = ( isset( $this->settings['show_advanced'] ) ) ? $this->settings['show_advanced'] : 'no'; // Advanced settings. $this->show_phone = ( isset( $this->settings['show_phone'] ) ) ? $this->settings['show_phone'] : ''; $this->show_termsandconditions = ( isset( $this->settings['show_termsandconditions'] ) ) ? $this->settings['show_termsandconditions'] : ''; $this->use_custom_housenumber_field = ( isset( $this->settings['use_custom_housenumber_field'] ) ) ? $this->settings['use_custom_housenumber_field'] : ''; $this->use_custom_housenumber_addition_field = ( isset( $this->settings['use_custom_housenumber_addition_field'] ) ) ? $this->settings['use_custom_housenumber_addition_field'] : ''; $this->ip_restriction = ( isset( $this->settings['ip_restriction'] ) ) ? $this->settings['ip_restriction'] : ''; // Captures and refunds. $this->captures = ( isset( $this->settings['captures'] ) ) ? $this->settings['captures'] : ''; $this->captures_way = ( isset( $this->settings['captures_way'] ) ) ? $this->settings['captures_way'] : ''; $this->captures_way_based_on_status = ( isset( $this->settings['captures_way_based_on_status'] ) ) ? $this->settings['captures_way_based_on_status'] : ''; $this->refunds = ( isset( $this->settings['refunds'] ) ) ? $this->settings['refunds'] : ''; $this->refund_tax_percentage = ( isset( $this->settings['refund_tax_percentage'] ) ) ? $this->settings['refund_tax_percentage'] : ''; if ( isset( $this->settings['refunds'] ) && 'yes' === $this->settings['refunds'] ) { $this->supports = array( 'refunds' ); } $afterpay_country = 'FI'; $afterpay_language = 'FI'; $afterpay_currency = 'EUR'; $afterpay_invoice_terms = 'https://documents.myafterpay.com/consumer-terms-conditions/fi_fi/' . $this->merchantid; $afterpay_installment_terms = ''; $afterpay_privacy_statement = 'https://documents.myafterpay.com/privacy-statement/fi_fi/' . $this->merchantid; $afterpay_invoice_icon = plugins_url( basename( dirname( __FILE__ ) ) . '/images/afterpay.png' ); // Apply filters to Country and language. $this->afterpay_country = apply_filters( 'afterpay_country', $afterpay_country ); $this->afterpay_language = apply_filters( 'afterpay_language', $afterpay_language ); $this->afterpay_currency = apply_filters( 'afterpay_currency', $afterpay_currency ); $this->afterpay_invoice_terms = apply_filters( 'afterpay_invoice_terms', $afterpay_invoice_terms ); $this->afterpay_installment_terms = apply_filters( 'afterpay_installment_terms', $afterpay_installment_terms ); $this->afterpay_privacy_statement = apply_filters( 'afterpay_privacy_statement', $afterpay_privacy_statement ); $this->icon = apply_filters( 'afterpay_invoice_icon', $afterpay_invoice_icon ); // Actions. /* 2.0.0 */ add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); add_action( 'woocommerce_receipt_afterpay', array( &$this, 'receipt_page' ) ); // Add event handler for an order Status change. add_action( 'woocommerce_order_status_changed', array( $this, 'order_status_change_callback' ), 1000, 4 ); } /** * Initialise Gateway Settings Form Fields * * @access public * @return void */ public function init_form_fields() { $this->form_fields = apply_filters( 'afterpay_fi_installments_form_fields', array( 'enabled' => array( 'title' => __( 'Enable/Disable', 'afterpay' ), 'type' => 'checkbox', 'label' => __( 'Enable AfterPay Finland Installments', 'afterpay' ), 'default' => 'no', ), 'title' => array( 'title' => __( 'Title', 'afterpay' ), 'type' => 'text', 'description' => __( 'This controls the title which the user sees during checkout.', 'afterpay' ), 'default' => __( 'Maksa joustavasti', 'afterpay' ), ), 'extra_information' => array( 'title' => __( 'Extra information', 'afterpay' ), 'type' => 'textarea', 'description' => __( 'Extra information to show to the customer in the checkout', 'afterpay' ), 'default' => '', ), 'api_key' => array( 'title' => __( 'API Key', 'afterpay' ), 'type' => 'text', 'description' => __( 'Please enter your AfterPay API Key; this is needed in order to take payment!', 'afterpay' ), 'default' => '', ), 'lower_threshold' => array( 'title' => __( 'Lower threshold', 'afterpay' ), 'type' => 'text', 'description' => __( 'Disable AfterPay Invoice if Cart Total is lower than the specified value. Leave blank to disable this feature.', 'afterpay' ), 'default' => '5', ), 'upper_threshold' => array( 'title' => __( 'Upper threshold', 'afterpay' ), 'type' => 'text', 'description' => __( 'Disable AfterPay Invoice if Cart Total is higher than the specified value. Leave blank to disable this feature.', 'afterpay' ), 'default' => '', ), 'testmode' => array( 'title' => __( 'Test Mode', 'afterpay' ), 'type' => 'select', 'description' => __( 'Enable or disable test mode. The sandbox option is for testing with API keys from developer.afterpay.io.', 'afterpay' ), 'options' => array( 'yes' => 'Yes', 'sandbox' => 'Yes - Sandbox', 'no' => 'No', ), ), 'debug_mail' => array( 'title' => __( 'Debug mail', 'afterpay' ), 'type' => 'text', 'description' => __( 'Use debug mail to send the complete AfterPay request to your mail, for debug functionality only. Leave empty to disable.', 'afterpay' ), 'default' => '', ), 'ip_restriction' => array( 'title' => __( 'IP restriction', 'afterpay' ), 'type' => 'text', 'description' => __( 'Fill in IP address to only show the payment method for that specific IP address. Leave empty to disable', 'afterpay' ), 'default' => '', ), 'show_advanced' => array( 'title' => __( 'Show advanced settings', 'afterpay' ), 'type' => 'select', 'description' => __( 'Show advanced settings' ), 'options' => array( 'yes' => 'Yes', 'no' => 'No', ), 'default' => 'no', ), 'display_settings' => array( 'title' => __( 'Display settings', 'afterpay' ), 'type' => 'title', 'description' => '', 'class' => 'afterpay_advanced_setting', ), 'show_phone' => array( 'title' => __( 'Show phone number', 'afterpay' ), 'type' => 'checkbox', 'description' => __( 'Show phone number field instead of Woocommerce default field. If this field is missing in default checkout', 'afterpay' ), 'default' => 'no', 'class' => 'afterpay_advanced_setting', ), 'show_termsandconditions' => array( 'title' => __( 'Show terms and conditions', 'afterpay' ), 'type' => 'checkbox', 'description' => __( 'Show terms and conditions of AfterPay', 'afterpay' ), 'default' => 'yes', 'class' => 'afterpay_advanced_setting', ), 'use_custom_housenumber_field' => array( 'title' => __( 'Use custom housenumber field', 'afterpay' ), 'type' => 'text', 'description' => __( 'Fill in the custom field name used for the housenumber, without billing_ or shipping_', 'afterpay' ), 'default' => '', 'class' => 'afterpay_advanced_setting', ), 'use_custom_housenumber_addition_field' => array( 'title' => __( 'Use custom housenumber addition field', 'afterpay' ), 'type' => 'text', 'description' => __( 'Fill in the custom field name used for the housenumber addition, without billing_ or shipping_', 'afterpay' ), 'default' => '', 'class' => 'afterpay_advanced_setting', ), 'merchantid' => array( 'title' => __( 'Merchant ID', 'afterpay' ), 'type' => 'text', 'default' => '', 'class' => 'afterpay_advanced_setting', ), 'captures_and_refunds_section' => array( 'title' => __( 'Captures and refunds', 'afterpay' ), 'type' => 'title', 'description' => '', 'class' => 'afterpay_advanced_setting', ), 'captures' => array( 'title' => __( 'Enable captures', 'afterpay' ), 'type' => 'checkbox', 'label' => __( 'Enable capturing', 'afterpay' ), 'default' => 'yes', 'class' => 'afterpay_advanced_setting', ), 'captures_way' => array( 'title' => __( 'Way of captures', 'afterpay' ), 'label' => __( 'Way of capturing', 'afterpay' ), 'type' => 'select', 'default' => 'auto_after_authorization', 'options' => array( 'auto_after_authorization' => __( 'Automatically after authorization', 'afterpay' ), 'based_on_status' => __( 'Based on Woocommerce Status', 'afterpay' ), ), 'class' => 'afterpay_advanced_setting', ), 'captures_way_based_on_status' => array( 'title' => __( 'Status to capture based on', 'afterpay' ), 'label' => __( 'Status to capture based on', 'afterpay' ), 'type' => 'select', 'options' => $this->get_all_possible_order_statuses(), 'class' => 'afterpay_advanced_setting', ), 'refunds' => array( 'title' => __( 'Enable refunds', 'afterpay' ), 'type' => 'checkbox', 'label' => __( 'Enable refunding', 'afterpay' ), 'default' => 'yes', 'class' => 'afterpay_advanced_setting', ), 'refund_tax_percentage' => array( 'title' => __( 'Refund tax percentage', 'afterpay' ), 'type' => 'text', 'label' => __( 'Default percentage calculated on refunds to AfterPay', 'afterpay' ), 'default' => '0', 'class' => 'afterpay_advanced_setting', ), ) ); } /** * Check if this gateway is enabled and available in the user's country * * @access public * @return boolean */ public function is_available() { global $woocommerce; if ( 'yes' === $this->enabled && $woocommerce->cart ) { // Cart totals check - Lower threshold. if ( ! is_admin() && '' !== $this->lower_threshold ) { if ( $woocommerce->cart->subtotal < $this->lower_threshold ) { return false; } } // Cart totals check - Upper threshold. if ( ! is_admin() && '' !== $this->upper_threshold ) { if ( $woocommerce->cart->subtotal > $this->upper_threshold ) { return false; } } // Only activate the payment gateway if the customers country is the same as the filtered shop country. if ( ! is_admin() ) { if ( $woocommerce->customer->get_billing_country() !== $this->afterpay_country ) { return false; } } // Check if variable with ip's contains the ip of the client. if ( ! is_admin() && '' !== $this->ip_restriction ) { if ( strpos( $this->ip_restriction, $this->get_afterpay_client_ip() ) === false ) { return false; } } // Check if subtotal is more than 0, if setup request for available installment plans. if ( $woocommerce->cart->total > 0 ) { require_once __DIR__ . '/vendor/autoload.php'; // Create AfterPay object. $afterpay = new \Afterpay\Afterpay(); $afterpay->setRest(); $afterpay->set_ordermanagement( 'installmentplans_lookup' ); // Get connection mode. $afterpay_mode = $this->get_connection_mode(); // Set up the additional information. $aporder['amount'] = $woocommerce->cart->total; $authorisation['apiKey'] = $this->settings['api_key']; $afterpay->set_order( $aporder, 'OM' ); $afterpay->do_request( $authorisation, $afterpay_mode ); // Check if the response from the webservice was correct, else the payment method is not available. if ( ! isset( $afterpay->order_result->return->availableInstallmentPlans ) ) { return false; } else { $this->available_installment_plans = $afterpay->order_result->return->availableInstallmentPlans; } return true; } } return false; } /** * Payment form on checkout page * * @acces public * @return void */ public function payment_fields() { global $woocommerce; if ( 'yes' === $this->testmode ) : ?>
testmode ) : ?> show_phone && ! isset( $_POST[ $this->id . '_phone' ] ) ) { wc_add_notice( __( 'Phone number is a required field', 'afterpay' ), 'error' ); return false; } if ( 'yes' === $this->show_termsandconditions && ! isset( $_POST[ $this->id . '_terms' ] ) ) { wc_add_notice( __( 'Please accept the AfterPay terms.', 'afterpay' ), 'error' ); return false; } if ( ! isset( $_POST[ $this->id . '_ssn' ] ) ) { wc_add_notice( __( 'Social security number is a required field', 'afterpay' ), 'error' ); return false; } if ( ! isset( $_POST[ $this->id . '_installmentplan' ] ) ) { wc_add_notice( __( 'Please choose an installment plan.', 'afterpay' ), 'error' ); return false; } return true; } }