PluginProbe
Pay with Vipps and MobilePay for WooCommerce / 6.1.4
Pay with Vipps and MobilePay for WooCommerce v6.1.4
6.3.1 6.2.6 6.3.0 6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.10 6.1.9 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1.0 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 All 190 releases
woo-vipps / recurring / includes / compat / wc-vipps-recurring-kc-support.php

wc-vipps-recurring-kc-support.php in Pay with Vipps and MobilePay for WooCommerce 6.1.4, at recurring/includes/compat/wc-vipps-recurring-kc-support.php

205 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ( ! isset( $create['external_payment_methods'] ) || ! is_array( $create['external_payment_methods'] ) ) {
128 $create['external_payment_methods'] = [];
129 }
130
131 if ( ! $activate ) {
132 return $create;
133 }
134
135 $gateway = WC_Vipps_Recurring::get_instance()->gateway();
136
137 $image_url = str_replace( '{brand}', $gateway->brand, $kco_settings['epm_vipps_recurring_img_url'] ?? '' );
138 $description = str_replace( '{brand}', $gateway->title, $kco_settings['epm_vipps_recurring_description'] ?? '' );
139
140 $gateway = [
141 'name' => $gateway->title,
142 'redirect_url' => add_query_arg( [
143 'kco-external-payment' => $gateway->id,
144 'order_id' => $create['agreement_id'] ?? '{checkout.order.id}'
145 ], $confirmation_url ),
146 'image_url' => $image_url,
147 'description' => $description,
148 ];
149
150 $create['external_payment_methods'][] = $gateway;
151
152 WC()->session->set( 'vipps_via_klarna', 1 );
153
154 return $create;
155 }
156
157 /**
158 * Add the Vipps/MobilePay Recurring Payments method to Klarna
159 */
160 public static function add_vipps_recurring_payment_method() {
161 if ( isset( $_GET['kco-external-payment'] ) && 'vipps_recurring' === $_GET['kco-external-payment'] ) { ?>
162 $('input#payment_method_vipps_recurring').prop('checked', true);
163 $('input#legal').prop('checked', true);
164 <?php
165 // In case other actions are needed we can add more Javascript to this hook
166 do_action( 'wc_vipps_recurring_klarna_checkout_support_on_submit_javascript' );
167 }
168 }
169
170 /**
171 * Reset the default payment method back to KCO after a completed Vipps/MobilePay payment
172 */
173 public static function reset_default_payment_method() {
174 if ( WC()->session && WC()->session->get( 'vipps_via_klarna' ) ) {
175 WC()->session->set( 'chosen_payment_method', 'kco' );
176 WC()->session->set( 'vipps_via_klarna', 0 );
177 }
178 }
179
180 /**
181 * If the setting to remove "select another gateway" is enabled we have to remove that button.
182 */
183 public static function maybe_remove_other_gateway_button() {
184 $kco_settings = get_option( 'woocommerce_kco_settings' );
185 $disable_button = isset( $kco_settings['epm_vipps_recurring_disable_button'] ) &&
186 $kco_settings['epm_vipps_recurring_disable_button'] === 'yes';
187
188 if ( $disable_button ) {
189 remove_action( 'kco_wc_after_order_review', 'kco_wc_show_another_gateway_button', 20 );
190 }
191 }
192
193 /**
194 * We need to remove + and all spaces from the phone number before handing it off to the Vipps/MobilePay API.
195 */
196 public static function canonicalize_phone_number( $klarna_order ) {
197 if ( isset( $_GET['kco-external-payment'] ) && 'vipps_recurring' === $_GET['kco-external-payment'] ) {
198 $phone_number = preg_replace( "/^\+|\s/", '', $klarna_order->billing_address->phone );
199 $klarna_order->billing_address->phone = $phone_number;
200 }
201
202 return $klarna_order;
203 }
204 }
205