class-wc-gateway-stripe-sepa.php
12.6 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class that handles SEPA payment method.
*
* @extends WC_Gateway_Stripe
*
* @since 4.0.0
*/
class WC_Gateway_Stripe_Sepa extends WC_Stripe_Payment_Gateway {
const ID = 'stripe_sepa';
/**
* 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;
/**
* Constructor
*/
public function __construct() {
$this->id = self::ID;
$this->method_title = __( 'Stripe SEPA Direct Debit', 'woocommerce-gateway-stripe' );
$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->has_fields = true;
$this->supports = [
'products',
'refunds',
'tokenization',
'add_payment_method',
];
// Load the form fields.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Check if subscriptions are enabled and add support for them.
$this->maybe_init_subscriptions();
// Check if pre-orders are enabled and add support for them.
$this->maybe_init_pre_orders();
$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->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? 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( 'wp_enqueue_scripts', [ $this, 'payment_scripts' ] );
}
/**
* Returns all supported currencies for this payment method.
*
* @since 4.0.0
* @version 4.0.0
* @return array
*/
public function get_supported_currency() {
return apply_filters(
'wc_stripe_sepa_supported_currencies',
[
'EUR',
]
);
}
/**
* Checks to see if all criteria is met before showing payment method.
*
* @since 4.0.0
* @version 4.0.0
* @return bool
*/
public function is_available() {
if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
return false;
}
if ( is_add_payment_method_page() && ! $this->saved_cards ) {
return false;
}
return parent::is_available();
}
/**
* Get_icon function.
*
* @since 1.0.0
* @version 4.0.0
* @return string
*/
public function get_icon() {
$icons = $this->payment_icons();
$icons_str = '';
$icons_str .= isset( $icons['sepa'] ) ? $icons['sepa'] : '';
return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
}
/**
* Outputs scripts used for stripe payment
*/
public function payment_scripts() {
if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
return;
}
wp_enqueue_style( 'stripe_styles' );
wp_enqueue_script( 'woocommerce_stripe' );
}
/**
* Initialize Gateway Settings Form Fields.
*/
public function init_form_fields() {
$this->form_fields = require WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-sepa-settings.php';
}
/**
* Displays the mandate acceptance notice to customer.
*
* @since 4.0.0
* @version 4.0.0
* @return string
*/
public function mandate_display() {
/* translators: statement descriptor */
printf( __( 'By providing your IBAN and confirming this payment, you are authorizing %s and Stripe, our payment service provider, to send instructions to your bank to debit your account and your bank to debit your account in accordance with those instructions. You are entitled to a refund from your bank under the terms and conditions of your agreement with your bank. A refund must be claimed within 8 weeks starting from the date on which your account was debited.', 'woocommerce-gateway-stripe' ), WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor ) );
}
/**
* Renders the Stripe elements form.
*
* @since 4.0.0
* @version 4.0.0
*/
public function form() {
?>
<fieldset id="wc-<?php echo esc_attr( $this->id ); ?>-form" class="wc-payment-form">
<?php do_action( 'woocommerce_credit_card_form_start', $this->id ); ?>
<p class="wc-stripe-sepa-mandate" style="margin-bottom:40px;"><?php $this->mandate_display(); ?></p>
<p class="form-row form-row-wide">
<label for="stripe-iban-element">
<?php esc_html_e( 'IBAN.', 'woocommerce-gateway-stripe' ); ?> <span class="required">*</span>
</label>
<div id="stripe-iban-element" class="wc-stripe-iban-element-field">
<!-- A Stripe Element will be inserted here. -->
</div>
</p>
<!-- Used to display form errors -->
<div class="stripe-source-errors" role="alert"></div>
<br />
<?php do_action( 'woocommerce_credit_card_form_end', $this->id ); ?>
<div class="clear"></div>
</fieldset>
<?php
}
/**
* Payment form on checkout page
*/
public function payment_fields() {
global $wp;
$total = WC()->cart->total;
$display_tokenization = $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards;
$description = $this->get_description();
$description = ! empty( $description ) ? $description : '';
// If paying from order, we need to get total from order not cart.
if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
$order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) );
$total = $order->get_total();
}
if ( is_add_payment_method_page() ) {
$total = '';
}
echo '<div
id="stripe-sepa_debit-payment-data"
data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
if ( $this->testmode ) {
$description .= ' ' . __( 'TEST MODE ENABLED. In test mode, you can use IBAN number DE89370400440532013000.', 'woocommerce-gateway-stripe' );
}
$description = trim( $description );
echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id );
if ( $display_tokenization ) {
$this->tokenization_script();
$this->saved_payment_methods();
}
$this->form();
if ( apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) && ! is_add_payment_method_page() && ! isset( $_GET['change_payment_method'] ) ) {
$this->save_payment_method_checkbox();
}
do_action( 'wc_stripe_payment_fields_stripe_sepa', $this->id );
echo '</div>';
}
/**
* Process the payment
*
* @param int $order_id Reference.
* @param bool $retry Should we retry on fail.
* @param bool $force_save_source Force save the payment source.
*
* @throws Exception If payment will not be accepted.
*
* @return array|void
*/
public function process_payment( $order_id, $retry = true, $force_save_source = false ) {
try {
$order = wc_get_order( $order_id );
if ( $this->has_subscription( $order_id ) ) {
$force_save_source = true;
}
if ( $this->maybe_change_subscription_payment_method( $order_id ) ) {
return $this->process_change_subscription_payment_method( $order_id );
}
if ( $this->maybe_process_pre_orders( $order_id ) ) {
return $this->process_pre_order( $order_id );
}
// This comes from the create account checkbox in the checkout page.
$create_account = ! empty( $_POST['createaccount'] ) ? true : false;
if ( $create_account ) {
$new_customer_id = $order->get_customer_id();
$new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
$new_stripe_customer->create_customer();
}
$prepared_source = $this->prepare_source( get_current_user_id(), $force_save_source );
$this->save_source_to_order( $order, $prepared_source );
// Result from Stripe API request.
$response = null;
if ( $order->get_total() > 0 ) {
// This will throw exception if not valid.
$this->validate_minimum_order_amount( $order );
WC_Stripe_Logger::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" );
// Make the request.
$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $prepared_source ) );
if ( ! empty( $response->error ) ) {
// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
if ( $this->is_no_such_customer_error( $response->error ) ) {
delete_user_option( $order->get_customer_id(), '_stripe_customer_id' );
$order->delete_meta_data( '_stripe_customer_id' );
$order->save();
}
if ( $this->is_no_such_token_error( $response->error ) && $prepared_source->token_id ) {
// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
$wc_token = WC_Payment_Tokens::get( $prepared_source->token_id );
$wc_token->delete();
$localized_message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
$order->add_order_note( $localized_message );
throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
}
// We want to retry.
if ( $this->is_retryable_error( $response->error ) ) {
if ( $retry ) {
// Don't do anymore retries after this.
if ( 5 <= $this->retry_interval ) {
return $this->process_payment( $order_id, false, $force_save_source );
}
sleep( $this->retry_interval );
$this->retry_interval++;
return $this->process_payment( $order_id, true, $force_save_source );
} else {
$localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' );
$order->add_order_note( $localized_message );
throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
}
}
$localized_messages = WC_Stripe_Helper::get_localized_messages();
if ( 'card_error' === $response->error->type ) {
$localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
} else {
$localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
}
$order->add_order_note( $localized_message );
throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
}
do_action( 'wc_gateway_stripe_process_payment', $response, $order );
// Process valid response.
$this->process_response( $response, $order );
} else {
$order->payment_complete();
}
// Remove cart.
WC()->cart->empty_cart();
// Return thank you page redirect.
return [
'result' => 'success',
'redirect' => $this->get_return_url( $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 );
if ( $order->has_status(
apply_filters(
'wc_stripe_allowed_payment_processing_statuses',
[ 'pending', 'failed' ],
$order
)
) ) {
$this->send_failed_order_email( $order_id );
}
return [
'result' => 'fail',
'redirect' => '',
];
}
}
}