Passed
Push — master ( 58ea02...3d9af1 )
by Brian
04:49
created

get_country_locale()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 54
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 29
nc 2
nop 0
dl 0
loc 54
rs 9.456
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Bank transfer payment gateway
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Bank transfer Payment Gateway class.
11
 *
12
 */
13
class GetPaid_Bank_Transfer_Gateway extends GetPaid_Payment_Gateway {
14
15
    /**
16
	 * Payment method id.
17
	 *
18
	 * @var string
19
	 */
20
    public $id = 'bank_transfer';
21
22
	/**
23
	 * An array of features that this gateway supports.
24
	 *
25
	 * @var array
26
	 */
27
	protected $supports = array( 'subscription', 'addons', 'single_subscription_group', 'multiple_subscription_groups' );
28
29
    /**
30
	 * Payment method order.
31
	 *
32
	 * @var int
33
	 */
34
	public $order = 8;
35
36
    /**
37
	 * Class constructor.
38
	 */
39
	public function __construct() {
40
        parent::__construct();
41
42
        $this->title                = __( 'Direct bank transfer', 'invoicing' );
43
        $this->method_title         = __( 'Bank transfer', 'invoicing' );
44
        $this->checkout_button_text = __( 'Proceed', 'invoicing' );
45
        $this->instructions         = apply_filters( 'wpinv_bank_instructions', $this->get_option( 'info' ) );
0 ignored issues
show
Bug Best Practice introduced by
The property instructions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
47
		add_action( 'wpinv_receipt_end', array( $this, 'thankyou_page' ) );
48
		add_action( 'getpaid_invoice_line_items', array( $this, 'thankyou_page' ), 40 );
49
		add_action( 'wpinv_pdf_content_billing', array( $this, 'thankyou_page' ), 11 );
50
		add_action( 'wpinv_email_invoice_details', array( $this, 'email_instructions' ), 10, 3 );
51
		add_action( 'getpaid_should_renew_subscription', array( $this, 'maybe_renew_subscription' ) );
52
		add_action( 'getpaid_invoice_status_publish', array( $this, 'invoice_paid' ), 20 );
53
54
    }
55
56
    /**
57
	 * Process Payment.
58
	 *
59
	 * @param WPInv_Invoice $invoice Invoice.
60
	 * @param array $submission_data Posted checkout fields.
61
	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
62
	 * @return array
63
	 */
64
	public function process_payment( $invoice, $submission_data, $submission ) {
65
66
        // Add a transaction id.
67
        $invoice->set_transaction_id( $invoice->generate_key('bt_') );
68
69
        // Set it as pending payment.
70
        if ( ! $invoice->needs_payment() ) {
71
            $invoice->mark_paid();
72
        } else if ( ! $invoice->is_paid() ) {
73
            $invoice->set_status( 'wpi-onhold' );
74
        }
75
76
        // Save it.
77
        $invoice->save();
78
79
        // Send to the success page.
80
        wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) );
81
82
    }
83
84
    /**
85
	 * Output for the order received page.
86
	 *
87
	 * @param WPInv_Invoice $invoice Invoice.
88
	 */
89
	public function thankyou_page( $invoice ) {
90
91
        if ( 'bank_transfer' === $invoice->get_gateway() && $invoice->needs_payment() ) {
92
93
			echo '<div class="mt-4 mb-2 getpaid-bank-transfer-details">' . PHP_EOL;
94
95
            if ( ! empty( $this->instructions ) ) {
96
                echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) );
97
			}
98
99
			$this->bank_details( $invoice );
100
101
			echo '</div>';
102
103
        }
104
105
	}
106
107
    /**
108
	 * Add content to the WPI emails.
109
	 *
110
	 * @param WPInv_Invoice $invoice Invoice.
111
	 * @param string     $email_type Email format: plain text or HTML.
112
	 * @param bool     $sent_to_admin Sent to admin.
113
	 */
114
	public function email_instructions( $invoice, $email_type, $sent_to_admin ) {
115
116
		if ( ! $sent_to_admin && 'bank_transfer' === $invoice->get_gateway() && $invoice->needs_payment() ) {
117
118
			echo '<div class="wpi-email-row getpaid-bank-transfer-details">';
119
120
			if ( $this->instructions ) {
121
				echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) . PHP_EOL );
0 ignored issues
show
Bug introduced by
It seems like $this->instructions can also be of type true; however, parameter $text of wptexturize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
				echo wp_kses_post( wpautop( wptexturize( /** @scrutinizer ignore-type */ $this->instructions ) ) . PHP_EOL );
Loading history...
122
            }
123
124
			$this->bank_details( $invoice );
125
126
			echo '</div>';
127
128
		}
129
130
    }
131
132
    /**
133
	 * Get bank details and place into a list format.
134
	 *
135
	 * @param WPInv_Invoice $invoice Invoice.
136
	 */
