| 1 |
<?php |
| 2 |
/* |
| 3 |
Initializes the admin settings page which uses React for the Vipps plugin |
| 4 |
|
| 5 |
This file is part of the plugin Pay with Vipps and MobilePay for WooCommerce |
| 6 |
Copyright (c) 2019 WP-Hosting AS |
| 7 |
|
| 8 |
MIT License |
| 9 |
|
| 10 |
Copyright (c) 2019 WP-Hosting AS |
| 11 |
|
| 12 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 |
of this software and associated documentation files (the "Software"), to deal |
| 14 |
in the Software without restriction, including without limitation the rights |
| 15 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 16 |
copies of the Software, and to permit persons to whom the Software is |
| 17 |
furnished to do so, subject to the following conditions: |
| 18 |
|
| 19 |
The above copyright notice and this permission notice shall be included in all |
| 20 |
copies or substantial portions of the Software. |
| 21 |
|
| 22 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 28 |
SOFTWARE. |
| 29 |
|
| 30 |
*/ |
| 31 |
if (!defined('ABSPATH')) { |
| 32 |
exit; // Exit if accessed directly |
| 33 |
} |
| 34 |
|
| 35 |
class VippsAdminSettings |
| 36 |
{ |
| 37 |
private static $instance = null; |
| 38 |
|
| 39 |
// This returns the singleton instance of this class |
| 40 |
public static function instance() |
| 41 |
{ |
| 42 |
if (null === self::$instance) { |
| 43 |
self::$instance = new self(); |
| 44 |
} |
| 45 |
return self::$instance; |
| 46 |
} |
| 47 |
|
| 48 |
// Get the singleton WC_GatewayVipps instance |
| 49 |
public function gateway() |
| 50 |
{ |
| 51 |
global $Vipps; |
| 52 |
if (class_exists('WC_Payment_Gateway')) { |
| 53 |
// Try to load payment gateways first, we need that infrastructure. IOK 2024-06-05 |
| 54 |
$gateways = WC()->payment_gateways(); |
| 55 |
return WC_Gateway_Vipps::instance(); |
| 56 |
} else { |
| 57 |
$Vipps->log(__("Error: Cannot instantiate payment gateway, because WooCommerce is not loaded! This can happen when WooCommerce updates itself; but if it didn't, please activate WooCommerce again", 'woo-vipps'), 'error'); |
| 58 |
return null; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// Handle the submission of the admin settings page |
| 63 |
public function ajax_vipps_update_admin_settings() |
| 64 |
{ |
| 65 |
$ok = wp_verify_nonce($_REQUEST['vippsadmin_nonce'], 'vippsadmin_nonce'); |
| 66 |
if (!$ok) { |
| 67 |
echo json_encode(array('ok' => 0, 'options' => [], 'msg' => __('You don\'t have sufficient rights to edit these settings', 'woo-vipps'))); |
| 68 |
exit(); |
| 69 |
} |
| 70 |
if (!current_user_can('manage_woocommerce')) { |
| 71 |
echo json_encode(array('ok' => 0, 'options' => [], 'msg' => __('You don\'t have sufficient rights to edit these settings', 'woo-vipps'))); |
| 72 |
exit(); |
| 73 |
} |
| 74 |
|
| 75 |
// Decode the settings from the values sents, then save them to "woocommerce_vipps_settings" and "woocommerce_vipps_card_settings" |
| 76 |
$new_settings = $_POST['values']; |
| 77 |
|
| 78 |
|
| 79 |
// IOK TODO This will ensure sanitization etc works as it is supposed to using the |
| 80 |
// admin settings api of WooCommerce. We will however want to run this code independently, so we'll handle this |
| 81 |
// by ourselves at a later point, ending it like so: |
| 82 |
// update_option('woocommerce_vipps_settings', $new_settings); // After sanitation etc |
| 83 |
// update_option('woocommerce_vipps_card_settings', $card_settings); // Separated out |
| 84 |
|
| 85 |
$admin_options = []; |
| 86 |
$card_admin_options= []; |
| 87 |
foreach ($new_settings as $key => $value) { |
| 88 |
// Checkbox settings (yes/no) can't be passed directly to set_post_data, because *any* value will be interpreted as "yes" |
| 89 |
// because of this, checkbox settings with the value "no" will be completely ignored |
| 90 |
if($value !== 'no') { |
| 91 |
// Because settings are processed manually, their keys need to prefixed with "woocommerce_<gatewayid>_" |
| 92 |
// options with prefix cc_ are for the credit card gateway. |
| 93 |
if (preg_match("!^cc_!", $key)) { |
| 94 |
$key = preg_replace("!^cc_!", "", $key); |
| 95 |
$card_admin_options['woocommerce_vipps_card_' . $key] = $value; |
| 96 |
} else { |
| 97 |
$admin_options['woocommerce_vipps_' . $key] = $value; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// Save options for the main gateway first |
| 103 |
// We need to initialize these again so "conditional" options are available |
| 104 |
// IOK 2024-06-05 |
| 105 |
$this->gateway()->init_form_fields(); |
| 106 |
$this->gateway()->set_post_data($admin_options); |
| 107 |
$this->gateway()->process_admin_options(); |
| 108 |
// $this->gateway()->add_error("Jaboloko!"); // Also add_warning, add_notice plz |
| 109 |
$form_errors = $this->gateway()->get_errors(); |
| 110 |
|
| 111 |
// Then the card gateway |
| 112 |
$cards= WC_Gateway_VippsCard::instance(); |
| 113 |
$cards->init_form_fields(); |
| 114 |
$cards->set_post_data($card_admin_options); |
| 115 |
$cards->process_admin_options(); |
| 116 |
$form_errors = array_merge($form_errors, $cards->get_errors()); |
| 117 |
|
| 118 |
|
| 119 |
$form_ok = empty($form_errors); |
| 120 |
// end use of process_admin_options IOK 2024-01-03 |
| 121 |
|
| 122 |
$connection_msg = ""; |
| 123 |
// Verify the connection to Vipps |
| 124 |
list($connection_ok, $error_message) = $this->gateway()->check_connection(); |
| 125 |
if ($connection_ok) { |
| 126 |
$connection_msg .= sprintf(__("Connection to %1\$s is OK", 'woo-vipps'), Vipps::CompanyName()); |
| 127 |
} else { |
| 128 |
$connection_msg .= sprintf(__("Could not connect to %1\$s", 'woo-vipps'), Vipps::CompanyName()) . ": $error_message"; |
| 129 |
} |
| 130 |
|
| 131 |
// Get the current options |
| 132 |
$options = get_option('woocommerce_vipps_settings'); |
| 133 |
// Augment with card gateway, with prefix |
| 134 |
$card_options = get_option('woocommerce_vipps_card_settings'); |
| 135 |
foreach($card_options as $key => $data) { |
| 136 |
$options["cc_$key"] = $data; |
| 137 |
} |
| 138 |
|
| 139 |
// Add receipt image URL if receipt image exists |
| 140 |
if (!empty($new_settings['receiptimage'])) { |
| 141 |
$options['receiptimage_url'] = wp_get_attachment_url($new_settings['receiptimage']); |
| 142 |
} |
| 143 |
|
| 144 |
// Return the result, sending the new options, the connection status/message and the form status/errors |
| 145 |
echo json_encode( |
| 146 |
array( |
| 147 |
'connection_ok' => $connection_ok, // Whether the connection to the Vipps servers is OK. |
| 148 |
'connection_msg' => $connection_msg, // The connection message, whether the connection is OK or not. |
| 149 |
'form_ok' => $form_ok, // Whether the form fields are OK. |
| 150 |
'form_errors' => $form_errors, // The form field errors, if any. |
| 151 |
'options' => $options // The new options |
| 152 |
) |
| 153 |
); |
| 154 |
exit(); |
| 155 |
} |
| 156 |
|
| 157 |
// Initializes the admin settings UI for VippsMobilePay |
| 158 |
function init_admin_settings_page_react_ui() { |
| 159 |
global $Vipps; |
| 160 |
echo "<div class='wrap vipps-admin-settings-page'>"; |
| 161 |
// Add nonce first. |
| 162 |
wp_nonce_field('vippsadmin_nonce', 'vippsadmin_nonce'); |
| 163 |
|
| 164 |
// We must first generate the root element for the React UI before we load the React app itself, otherwise React will fail to load. |
| 165 |
?> |
| 166 |
<div id="vipps-mobilepay-react-ui"></div> |
| 167 |
<?php |
| 168 |
|
| 169 |
// Initializing the wordpress media plugin, so we can upload images |
| 170 |
wp_enqueue_media(); |
| 171 |
$gw = $this->gateway(); |
| 172 |
$card_gw = WC_Gateway_VippsCard::instance(); |
| 173 |
|
| 174 |
// Loads the React UI |
| 175 |
$reactpath = "dist"; |
| 176 |
wp_enqueue_script('vipps-mobilepay-react-ui', plugins_url($reactpath . '/plugin.js', __FILE__), array('wp-element'), filemtime(__DIR__ . "/$reactpath/plugin.js"), true); |
| 177 |
// wp_enqueue_style('vipps-mobilepay-react-ui', plugins_url($reactpath . '/plugin.css', __FILE__), array(), filemtime(__DIR__ . "/$reactpath/plugin.css")); |
| 178 |
|
| 179 |
$metadata = array( |
| 180 |
'admin_url' => admin_url('admin-ajax.php'), |
| 181 |
'page' => 'admin_settings_page', |
| 182 |
'currency' => get_woocommerce_currency(), |
| 183 |
// for debugging/testing: show wizard screen always IOK 2025-01-20 |
| 184 |
'__dev_force_wizard_screen' => defined('WOO_VIPPS_FORCE_WIZARD') && WOO_VIPPS_FORCE_WIZARD, |
| 185 |
// Only show checkout options if checkout has actually been activated. |
| 186 |
'vipps_checkout_activated' => intval(get_option('woo_vipps_checkout_activated')) |
| 187 |
); |
| 188 |
|
| 189 |
// Add some extra common translations only used by the React UI |
| 190 |
$commonTranslations = array( |
| 191 |
'save_changes' => __('Save changes', 'woo-vipps'), |
| 192 |
'initial_settings' => __('Initial settings', 'woo-vipps'), |
| 193 |
'upload_image' => __('Upload image', 'woo-vipps'), |
| 194 |
'remove_image' => __('Remove image', 'woo-vipps'), |
| 195 |
'next_step' => __('Next step', 'woo-vipps'), |
| 196 |
'previous_step' => __('Previous step', 'woo-vipps'), |
| 197 |
'receipt_image_size_requirement' => __('The image must be at least 167 pixels in height', 'woo-vipps'), |
| 198 |
'receipt_image_error' => __('The uploaded image is too small. It must be at least 167 pixels in height.', 'woo-vipps'), |
| 199 |
'settings_saved' => __('Settings saved', 'woo-vipps'), |
| 200 |
|
| 201 |
'kustom_sale_1' => __('Checkout - Important Update', 'woo-vipps'), |
| 202 |
'kustom_sale_2' => __('Vipps MobilePay has entered into an agreement to sell the Checkout solution to Kustom. As part of this transition, <b>Vipps MobilePay Checkout will become Kustom Checkout</b>. You can follow <a href="https://docs.kustom.co/contents/partners/e-commerce-platforms/woocommerce-vipps-guide#switch-from-vipps-checkout-to-kustom-checkoutguide" target="_blank">this guide</a> to migrate over to Kustom Checkout.', 'woo-vipps'), |
| 203 |
'kustom_sale_3' => __('Going forward, Kustom will be responsible for delivering and developing the Checkout solution. <b>Vipps MobilePay will remain available as a payment method in Kustom Checkout</b>, so your customers can continue to pay with Vipps MobilePay in the familiar way.', 'woo-vipps'), |
| 204 |
'kustom_sale_4' => __('Please note that <b>accounts created after March 27, 2026 will not support Checkout in this plugin</b>.', 'woo-vipps'), |
| 205 |
'kustom_sale_5' => __('If you have any questions about what the transition means for you, please see our <a href="https://vippsmobilepay.com/en-NO/vippsmobilepay-kustom" target="_blank">FAQ</a>.', 'woo-vipps'), |
| 206 |
'kustom_sale_6' => sprintf(__('For help you can reach out to <a href="mailto:%1$s">%1$s</a> and <a href="tel:%2$s">%3$s</a>. You can also find the Kustom portal <a href="%4$s" target="_blank">here</a>.', 'woo-vipps'), 'support@kustom.co', '+4721564684', '+47 21 56 46 84', 'https://portal.kustom.co/'), |
| 207 |
); |
| 208 |
|
| 209 |
/* We need to postprocess the settings for.. various reasons IOK 2024-06-04 */ |
| 210 |
/* Also we need to run init_form_fields here, because for whatever reason the |
| 211 |
* first time it is called, it does wrong things in this context. IOK 2024-06-04 */ |
| 212 |
$gw->init_form_fields(); |
| 213 |
$settings = $gw->settings; |
| 214 |
|
| 215 |
$wizardTranslations = [ |
| 216 |
'wizard_header' => [ |
| 217 |
'title' => __('Initial settings', 'woo-vipps'), |
| 218 |
'description' => sprintf(__('Welcome! You are almost ready to accept payments with %1$s', 'woo-vipps'), Vipps::CompanyName()), |
| 219 |
], |
| 220 |
'checkout_options_wizard' => array( |
| 221 |
'title' => sprintf(__('Get started with %1$s', 'woo-vipps'), Vipps::CheckoutName()), |
| 222 |
'description' => sprintf(__('%1$s is a service from %2$s, which allows you to replace the usual WooCommerce checkout page with a super simple checkout screen, where your customers can pay with Vipps, Visa, and MasterCard!', 'woo-vipps'), Vipps::CheckoutName(), Vipps::CompanyName()), |
| 223 |
), |
| 224 |
'enablestaticshipping_checkout_wizard' => array( |
| 225 |
'title' => __('Are you going to base shipping price on the customers address?', 'woo-vipps'), |
| 226 |
'label' => __('Yes, I want dynamic shipping calculation', 'woo-vipps'), |
| 227 |
'description' => __('If your shipping prices are the same no matter where the customer lives, you don\'t need dynamic shipping calculation.'), |
| 228 |
), |
| 229 |
'checkoutcreateuser_wizard' => array( |
| 230 |
'title' => sprintf(__('Do you want to create new customers on %1$s?', 'woo-vipps'), Vipps::CheckoutName()), |
| 231 |
'label' => sprintf(__('Yes, create new customers on %1$s', 'woo-vipps'), Vipps::CheckoutName()), |
| 232 |
'description' => sprintf(__('By creating new customers, you avoid orders showing up as guest orders. We recommend combining this with Log in with %1$s for a full overview of your customers.', 'woo-vipps'), Vipps::CompanyName()), |
| 233 |
), |
| 234 |
'noAddressFields_wizard' => array( |
| 235 |
'title' => __('Do you want the customer to enter their address?', 'woo-vipps'), |
| 236 |
'label' => __('Yes, all customers must enter their address', 'woo-vipps'), |
| 237 |
'description' => __('If you only sell products that are not to be shipped, you don\'t need the customer to enter their address.', 'woo-vipps'), |
| 238 |
), |
| 239 |
'checkout_shipping_wizard' => array( |
| 240 |
'title' => sprintf(__('Shipping alternatives available with %1$s', 'woo-vipps'), Vipps::CheckoutName()), |
| 241 |
'description' => sprintf(__('When you use %1$s, you have a variety of shipping options to choose from, giving you even more choices from certain shipping providers.'), Vipps::CheckoutName()) |
| 242 |
), |
| 243 |
'vcs_posten_wizard' => array( |
| 244 |
'title' => __('Posten Norge', 'woo-vipps'), |
| 245 |
'custom_attributes' => array('data-vcs-show' => '.vcs_depend.vcs_posten'), |
| 246 |
'label' => __('Offer Posten Norge as a shipping method', 'woo-vipps'), |
| 247 |
'description' => __('Select this to offer this shipping method.', 'woo-vipps'), |
| 248 |
), |
| 249 |
'vcs_helthjem_wizard' => array( |
| 250 |
'title' => __('Helthjem', 'woo-vipps'), |
| 251 |
'label' => __('Offer Helthjem as a shipping method', 'woo-vipps'), |
| 252 |
'description' => __('Select this to offer this shipping method.', 'woo-vipps'), |
| 253 |
), |
| 254 |
'vcs_posti_wizard' => array( |
| 255 |
'title' => __('Posti', 'woo-vipps'), |
| 256 |
'label' => __('Offer Posti as a shipping method', 'woo-vipps'), |
| 257 |
'description' => __('Select this to offer this shipping method.', 'woo-vipps'), |
| 258 |
), |
| 259 |
'vcs_postnord_wizard' => array( |
| 260 |
'title' => __('PostNord', 'woo-vipps'), |
| 261 |
'label' => __('Offer PostNord as a shipping method', 'woo-vipps'), |
| 262 |
'description' => __('Select this to offer this shipping method.', 'woo-vipps'), |
| 263 |
), |
| 264 |
'vcs_porterbuddy_wizard' => array( |
| 265 |
'title' => __('Porterbuddy', 'woo-vipps'), |
| 266 |
'label' => __('Offer Porterbuddy as a shipping method', 'woo-vipps'), |
| 267 |
'description' => __('Select this to offer this shipping method.', 'woo-vipps'), |
| 268 |
), |
| 269 |
'help_box' => [ |
| 270 |
'get_started' => __('Get started', 'woo_vipps'), |
| 271 |
'documentation' => __('Documentation', 'woo-vipps'), |
| 272 |
'portal' => sprintf(__('%1$s Portal', 'woo-vipps'), Vipps::CompanyName()), |
| 273 |
'support' => [ |
| 274 |
'title' => __('Support', 'woo-vipps'), |
| 275 |
'description' => __('If you have any questions related to this plugin, you are welcome to check out the <a href="https://wordpress.org/support/plugin/woo-vipps/" target="_blank">support forum.</a>', 'woo-vipps'), |
| 276 |
], |
| 277 |
], |
| 278 |
'checkout_confirm' => [ |
| 279 |
'title' => sprintf(__('Upgrade your customer experience with %1$s', 'woo-vipps'), Vipps::CheckoutName()), |
| 280 |
'img' => [ |
| 281 |
'vipps' => [ |
| 282 |
'src' => plugins_url('img/no_vippsmobilepay_checkout_app-teaser_2400x1600.webp', WC_VIPPS_PAYMENT_MAIN_FILE), |
| 283 |
'alt' => __('Sketch of', 'woo-vipps') . ' Vipps Checkout', // dont translate checkout name |
| 284 |
], |
| 285 |
'mobilepay' => [ |
| 286 |
'src' => plugins_url('img/dk_vm_checkout_app-teaser_2400x1600.webp', WC_VIPPS_PAYMENT_MAIN_FILE), |
| 287 |
'alt' => __('Sketch of', 'woo-vipps') . ' MobilePay Checkout', |
| 288 |
], |
| 289 |
], |
| 290 |
'paragraph1' => [ |
| 291 |
'header' => sprintf(__('Why %1$s?', 'woo-vipps'), Vipps::CheckoutName()), |
| 292 |
'first' => __('Effortless Payments: Accept payments smoothly with Vipps/MobilePay, Visa, and Mastercard.', 'woo-vipps'), |
| 293 |
'second' => __('Streamlined Shopping Process: Simplify your customers\' journey from cart to confirmation', 'woo-vipps'), |
| 294 |
'third' => __('All-in-One Solution: No fixed monthly fees-just pure convenience.', 'woo-vipps'), |
| 295 |
], |
| 296 |
'paragraph2' => [ |
| 297 |
'header' => __('Perfect for you if:', 'woo-vipps'), |
| 298 |
'first' => __('You want simple, varied payment options', 'woo-vipps'), |
| 299 |
'second' => __('You need easy shipping solutions with diverse choices.', 'woo-vipps'), |
| 300 |
], |
| 301 |
'accept' => sprintf(__('Start using %1$s', 'woo-vipps'), Vipps::CheckoutName()), |
| 302 |
'skip' => __('Skip & save', 'woo-vipps'), |
| 303 |
], |
| 304 |
'express_options_wizard' => array( |
| 305 |
'title' => sprintf(__('Get started with %1$s', 'woo-vipps'), Vipps::ExpressCheckoutName()), |
| 306 |
'description' => sprintf(__("%1\$s allows you to buy products by a single click from the cart, checkout, or directly from product or catalog pages. Product will get a 'buy now' button which will start the purchase process immediately.", 'woo-vipps'), Vipps::ExpressCheckoutName()), |
| 307 |
'readmore' => __('Read more', 'woo-vipps'), |
| 308 |
), |
| 309 |
'singleproductexpressarchives_wizard' => [ |
| 310 |
'title' => sprintf(__('%s on product catalog pages', 'woo-vipps'), Vipps::ExpressCheckoutName()), |
| 311 |
'label' => sprintf(__('Enable a %s button for relevant products on catalog pages', 'woo-vipps'), Vipps::ExpressCheckoutName()), |
| 312 |
], |
| 313 |
]; |
| 314 |
|
| 315 |
if (!empty($settings['receiptimage'])) { |
| 316 |
$settings['receiptimage_url'] = wp_get_attachment_url($settings['receiptimage']); |
| 317 |
} |
| 318 |
if (!$gw->allow_external_payments_in_checkout()) { |
| 319 |
unset($settings['checkout_external_payments_klarna']); |
| 320 |
} else { |
| 321 |
// nop right now |
| 322 |
} |
| 323 |
|
| 324 |
/* Add the options from the credit card gateway too, with a cc_prefix which we strip on process_payments. IOK 2026-05-27*/ |
| 325 |
$card_gw->init_form_fields(); |
| 326 |
$cc_translations = []; |
| 327 |
foreach($card_gw->settings as $key => $data) { |
| 328 |
$settings["cc_$key"] = $data; |
| 329 |
} |
| 330 |
foreach($card_gw->form_fields as $key => $data) { |
| 331 |
$cc_translations["cc_$key"] = $data; |
| 332 |
} |
| 333 |
$cc_translations['cc_options'] = [ |
| 334 |
'title' => __('Card payments', 'woo-vipps'), |
| 335 |
'description' => __("Provide a card payment method for customers that haven't got the app (yet!)", 'woo-vipps'), |
| 336 |
'test_mode_warning' => sprintf(__('Warning: card payments may not yet be available in the test environment, please check the %1$s <a href="https://developer.vippsmobilepay.com/docs/knowledge-base/test-environment/">knowledge base</a> for updated status about the test environment.'), Vipps::CompanyName()), |
| 337 |
]; |
| 338 |
|
| 339 |
wp_localize_script('vipps-mobilepay-react-ui', 'VippsMobilePayReactTranslations', array_merge($gw->form_fields, $cc_translations, $commonTranslations, $wizardTranslations)); |
| 340 |
wp_localize_script('vipps-mobilepay-react-ui', 'VippsMobilePayReactOptions', $settings); |
| 341 |
wp_localize_script('vipps-mobilepay-react-ui', 'VippsMobilePayReactMetadata', $metadata); |
| 342 |
|
| 343 |
echo "</div>"; |
| 344 |
} |
| 345 |
} |
| 346 |
?> |
| 347 |
|