| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
class WC_Vipps_Recurring_Kc_Support { |
| 6 |
/** |
| 7 |
* Initialize Vipps/MobilePay Recurring KC Support class. |
| 8 |
*/ |
| 9 |
public static function init() { |
| 10 |
add_filter( 'kco_wc_gateway_settings', [ __CLASS__, 'form_fields' ] ); |
| 11 |
|
| 12 |
add_filter( 'kco_wc_api_request_args', [ |
| 13 |
__CLASS__, |
| 14 |
'create_vipps_recurring_order' |
| 15 |
], 90 ); |
| 16 |
|
| 17 |
add_filter( 'kco_wc_klarna_order_pre_submit', [ |
| 18 |
__CLASS__, |
| 19 |
'canonicalize_phone_number' |
| 20 |
], 11 ); |
| 21 |
|
| 22 |
add_action( 'init', [ __CLASS__, 'maybe_remove_other_gateway_button' ] ); |
| 23 |
|
| 24 |
add_action( 'kco_wc_before_submit', [ |
| 25 |
__CLASS__, |
| 26 |
'add_vipps_recurring_payment_method' |
| 27 |
] ); |
| 28 |
|
| 29 |
add_action( 'woocommerce_payment_complete', [ |
| 30 |
__CLASS__, |
| 31 |
'reset_default_payment_method' |
| 32 |
], 10 ); |
| 33 |
|
| 34 |
add_filter( 'wc_vipps_recurring_transaction_id_for_order', [ |
| 35 |
__CLASS__, |
| 36 |
'fix_transaction_id' |
| 37 |
], 10, 2 ); |
| 38 |
|
| 39 |
add_action( 'wc_vipps_recurring_before_process_order_charge', [ |
| 40 |
__CLASS__, |
| 41 |
'fix_payment_method_on_subscription' |
| 42 |
] ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Klarna messes up our transaction id by inserting their own. We don't want theirs! |
| 47 |
*/ |
| 48 |
public static function fix_transaction_id( $transaction_id, $order ) { |
| 49 |
$_wc_klarna_order_id = WC_Vipps_Recurring_Helper::get_meta( $order, '_wc_klarna_order_id' ); |
| 50 |
if ( $_wc_klarna_order_id === $transaction_id ) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
return $transaction_id; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Fix 16.02.2022 - KCO did not set the correct external payment method on a subscription after completed payment |
| 59 |
* KCO 2.6.4, WooCommerce 6.2.0 |
| 60 |
*/ |
| 61 |
public static function fix_payment_method_on_subscription( $order ) { |
| 62 |
$gateway = WC_Vipps_Recurring::get_instance()->gateway(); |
| 63 |
|
| 64 |
$subscriptions = $gateway->get_subscriptions_for_order( $order ); |
| 65 |
foreach ( $subscriptions as $subscription ) { |
| 66 |
if ( $subscription->get_payment_method() === 'kco' && ! empty( WC_Vipps_Recurring_Helper::get_agreement_id_from_order( $order ) ) ) { |
| 67 |
$subscription->set_payment_method( $gateway->id ); |
| 68 |
$subscription->save(); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Add custom setting fields to Klarna's Vipps/MobilePay Recurring settings. |
| 75 |
*/ |
| 76 |
public static function form_fields( $settings ) { |
| 77 |
$settings['epm_vipps_recurring_settings_title'] = [ |
| 78 |
'title' => __( 'External Payment Method - Vipps/MobilePay Recurring Payments', 'woo-vipps' ), |
| 79 |
'type' => 'title', |
| 80 |
]; |
| 81 |
|
| 82 |
$settings['epm_vipps_recurring_activate'] = [ |
| 83 |
'title' => __( 'Activate', 'woo-vipps' ), |
| 84 |
'type' => 'checkbox', |
| 85 |
'description' => __( 'Activate Vipps/MobilePay Recurring Payments as an external payment method for Klarna Checkout', 'woo-vipps' ), |
| 86 |
'default' => 'yes', |
| 87 |
]; |
| 88 |
|
| 89 |
$settings['epm_vipps_recurring_description'] = [ |
| 90 |
'title' => __( 'Description', 'woo-vipps' ), |
| 91 |
'type' => 'textarea', |
| 92 |
'description' => __( 'Description for Vipps/MobilePay Recurring Payments method. This controls the description which the user sees in the checkout form.', 'woo-vipps' ), |
| 93 |
// translators: {brand}: brand name, Vipps or MobilePay |
| 94 |
'default' => __( 'Remember: {brand} is always has no fees when paying businesses.', 'woo-vipps' ), |
| 95 |
]; |
| 96 |
|
| 97 |
$settings['epm_vipps_recurring_img_url'] = [ |
| 98 |
'title' => __( 'Image url', 'woo-vipps' ), |
| 99 |
'type' => 'text', |
| 100 |
'description' => __( 'URL to the Vipps/MobilePay logo', 'woo-vipps' ), |
| 101 |
'default' => WC_VIPPS_RECURRING_PLUGIN_URL . '/assets/images/{brand}-logo-black.svg' |
| 102 |
]; |
| 103 |
|
| 104 |
$settings['epm_vipps_recurring_disable_button'] = [ |
| 105 |
'title' => __( 'Disable other gateway button', 'woo-vipps' ), |
| 106 |
'type' => 'checkbox', |
| 107 |
'description' => __( 'Disables the "Select another Payment method" button in Klarna Checkout.', 'woo-vipps' ), |
| 108 |
'default' => 'no', |
| 109 |
]; |
| 110 |
|
| 111 |
return $settings; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Add Vipps/MobilePay Recurring to Klarna Checkout. |
| 116 |
*/ |
| 117 |
public static function create_vipps_recurring_order( $create ) { |
| 118 |
$merchant_urls = KCO_WC()->merchant_urls->get_urls(); |
| 119 |
$confirmation_url = $merchant_urls['confirmation']; |
| 120 |
|
| 121 |
$kco_settings = get_option( 'woocommerce_kco_settings' ); |
| 122 |
|
| 123 |
$activate = ( ! isset( $kco_settings['epm_vipps_recurring_activate'] ) || $kco_settings['epm_vipps_recurring_activate'] === 'yes' ) |
| 124 |
&& WC_Vipps_Recurring::get_instance()->gateway_should_be_active(); |
| 125 |
$activate = apply_filters( 'wc_vipps_recurring_activate_kco_external_payment', $activate ); |
| 126 |
|
| 127 |
if ( get_option( WC_Vipps_Recurring_Helper::OPTION_CHECKOUT_ENABLED, false ) ) { |
| 128 |
$activate = false; |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! isset( $create['external_payment_methods'] ) || ! is_array( $create['external_payment_methods'] ) ) { |
| 132 |
$create['external_payment_methods'] = []; |
| 133 |
} |
| 134 |
|
| 135 |
if ( ! $activate ) { |
| 136 |
return $create; |
| 137 |
} |
| 138 |
|
| 139 |
$gateway = WC_Vipps_Recurring::get_instance()->gateway(); |
| 140 |
|
| 141 |
$image_url = str_replace( '{brand}', $gateway->brand, $kco_settings['epm_vipps_recurring_img_url'] ?? '' ); |
| 142 |
$description = str_replace( '{brand}', $gateway->title, $kco_settings['epm_vipps_recurring_description'] ?? '' ); |
| 143 |
|
| 144 |
$gateway = [ |
| 145 |
'name' => $gateway->title, |
| 146 |
'redirect_url' => add_query_arg( [ |
| 147 |
'kco-external-payment' => $gateway->id, |
| 148 |
'order_id' => $create['agreement_id'] ?? '{checkout.order.id}' |
| 149 |
], $confirmation_url ), |
| 150 |
'image_url' => $image_url, |
| 151 |
'description' => $description, |
| 152 |
]; |
| 153 |
|
| 154 |
$create['external_payment_methods'][] = $gateway; |
| 155 |
|
| 156 |
WC()->session->set( 'vipps_via_klarna', 1 ); |
| 157 |
|
| 158 |
return $create; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Add the Vipps/MobilePay Recurring Payments method to Klarna |
| 163 |
*/ |
| 164 |
public static function add_vipps_recurring_payment_method() { |
| 165 |
if ( isset( $_GET['kco-external-payment'] ) && 'vipps_recurring' === $_GET['kco-external-payment'] ) { ?> |
| 166 |
$('input#payment_method_vipps_recurring').prop('checked', true); |
| 167 |
$('input#legal').prop('checked', true); |
| 168 |
<?php |
| 169 |
// In case other actions are needed we can add more Javascript to this hook |
| 170 |
do_action( 'wc_vipps_recurring_klarna_checkout_support_on_submit_javascript' ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Reset the default payment method back to KCO after a completed Vipps/MobilePay payment |
| 176 |
*/ |
| 177 |
public static function reset_default_payment_method() { |
| 178 |
if ( WC()->session && WC()->session->get( 'vipps_via_klarna' ) ) { |
| 179 |
WC()->session->set( 'chosen_payment_method', 'kco' ); |
| 180 |
WC()->session->set( 'vipps_via_klarna', 0 ); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* If the setting to remove "select another gateway" is enabled we have to remove that button. |
| 186 |
*/ |
| 187 |
public static function maybe_remove_other_gateway_button() { |
| 188 |
$kco_settings = get_option( 'woocommerce_kco_settings' ); |
| 189 |
$disable_button = isset( $kco_settings['epm_vipps_recurring_disable_button'] ) && |
| 190 |
$kco_settings['epm_vipps_recurring_disable_button'] === 'yes'; |
| 191 |
|
| 192 |
if ( $disable_button ) { |
| 193 |
remove_action( 'kco_wc_after_order_review', 'kco_wc_show_another_gateway_button', 20 ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* We need to remove + and all spaces from the phone number before handing it off to the Vipps/MobilePay API. |
| 199 |
*/ |
| 200 |
public static function canonicalize_phone_number( $klarna_order ) { |
| 201 |
if ( isset( $_GET['kco-external-payment'] ) && 'vipps_recurring' === $_GET['kco-external-payment'] ) { |
| 202 |
$phone_number = preg_replace( "/^\+|\s/", '', $klarna_order->billing_address->phone ); |
| 203 |
$klarna_order->billing_address->phone = $phone_number; |
| 204 |
} |
| 205 |
|
| 206 |
return $klarna_order; |
| 207 |
} |
| 208 |
} |
| 209 |
|