Passed
Push — master ( e76702...e1fb16 )
by Reüel
04:32
created

Gateway::get_issuer_field()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 0
dl 0
loc 19
rs 9.7
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Gateway::get_supported_payment_methods() 0 6 1
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Icepay;
4
5
use Exception;
6
use Icepay_Api_Webservice;
7
use Icepay_Basicmode;
8
use Icepay_Paymentmethod_Creditcard;
9
use Icepay_Paymentmethod_Ddebit;
10
use Icepay_Paymentmethod_Ideal;
11
use Icepay_Paymentmethod_Mistercash;
12
use Icepay_PaymentObject;
13
use Icepay_Result;
14
use Icepay_StatusCode;
15
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
16
use Pronamic\WordPress\Pay\Core\PaymentMethods;
17
use Pronamic\WordPress\Pay\Core\Statuses;
18
use Pronamic\WordPress\Pay\Payments\Payment;
19
use WP_Error;
0 ignored issues
show
Bug introduced by
The type WP_Error was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
/**
22
 * Title: ICEPAY gateway
23
 * Description:
24
 * Copyright: Copyright (c) 2005 - 2018
25
 * Company: Pronamic
26
 *
27
 * @author Remco Tolsma
28
 * @version 2.0.1
29
 * @since 1.0.0
30
 */
