Code Duplication    Length = 329-329 lines in 3 locations

includes/payment-methods/class-wc-gateway-stripe-bancontact.php 1 location

@@ 13-341 (lines=329) @@
10
 *
11
 * @since 4.0.0
12
 */
13
class WC_Gateway_Stripe_Bancontact extends WC_Stripe_Payment_Gateway {
14
	/**
15
	 * Notices (array)
16
	 * @var array
17
	 */
18
	public $notices = array();
19
20
	/**
21
	 * Is test mode active?
22
	 *
23
	 * @var bool
24
	 */
25
	public $testmode;
26
27
	/**
28
	 * Alternate credit card statement name
29
	 *
30
	 * @var bool
31
	 */
32
	public $statement_descriptor;
33
34
	/**
35
	 * API access secret key
36
	 *
37
	 * @var string
38
	 */
39
	public $secret_key;
40
41
	/**
42
	 * Api access publishable key
43
	 *
44
	 * @var string
45
	 */
46
	public $publishable_key;
47
48
	/**
49
	 * Should we store the users credit cards?
50
	 *
51
	 * @var bool
52
	 */
53
	public $saved_cards;
54
55
	/**
56
	 * Constructor
57
	 */
58
	public function __construct() {
59
		$this->id                   = 'stripe_bancontact';
60
		$this->method_title         = __( 'Stripe Bancontact', 'woocommerce-gateway-stripe' );
61
		/* translators: link */
62
		$this->method_description   = sprintf( __( 'All other general Stripe settings can be adjusted <a href="%s">here</a>.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' ) );
63
		$this->supports             = array(
64
			'products',
65
			'refunds',
66
		);
67
68
		// Load the form fields.
69
		$this->init_form_fields();
70
71
		// Load the settings.
72
		$this->init_settings();
73
74
		$main_settings              = get_option( 'woocommerce_stripe_settings' );
75
		$this->title                = $this->get_option( 'title' );
76
		$this->description          = $this->get_option( 'description' );
77
		$this->enabled              = $this->get_option( 'enabled' );
78
		$this->testmode             = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
79
		$this->saved_cards          = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
80
		$this->publishable_key      = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
81
		$this->secret_key           = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
82
		$this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : '';
83
84
		if ( $this->testmode ) {
85
			$this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : '';
86
			$this->secret_key      = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : '';
87
		}
88
89
		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
90
		add_action( 'admin_notices', array( $this, 'check_environment' ) );
91
		add_action( 'admin_head', array( $this, 'remove_admin_notice' ) );
92
		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
93
	}
94
95
	/**
96
	 * Checks to make sure environment is setup correctly to use this payment method.
97
	 *
98
	 * @since 4.0.0
99
	 * @version 4.0.0
100
	 */
101
	public function check_environment() {
102
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
103
			return;
104
		}
105
106
		$environment_warning = $this->get_environment_warning();
107
108
		if ( $environment_warning ) {
109
			$this->add_admin_notice( 'bad_environment', 'error', $environment_warning );
110
		}
111
112
		foreach ( (array) $this->notices as $notice_key => $notice ) {
113
			echo "<div class='" . esc_attr( $notice['class'] ) . "'><p>";
114
			echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) );
115
			echo '</p></div>';
116
		}
117
	}
118
119
	/**
120
	 * Checks the environment for compatibility problems. Returns a string with the first incompatibility
121
	 * found or false if the environment has no problems.
122
	 *
123
	 * @since 4.0.0
124
	 * @version 4.0.0
125
	 */
126
	public function get_environment_warning() {
127
		if ( 'yes' === $this->enabled && ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
128
			$message = __( 'Bancontact is enabled - it requires store currency to be set to Euros.', 'woocommerce-gateway-stripe' );
129
130
			return $message;
131
		}
132
133
		return false;
134
	}
135
136
	/**
137
	 * Returns all supported currencies for this payment method.
138
	 *
139
	 * @since 4.0.0
140
	 * @version 4.0.0
141
	 * @return array
142
	 */
143
	public function get_supported_currency() {
144
		return apply_filters( 'wc_stripe_bancontact_supported_currencies', array(
145
			'EUR',
146
		) );
147
	}
