Test Failed
Push — develop ( 038caf...6d438f )
by Reüel
03:03
created

Gateway::update_status()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 51
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 51
rs 9.392
c 0
b 0
f 0
cc 4
nc 4
nop 1

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
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\PaymentMethods;
7
use Pronamic\WordPress\Pay\Payments\Payment;
8
9
/**
10
 * Title: OmniKassa
11
 * Description:
12
 * Copyright: 2005-2019 Pronamic
13
 * Company: Pronamic
14
 *
15
 * @author  Remco Tolsma
16
 * @version 2.0.0
17
 * @since   1.0.0
18
 */
19
class Gateway extends Core_Gateway {
20
	/**
21
	 * Client.
22
	 *
23
	 * @var Client
24
	 */
25
	protected $client;
26
27
	/**
28
	 * Constructs and initializes an OmniKassa gateway
29
	 *
30
	 * @param Config $config Config.
31
	 */
32
	public function __construct( Config $config ) {
33
		parent::__construct( $config );
34
35
		$this->set_method( self::METHOD_HTML_FORM );
36
37
		// Client.
38
		$this->client = new Client();
39
40
		$action_url = Client::ACTION_URL_PRUDCTION;
41
42
		if ( self::MODE_TEST === $config->mode ) {
43
			$action_url = Client::ACTION_URL_TEST;
44
		}
45
46
		$this->client->set_action_url( $action_url );
47
		$this->client->set_merchant_id( $config->merchant_id );
48
		$this->client->set_key_version( $config->key_version );
49
		$this->client->set_secret_key( $config->secret_key );
50
	}
51
52
	/**
53
	 * Get supported payment methods
54
	 *
55
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
56
	 */
57
	public function get_supported_payment_methods() {
58
		return array(
59
			PaymentMethods::IDEAL,
60
			PaymentMethods::CREDIT_CARD,
61
			PaymentMethods::DIRECT_DEBIT,
62
			PaymentMethods::BANCONTACT,
63
		);
64
	}
65
66
	/**
67
	 * Start
68
	 *
69
	 * @see Pronamic_WP_Pay_Gateway::start()
70
	 *
71
	 * @param Payment $payment Payment.
72
	 */
73
	public function start( Payment $payment ) {
74
		$transaction_reference = $payment->get_meta( 'omnikassa_transaction_reference' );
75
76
		if ( empty( $transaction_reference ) ) {
77
			$transaction_reference = md5( uniqid( '', true ) );
78
79
			$payment->set_meta( 'omnikassa_transaction_reference', $transaction_reference );
80
		}
81
82
		$payment->set_transaction_id( $transaction_reference );
83
		$payment->set_action_url( $this->client->get_action_url() );
84
85
		$language = null;
86
87
		if ( null !== $payment->get_customer() ) {
88
			$language = $payment->get_customer()->get_language();
89
		}
90
91
		$this->client->set_customer_language( LocaleHelper::transform( $language ) );
92
		$this->client->set_currency_numeric_code( $payment->get_total_amount()->get_currency()->get_numeric_code() );
93
		$this->client->set_order_id( $payment->format_string( $this->config->order_id ) );
94
		$this->client->set_normal_return_url( home_url( '/' ) );
0 ignored issues
show
Bug introduced by
The function home_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

94
		$this->client->set_normal_return_url( /** @scrutinizer ignore-call */ home_url( '/' ) );
Loading history...
95
		$this->client->set_automatic_response_url( home_url( '/' ) );
96
		$this->client->set_amount( $payment->get_total_amount()->get_cents() );
97
		$this->client->set_transaction_reference( $transaction_reference );
98
99
		switch ( $payment->get_method() ) {
100
			/*
101
			 * If this field is not supplied in the payment request, then
102
			 * by default the customer will be redirected to the Rabo
103
			 * OmniKassa payment page. On the payment page, the
104
			 * customer can choose from the payment methods
105
			 * offered by the Rabo OmniKassa. These are the payment
106
			 * methods: IDEAL, VISA, MASTERCARD,
107
			 * MAESTRO, V PAY and BCMC.
108
			 *
109
			 * Exception: the register services INCASSO (direct debit),
110
			 * ACCEPTGIRO (giro collection form) and REMBOURS
111
			 * (cash on delivery) are not displayed on the Rabo
112
			 * OmniKassa payment page by default.
113
			 * If you wish to offer these register services to the
114
			 * customer on the payment page, then you need to
115
			 * always populate the paymentMeanBrandList field with
116
			 * all the payment methods you wish to offer (provided
117
			 * these have been requested and activated): IDEAL,
118
			 * VISA, MASTERCARD, MAESTRO, VPAY, BCMC,
119
			 * INCASSO, ACCEPTGIRO, REMBOURS.
120
			 *
121
			 * If you let the customer choose the payment method
122
			 * while still in your webshop, then you must populate
123
			 * this field of the payment request with only the selected
124
			 * payment method. Populating this field with only one
125
			 * payment method will instruct the Rabo OmniKassa to
126
			 * redirect the customer directly to the payment page for
127
			 * this payment method.
128
			 */
129
			case PaymentMethods::BANCONTACT:
130
			case 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

130
			case /** @scrutinizer ignore-deprecated */ 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...
131
				$this->client->add_payment_mean_brand( Methods::BCMC );
132
133
				break;
134
			case PaymentMethods::CREDIT_CARD:
135
				$this->client->add_payment_mean_brand( Methods::MAESTRO );
136
				$this->client->add_payment_mean_brand( Methods::MASTERCARD );
137
				$this->client->add_payment_mean_brand( Methods::VISA );
138
				$this->client->add_payment_mean_brand( Methods::VPAY );
139
140
				break;
141
			case PaymentMethods::DIRECT_DEBIT:
142
				$this->client->add_payment_mean_brand( Methods::INCASSO );
143
144
				break;
145
			case PaymentMethods::MAESTRO:
146
				$this->client->add_payment_mean_brand( Methods::MAESTRO );
147
148
				break;
149
			case PaymentMethods::IDEAL:
150
				$this->client->add_payment_mean_brand( Methods::IDEAL );
151
152
				break;
153
			default:
154
				$this->client->add_payment_mean_brand( Methods::IDEAL );
155
				$this->client->add_payment_mean_brand( Methods::VISA );
156
				$this->client->add_payment_mean_brand( Methods::MASTERCARD );
157
				$this->client->add_payment_mean_brand( Methods::MAESTRO );
158
				$this->client->add_payment_mean_brand( Methods::VPAY );
159
				$this->client->add_payment_mean_brand( Methods::BCMC );
160
				$this->client->add_payment_mean_brand( Methods::INCASSO );
161
				$this->client->add_payment_mean_brand( Methods::ACCEPTGIRO );
162
				$this->client->add_payment_mean_brand( Methods::REMBOURS );
163
164
				break;
165
		}
166
	}
167
168
	/**
169
	 * Get the output HTML
170
	 *
171
	 * @since 1.1.2
172
	 * @see   Pronamic_WP_Pay_Gateway::get_output_html()
173
	 */
174
	public function get_output_fields() {
175
		return $this->client->get_fields();
176
	}
177
178
	/**
179
	 * Update status of the specified payment
180
	 *
181
	 * @param Payment $payment Payment.
182
	 */
183
	public function update_status( Payment $payment ) {
184
		$input_data = filter_input( INPUT_POST, 'Data' );
185
		$input_seal = filter_input( INPUT_POST, 'Seal', FILTER_SANITIZE_STRING );
186
187
		$data = Client::parse_piped_string( $input_data );
188
189
		$seal = Client::compute_seal( $input_data, $this->config->secret_key );
190
191
		// Check if the posted seal is equal to our seal.
192
		if ( 0 === strcasecmp( $input_seal, $seal ) ) {
193
			$response_code = $data['responseCode'];
194
195
			$status = ResponseCodes::transform( $response_code );
196
197
			// Set the status of the payment.
198
			$payment->set_status( $status );
199
200
			$labels = array(
201
				'amount'               => __( 'Amount', 'pronamic_ideal' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

201
				'amount'               => /** @scrutinizer ignore-call */ __( 'Amount', 'pronamic_ideal' ),
Loading history...
202
				'captureDay'           => _x( 'Capture Day', 'creditcard', 'pronamic_ideal' ),
0 ignored issues
show
Bug introduced by
The function _x was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

202
				'captureDay'           => /** @scrutinizer ignore-call */ _x( 'Capture Day', 'creditcard', 'pronamic_ideal' ),
Loading history...
203
				'captureMode'          => _x( 'Capture Mode', 'creditcard', 'pronamic_ideal' ),
204
				'currencyCode'         => __( 'Currency Code', 'pronamic_ideal' ),
205
				'merchantId'           => __( 'Merchant ID', 'pronamic_ideal' ),
206
				'orderId'              => __( 'Order ID', 'pronamic_ideal' ),
207
				'transactionDateTime'  => __( 'Transaction Date Time', 'pronamic_ideal' ),
208
				'transactionReference' => __( 'Transaction Reference', 'pronamic_ideal' ),
209
				'keyVersion'           => __( 'Key Version', 'pronamic_ideal' ),
210
				'authorisationId'      => __( 'Authorisation ID', 'pronamic_ideal' ),
211
				'paymentMeanBrand'     => __( 'Payment Mean Brand', 'pronamic_ideal' ),
212
				'paymentMeanType'      => __( 'Payment Mean Type', 'pronamic_ideal' ),
213
				'responseCode'         => __( 'Response Code', 'pronamic_ideal' ),
214
			);
215
216
			$note = '';
217
218
			$note .= '<p>';
219
			$note .= __( 'OmniKassa transaction data in response message:', 'pronamic_ideal' );
220
			$note .= '</p>';
221
222
			$note .= '<dl>';
223
224
			foreach ( $labels as $key => $label ) {
225
				if ( isset( $data[ $key ] ) ) {
226
					$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
0 ignored issues
show
Bug introduced by
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

226
					$note .= sprintf( '<dt>%s</dt>', /** @scrutinizer ignore-call */ esc_html( $label ) );
Loading history...
227
					$note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) );
228
				}
229
			}
230
231
			$note .= '</dl>';
232
233
			$payment->add_note( $note );
234
		}
235
	}
236
}
237