Passed
Push — master ( 2975b0...fd42df )
by Brian
05:40 queued 01:38
created

GetPaid_Bank_Transfer_Gateway::process_addons()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
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( 'addons' );
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_email_invoice_details', array( $this, 'email_instructions' ), 10, 3 );
50
51
    }
52
53
    /**
54
	 * Process Payment.
55
	 *
56
	 *
57
	 * @param WPInv_Invoice $invoice Invoice.
58
	 * @param array $submission_data Posted checkout fields.
59
	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
60
	 * @return array
61
	 */
62
	public function process_payment( $invoice, $submission_data, $submission ) {
63
64
        // Add a transaction id.
65
        $invoice->set_transaction_id( $invoice->generate_key('trans_') );
66
67
        // Set it as pending payment.
68
        if ( ! $invoice->needs_payment() ) {
69
            $invoice->mark_paid();
70
        } else if ( ! $invoice->is_paid() ) {
71
            $invoice->set_status( 'wpi-onhold' );
72
        }
73
74
        // Save it.
75
        $invoice->save();
76
77
        // Send to the success page.
78
        wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) );
79
80
    }
81
82
    /**
83
	 * Output for the order received page.
84
	 *
85
	 * @param WPInv_Invoice $invoice Invoice.
86
	 */
87
	public function thankyou_page( $invoice ) {
88
89
        if ( 'bank_transfer' === $invoice->get_gateway() && $invoice->needs_payment() ) {
90
91
			echo '<div class="mt-4 mb-2 getpaid-bank-transfer-details">' . PHP_EOL;
92
93
            if ( ! empty( $this->instructions ) ) {
94
                echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) );
95
			}
96
97
			$this->bank_details( $invoice );
98
			
99
			echo '</div>';
100
        
101
        }
102
103
	}
104
    
105
    /**
106
	 * Add content to the WPI emails.
107
	 *
108
	 * @param WPInv_Invoice $invoice Invoice.
109
	 * @param string     $email_type Email format: plain text or HTML.
110
	 * @param bool     $sent_to_admin Sent to admin.
111
	 */
112
	public function email_instructions( $invoice, $email_type, $sent_to_admin ) {
113
114
		if ( ! $sent_to_admin && 'bank_transfer' === $invoice->get_gateway() && $invoice->needs_payment() ) {
115
116
			echo '<div class="wpi-email-row getpaid-bank-transfer-details">';
117
118
			if ( $this->instructions ) {
119
				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

119
				echo wp_kses_post( wpautop( wptexturize( /** @scrutinizer ignore-type */ $this->instructions ) ) . PHP_EOL );
Loading history...
120
            }
121
122
			$this->bank_details( $invoice );
123
			
124
			echo '</div>';
125
126
		}
127
128
    }
129
    
130
    /**
131
	 * Get bank details and place into a list format.
132
	 *
133
	 * @param WPInv_Invoice $invoice Invoice.
134
	 */
135
	protected function bank_details( $invoice ) {
136
137
		// Get the invoice country and country $locale.
138
		$country = $invoice->get_country();
139
		$locale  = $this->get_country_locale();
140
141
		// Get sortcode label in the $locale array and use appropriate one.
142
		$sortcode = isset( $locale[ $country ]['sortcode']['label'] ) ? $locale[ $country ]['sortcode']['label'] : __( 'Sort code', 'invoicing' );
143
144
        $bank_fields = array(
145
            'ac_name'     => __( 'Account Name', 'invoicing' ),
146
            'ac_no'       => __( 'Account Number', 'invoicing' ),
147
            'bank_name'   => __( 'Bank Name', 'invoicing' ),
148
            'ifsc'        => __( 'IFSC code', 'invoicing' ),
149
            'iban'        => __( 'IBAN', 'invoicing' ),
150
            'bic'         => __( 'BIC/Swift code', 'invoicing' ),
151
            'sort_code'   => $sortcode,
152
        );
153
154
        $bank_info = array();
155
156
        foreach ( $bank_fields as $field => $label ) {
157
            $value = $this->get_option( $field );
158
159
            if ( ! empty( $value ) ) {
160
                $bank_info[$field] = array( 'label' => $label, 'value' => $value );
161
            }
162
163
        }
164
165
        $bank_info = apply_filters( 'wpinv_bank_info', $bank_info );
166
167
        if ( empty( $bank_info ) ) {
168
            return;
169
        }
170
171
		echo '<h3 class="getpaid-bank-transfer-title"> ' . apply_filters( 'wpinv_receipt_bank_details_title', __( 'Bank Details', 'invoicing' ) ) . '</h3>' . PHP_EOL;
172
173
		echo '<table class="table table-bordered getpaid-bank-transfer-details">' . PHP_EOL;
174
175
		foreach ( $bank_info as $key => $data ) {
176
177
			$key   = sanitize_html_class( $key );
178
			$label = wp_kses_post( $data['label'] );
179
			$value = wp_kses_post( wptexturize( $data['value'] ) );
180
181
			echo "<tr class='getpaid-bank-transfer-$key'><th>$label</th><td>$value</td></tr>" . PHP_EOL;
182
		}
183
184
		echo '</table>';
185
186
    }
