Failed Conditions
Push — develop ( 230387...d33540 )
by Remco
04:06
created

Gateway::start()   B

Complexity

Conditions 11
Paths 20

Size

Total Lines 56
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 39
c 1
b 0
f 0
nc 20
nop 1
dl 0
loc 56
ccs 0
cts 43
cp 0
crap 132
rs 7.3166

How to fix   Long Method    Complexity   

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
namespace Pronamic\WordPress\Pay\Gateways\Buckaroo;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\PaymentMethods as Core_PaymentMethods;
7
use Pronamic\WordPress\Pay\Core\Server;
8
use Pronamic\WordPress\Pay\Payments\Payment;
9
10
/**
11
 * Title: Buckaroo gateway
12
 * Description:
13
 * Copyright: 2005-2019 Pronamic
14
 * Company: Pronamic
15
 *
16
 * @author Remco Tolsma
17
 * @version 2.0.2
18
 * @since 1.0.0
19
 */
20
class Gateway extends Core_Gateway {
21
	/**
22
	 * Client.
23
	 *
24
	 * @var Client
25
	 */
26
	protected $client;
27
28
	/**
29
	 * Constructs and initializes an Buckaroo gateway
30
	 *
31
	 * @param Config $config Config.
32
	 */
33
	public function __construct( Config $config ) {
34
		parent::__construct( $config );
35
36
		$this->set_method( self::METHOD_HTML_FORM );
37
38
		$this->client = new Client();
39
		$this->client->set_website_key( $config->website_key );
40
		$this->client->set_secret_key( $config->secret_key );
41
		$this->client->set_excluded_services( $config->excluded_services );
42
		$this->client->set_invoice_number( $config->invoice_number );
43
		$this->client->set_push_url( add_query_arg( 'buckaroo_push', '', home_url( '/' ) ) );
44
45
		if ( self::MODE_TEST === $config->mode ) {
46
			$this->client->set_payment_server_url( Client::GATEWAY_TEST_URL );
47
		}
48
	}
49
50
	/**
51
	 * Get issuers.
52
	 *
53
	 * @since 1.2.4
54
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
55
	 */
56
	public function get_issuers() {
57
		$groups = array();
58
59
		$result = $this->client->get_issuers();
60
61
		if ( $result ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
introduced by
$result is an empty array, thus is always false.
Loading history...
62
			$groups[] = array(
63
				'options' => $result,
64
			);
65
66
			return $groups;
67
		}
68
69
		$this->error = $this->client->get_error();
70
	}
71
72
	/**
73
	 * Get supported payment methods
74
	 *
75
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
76
	 */
77
	public function get_supported_payment_methods() {
78
		return array(
79
			Core_PaymentMethods::BANK_TRANSFER,
80
			Core_PaymentMethods::BANCONTACT,
81
			Core_PaymentMethods::CREDIT_CARD,
82
			Core_PaymentMethods::GIROPAY,
83
			Core_PaymentMethods::IDEAL,
84
			Core_PaymentMethods::PAYPAL,
85
			Core_PaymentMethods::SOFORT,
86
		);
87
	}
88
89
	/**
90
	 * Start
91
	 *
92
	 * @param Payment $payment Payment.
93
	 *
94
	 * @see Core_Gateway::start()
95
	 */
96
	public function start( Payment $payment ) {
97
		$payment->set_action_url( $this->client->get_payment_server_url() );
98
99
		$payment_method = $payment->get_method();
100
101
		switch ( $payment_method ) {
102
			case Core_PaymentMethods::IDEAL:
103
				$this->client->set_payment_method( PaymentMethods::IDEAL );
104
				$this->client->set_ideal_issuer( $payment->get_issuer() );
105
106
				break;
107
			case Core_PaymentMethods::CREDIT_CARD:
108
				$this->client->add_requested_service( PaymentMethods::AMERICAN_EXPRESS );
109
				$this->client->add_requested_service( PaymentMethods::MAESTRO );
110
				$this->client->add_requested_service( PaymentMethods::MASTERCARD );
111
				$this->client->add_requested_service( PaymentMethods::VISA );
112
113
				break;
114
			case Core_PaymentMethods::BANK_TRANSFER:
115
			case Core_PaymentMethods::BANCONTACT:
116
			case Core_PaymentMethods::MISTER_CASH:
0 ignored issues
show
Deprecated Code introduced by
The constant Pronamic\WordPress\Pay\C...entMethods::MISTER_CASH has been deprecated: "Bancontact/Mister Cash" was renamed to just "Bancontact". ( Ignorable by Annotation )

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

116
			case /** @scrutinizer ignore-deprecated */ Core_PaymentMethods::MISTER_CASH:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
117
			case Core_PaymentMethods::GIROPAY:
118
			case Core_PaymentMethods::PAYPAL:
119
			case Core_PaymentMethods::SOFORT:
120
				$this->client->set_payment_method( PaymentMethods::transform( $payment_method ) );
121
122
				break;
123
			default:
124
				if ( '0' !== $payment_method ) {
125
					// Leap of faith if the WordPress payment method could not transform to a Buckaroo method?
126
					$this->client->set_payment_method( $payment_method );
127
				}
128
129
				break;
130
		}
131
132
		// Locale.
133
		$locale = '';
134
135
		if ( null !== $payment->get_customer() ) {
136
			$locale = $payment->get_customer()->get_locale();
137
		}
138
139
		// Buckaroo uses 'nl-NL' instead of 'nl_NL'.
140
		$culture = str_replace( '_', '-', $locale );
141
142
		$this->client->set_payment_id( $payment->get_id() );
143
		$this->client->set_culture( $culture );
144
		$this->client->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() );
145
		$this->client->set_description( $payment->get_description() );
146
		$this->client->set_amount( $payment->get_total_amount()->get_value() );
0 ignored issues
show
Bug introduced by
$payment->get_total_amount()->get_value() of type double is incompatible with the type integer expected by parameter $amount of Pronamic\WordPress\Pay\G...oo\Client::set_amount(). ( Ignorable by Annotation )

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

146
		$this->client->set_amount( /** @scrutinizer ignore-type */ $payment->get_total_amount()->get_value() );
Loading history...
147
		$this->client->set_invoice_number( Util::get_invoice_number( $this->client->get_invoice_number(), $payment ) );
148
		$this->client->set_return_url( $payment->get_return_url() );
149
		$this->client->set_return_cancel_url( $payment->get_return_url() );
150
		$this->client->set_return_error_url( $payment->get_return_url() );
151
		$this->client->set_return_reject_url( $payment->get_return_url() );
152
	}
