blob: 86e98a8d18284eb309265e0202ad5e5c62f303cb [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4}
5
6/**
7 * Class that handles iDEAL payment method.
8 *
9 * @extends WC_Gateway_Stripe
10 *
11 * @since 4.0.0
12 */
13class WC_Gateway_Stripe_Ideal extends WC_Stripe_Payment_Gateway {
14
15 const ID = 'stripe_ideal';
16
17 /**
18 * Notices (array)
19 *
20 * @var array
21 */
22 public $notices = [];
23
24 /**
25 * Is test mode active?
26 *
27 * @var bool
28 */
29 public $testmode;
30
31 /**
32 * Alternate credit card statement name
33 *
34 * @var bool
35 */
36 public $statement_descriptor;
37
38 /**
39 * API access secret key
40 *
41 * @var string
42 */
43 public $secret_key;
44
45 /**
46 * Api access publishable key
47 *
48 * @var string
49 */
50 public $publishable_key;
51
52 /**
53 * Should we store the users credit cards?
54 *
55 * @var bool
56 */
57 public $saved_cards;
58
59 /**
60 * Constructor
61 */
62 public function __construct() {
63 $this->id = self::ID;
64 $this->method_title = __( 'Stripe iDEAL', 'woocommerce-gateway-stripe' );
65 $this->method_description = sprintf(
66 /* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */
67 __( 'All other general Stripe settings can be adjusted %1$shere%2$s.', 'woocommerce-gateway-stripe' ),
68 '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' ) ) . '">',
69 '</a>'
70 );
71 $this->supports = [
72 'products',
73 'refunds',
74 ];
75
76 // Load the form fields.
77 $this->init_form_fields();
78
79 // Load the settings.
80 $this->init_settings();
81
82 $main_settings = get_option( 'woocommerce_stripe_settings' );
83 $this->title = $this->get_option( 'title' );
84 $this->description = $this->get_option( 'description' );
85 $this->enabled = $this->get_option( 'enabled' );
86 $this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
87 $this->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
88 $this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
89 $this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
90 $this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : '';
91
92 if ( $this->testmode ) {
93 $this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : '';
94 $this->secret_key = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : '';
95 }
96
97 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] );
98 add_action( 'wp_enqueue_scripts', [ $this, 'payment_scripts' ] );
99 }
100
101 /**
102 * Returns all supported currencies for this payment method.
103 *
104 * @since 4.0.0
105 * @version 4.0.0
106 * @return array
107 */
108 public function get_supported_currency() {
109 return apply_filters(
110 'wc_stripe_ideal_supported_currencies',
111 [
112 'EUR',
113 ]
114 );
115 }
116
117 /**
118 * Checks to see if all criteria is met before showing payment method.
119 *
120 * @since 4.0.0
121 * @version 4.0.0
122 * @return bool
123 */
124 public function is_available() {
125 if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
126 return false;
127 }
128
129 return parent::is_available();
130 }
131
132 /**
133 * Get_icon function.
134 *
135 * @since 1.0.0
136 * @version 4.0.0
137 * @return string
138 */
139 public function get_icon() {
140 $icons = $this->payment_icons();
141
142 $icons_str = '';
143
144 $icons_str .= isset( $icons['ideal'] ) ? $icons['ideal'] : '';
145
146 return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
147 }
148
149 /**
150 * Outputs scripts used for stripe payment
151 */
152 public function payment_scripts() {
153 if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
154 return;
155 }
156
157 wp_enqueue_style( 'stripe_styles' );
158 wp_enqueue_script( 'woocommerce_stripe' );
159 }
160
161 /**
162 * Initialize Gateway Settings Form Fields.
163 */
164 public function init_form_fields() {
165 $this->form_fields = require WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-ideal-settings.php';
166 }
167
168 /**
169 * Payment form on checkout page
170 */
171 public function payment_fields() {
172 global $wp;
173 $user = wp_get_current_user();
174 $total = WC()->cart->total;
175 $description = $this->get_description();
176
177 // If paying from order, we need to get total from order not cart.
178 if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
179 $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) );
180 $total = $order->get_total();
181 }
182
183 if ( is_add_payment_method_page() ) {
184 $pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' );
185 $total = '';
186 } else {
187 $pay_button_text = '';
188 }
189
190 echo '<div
191 id="stripe-ideal-payment-data"
192 data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
193 data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
194
195 if ( $description ) {
196 echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id );
197 }
198
199 echo '</div>';
200 }
201
202 /**
203 * Creates the source for charge.
204 *
205 * @since 4.0.0
206 * @version 4.0.0
207 * @param object $order
208 * @return mixed
209 */
210 public function create_source( $order ) {
211 $currency = $order->get_currency();
212 $return_url = $this->get_stripe_return_url( $order );
213 $post_data = [];
214 $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency );
215 $post_data['currency'] = strtolower( $currency );
216 $post_data['type'] = 'ideal';
217 $post_data['owner'] = $this->get_owner_details( $order );
218 $post_data['redirect'] = [ 'return_url' => $return_url ];
219
220 if ( ! empty( $this->statement_descriptor ) ) {
221 $post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor );
222 }
223
224 WC_Stripe_Logger::log( 'Info: Begin creating iDEAL source' );
225
226 return WC_Stripe_API::request( apply_filters( 'wc_stripe_ideal_source', $post_data, $order ), 'sources' );
227 }
228
229 /**
230 * Process the payment
231 *
232 * @param int $order_id Reference.
233 * @param bool $retry Should we retry on fail.
234 * @param bool $force_save_source Force payment source to be saved.
235 *
236 * @throws Exception If payment will not be accepted.
237 *
238 * @return array|void
239 */
240 public function process_payment( $order_id, $retry = true, $force_save_source = false ) {
241 try {
242 $order = wc_get_order( $order_id );
243
244 // This will throw exception if not valid.
245 $this->validate_minimum_order_amount( $order );
246
247 // This comes from the create account checkbox in the checkout page.
248 $create_account = ! empty( $_POST['createaccount'] ) ? true : false;
249
250 if ( $create_account ) {
251 $new_customer_id = $order->get_customer_id();
252 $new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
253 $new_stripe_customer->create_customer();
254 }
255
256 $response = $this->create_source( $order );
257
258 if ( ! empty( $response->error ) ) {
259 $order->add_order_note( $response->error->message );
260
261 throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
262 }
263
264 $order->update_meta_data( '_stripe_source_id', $response->id );
265 $order->save();
266
267 WC_Stripe_Logger::log( 'Info: Redirecting to iDEAL...' );
268
269 return [
270 'result' => 'success',
271 'redirect' => esc_url_raw( $response->redirect->url ),
272 ];
273 } catch ( WC_Stripe_Exception $e ) {
274 wc_add_notice( $e->getLocalizedMessage(), 'error' );
275 WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
276
277 do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
278
279 if ( $order->has_status(
280 apply_filters(
281 'wc_stripe_allowed_payment_processing_statuses',
282 [ 'pending', 'failed' ],
283 $order
284 )
285 ) ) {
286 $this->send_failed_order_email( $order_id );
287 }
288
289 return [
290 'result' => 'fail',
291 'redirect' => '',
292 ];
293 }
294 }
295}