blob: 080fead35fe312bc7ffdd3205d6f1ec472b9ea69 [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4}
5
6// phpcs:disable WordPress.Files.FileName
7/**
8 * Abstract class that will be inherited by voucher payment methods.
9 * Used by Boleto and OXXO
10 *
11 * @extends WC_Gateway_Stripe
12 *
13 * @since 5.8.0
14 */
15abstract class WC_Stripe_Payment_Gateway_Voucher extends WC_Stripe_Payment_Gateway {
16
17 /**
18 * ID used by UPE
19 *
20 * @var string
21 */
22 const ID = '';
23
24 /**
25 * ID used by WooCommerce to identify the payment method
26 * Override this when extending the class
27 *
28 * @var string
29 */
30 public $id = '';
31
32 /**
33 * ID used by stripe
34 * Change this when extending this class
35 */
36 protected $stripe_id = '';
37
38 /**
39 * List of accepted currencies
40 * Change this when extending this class
41 *
42 * @var array
43 */
44 protected $supported_currencies = [];
45
46 /**
47 * List of accepted countries
48 * Change this when extending this class
49 */
50 protected $supported_countries = [];
51
52 /**
53 * Notices (array)
54 *
55 * @var array
56 */
57 public $notices = [];
58
59 /**
60 * Is test mode active?
61 *
62 * @var bool
63 */
64 public $testmode;
65
66 /**
67 * Alternate credit card statement name
68 *
69 * @var bool
70 */
71 public $statement_descriptor;
72
73 /**
74 * API access secret key
75 *
76 * @var string
77 */
78 public $secret_key;
79
80 /**
81 * Api access publishable key
82 *
83 * @var string
84 */
85 public $publishable_key;
86
87 /**
88 * Should we store the users credit cards?
89 *
90 * @var bool
91 */
92 public $saved_cards;
93
94 /**
95 * Gateway has additional fields during checkout
96 *
97 * @var bool
98 */
99 public $has_fields = true;
100
101 /**
102 * Constructor
103 *
104 * @since 5.8.0
105 */
106 public function __construct() {
107 $this->method_description = sprintf(
108 /* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */
109 __( 'All other general Stripe settings can be adjusted %1$shere%2$s ', 'woocommerce-gateway-stripe' ),
110 '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' ) ) . '">',
111 '</a>'
112 );
113 $this->supports = [
114 'products',
115 ];
116
117 // Load the form fields.
118 $this->init_form_fields();
119
120 // Load the settings.
121 $this->init_settings();
122
123 $main_settings = get_option( 'woocommerce_stripe_settings' );
124 $this->title = $this->get_option( 'title' );
125 $this->description = $this->get_option( 'description' );
126 $this->enabled = $this->get_option( 'enabled' );
127 $this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
128 $this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
129 $this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
130 $this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : '';
131
132 if ( $this->testmode ) {
133 $this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : '';
134 $this->secret_key = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : '';
135 }
136
137 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] );
138 add_action(
139 'wc_ajax_wc_stripe_' . $this->stripe_id . '_update_payment_intent',
140 [
141 $this,
142 'update_payment_intent_ajax',
143 ]
144 );
145 add_action( 'wp_enqueue_scripts', [ $this, 'payment_scripts' ] );
146 }
147
148 /**
149 * Checks to see if all criteria is met before showing payment method.
150 *
151 * @return bool
152 * @return bool
153 * @since 5.8.0
154 */
155 public function is_available() {
156 if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
157 return false;
158 }
159
160 return parent::is_available();
161 }
162
163 /**
164 * Hides refund through stripe when payment method does not allow refund
165 *
166 * @param WC_Order $order
167 *
168 * @return array|bool
169 */
170 public function can_refund_order( $order ) {
171 return false;
172 }
173
174 /**
175 * Returns all supported currencies for this payment method.
176 *
177 * @return array
178 * @return array
179 * @since 5.8.0
180 */
181 public function get_supported_currency() {
182 return apply_filters(
183 'wc_stripe_' . $this->stripe_id . '_supported_currencies',
184 $this->supported_currencies
185 );
186 }
187
188 /**
189 * Get_icon function.
190 *
191 * @return string
192 * @since 5.8.0
193 */
194 public function get_icon() {
195 $icons = $this->payment_icons();
196
197 $icons_str = '';
198
199 $icons_str .= isset( $icons[ $this->stripe_id ] ) ? $icons[ $this->stripe_id ] : '';
200
201 return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
202 }
203
204 /**
205 * Payment_scripts function.
206 *
207 * @since 5.8.0
208 */
209 public function payment_scripts() {
210 if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
211 return;
212 }
213
214 parent::payment_scripts();
215 }
216
217 /**
218 * Initialize Gateway Settings Form Fields.
219 *
220 * @since 5.8.0
221 */
222 public function init_form_fields() {
223 $this->form_fields = require WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-' . $this->stripe_id . '-settings.php';
224 }
225
226 /**
227 * Process the payment
228 *
229 * @param int $order_id Reference.
230 * @param bool $retry Should we retry on fail.
231 * @param bool $force_save_source Force payment source to be saved.
232 *
233 * @throws Exception If payment will not be accepted.
234 *
235 * @since 5.8.0
236 * @since 5.8.0
237 */
238 public function process_payment( $order_id, $retry = true, $force_save_save = false ) {
239 try {
240 $order = wc_get_order( $order_id );
241
242 if ( ! in_array( $order->get_billing_country(), $this->supported_countries ) ) {
243 throw new \Exception( __( 'This payment method is not available in the selected country', 'woocommerce-gateway-stripe' ) );
244 }
245
246 $intent = $this->create_or_update_payment_intent( $order );
247
248 $order->update_meta_data( '_stripe_upe_payment_type', $this->stripe_id );
249 $order->update_status( 'pending', __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) );
250 $order->save();
251
252 WC_Stripe_Helper::add_payment_intent_to_order( $intent->id, $order );
253
254 return [
255 'result' => 'success',
256 'redirect' => $this->get_return_url( $order ),
257 'intent_id' => $intent->id,
258 'client_secret' => $intent->client_secret,
259 'order_id' => $order_id,
260 'confirm_payment_data' => $this->get_confirm_payment_data( $order ),
261 ];
262 } catch ( WC_Stripe_Exception $e ) {
263 wc_add_notice( $e->getLocalizedMessage(), 'error' );
264 WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
265
266 do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
267
268 $statuses = apply_filters(
269 'wc_stripe_allowed_payment_processing_statuses',
270 [ 'pending', 'failed' ],
271 $order
272 );
273
274 if ( $order->has_status( $statuses ) ) {
275 $this->send_failed_order_email( $order_id );
276 }
277
278 return [
279 'result' => 'fail',
280 'redirect' => '',
281 ];
282 }
283 }
284
285 /**
286 * Creates payment intent using order and store details.
287 * If the order already has a Payment Intent it gets updated
288 *
289 * @param WC_Order $order The order.
290 *
291 * @return object
292 * @throws Exception - If the create intent call returns with an error.
293 * @since 5.8.0
294 */
295 public function create_or_update_payment_intent( $order ) {
296 $amount = $order->get_total();
297 $currency = $order->get_currency();
298
299 $this->validate_amount_limits( $amount );
300
301 $intent = $this->get_intent_from_order( $order );
302
303 $intent_to_be_updated = '';
304
305 if ( $intent ) {
306 $intent_to_be_updated = '/' . $intent->id;
307 }
308
309 $body = [
310 'amount' => WC_Stripe_Helper::get_stripe_amount( $amount, strtolower( $currency ) ),
311 'currency' => strtolower( $currency ),
312 'payment_method_types' => [ $this->stripe_id ],
313 'description' => __( 'stripe - Order', 'woocommerce-gateway-stripe' ) . ' ' . $order->get_id(),
314 ];
315
316 if ( method_exists( $this, 'update_request_body_on_create_or_update_payment_intent' ) ) {
317 $body = $this->update_request_body_on_create_or_update_payment_intent( $body );
318 }
319
320 $payment_intent = WC_Stripe_API::request(
321 $body,
322 'payment_intents' . $intent_to_be_updated
323 );
324
325 if ( ! empty( $payment_intent->error ) ) {
326 throw new Exception( $payment_intent->error->message );
327 }
328
329 return $payment_intent;
330 }
331
332 /**
333 * Validates the minimum and maximum amount.
334 * Override this method when extending the class
335 *
336 * @param $amount
337 *
338 * @throws WC_Stripe_Exception when amount is out of range
339 * @since 5.8.0
340 */
341 abstract protected function validate_amount_limits( $amount );
342
343 /**
344 * Updates the payment intent when trying to pay again via Pay Order Page
345 */
346 public function update_payment_intent_ajax() {
347 try {
348 $is_nonce_valid = check_ajax_referer( 'wc_stripe_update_payment_intent_nonce', false, false );
349 if ( ! $is_nonce_valid ) {
350 throw new Exception( __( "We're not able to process this payment. Please refresh the page and try again.", 'woocommerce-gateway-stripe' ) );
351 }
352
353 $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : null;
354
355 if ( ! $order_id ) {
356 throw new \Exception( __( 'Order Id not found, send an order id', 'woocommerce-gateway-stripe' ) );
357 }
358
359 $order = wc_get_order( $order_id );
360 $order->set_payment_method( $this );
361 $intent = $this->create_or_update_payment_intent( $order );
362
363 $order->update_status( 'pending', __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) );
364 $order->update_meta_data( '_stripe_upe_payment_type', $this->stripe_id );
365 $order->save();
366
367 wp_send_json(
368 [
369 'redirect' => $this->get_return_url( $order ),
370 'intent_id' => $intent->id,
371 'client_secret' => $intent->client_secret,
372 'order_id' => $order_id,
373 'result' => 'success',
374 'confirm_payment_data' => $this->get_confirm_payment_data( $order ),
375 ]
376 );
377 } catch ( Exception $e ) {
378 // Send back error so it can be displayed to the customer.
379 wp_send_json(
380 [
381 'result' => 'fail',
382 'messages' => __( "We're not able to process this payment. Please refresh the page and try again.", 'woocommerce-gateway-stripe' ),
383 ]
384 );
385 }
386 }
387
388 /**
389 * Gather the data necessary to confirm the payment via javascript
390 * Override this when extending the class
391 *
392 * @param WC_Order $order
393 *
394 * @return array
395 */
396 protected function get_confirm_payment_data( $order ) {
397 return [];
398 }
399}