sprintf(__( 'External Payment Method - %1$s', 'woo-vipps' ), "Vipps"), 'type' => 'title', ); $settings['epm_vipps_activate'] = array( 'title' => __( 'Activate', 'woo-vipps' ), 'type' => 'checkbox', 'description' => sprintf(__( 'Activate %1$s as an external payment method for Klarna Checkout', 'woo-vipps' ), "Vipps"), 'default' => 'yes', ); $settings['epm_vipps_name'] = array( 'title' => __( 'Name', 'woo-vipps' ), 'type' => 'text', 'description' => sprintf(__( 'Title for %1$s payment method. This controls the title which the user sees in the checkout form.', 'woo-vipps' ), "Vipps"), 'default' => sprintf(__( '%1$s', 'woo-vipps' ), "Vipps"), ); $settings['epm_vipps_description'] = array( 'title' => __( 'Description', 'woo-vipps' ), 'type' => 'textarea', 'description' => sprintf(__( 'Description for %1$s payment method. This controls the description which the user sees in the checkout form.', 'woo-vipps' ), "Vipps"), 'default' => '', ); $settings['epm_vipps_img_url'] = array( 'title' => __( 'Image url', 'woo-vipps' ), 'type' => 'text', 'description' => sprintf(__( 'URL to the %1$s logo', 'woo-vipps' ), "Vipps"), 'default' => plugins_url('img/vipps-rgb-sort.png',__FILE__) ); $settings['epm_vipps_disable_button'] = array( 'title' => __( 'Disable other gateway button', 'woo-vipps' ), 'type' => 'checkbox', 'description' => __( 'Disables the "Select another Payment method" button on the Klarna Checkout.', 'woo-vipps' ), 'default' => 'no', ); return $settings; } // Add Vipps as Payment Method to the KCO iframe. public static function create_order_vipps( $create ) { $merchant_urls = KCO_WC()->merchant_urls->get_urls(); $confirmation_url = $merchant_urls['confirmation']; $kco_settings = get_option( 'woocommerce_kco_settings' ); $activate = isset( $kco_settings['epm_vipps_activate'] ) ? ($kco_settings['epm_vipps_activate'] == 'yes') : true; global $Vipps; $activate = apply_filters( 'woo_vipps_activate_kco_external_payment', ($activate && $Vipps->gateway()->is_available())); // Klarna will absolutely cache your external payment methods, so to be able to deactivate these based on the // cart, we must send this always if (!isset($create['external_payment_methods']) || !is_array($create['external_payment_methods'])) { $create['external_payment_methods'] = array(); } if (!$activate) return $create; $name = isset( $kco_settings['epm_vipps_name'] ) ? $kco_settings['epm_vipps_name'] : ''; $image_url = isset( $kco_settings['epm_vipps_img_url'] ) ? $kco_settings['epm_vipps_img_url'] : ''; $description = isset( $kco_settings['epm_vipps_description'] ) ? $kco_settings['epm_vipps_description'] : ''; $klarna_external_payment = array( 'name' => $name, 'redirect_url' => add_query_arg( 'kco-external-payment', 'vipps', $confirmation_url ), 'image_url' => $image_url, 'description' => $description, ); if (!isset($create['external_payment_methods']) || !is_array($create['external_payment_methods'])) { $create['external_payment_methods'] = array(); } $create['external_payment_methods'][] = $klarna_external_payment; // Ensure we don't do Vipps as the default pament method. This is checked in "woocommerce_checkout_order_processed" hook. WC()->session->set('vipps_via_klarna', 1); return $create; } // On checkout, before submit, if external payment is selected and Vipps is selected as external payment, // using javascript, set payment method to Vipps and ensure terms and conditions are selected too. IOK public static function add_vipps_payment_method() { if ( isset( $_GET['kco-external-payment'] ) && 'vipps' == $_GET['kco-external-payment'] ) { ?> $('input#payment_method_vipps').prop('checked', true); // Check terms and conditions to prevent error. $('input#legal').prop('checked', true); session->get('vipps_via_klarna')) { WC()->session->set('chosen_payment_method', 'kco'); WC()->session->set('vipps_via_klarna', 0); } } // Based on settings, remove "Select another payment method" public static function maybe_remove_other_gateway_button() { $kco_settings = get_option( 'woocommerce_kco_settings' ); $disable_button = isset( $kco_settings['epm_vipps_disable_button'] ) ? $kco_settings['epm_vipps_disable_button'] : 'no'; $remove = ('yes' === $disable_button); // Let the user decide whether or not to use the 'use external payment method' button. IOK 2020-05-29 // This is present for legacy reasons only, and is probably not the one you want. See the 'woo_vipps_activate_kco_exteral_payment' filter instad. IOK 2021-05-14 $remove = apply_filters('woo_vipps_remove_klarna_another_payment_button', $remove); if ($remove) { remove_action( 'kco_wc_after_order_review', 'kco_wc_show_another_gateway_button', 20 ); } } // Change phone number format to the one expected by Vipps - no +47 public static function canonicalize_phone_number( $klarna_order ) { if ( isset( $_GET['kco-external-payment'] ) && 'vipps' == $_GET['kco-external-payment'] ) { $country_code = '47'; $phone_no = $klarna_order->billing_address->phone; $telefonnummer1 = preg_replace( "/^\+{$country_code}/", '', $phone_no ); $telefonnummer2 = str_replace( ' ', '', $telefonnummer1 ); $klarna_order->billing_address->phone = $telefonnummer2; } return $klarna_order; } }