148
149
	/**
150
	 * Checks to see if all criteria is met before showing payment method.
151
	 *
152
	 * @since 4.0.0
153
	 * @version 4.0.0
154
	 * @return bool
155
	 */
156
	public function is_available() {
157
		if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
158
			return false;
159
		}
160
161
		return parent::is_available();
162
	}
163
164
	/**
165
	 * All payment icons that work with Stripe.
166
	 *
167
	 * @since 4.0.0
168
	 * @version 4.0.0
169
	 * @return array
170
	 */
171
	public function payment_icons() {
172
		return apply_filters( 'wc_stripe_payment_icons', array(
173
			'bancontact' => '<i class="stripe-pf stripe-pf-bancontact-mister-cash stripe-pf-right" alt="Bancontact" aria-hidden="true"></i>',
174
		) );
175
	}
176
177
	/**
178
	 * Get_icon function.
179
	 *
180
	 * @since 1.0.0
181
	 * @version 4.0.0
182
	 * @return string
183
	 */
184
	public function get_icon() {
185
		$icons = $this->payment_icons();
186
187
		$icons_str = '';
188
189
		$icons_str .= $icons['bancontact'];
190
191
		return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
192
	}
193
194
	/**
195
	 * payment_scripts function.
196
	 *
197
	 * Outputs scripts used for stripe payment
198
	 *
199
	 * @access public
200
	 */
201
	public function payment_scripts() {
202
		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
203
			return;
204
		}
205
206
		wp_enqueue_style( 'stripe_paymentfonts' );
207
		wp_enqueue_script( 'woocommerce_stripe' );
208
	}
209
210
	/**
211
	 * Initialize Gateway Settings Form Fields.
212
	 */
213
	public function init_form_fields() {
214
		$this->form_fields = require( WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-bancontact-settings.php' );
215
	}
216
217
	/**
218
	 * Payment form on checkout page
219
	 */
220
	public function payment_fields() {
221
		$user  = wp_get_current_user();
222
		$total = WC()->cart->total;
223
224
		// If paying from order, we need to get total from order not cart.
225
		if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
226
			$order = wc_get_order( wc_get_order_id_by_order_key( wc_clean( $_GET['key'] ) ) );
227
			$total = $order->get_total();
228
		}
229
230
		if ( is_add_payment_method_page() ) {
231
			$pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' );
232
			$total        = '';
233
		} else {
234
			$pay_button_text = '';
235
		}
236
237
		echo '<div
238
			id="stripe-bancontact-payment-data"
239
			data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
240
			data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
241
242
		if ( $this->description ) {
243
			echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $this->description ) ) );
244
		}
245
246
		echo '</div>';
247
	}
248
249
	/**
250
	 * Creates the source for charge.
251
	 *
252
	 * @since 4.0.0
253
	 * @version 4.0.0
254
	 * @param object $order
255
	 * @return mixed
256
	 */
257
	public function create_source( $order ) {
258
		$currency                          = WC_Stripe_Helper::is_pre_30() ? $order->get_order_currency() : $order->get_currency();
259
		$order_id                          = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
260
		$return_url                        = $this->get_stripe_return_url( $order );
261
		$post_data                         = array();
262
		$post_data['amount']               = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency );
263
		$post_data['currency']             = strtolower( $currency );
264
		$post_data['type']                 = 'bancontact';
265
		$post_data['owner']                = $this->get_owner_details( $order );
266
		$post_data['redirect']             = array( 'return_url' => $return_url );
267
268
		if ( ! empty( $this->statement_descriptor ) ) {
269
			$post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor );
270
		}
271
272
		WC_Stripe_Logger::log( 'Info: Begin creating Bancontact source' );
273
274
		return WC_Stripe_API::request( $post_data, 'sources' );
275
	}
276
277
	/**
278
	 * Process the payment
279
	 *
280
	 * @param int  $order_id Reference.
281
	 * @param bool $retry Should we retry on fail.
282
	 * @param bool $force_save_source Force payment source to be saved.
283
	 *
284
	 * @throws Exception If payment will not be accepted.
285
	 *
286
	 * @return array|void
287
	 */