187
    
188
    /**
189
	 * Get country locale if localized.
190
	 *
191
	 * @return array
192
	 */
193
	public function get_country_locale() {
194
195
		if ( empty( $this->locale ) ) {
196
197
			// Locale information to be used - only those that are not 'Sort Code'.
198
			$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...
199
				'getpaid_get_bank_transfer_locale',
200
				array(
201
					'AU' => array(
202
						'sortcode' => array(
203
							'label' => __( 'BSB', 'invoicing' ),
204
						),
205
					),
206
					'CA' => array(
207
						'sortcode' => array(
208
							'label' => __( 'Bank transit number', 'invoicing' ),
209
						),
210
					),
211
					'IN' => array(
212
						'sortcode' => array(
213
							'label' => __( 'IFSC', 'invoicing' ),
214
						),
215
					),
216
					'IT' => array(
217
						'sortcode' => array(
218
							'label' => __( 'Branch sort', 'invoicing' ),
219
						),
220
					),
221
					'NZ' => array(
222
						'sortcode' => array(
223
							'label' => __( 'Bank code', 'invoicing' ),
224
						),
225
					),
226
					'SE' => array(
227
						'sortcode' => array(
228
							'label' => __( 'Bank code', 'invoicing' ),
229
						),
230
					),
231
					'US' => array(
232
						'sortcode' => array(
233
							'label' => __( 'Routing number', 'invoicing' ),
234
						),
235
					),
236
					'ZA' => array(
237
						'sortcode' => array(
238
							'label' => __( 'Branch code', 'invoicing' ),
239
						),
240
					),
241
				)
242
			);
243
244
		}
245
246
		return $this->locale;
247
248
	}
249
250
	/**
251
	 * Filters the gateway settings.
252
	 * 
253
	 * @param array $admin_settings
254
	 */
255
	public function admin_settings( $admin_settings ) {
256
257
        $admin_settings['worldpay_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' );
258
		$admin_settings['worldpay_active']['desc'] = __( 'Enable bank transfer', 'invoicing' );
259
260
		$locale  = $this->get_country_locale();
261
262
		// Get sortcode label in the $locale array and use appropriate one.
263
		$country  = wpinv_default_billing_country();
264
		$sortcode = isset( $locale[ $country ]['sortcode']['label'] ) ? $locale[ $country ]['sortcode']['label'] : __( 'Sort code', 'invoicing' );
265
266
		$admin_settings['bank_transfer_ac_name'] = array(
267
            'type' => 'text',
268
            'id'   => 'bank_transfer_ac_name',
269
            'name' => __( 'Account Name', 'invoicing' ),
270
		);
271
		
272
		$admin_settings['bank_transfer_ac_no'] = array(
273
            'type' => 'text',
274
            'id'   => 'bank_transfer_ac_no',
275
            'name' => __( 'Account Number', 'invoicing' ),
276
		);
277
		
278
		$admin_settings['bank_transfer_bank_name'] = array(
279
            'type' => 'text',
280
            'id'   => 'bank_transfer_bank_name',
281
            'name' => __( 'Bank Name', 'invoicing' ),
282
		);
283
284
		$admin_settings['bank_transfer_ifsc'] = array(
285
            'type' => 'text',
286
            'id'   => 'bank_transfer_ifsc',
287
            'name' => __( 'IFSC Code', 'invoicing' ),
288
		);
289
290
		$admin_settings['bank_transfer_iban'] = array(
291
            'type' => 'text',
292
            'id'   => 'bank_transfer_iban',
293
            'name' => __( 'IBAN', 'invoicing' ),
294
		);
295
296
		$admin_settings['bank_transfer_bic'] = array(
297
            'type' => 'text',
298
            'id'   => 'bank_transfer_bic',
299
            'name' => __( 'BIC/Swift Code', 'invoicing' ),
300
		);
301
		
302
		$admin_settings['bank_transfer_sort_code'] = array(
303
			'type' => 'text',
304
			'id'   => 'bank_transfer_sort_code',
305
			'name' => $sortcode,
306
		);
307
308
		$admin_settings['bank_transfer_info'] = array(
309
            'id'   => 'bank_transfer_info',
310
            'name' => __( 'Instructions', 'invoicing' ),
311
            'desc' => __( 'Instructions that will be added to the thank you page and emails.', 'invoicing' ),
312
            'type' => 'textarea',
313
            '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' ),
314
            'cols' => 50,
315
            'rows' => 5
316
        );
317
318
		return $admin_settings;
319
	}
320
321
	/**
322
	 * Processes invoice addons.
323
	 *
324
	 * @param WPInv_Invoice $invoice
325
	 * @param GetPaid_Form_Item[] $items
326
	 * @return WPInv_Invoice
327
	 */
328
	public function process_addons( $invoice, $items ) {
329
330
        foreach ( $items as $item ) {
331
            $invoice->add_item( $item );
332
        }
333
334
        $invoice->recalculate_total();
335
        $invoice->save();
336
	}
337
338
}
339