31
class Gateway extends Core_Gateway {
32
	/**
33
	 * Constructs and intializes an ICEPAY gateway
34
	 *
35
	 * @param Config $config Config.
36
	 */
37
	public function __construct( Config $config ) {
38
		parent::__construct( $config );
39
40
		// Default properties for this gateway.
41
		$this->set_method( self::METHOD_HTTP_REDIRECT );
42
		$this->set_slug( 'icepay' );
43
	}
44
45
	/**
46
	 * Filter iDEAL
47
	 *
48
	 * @param array $method Payment method.
49
	 *
50
	 * @return bool
51
	 */
52
	private function filter_ideal( $method ) {
53
		return is_array( $method ) && isset( $method['PaymentMethodCode'] ) && 'IDEAL' === $method['PaymentMethodCode'];
54
	}
55
56
	/**
57
	 * Get issuers
58
	 *
59
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
60
	 */
61
	public function get_issuers() {
62
		$groups  = array();
63
		$issuers = array();
64
65
		try {
66
			$methods = Icepay_Api_Webservice::getInstance()
67
						->paymentmethodService()
68
						->setMerchantID( $this->config->merchant_id )
69
						->setSecretCode( $this->config->secret_code )
70
						->retrieveAllPaymentmethods()
71
						->asArray();
72
		} catch ( Exception $e ) {
73
			return $groups;
74
		}
75
76
		$ideal_methods = array_filter( $methods, array( $this, 'filter_ideal' ) );
77
78
		if ( ! empty( $ideal_methods ) ) {
79
			$issuers = Icepay_Api_Webservice::getInstance()->singleMethod()
80
						->loadFromArray( $methods )
81
						->selectPaymentMethodByCode( 'IDEAL' )
82
						->getIssuers();
83
		}
84
85
		if ( $issuers ) {
86
			$options = array();
87
88
			foreach ( $issuers as $issuer ) {
89
				$options[ $issuer['IssuerKeyword'] ] = $issuer['Description'];
90
			}
91
92
			$groups[] = array(
93
				'options' => $options,
94
			);
95
		}
96
97
		return $groups;
98
	}
99
100
	/**
101
	 * Get issuers
102
	 *
103
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
104
	 */
105
	public function get_credit_card_issuers() {
106
		$groups  = array();
107
		$issuers = array();
108
109
		$method = new Icepay_Paymentmethod_Creditcard();
110
111
		if ( isset( $method->_issuer ) ) {
112
			$issuers = $method->_issuer;
113
		}
114
115
		if ( $issuers ) {
116
			$options = array();
117
118
			foreach ( $issuers as $issuer ) {
119
				switch ( $issuer ) {
120
					case 'AMEX':
121
						$name = _x( 'AMEX', 'Payment method name', '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

121
						$name = /** @scrutinizer ignore-call */ _x( 'AMEX', 'Payment method name', 'pronamic_ideal' );
Loading history...
122
123
						break;
124
					case 'MASTER':
125
						$name = _x( 'MASTER', 'Payment method name', 'pronamic_ideal' );
126
127
						break;
128
					case 'VISA':
129
						$name = _x( 'VISA', 'Payment method name', 'pronamic_ideal' );
130
131
						break;
132
					default:
133
						$name = $issuer;
134
135
						break;
136
				}
137
138
				$options[ $issuer ] = $name;
139
			}
140
141
			$groups[] = array(
142
				'options' => $options,
143
			);
144
		}
145
146
		return $groups;
147
	}
148
149
	/**
150
	 * Get supported payment methods
151
	 *
152
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
153
	 */
154
	public function get_supported_payment_methods() {
155
		return array(
156
			PaymentMethods::IDEAL,
157
			PaymentMethods::CREDIT_CARD,
158
			PaymentMethods::DIRECT_DEBIT,
159
			PaymentMethods::BANCONTACT,
160
		);
161
	}
162
163
	/**
164
	 * Start an transaction
165
	 *
166
	 * @see Core_Gateway::start()
167
	 *
168
	 * @param Payment $payment Payment.
169
	 */
170
	public function start( Payment $payment ) {
171
		try {
172
			/*
173
			 * Order ID
174
			 * Your unique order number.
175
			 * This can be auto incremental number from your payments table
176
			 *
177
			 * Data type  = String
178
			 * Max length = 10
179
			 * Required   = Yes
180
			 */
181
182
			// Payment object.
183
			$payment_object = new Icepay_PaymentObject();
184
			$payment_object
185
				->setAmount( $payment->get_total_amount()->get_cents() )
0 ignored issues
show
Bug introduced by
$payment->get_total_amount()->get_cents() of type double is incompatible with the type integer expected by parameter $amount of Icepay_PaymentObject::setAmount(). ( Ignorable by Annotation )

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

185
				->setAmount( /** @scrutinizer ignore-type */ $payment->get_total_amount()->get_cents() )
Loading history...
186
				->setReference( $payment->get_order_id() )
187
				->setDescription( $payment->get_description() )
188
				->setCurrency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() )
189
				->setIssuer( $payment->get_issuer() )
190
				->setOrderID( $payment->format_string( $this->config->order_id ) );
191
192
			if ( null !== $payment->get_customer() ) {
193
				// Language.
194
				$language = strtoupper( $payment->get_customer()->get_language() );
195
196
				$payment_object->setLanguage( $language );
197
198
				// Country.
199
				$locale = $payment->get_customer()->get_locale();
200
201
				$country = strtoupper( substr( $locale, 3, 2 ) );
202
203
				$payment_object->setCountry( $country );
204
			}
205
206
			/*
207
			 * Payment method
208
			 * @since 1.2.0
209
			 */
210
			$icepay_method = null;
211
212
			switch ( $payment->get_method() ) {
213
				case PaymentMethods::CREDIT_CARD:
214
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/creditcard.php
215
					$icepay_method = new Icepay_Paymentmethod_Creditcard();
216
217
					break;
218
				case PaymentMethods::DIRECT_DEBIT:
219
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/ddebit.php
220
					$icepay_method = new Icepay_Paymentmethod_Ddebit();
221
222
					break;
223
				case PaymentMethods::IDEAL:
224
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/ideal.php
225
					$icepay_method = new Icepay_Paymentmethod_Ideal();
226
227
					break;
228
				case PaymentMethods::BANCONTACT:
229
				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

229
				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...
230
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/mistercash.php
231
					$icepay_method = new Icepay_Paymentmethod_Mistercash();
232
233
					break;
234
			}
235
236
			if ( isset( $icepay_method ) ) {
237
				// @link https://github.com/icepay/icepay/blob/2.4.0/api/icepay_api_base.php#L342-L353
238
				$payment_object->setPaymentMethod( $icepay_method->getCode() );
239
			}
240
241
			// Protocol.
242
			$protocol = is_ssl() ? 'https' : 'http';
0 ignored issues
show
Bug introduced by
The function is_ssl 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

242
			$protocol = /** @scrutinizer ignore-call */ is_ssl() ? 'https' : 'http';
Loading history...
243
244
			// Basic mode.
245
			$basicmode = Icepay_Basicmode::getInstance();
246
			$basicmode
247
				->setMerchantID( $this->config->merchant_id )
248
				->setSecretCode( $this->config->secret_code )
249
				->setProtocol( $protocol )
250
				->setSuccessURL( $payment->get_return_url() )
251
				->setErrorURL( $payment->get_return_url() )
252
				->validatePayment( $payment_object );
253
254
			// Action URL.
255
			$payment->set_action_url( $basicmode->getURL() );
256
		} catch ( Exception $exception ) {
257
			$this->error = new WP_Error( 'icepay_error', $exception->getMessage(), $exception );
258
		}
259
	}
260
261
	/**
262
	 * Update the status of the specified payment
263
	 *
264
	 * @param Payment $payment Payment.
265
	 *
266
	 * @throws Exception
267
	 */
268
	public function update_status( Payment $payment ) {
269
		// Get the Icepay Result and set the required fields.
270
		$result = new Icepay_Result();
271
		$result
272
			->setMerchantID( $this->config->merchant_id )
273
			->setSecretCode( $this->config->secret_code );
274
275
		try {
276
			// Determine if the result can be validated.
277
			if ( $result->validate() ) {
278
				// What was the status response.
279
				switch ( $result->getStatus() ) {
280
					case Icepay_StatusCode::SUCCESS:
281
						$payment->set_status( Statuses::SUCCESS );
282
283
						break;
284
					case Icepay_StatusCode::OPEN:
285
						$payment->set_status( Statuses::OPEN );
286
287
						break;
288
					case Icepay_StatusCode::ERROR:
289
						$payment->set_status( Statuses::FAILURE );
290
291
						break;
292
				}
293
			}
294
		} catch ( Exception $exception ) {
295
			$this->error = new WP_Error( 'icepay_error', $exception->getMessage(), $exception );
296
		}
297
	}
298
}
299