288
	public function process_payment( $order_id, $retry = true, $force_save_source = false ) {
289
		try {
290
			$order = wc_get_order( $order_id );
291
292
			// This will throw exception if not valid.
293
			$this->validate_minimum_order_amount( $order );
294
295
			// This comes from the create account checkbox in the checkout page.
296
			$create_account = ! empty( $_POST['createaccount'] ) ? true : false;
297
298
			if ( $create_account ) {
299
				$new_customer_id     = WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
300
				$new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
301
				$new_stripe_customer->create_customer();
302
			}
303
304
			$response = $this->create_source( $order );
305
306
			if ( ! empty( $response->error ) ) {
307
				$order->add_order_note( $response->error->message );
308
309
				throw new Exception( $response->error->message );
310
			}
311
312
			if ( WC_Stripe_Helper::is_pre_30() ) {
313
				update_post_meta( $order_id, '_stripe_source_id', $response->id );
314
			} else {
315
				$order->update_meta_data( '_stripe_source_id', $response->id );
316
				$order->save();
317
			}
318
319
			WC_Stripe_Logger::log( 'Info: Redirecting to Bancontact...' );
320
321
			return array(
322
				'result'   => 'success',
323
				'redirect' => esc_url_raw( $response->redirect->url ),
324
			);
325
		} catch ( Exception $e ) {
326
			wc_add_notice( $e->getMessage(), 'error' );
327
			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
328
329
			do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
330
331
			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
332
				$this->send_failed_order_email( $order_id );
333
			}
334
335
			return array(
336
				'result'   => 'fail',
337
				'redirect' => '',
338
			);
339
		}
340
	}
341
}
342

includes/payment-methods/class-wc-gateway-stripe-giropay.php 1 location

@@ 13-341 (lines=329) @@
10
 *
11
 * @since 4.0.0
12
 */
13
class WC_Gateway_Stripe_Giropay extends WC_Stripe_Payment_Gateway {
14
	/**
15
	 * Notices (array)
16
	 * @var array
17
	 */
18
	public $notices = array();
19
20
	/**
21
	 * Is test mode active?
22
	 *
23
	 * @var bool
24
	 */
25
	public $testmode;
26
27
	/**
28
	 * Alternate credit card statement name
29
	 *
30
	 * @var bool
31
	 */
32
	public $statement_descriptor;
33
34
	/**
35
	 * API access secret key
36
	 *
37
	 * @var string
38
	 */
39
	public $secret_key;
40
41
	/**
42
	 * Api access publishable key
43
	 *
44
	 * @var string
45
	 */
46
	public $publishable_key;
47
48
	/**
49
	 * Should we store the users credit cards?
50
	 *
51
	 * @var bool
52
	 */
53
	public $saved_cards;
54
55
	/**
56
	 * Constructor
57
	 */
58
	public function __construct() {
59
		$this->id                   = 'stripe_giropay';
60
		$this->method_title         = __( 'Stripe Giropay', 'woocommerce-gateway-stripe' );
61
		/* translators: link */
62
		$this->method_description   = sprintf( __( 'All other general Stripe settings can be adjusted <a href="%s">here</a>.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' ) );
63
		$this->supports             = array(
64
			'products',
65
			'refunds',
66
		);
67
68
		// Load the form fields.
69
		$this->init_form_fields();
70
71
		// Load the settings.
72
		$this->init_settings();
73
74
		$main_settings              = get_option( 'woocommerce_stripe_settings' );
75
		$this->title                = $this->get_option( 'title' );
76
		$this->description          = $this->get_option( 'description' );
77
		$this->enabled              = $this->get_option( 'enabled' );
78
		$this->testmode             = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
79
		$this->saved_cards          = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
80
		$this->publishable_key      = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
81
		$this->secret_key           = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
82
		$this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : '';
83
84
		if ( $this->testmode ) {
85
			$this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : '';
86
			$this->secret_key      = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : '';
87
		}
88
89
		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
90
		add_action( 'admin_notices', array( $this, 'check_environment' ) );
91
		add_action( 'admin_head', array( $this, 'remove_admin_notice' ) );
92
		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
93
	}
94
95
	/**
96
	 * Checks to make sure environment is setup correctly to use this payment method.
97
	 *
98
	 * @since 4.0.0
99
	 * @version 4.0.0
100
	 */
101
	public function check_environment() {
102
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
103
			return;
104
		}
105
106
		$environment_warning = $this->get_environment_warning();
107
108
		if ( $environment_warning ) {
109
			$this->add_admin_notice( 'bad_environment', 'error', $environment_warning );
110
		}
111
112
		foreach ( (array) $this->notices as $notice_key => $notice ) {
113
			echo "<div class='" . esc_attr( $notice['class'] ) . "'><p>";
114
			echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) );
