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