Completed
Push — master ( 3ad42c...16627f )
by Brian
25s queued 14s
created

email_instructions()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 6
c 1
b 1
f 0
dl 0
loc 13
rs 9.6111
cc 5
nc 3
nop 3
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
	 * Payment method order.
24
	 *
25
	 * @var int
26
	 */
27
	public $order = 8;
28
    
29
    /**
30
	 * Class constructor.
31
	 */
32
	public function __construct() {
33
        parent::__construct();
34
35
        $this->title                = __( 'Direct bank transfer', 'invoicing' );
36
        $this->method_title         = __( 'Bank transfer', 'invoicing' );
37
        $this->checkout_button_text = __( 'Proceed', 'invoicing' );
38
        $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...
39
40
		add_action( 'wpinv_receipt_end', array( $this, 'thankyou_page' ) );
41
		add_action( 'getpaid_invoice_line_items', array( $this, 'thankyou_page' ), 40 );
42
		add_action( 'wpinv_email_invoice_details', array( $this, 'email_instructions' ), 10, 3 );
43
44
    }
45
46
    /**
47
	 * Process Payment.
48
	 *
49
	 *
50
	 * @param WPInv_Invoice $invoice Invoice.
51
	 * @param array $submission_data Posted checkout fields.
52
	 * @param GetPaid_Payment_Form_Submission $submission Checkout submission.
53
	 * @return array
54
	 */
55
	public function process_payment( $invoice, $submission_data, $submission ) {
56
57
        // Add a transaction id.
58
        $invoice->set_transaction_id( $invoice->generate_key('trans_') );
59
60
        // Set it as pending payment.
61
        if ( ! $invoice->needs_payment() ) {
62
            $invoice->mark_paid();
63
        } else if ( ! $invoice->is_paid() ) {
64
            $invoice->set_status( 'wpi-onhold' );
65
        }
66
67
        // Save it.
68
        $invoice->save();
69
70
        // Send to the success page.
71
        wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) );
72
73
    }
74
75
    /**
76
	 * Output for the order received page.
77
	 *
78
	 * @param WPInv_Invoice $invoice Invoice.
79
	 */
80
	public function thankyou_page( $invoice ) {
81
82
        if ( 'bank_transfer' === $invoice->get_gateway() && $invoice->needs_payment() ) {
83
84
			echo '<div class="mt-4 mb-2 getpaid-bank-transfer-details">' . PHP_EOL;
85
86
            if ( ! empty( $this->instructions ) ) {
87
                echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) );
88
			}
89
90
			$this->bank_details( $invoice );
91
			
92
			echo '</div>';
93
        
94
        }
95
96
	}
97
    
98
    /**
99
	 * Add content to the WooCommerce emails.
100
	 *
101
	 * @param WPInv_Invoice $invoice Invoice.
102
	 * @param string     $email_type Email format: plain text or HTML.
103
	 * @param bool     $sent_to_admin Sent to admin.
104
	 */
105
	public function email_instructions( $invoice, $email_type, $sent_to_admin ) {
106
107
		if ( ! $sent_to_admin && 'bank_transfer' === $invoice->get_gateway() && $invoice->needs_payment() ) {
108
109
			echo '<div class="wpi-email-row getpaid-bank-transfer-details">';
110
111
			if ( $this->instructions ) {
112
				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

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