115
			echo '</p></div>';
116
		}
117
	}
118
119
	/**
120
	 * Checks the environment for compatibility problems. Returns a string with the first incompatibility
121
	 * found or false if the environment has no problems.
122
	 *
123
	 * @since 4.0.0
124
	 * @version 4.0.0
125
	 */
126
	public function get_environment_warning() {
127
		if ( 'yes' === $this->enabled && ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
128
			$message = __( 'Giropay is enabled - it requires store currency to be set to Euros.', 'woocommerce-gateway-stripe' );
129
130
			return $message;
131
		}
132
133
		return false;
134
	}
135
136
	/**
137
	 * Returns all supported currencies for this payment method.
138
	 *
139
	 * @since 4.0.0
140
	 * @version 4.0.0
141
	 * @return array
142
	 */
143
	public function get_supported_currency() {
144
		return apply_filters( 'wc_stripe_giropay_supported_currencies', array(
145
			'EUR',
146
		) );
147
	}
148
149
	/**
150
	 * Checks to see if all criteria is met before showing payment method.
151
	 *
152
	 * @since 4.0.0
153
	 * @version 4.0.0
154
	 * @return bool
155
	 */
156
	public function is_available() {
157
		if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
158
			return false;
159
		}
160
161
		return parent::is_available();
162
	}
163
164
	/**
165
	 * All payment icons that work with Stripe.
166
	 *
167
	 * @since 4.0.0
168
	 * @version 4.0.0
169
	 * @return array
170
	 */
171
	public function payment_icons() {
172
		return apply_filters( 'wc_stripe_payment_icons', array(
173
			'giropay' => '<i class="stripe-pf stripe-pf-giropay stripe-pf-right" alt="Giropay" aria-hidden="true"></i>',
174
		) );
175
	}
176
177
	/**
178
	 * Get_icon function.
179
	 *
180
	 * @since 1.0.0
181
	 * @version 4.0.0
182
	 * @return string
183
	 */
184
	public function get_icon() {
185
		$icons = $this->payment_icons();
186
187
		$icons_str = '';
188
189
		$icons_str .= $icons['giropay'];
190
191
		return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
192
	}
193
194
	/**
195
	 * payment_scripts function.
196
	 *
197
	 * Outputs scripts used for stripe payment
198
	 *
199
	 * @access public
200
	 */
201
	public function payment_scripts() {
202
		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
203
			return;
204
		}
205
206
		wp_enqueue_style( 'stripe_paymentfonts' );
207
		wp_enqueue_script( 'woocommerce_stripe' );
208
	}
209
210
	/**
211
	 * Initialize Gateway Settings Form Fields.
212
	 */
213
	public function init_form_fields() {
214
		$this->form_fields = require( WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-giropay-settings.php' );
215
	}
216
217
	/**
218
	 * Payment form on checkout page
219
	 */
220
	public function payment_fields() {
221
		$user                 = wp_get_current_user();
222
		$total                = WC()->cart->total;
223
224
		// If paying from order, we need to get total from order not cart.
225
		if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
226
			$order = wc_get_order( wc_get_order_id_by_order_key( wc_clean( $_GET['key'] ) ) );
227
			$total = $order->get_total();
228
		}
229
230
		if ( is_add_payment_method_page() ) {
231
			$pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' );
232
			$total        = '';
233
		} else {
234
			$pay_button_text = '';
235
		}
236
237
		echo '<div
238
			id="stripe-giropay-payment-data"
239
			data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
240
			data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
241
242
		if ( $this->description ) {
243
			echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $this->description ) ) );
244
		}
245
246
		echo '</div>';
247
	}
248
249
	/**
250
	 * Creates the source for charge.
251
	 *
252
	 * @since 4.0.0
253
	 * @version 4.0.0
254
	 * @param object $order
255
	 * @return mixed
256
	 */
