| 1 |
<?php |
| 2 |
/* |
| 3 |
This is the VippsKCSupport class, a static class implementing support for the Klarna Checkout plugin, if installed. |
| 4 |
|
| 5 |
|
| 6 |
This file is part of the plugin Pay with Vipps and MobilePay for WooCommerce |
| 7 |
Copyright (c) 2019 WP-Hosting AS |
| 8 |
|
| 9 |
MIT License |
| 10 |
|
| 11 |
Copyright (c) 2019 WP-Hosting AS |
| 12 |
|
| 13 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 |
of this software and associated documentation files (the "Software"), to deal |
| 15 |
in the Software without restriction, including without limitation the rights |
| 16 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 |
copies of the Software, and to permit persons to whom the Software is |
| 18 |
furnished to do so, subject to the following conditions: |
| 19 |
|
| 20 |
The above copyright notice and this permission notice shall be included in all |
| 21 |
copies or substantial portions of the Software. |
| 22 |
|
| 23 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 |
SOFTWARE. |
| 30 |
|
| 31 |
*/ |
| 32 |
if ( ! defined( 'ABSPATH' ) ) { |
| 33 |
exit; // Exit if accessed directly |
| 34 |
} |
| 35 |
|
| 36 |
class VippsKCSupport { |
| 37 |
|
| 38 |
// To be run in "after_setup_theme" - extend Klarna Checkout with support for the Vipps external payment method. |
| 39 |
public static function init() { |
| 40 |
add_filter( 'kco_wc_gateway_settings', array('VippsKCSupport','form_fields')); |
| 41 |
add_filter( 'kco_wc_api_request_args', array('VippsKCSupport','create_order_vipps' ), 90); |
| 42 |
add_filter( 'kco_wc_klarna_order_pre_submit', array('VippsKCSupport','canonicalize_phone_number'), 11 ); |
| 43 |
add_action( 'init', array('VippsKCSupport','maybe_remove_other_gateway_button' )); |
| 44 |
add_action( 'kco_wc_before_submit', array('VippsKCSupport','add_vipps_payment_method' )); |
| 45 |
add_action( 'woocommerce_checkout_order_processed', array('VippsKCSupport','reset_default_payment_method'), 10, 3); |
| 46 |
add_action( 'woo_vipps_before_redirect_to_vipps', array('VippsKCSupport','reset_default_payment_method'), 10, 1); |
| 47 |
} |
| 48 |
|
| 49 |
public static function form_fields( $settings ) { |
| 50 |
$settings['epm_vipps_settings_title'] = array( |
| 51 |
'title' => sprintf(__( 'External Payment Method - %1$s', 'woo-vipps' ), "Vipps"), |
| 52 |
'type' => 'title', |
| 53 |
); |
| 54 |
$settings['epm_vipps_activate'] = array( |
| 55 |
'title' => __( 'Activate', 'woo-vipps' ), |
| 56 |
'type' => 'checkbox', |
| 57 |
'description' => sprintf(__( 'Activate %1$s as an external payment method for Klarna Checkout', 'woo-vipps' ), "Vipps"), |
| 58 |
'default' => 'yes', |
| 59 |
); |
| 60 |
$settings['epm_vipps_name'] = array( |
| 61 |
'title' => __( 'Name', 'woo-vipps' ), |
| 62 |
'type' => 'text', |
| 63 |
'description' => sprintf(__( 'Title for %1$s payment method. This controls the title which the user sees in the checkout form.', 'woo-vipps' ), "Vipps"), |
| 64 |
'default' => sprintf(__( '%1$s', 'woo-vipps' ), "Vipps"), |
| 65 |
); |
| 66 |
$settings['epm_vipps_description'] = array( |
| 67 |
'title' => __( 'Description', 'woo-vipps' ), |
| 68 |
'type' => 'textarea', |
| 69 |
'description' => sprintf(__( 'Description for %1$s payment method. This controls the description which the user sees in the checkout form.', 'woo-vipps' ), "Vipps"), |
| 70 |
'default' => '', |
| 71 |
); |
| 72 |
$settings['epm_vipps_img_url'] = array( |
| 73 |
'title' => __( 'Image url', 'woo-vipps' ), |
| 74 |
'type' => 'text', |
| 75 |
'description' => sprintf(__( 'URL to the %1$s logo', 'woo-vipps' ), "Vipps"), |
| 76 |
'default' => plugins_url('img/vipps-rgb-sort.png',__FILE__) |
| 77 |
); |
| 78 |
$settings['epm_vipps_disable_button'] = array( |
| 79 |
'title' => __( 'Disable other gateway button', 'woo-vipps' ), |
| 80 |
'type' => 'checkbox', |
| 81 |
'description' => __( 'Disables the "Select another Payment method" button on the Klarna Checkout.', 'woo-vipps' ), |
| 82 |
'default' => 'no', |
| 83 |
); |
| 84 |
return $settings; |
| 85 |
} |
| 86 |
|
| 87 |
// Add Vipps as Payment Method to the KCO iframe. |
| 88 |
public static function create_order_vipps( $create ) { |
| 89 |
$merchant_urls = KCO_WC()->merchant_urls->get_urls(); |
| 90 |
$confirmation_url = $merchant_urls['confirmation']; |
| 91 |
|
| 92 |
$kco_settings = get_option( 'woocommerce_kco_settings' ); |
| 93 |
$activate = isset( $kco_settings['epm_vipps_activate'] ) ? ($kco_settings['epm_vipps_activate'] == 'yes') : true; |
| 94 |
|
| 95 |
global $Vipps; |
| 96 |
$activate = apply_filters( 'woo_vipps_activate_kco_external_payment', ($activate && $Vipps->gateway()->is_available())); |
| 97 |
|
| 98 |
// Klarna will absolutely cache your external payment methods, so to be able to deactivate these based on the |
| 99 |
// cart, we must send this always |
| 100 |
if (!isset($create['external_payment_methods']) || !is_array($create['external_payment_methods'])) { |
| 101 |
$create['external_payment_methods'] = array(); |
| 102 |
} |
| 103 |
if (!$activate) return $create; |
| 104 |
|
| 105 |
$name = isset( $kco_settings['epm_vipps_name'] ) ? $kco_settings['epm_vipps_name'] : ''; |
| 106 |
$image_url = isset( $kco_settings['epm_vipps_img_url'] ) ? $kco_settings['epm_vipps_img_url'] : ''; |
| 107 |
$description = isset( $kco_settings['epm_vipps_description'] ) ? $kco_settings['epm_vipps_description'] : ''; |
| 108 |
|
| 109 |
|
| 110 |
$klarna_external_payment = array( |
| 111 |
'name' => $name, |
| 112 |
'redirect_url' => add_query_arg( 'kco-external-payment', 'vipps', $confirmation_url ), |
| 113 |
'image_url' => $image_url, |
| 114 |
'description' => $description, |
| 115 |
); |
| 116 |
|
| 117 |
if (!isset($create['external_payment_methods']) || !is_array($create['external_payment_methods'])) { |
| 118 |
$create['external_payment_methods'] = array(); |
| 119 |
} |
| 120 |
$create['external_payment_methods'][] = $klarna_external_payment; |
| 121 |
|
| 122 |
// Ensure we don't do Vipps as the default pament method. This is checked in "woocommerce_checkout_order_processed" hook. |
| 123 |
WC()->session->set('vipps_via_klarna', 1); |
| 124 |
|
| 125 |
return $create; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
// On checkout, before submit, if external payment is selected and Vipps is selected as external payment, |
| 130 |
// using javascript, set payment method to Vipps and ensure terms and conditions are selected too. IOK |
| 131 |
public static function add_vipps_payment_method() { |
| 132 |
if ( isset( $_GET['kco-external-payment'] ) && 'vipps' == $_GET['kco-external-payment'] ) { ?> |
| 133 |
$('input#payment_method_vipps').prop('checked', true); |
| 134 |
// Check terms and conditions to prevent error. |
| 135 |
$('input#legal').prop('checked', true); |
| 136 |
<?php |
| 137 |
// In case other actions are needed, add more Javascript to this hook |
| 138 |
do_action('woo_vipps_klarna_checkout_support_on_submit_javascript'); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// Ensure, after order is processed, that default payment method isn't set to Vipps |
| 143 |
public static function reset_default_payment_method ($orderid, $post_data=null, $order=null) { |
| 144 |
// If payment method is Vipps because we did an external payment call from Klarna, ensure Klarna Checkout is default payment method (kco). 2020-05-18 IOK |
| 145 |
if (WC()->session->get('vipps_via_klarna')) { |
| 146 |
WC()->session->set('chosen_payment_method', 'kco'); |
| 147 |
WC()->session->set('vipps_via_klarna', 0); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// Based on settings, remove "Select another payment method" |
| 152 |
public static function maybe_remove_other_gateway_button() { |
| 153 |
$kco_settings = get_option( 'woocommerce_kco_settings' ); |
| 154 |
$disable_button = isset( $kco_settings['epm_vipps_disable_button'] ) ? $kco_settings['epm_vipps_disable_button'] : 'no'; |
| 155 |
$remove = ('yes' === $disable_button); |
| 156 |
|
| 157 |
// Let the user decide whether or not to use the 'use external payment method' button. IOK 2020-05-29 |
| 158 |
// 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 |
| 159 |
$remove = apply_filters('woo_vipps_remove_klarna_another_payment_button', $remove); |
| 160 |
|
| 161 |
if ($remove) { |
| 162 |
remove_action( 'kco_wc_after_order_review', 'kco_wc_show_another_gateway_button', 20 ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// Change phone number format to the one expected by Vipps - no +47 |
| 167 |
public static function canonicalize_phone_number( $klarna_order ) { |
| 168 |
if ( isset( $_GET['kco-external-payment'] ) && 'vipps' == $_GET['kco-external-payment'] ) { |
| 169 |
$country_code = '47'; |
| 170 |
$phone_no = $klarna_order->billing_address->phone; |
| 171 |
$telefonnummer1 = preg_replace( "/^\+{$country_code}/", '', $phone_no ); |
| 172 |
$telefonnummer2 = str_replace( ' ', '', $telefonnummer1 ); |
| 173 |
$klarna_order->billing_address->phone = $telefonnummer2; |
| 174 |
} |
| 175 |
|
| 176 |
return $klarna_order; |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|