id = 'afterpay_belgium'; $this->method_title = __( 'AfterPay Belgium Digital Invoice', 'afterpay' ); $this->has_fields = true; // 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->portfolioid = ( isset( $this->settings['portfolioid'] ) ) ? $this->settings['portfolioid'] : ''; $this->password = ( isset( $this->settings['password'] ) ) ? $this->settings['password'] : ''; $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'] : ''; // Advanced settings. $this->show_phone = ( isset( $this->settings['show_phone'] ) ) ? $this->settings['show_phone'] : ''; $this->show_gender = ( isset( $this->settings['show_gender'] ) ) ? $this->settings['show_gender'] : ''; $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->order_update_mail = ( isset( $this->settings['order_update_mail'] ) ) ? $this->settings['order_update_mail'] : ''; // 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'] : ''; if ( isset( $this->settings['refunds'] ) && 'yes' === $this->settings['refunds'] ) { $this->supports = array( 'refunds' ); } // Country information. $afterpay_country = 'BE'; $afterpay_language = 'NL'; $afterpay_currency = 'EUR'; $afterpay_invoice_terms = 'https://www.afterpay.be/nl/klantenservice/betalingsvoorwaarden/'; $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->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_action( 'woocommerce_api_' . strtolower( get_class( $this ) ), array( &$this, 'push_afterpay_order_state' ) ); // 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_be_openinvoice_form_fields', array( 'enabled' => array( 'title' => __( 'Enable/Disable', 'afterpay' ), 'type' => 'checkbox', 'label' => __( 'Enable AfterPay Belgium Digital Invoice', 'afterpay' ), 'default' => 'no', ), 'title' => array( 'title' => __( 'Title', 'afterpay' ), 'type' => 'text', 'description' => __( 'This controls the title which the user sees during checkout.', 'afterpay' ), 'default' => __( 'Veilig achteraf betalen', 'afterpay' ), ), 'extra_information' => array( 'title' => __( 'Extra information', 'afterpay' ), 'type' => 'textarea', 'description' => __( 'Extra information to show to the customer in the checkout', 'afterpay' ), 'default' => '', ), 'merchantid' => array( 'title' => __( 'Merchant ID', 'afterpay' ), 'type' => 'text', 'description' => __( 'Please enter your AfterPay Merchant ID; this is needed in order to take payment!', 'afterpay' ), 'default' => '', ), 'portfolioid' => array( 'title' => __( 'Portfolio number', 'afterpay' ), 'type' => 'text', 'description' => __( 'Please enter your AfterPay Portfolio Number; this is needed in order to take payment!', 'afterpay' ), 'default' => '', ), 'password' => array( 'title' => __( 'Portefeuille password', 'afterpay' ), 'type' => 'text', 'description' => __( 'Please enter your AfterPay Portfolio Password; 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' => 'checkbox', 'label' => __( 'Enable AfterPay Test Mode. This will only work if you have a AfterPay test account. For test purchases with a live account.', 'afterpay' ), 'default' => 'yes', ), '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', ), ), '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_gender' => array( 'title' => __( 'Show gender', 'afterpay' ), 'type' => 'checkbox', 'description' => __( 'Show gender field in AfterPay form in the 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', ), 'order_update_mail' => array( 'title' => __( 'Order update mail', 'afterpay' ), 'type' => 'text', 'description' => __( 'Use order update mail to send a notification mail when the order is accepted after pending state. Leave empty to disable.', 'afterpay' ), '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' => 'no', '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' => 'no', 'class' => 'afterpay_advanced_setting', ), ) ); } /** * Function to proces push messages from AfterPay Belgium * * @access public * @return void */ public function push_afterpay_order_state() { global $woocommerce; // Check if POST contains all values, else quit. if ( ! isset( $_POST['merchantId'] ) || ! isset( $_POST['portefeuilleId'] ) || ! isset( $_POST['orderReference'] ) || ! isset( $_POST['statusCode'] ) || ! isset( $_POST['signature'] ) ) { $pushlog = 'Required field is missing'; $this->mail_afterpay_debug_log( $pushlog ); return; } // Set POST variable. $merchant_id = wc_clean( sanitize_text_field( wp_unslash( $_POST['merchantId'] ) ) ); $portefeuille_id = wc_clean( sanitize_text_field( wp_unslash( $_POST['portefeuilleId'] ) ) ); $order_reference = wc_clean( sanitize_text_field( wp_unslash( $_POST['orderReference'] ) ) ); $status_code = wc_clean( sanitize_text_field( wp_unslash( $_POST['statusCode'] ) ) ); $signature = wc_clean( sanitize_text_field( wp_unslash( $_POST['signature'] ) ) ); $hash = $merchant_id . $portefeuille_id . $this->password . $order_reference . $status_code; $pushlog .= "\n Hash: " . $hash; $checksum = md5( $hash ); $pushlog .= "\n Checksum: " . $checksum; $pushlog .= "\n Signature: " . $signature; // Check if Checksum is the same as the signature. if ( $signature !== $checksum ) { $pushlog .= "\n" . 'Checksum does not match signature'; $this->mail_afterpay_debug_log( $pushlog ); return; } $pushlog .= "\n Signature: " . $order_reference; $order = new WC_order( $order_reference ); // phpcs:disable WordPress.PHP.DevelopmentFunctions $pushlog .= print_r( $order, 1 ); // phpcs:enable if ( 'on-hold' !== $order->get_status() ) { $pushlog .= "\n" . 'Order ' . $order_reference . 'does not exists or does not have the state: on-hold'; $this->mail_afterpay_debug_log( $pushlog ); return; } switch ( $status_code ) { case 'A': $pushlog .= "\n" . 'The status is A, so try to complete the payment of the order'; // Set Payment complete. $order->add_order_note( __( 'The order is accepted by AfterPay and can be processed', 'afterpay' ) ); $order->payment_complete(); // Sent notification to admin. wp_mail( $this->order_update_mail, 'ORDER ' . $order_reference . ' is accepted by AfterPay and can be processed', 'Dear sir/madam, The order ' . $order_reference . ' is accepted by AfterPay and can be processed. With regards, AfterPay / Woocommerce Plugin' ); $this->mail_afterpay_debug_log( $pushlog ); break; case 'W': $pushlog .= "\n" . 'The status is W, so try to cancel the order'; // Cancel the order. $order->add_order_note( __( 'The order is rejected by AfterPay and can be cancelled', 'afterpay' ) ); $order->cancel_order(); $this->mail_afterpay_debug_log( $pushlog ); break; case 'V': $pushlog .= "\n" . 'The status is V, so try to cancel the order'; // Cancel the order. $order->add_order_note( __( 'The order is removed by AfterPay and can be cancelled', 'afterpay' ) ); $order->cancel_order(); $this->mail_afterpay_debug_log( $pushlog ); break; case 'P': $pushlog .= "\n" . 'The status is still pending, so do nothing'; $order->add_order_note( __( 'The order is still pending by AfterPay', 'afterpay' ) ); $this->mail_afterpay_debug_log( $pushlog ); break; default: $pushlog .= "\n" . 'Error in push, statuscode is missing'; $this->mail_afterpay_debug_log( $pushlog ); break; } } /** * Function to send AfterPay push log * * @access public * @param string $pushlog The current log variable. * @return void **/ public function mail_afterpay_debug_log( $pushlog ) { global $woocommerce; if ( '' !== $this->debug_mail ) { wp_mail( $this->debug_mail, 'WOOCOMMERCE PUSH LOG', $pushlog ); } } }