257
	public function create_source( $order ) {
258
		$currency                          = WC_Stripe_Helper::is_pre_30() ? $order->get_order_currency() : $order->get_currency();
259
		$order_id                          = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
260
		$return_url                        = $this->get_stripe_return_url( $order );
261
		$post_data                         = array();
262
		$post_data['amount']               = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency );
263
		$post_data['currency']             = strtolower( $currency );
264
		$post_data['type']                 = 'giropay';
265
		$post_data['owner']                = $this->get_owner_details( $order );
266
		$post_data['redirect']             = array( 'return_url' => $return_url );
267
268
		if ( ! empty( $this->statement_descriptor ) ) {
269
			$post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor );
270
		}
271
272
		WC_Stripe_Logger::log( 'Info: Begin creating Giropay source' );
273
274
		return WC_Stripe_API::request( $post_data, 'sources' );
275
	}
276
277
	/**
278
	 * Process the payment
279
	 *
280
	 * @param int  $order_id Reference.
281
	 * @param bool $retry Should we retry on fail.
282
	 * @param bool $force_save_source Force payment source to be saved.
283
	 *
284
	 * @throws Exception If payment will not be accepted.
285
	 *
286
	 * @return array|void
287
	 */
288
	public function process_payment( $order_id, $retry = true, $force_save_source = false ) {
289
		try {
290
			$order = wc_get_order( $order_id );
291
292
			// This will throw exception if not valid.
293
			$this->validate_minimum_order_amount( $order );
294
295
			// This comes from the create account checkbox in the checkout page.
296
			$create_account = ! empty( $_POST['createaccount'] ) ? true : false;
297
298
			if ( $create_account ) {
299
				$new_customer_id     = WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
300
				$new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
301
				$new_stripe_customer->create_customer();
302
			}
303
304
			$response = $this->create_source( $order );
305
306
			if ( ! empty( $response->error ) ) {
307
				$order->add_order_note( $response->error->message );
308
309
				throw new Exception( $response->error->message );
310
			}
311
312
			if ( WC_Stripe_Helper::is_pre_30() ) {
313
				update_post_meta( $order_id, '_stripe_source_id', $response->id );
314
			} else {
315
				$order->update_meta_data( '_stripe_source_id', $response->id );
316
				$order->save();
317
			}
318
319
			WC_Stripe_Logger::log( 'Info: Redirecting to Giropay...' );
320
321
			return array(
322
				'result'   => 'success',
323
				'redirect' => esc_url_raw( $response->redirect->url ),
324
			);
325
		} catch ( Exception $e ) {
326
			wc_add_notice( $e->getMessage(), 'error' );
327
			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
328
329
			do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
330
331
			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
332
				$this->send_failed_order_email( $order_id );
333
			}
334
335
			return array(
336
				'result'   => 'fail',
337
				'redirect' => '',
338
			);
339
		}
340
	}
341
}
342

includes/payment-methods/class-wc-gateway-stripe-ideal.php 1 location

@@ 13-341 (lines=329) @@
10
 *
11
 * @since 4.0.0
12
 */