137
	protected function bank_details( $invoice ) {
138
139
		// Get the invoice country and country $locale.
140
		$country = $invoice->get_country();
141
		$locale  = $this->get_country_locale();
142
143
		// Get sortcode label in the $locale array and use appropriate one.
144
		$sortcode = isset( $locale[ $country ]['sortcode']['label'] ) ? $locale[ $country ]['sortcode']['label'] : __( 'Sort code', 'invoicing' );
145
146
        $bank_fields = array(
147
            'ac_name'     => __( 'Account Name', 'invoicing' ),
148
            'ac_no'       => __( 'Account Number', 'invoicing' ),
149
            'bank_name'   => __( 'Bank Name', 'invoicing' ),
150
            'ifsc'        => __( 'IFSC code', 'invoicing' ),
151
            'iban'        => __( 'IBAN', 'invoicing' ),
152
            'bic'         => __( 'BIC/Swift code', 'invoicing' ),
153
            'sort_code'   => $sortcode,
154
        );
155
156
        $bank_info = array();
157
158
        foreach ( $bank_fields as $field => $label ) {
159
            $value = $this->get_option( $field );
160
161
            if ( ! empty( $value ) ) {
162
                $bank_info[$field] = array( 'label' => $label, 'value' => $value );
163
            }
164
165
        }
166
167
        $bank_info = apply_filters( 'wpinv_bank_info', $bank_info );
168
169
        if ( empty( $bank_info ) ) {
170
            return;
171
        }
172
173
		echo '<h3 class="getpaid-bank-transfer-title"> ' . apply_filters( 'wpinv_receipt_bank_details_title', __( 'Bank Details', 'invoicing' ) ) . '</h3>' . PHP_EOL;
174
175
		echo '<table class="table table-bordered getpaid-bank-transfer-details">' . PHP_EOL;
176
177
		foreach ( $bank_info as $key => $data ) {
178
179
			$key   = sanitize_html_class( $key );
180
			$label = wp_kses_post( $data['label'] );
181
			$value = wp_kses_post( wptexturize( $data['value'] ) );
182
183
			echo "<tr class='getpaid-bank-transfer-$key'><th class='font-weight-bold'>$label</th><td class='w-75'>$value</td></tr>" . PHP_EOL;
184
		}
185
186
		echo '</table>';
187
188
    }
189
190
    /**
191
	 * Get country locale if localized.
192
	 *
193
	 * @return array
194
	 */
195
	public function get_country_locale() {
196
197
		if ( empty( $this->locale ) ) {
198
199
			// Locale information to be used - only those that are not 'Sort Code'.
200
			$this->locale = apply_filters(
0 ignored issues
show
Bug Best Practice introduced by
The property locale does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
201
				'getpaid_get_bank_transfer_locale',
202
				array(
203
					'AU' => array(
204
						'sortcode' => array(
205
							'label' => __( 'BSB', 'invoicing' ),
206
						),
207
					),
208
					'CA' => array(
209
						'sortcode' => array(
210
							'label' => __( 'Bank transit number', 'invoicing' ),
211
						),
212
					),
213
					'IN' => array(
214
						'sortcode' => array(
215
							'label' => __( 'IFSC', 'invoicing' ),
216
						),
217
					),
218
					'IT' => array(
219
						'sortcode' => array(
220
							'label' => __( 'Branch sort', 'invoicing' ),
221
						),
222
					),
223
					'NZ' => array(
224
						'sortcode' => array(
225
							'label' => __( 'Bank code', 'invoicing' ),
226
						),
227
					),
228
					'SE' => array(
229
						'sortcode' => array(
230
							'label' => __( 'Bank code', 'invoicing' ),
231
						),
232
					),
233
					'US' => array(
234
						'sortcode' => array(
235
							'label' => __( 'Routing number', 'invoicing' ),
236
						),
237
					),
238
					'ZA' => array(
239
						'sortcode' => array(
240
							'label' => __( 'Branch code', 'invoicing' ),
241
						),
242
					),
243
				)
244
			);
245
246
		}
247
248
		return $this->locale;
249
250
	}
251
252
	/**
253
	 * Filters the gateway settings.
254
	 *
255
	 * @param array $admin_settings
256
	 */