153
154
	/**
155
	 * Get output HTML
156
	 *
157
	 * @since 1.1.1
158
	 * @see Pronamic_WP_Pay_Gateway::get_output_html()
159
	 */
160
	public function get_output_fields() {
161
		return $this->client->get_fields();
162
	}
163
164
	/**
165
	 * Update status of the specified payment
166
	 *
167
	 * @param Payment $payment Payment.
168
	 */
169
	public function update_status( Payment $payment ) {
170
		$method = Server::get( 'REQUEST_METHOD', FILTER_SANITIZE_STRING );
171
172
		$data = array();
173
174
		switch ( $method ) {
175
			case 'GET':
176
				$data = $_GET; // WPCS: CSRF OK.
177
178
				break;
179
			case 'POST':
180
				$data = $_POST; // WPCS: CSRF OK.
181
182
				break;
183
		}
184
185
		$data = Util::urldecode( $data );
186
187
		$data = stripslashes_deep( $data );
188
189
		$data = $this->client->verify_request( $data );
190
191
		if ( $data ) {
192
			$payment->set_transaction_id( $data[ Parameters::PAYMENT ] );
193
			$payment->set_status( Statuses::transform( $data[ Parameters::STATUS_CODE ] ) );
194
			$payment->set_consumer_iban( $data[ Parameters::SERVICE_IDEAL_CONSUMER_IBAN ] );
195
			$payment->set_consumer_bic( $data[ Parameters::SERVICE_IDEAL_CONSUMER_BIC ] );
196
			$payment->set_consumer_name( $data[ Parameters::SERVICE_IDEAL_CONSUMER_NAME ] );
197
198
			$labels = array(
199
				Parameters::PAYMENT                       => __( 'Payment', 'pronamic_ideal' ),
200
				Parameters::PAYMENT_METHOD                => __( 'Payment Method', 'pronamic_ideal' ),
201
				Parameters::STATUS_CODE                   => __( 'Status Code', 'pronamic_ideal' ),
202
				Parameters::STATUS_CODE_DETAIL            => __( 'Status Code Detail', 'pronamic_ideal' ),
203
				Parameters::STATUS_MESSAGE                => __( 'Status Message', 'pronamic_ideal' ),
204
				Parameters::INVOICE_NUMBER                => __( 'Invoice Number', 'pronamic_ideal' ),
205
				Parameters::AMOUNT                        => __( 'Amount', 'pronamic_ideal' ),
206
				Parameters::CURRENCY                      => __( 'Currency', 'pronamic_ideal' ),
207
				Parameters::TIMESTAMP                     => __( 'Timestamp', 'pronamic_ideal' ),
208
				Parameters::SERVICE_IDEAL_CONSUMER_ISSUER => __( 'Service iDEAL Consumer Issuer', 'pronamic_ideal' ),
209
				Parameters::SERVICE_IDEAL_CONSUMER_NAME   => __( 'Service iDEAL Consumer Name', 'pronamic_ideal' ),
210
				Parameters::SERVICE_IDEAL_CONSUMER_IBAN   => __( 'Service iDEAL Consumer IBAN', 'pronamic_ideal' ),
211
				Parameters::SERVICE_IDEAL_CONSUMER_BIC    => __( 'Service iDEAL Consumer BIC', 'pronamic_ideal' ),
212
				Parameters::TRANSACTIONS                  => __( 'Transactions', 'pronamic_ideal' ),
213
			);
214
215
			$note = '';
216
217
			$note .= '<p>';
218
			$note .= __( 'Buckaroo data:', 'pronamic_ideal' );
219
			$note .= '</p>';
220
221
			$note .= '<dl>';
222
223
			foreach ( $labels as $key => $label ) {
224
				if ( isset( $data[ $key ] ) ) {
225
					$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
226
					$note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) );
227
				}
228
			}
229
230
			$note .= '</dl>';
231
232
			$payment->add_note( $note );
233
		}
234
	}
235
}
236