13
class WC_Gateway_Stripe_Ideal extends WC_Stripe_Payment_Gateway {
14
	/**
15
	 * Notices (array)
16
	 * @var array
17
	 */
18
	public $notices = array();
19
20
	/**
21
	 * Is test mode active?
22
	 *
23
	 * @var bool
24
	 */
25
	public $testmode;
26
27
	/**
28
	 * Alternate credit card statement name
29
	 *
30
	 * @var bool
31
	 */
32
	public $statement_descriptor;
33
34
	/**
35
	 * API access secret key
36
	 *
37
	 * @var string
38
	 */
39
	public $secret_key;
40
41
	/**
42
	 * Api access publishable key
43
	 *
44
	 * @var string
45
	 */
46
	public $publishable_key;
47
48
	/**
49
	 * Should we store the users credit cards?
50
	 *
51
	 * @var bool
52
	 */
53
	public $saved_cards;
54
55
	/**
56
	 * Constructor
57
	 */
58
	public function __construct() {
59
		$this->id                   = 'stripe_ideal';
60
		$this->method_title         = __( 'Stripe iDeal', 'woocommerce-gateway-stripe' );
61
		/* translators: link */
62
		$this->method_description   = sprintf( __( 'All other general Stripe settings can be adjusted <a href="%s">here</a>.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' ) );
63
		$this->supports             = array(
64
			'products',
65
			'refunds',
66
		);
67
68
		// Load the form fields.
69
		$this->init_form_fields();
70
71
		// Load the settings.
72
		$this->init_settings();
73
74
		$main_settings              = get_option( 'woocommerce_stripe_settings' );
75
		$this->title                = $this->get_option( 'title' );
76
		$this->description          = $this->get_option( 'description' );
77
		$this->enabled              = $this->get_option( 'enabled' );
78
		$this->testmode             = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
79
		$this->saved_cards          = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
80
		$this->publishable_key      = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
81
		$this->secret_key           = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
82
		$this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : '';
83
84
		if ( $this->testmode ) {
85
			$this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : '';
86
			$this->secret_key      = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : '';
87
		}
88
89
		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
90
		add_action( 'admin_notices', array( $this, 'check_environment' ) );
91
		add_action( 'admin_head', array( $this, 'remove_admin_notice' ) );
92
		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
93
	}
94
95
	/**
96
	 * Checks to make sure environment is setup correctly to use this payment method.
97
	 *
98
	 * @since 4.0.0
99
	 * @version 4.0.0
100
	 */
101
	public function check_environment() {
102
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
103
			return;
104
		}
105
106
		$environment_warning = $this->get_environment_warning();
107
108
		if ( $environment_warning ) {
109
			$this->add_admin_notice( 'bad_environment', 'error', $environment_warning );
110
		}
111
112
		foreach ( (array) $this->notices as $notice_key => $notice ) {
113
			echo "<div class='" . esc_attr( $notice['class'] ) . "'><p>";
114
			echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) );
115
			echo '</p></div>';
116
		}
117
	}
118
119
	/**
120
	 * Checks the environment for compatibility problems. Returns a string with the first incompatibility
121
	 * found or false if the environment has no problems.
122
	 *
123
	 * @since 4.0.0
124
	 * @version 4.0.0
125
	 */
126
	public function get_environment_warning() {
127
		if ( 'yes' === $this->enabled && ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
128
			$message = __( 'iDeal is enabled - it requires store currency to be set to Euros.', 'woocommerce-gateway-stripe' );
129
130
			return $message;
131
		}
132
133
		return false;
134
	}
135
136
	/**
137
	 * Returns all supported currencies for this payment method.
138
	 *
139
	 * @since 4.0.0
140
	 * @version 4.0.0
141
	 * @return array
142
	 */
143
	public function get_supported_currency() {
144
		return apply_filters( 'wc_stripe_ideal_supported_currencies', array(
145
			'EUR',
146
		) );
147
	}
148
149
	/**
150
	 * Checks to see if all criteria is met before showing payment method.
151
	 *
152
	 * @since 4.0.0
153
	 * @version 4.0.0
154
	 * @return bool
155
	 */
156
	public function is_available() {
157
		if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
158
			return false;
159
		}
160
161
		return parent::is_available();
162
	}
163
164
	/**
165
	 * All payment icons that work with Stripe.
166
	 *
167
	 * @since 4.0.0
168
	 * @version 4.0.0
169
	 * @return array
170
	 */
171
	public function payment_icons() {
172
		return apply_filters( 'wc_stripe_payment_icons', array(
173
			'ideal' => '<i class="stripe-pf stripe-pf-ideal stripe-pf-right" alt="iDeal" aria-hidden="true"></i>',
174
		) );
175
	}
176
177
	/**
178
	 * Get_icon function.
179
	 *
180
	 * @since 1.0.0
181
	 * @version 4.0.0
182
	 * @return string
183
	 */
184
	public function get_icon() {
185
		$icons = $this->payment_icons();
186
187
		$icons_str = '';
188
189
		$icons_str .= $icons['ideal'];
190
191
		return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
192
	}
193
194
	/**
195
	 * payment_scripts function.
196
	 *
197
	 * Outputs scripts used for stripe payment
198
	 *
199
	 * @access public
200
	 */
201
	public function payment_scripts() {
202
		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
203
			return;
204
		}
205
206
		wp_enqueue_style( 'stripe_paymentfonts' );