257
	public function admin_settings( $admin_settings ) {
258
259
        $admin_settings['bank_transfer_desc']['std']    = __( "Make your payment directly into our bank account. Please use your Invoice Number as the payment reference. Your invoice won't be processed until the funds have cleared in our account.", 'invoicing' );
260
		$admin_settings['bank_transfer_active']['desc'] = __( 'Enable bank transfer', 'invoicing' );
261
262
		$locale  = $this->get_country_locale();
263
264
		// Get sortcode label in the $locale array and use appropriate one.
265
		$country  = wpinv_default_billing_country();
266
		$sortcode = isset( $locale[ $country ]['sortcode']['label'] ) ? $locale[ $country ]['sortcode']['label'] : __( 'Sort code', 'invoicing' );
267
268
		$admin_settings['bank_transfer_ac_name'] = array(
269
            'type' => 'text',
270
            'id'   => 'bank_transfer_ac_name',
271
            'name' => __( 'Account Name', 'invoicing' ),
272
		);
273
274
		$admin_settings['bank_transfer_ac_no'] = array(
275
            'type' => 'text',
276
            'id'   => 'bank_transfer_ac_no',
277
            'name' => __( 'Account Number', 'invoicing' ),
278
		);
279
280
		$admin_settings['bank_transfer_bank_name'] = array(
281
            'type' => 'text',
282
            'id'   => 'bank_transfer_bank_name',
283
            'name' => __( 'Bank Name', 'invoicing' ),
284
		);
285
286
		$admin_settings['bank_transfer_ifsc'] = array(
287
            'type' => 'text',
288
            'id'   => 'bank_transfer_ifsc',
289
            'name' => __( 'IFSC Code', 'invoicing' ),
290
		);
291
292
		$admin_settings['bank_transfer_iban'] = array(
293
            'type' => 'text',
294
            'id'   => 'bank_transfer_iban',
295
            'name' => __( 'IBAN', 'invoicing' ),
296
		);
297
298
		$admin_settings['bank_transfer_bic'] = array(
299
            'type' => 'text',
300
            'id'   => 'bank_transfer_bic',
301
            'name' => __( 'BIC/Swift Code', 'invoicing' ),
302
		);
303
304
		$admin_settings['bank_transfer_sort_code'] = array(
305
			'type' => 'text',
306
			'id'   => 'bank_transfer_sort_code',
307
			'name' => $sortcode,
308
		);
309
310
		$admin_settings['bank_transfer_info'] = array(
311
            'id'   => 'bank_transfer_info',
312
            'name' => __( 'Instructions', 'invoicing' ),
313
            'desc' => __( 'Instructions that will be added to the thank you page and emails.', 'invoicing' ),
314
            'type' => 'textarea',
315
            'std'  => __( "Make your payment directly into our bank account. Please use your Invoice Number as the payment reference. Your invoice won't be processed until the funds have cleared in our account.", 'invoicing' ),
316
            'cols' => 50,
317
            'rows' => 5
318
        );
319
320
		return $admin_settings;
321
	}
322
323
	/**
324
	 * Processes invoice addons.
325
	 *
326
	 * @param WPInv_Invoice $invoice
327
	 * @param GetPaid_Form_Item[] $items
328
	 * @return WPInv_Invoice
329
	 */
330
	public function process_addons( $invoice, $items ) {
331
332
        foreach ( $items as $item ) {
333
            $invoice->add_item( $item );
334
        }
335
336
        $invoice->recalculate_total();
337
        $invoice->save();
338
	}
339
340
	/**
341
	 * (Maybe) renews a bank transfer subscription profile.
342
	 *
343
	 *
344
     * @param WPInv_Subscription $subscription
345
	 */
346
	public function maybe_renew_subscription( $subscription ) {
347
348
        // Ensure its our subscription && it's active.
349
        if ( $this->id == $subscription->get_gateway() && $subscription->has_status( 'active trialling' ) ) {
350
			$subscription->create_payment();
351
        }
352
353
    }
354
355
	/**
356
	 * Process a bank transfer payment.
357
	 *
358
	 *
359
     * @param WPInv_Invoice $invoice
360
	 */
361
	public function invoice_paid( $invoice ) {
362
363
		// Abort if not paid by bank transfer.
364
		if ( $this->id !== $invoice->get_gateway() || ! $invoice->is_recurring() ) {
365
			return;
366
		}
367
368
		// Is it a parent payment?
369
		if ( 0 == $invoice->get_parent_id() ) {
370
371
			// (Maybe) activate subscriptions.
372
			$subscriptions = getpaid_get_invoice_subscriptions( $invoice );
373
374
			if ( ! empty( $subscriptions ) ) {
375
				$subscriptions = is_array( $subscriptions ) ? $subscriptions : array( $subscriptions );
0 ignored issues
show
introduced by
The condition is_array($subscriptions) is always false.
Loading history...
376
377
				foreach ( $subscriptions as $subscription ) {
378
					if ( $subscription->exists() ) {
379
						$duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() );
380
						$expiry   = date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) );
381
382
						$subscription->set_next_renewal_date( $expiry );
383
						$subscription->set_date_created( current_time( 'mysql' ) );
384
						$subscription->set_profile_id( 'bt_sub_' . $invoice->get_id() . '_' . $subscription->get_id() );
385
						$subscription->activate();
386
					}
387
				}
388
389
			}
390
391
		} else {
392
393
			$subscription = getpaid_get_subscription( $invoice->get_subscription_id() );
394
395
			// Renew the subscription.
396
			if ( $subscription && $subscription->exists() ) {
397
				$subscription->add_payment( array(), $invoice );
398
				$subscription->renew();
399
			}
400
401
		}
402
403
    }
404
405
}
406