207
		wp_enqueue_script( 'woocommerce_stripe' );
208
	}
209
210
	/**
211
	 * Initialize Gateway Settings Form Fields.
212
	 */
213
	public function init_form_fields() {
214
		$this->form_fields = require( WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-ideal-settings.php' );
215
	}
216
217
	/**
218
	 * Payment form on checkout page
219
	 */
220
	public function payment_fields() {
221
		$user                 = wp_get_current_user();
222
		$total                = WC()->cart->total;
223
224
		// If paying from order, we need to get total from order not cart.
225
		if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
226
			$order = wc_get_order( wc_get_order_id_by_order_key( wc_clean( $_GET['key'] ) ) );
227
			$total = $order->get_total();
228
		}
229
230
		if ( is_add_payment_method_page() ) {
231
			$pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' );
232
			$total        = '';
233
		} else {
234
			$pay_button_text = '';
235
		}
236
237
		echo '<div
238
			id="stripe-ideal-payment-data"
239
			data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
240
			data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
241
242
		if ( $this->description ) {
243
			echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $this->description ) ) );
244
		}
245
246
		echo '</div>';
247
	}
248
249
	/**
250
	 * Creates the source for charge.
251
	 *
252
	 * @since 4.0.0
253
	 * @version 4.0.0
254
	 * @param object $order
255
	 * @return mixed
256
	 */
257
	public function create_source( $order ) {
258
		$currency                          = WC_Stripe_Helper::is_pre_30() ? $order->get_order_currency() : $order->get_currency();
259
		$order_id                          = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
260
		$return_url                        = $this->get_stripe_return_url( $order );
261
		$post_data                         = array();
262
		$post_data['amount']               = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency );
263
		$post_data['currency']             = strtolower( $currency );
264
		$post_data['type']                 = 'ideal';
265
		$post_data['owner']                = $this->get_owner_details( $order );
266
		$post_data['redirect']             = array( 'return_url' => $return_url );
267
268
		if ( ! empty( $this->statement_descriptor ) ) {
269
			$post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor );
270
		}
271
272
		WC_Stripe_Logger::log( 'Info: Begin creating iDeal source' );
273
274
		return WC_Stripe_API::request( $post_data, 'sources' );
275
	}
276
277
	/**
278
	 * Process the payment
279
	 *
280
	 * @param int  $order_id Reference.
281
	 * @param bool $retry Should we retry on fail.
282
	 * @param bool $force_save_source Force payment source to be saved.
283
	 *
284
	 * @throws Exception If payment will not be accepted.
285
	 *
286
	 * @return array|void
287
	 */
288
	public function process_payment( $order_id, $retry = true, $force_save_source = false ) {
289
		try {
290
			$order = wc_get_order( $order_id );
291
292
			// This will throw exception if not valid.
293
			$this->validate_minimum_order_amount( $order );
294
295
			// This comes from the create account checkbox in the checkout page.
296
			$create_account = ! empty( $_POST['createaccount'] ) ? true : false;
297
298
			if ( $create_account ) {
299
				$new_customer_id     = WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
300
				$new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
301
				$new_stripe_customer->create_customer();
302
			}
303
304
			$response = $this->create_source( $order );
305
306
			if ( ! empty( $response->error ) ) {
307
				$order->add_order_note( $response->error->message );
308
309
				throw new Exception( $response->error->message );
310
			}
311
312
			if ( WC_Stripe_Helper::is_pre_30() ) {
313
				update_post_meta( $order_id, '_stripe_source_id', $response->id );
314
			} else {
315
				$order->update_meta_data( '_stripe_source_id', $response->id );
316
				$order->save();
317
			}
318
319
			WC_Stripe_Logger::log( 'Info: Redirecting to iDeal...' );
320
321
			return array(
322
				'result'   => 'success',
323
				'redirect' => esc_url_raw( $response->redirect->url ),
324
			);
325
		} catch ( Exception $e ) {
326
			wc_add_notice( $e->getMessage(), 'error' );
327
			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
328
329
			do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
330
331
			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
332
				$this->send_failed_order_email( $order_id );
333
			}
334
335
			return array(
336
				'result'   => 'fail',
337
				'redirect' => '',
338
			);
339
		}
340
	}
341
}
342