Gateway::start()   F
last analyzed

Complexity

Conditions 14
Paths 720

Size

Total Lines 152
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 11
Bugs 0 Features 0
Metric Value
cc 14
eloc 75
c 11
b 0
f 0
nc 720
nop 1
dl 0
loc 152
ccs 0
cts 96
cp 0
crap 210
rs 2.5365

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\PayNL;
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: Pay.nl gateway
11
 * Description:
12
 * Copyright: 2005-2021 Pronamic
13
 * Company: Pronamic
14
 *
15
 * @author  Remco Tolsma
16
 * @version 3.0.1
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 Pay.nl 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_HTTP_REDIRECT );
36
37
		// Supported features.
38
		$this->supports = array(
39
			'payment_status_request',
40
		);
41
42
		// Client.
43
		$this->client = new Client( $config->token, $config->service_id );
44
	}
45
46
	/**
47
	 * Get issuers
48
	 *
49
	 * @see Core_Gateway::get_issuers()
50
	 */
51
	public function get_issuers() {
52
		$groups = array();
53
54
		try {
55
			$result = $this->client->get_issuers();
56
57
			if ( is_array( $result ) ) {
58
				$groups[] = array(
59
					'options' => $result,
60
				);
61
			}
62
		} catch ( \Exception $e ) {
63
			$this->error = new \WP_Error( 'pay_nl_error', $e->getMessage() );
64
65
			return $groups;
66
		}
67
68
		return $groups;
69
	}
70
71
	/**
72
	 * Get supported payment methods
73
	 *
74
	 * @see Core_Gateway::get_supported_payment_methods()
75
	 */
76
	public function get_supported_payment_methods() {
77
		return array(
78
			PaymentMethods::AFTERPAY,
79
			PaymentMethods::BANCONTACT,
80
			PaymentMethods::BANK_TRANSFER,
81
			PaymentMethods::CREDIT_CARD,
82
			PaymentMethods::FOCUM,
83
			PaymentMethods::GIROPAY,
84
			PaymentMethods::IDEAL,
85
			PaymentMethods::IN3,
86
			PaymentMethods::KLARNA_PAY_LATER,
87
			PaymentMethods::MAESTRO,
88
			PaymentMethods::PAYPAL,
89
			PaymentMethods::SOFORT,
90
			PaymentMethods::SPRAYPAY,
91
		);
92
	}
93
94
	/**
95
	 * Start.
96
	 *
97
	 * @see Core_Gateway::start()
98
	 *
99
	 * @param Payment $payment Payment.
100
	 */
101
	public function start( Payment $payment ) {
102
		$payment_method = $payment->get_method();
103
104
		$customer = $payment->get_customer();
105
106
		/**
107
		 * End user.
108
		 */
109
		$end_user = array();
110
111
		if ( null !== $customer ) {
112
			$end_user['gender']       = $customer->get_gender();
113
			$end_user['phoneNumber']  = $customer->get_phone();
114
			$end_user['emailAddress'] = $customer->get_email();
115
			$end_user['language']     = $customer->get_language();
116
117
			/**
118
			 * Name.
119
			 */
120
			$name = $customer->get_name();
121
122
			if ( null !== $name ) {
123
				$end_user['initials'] = \substr( (string) $name->get_first_name(), 0, 32 );
124
				$end_user['lastName'] = \substr( (string) $name->get_last_name(), 0, 32 );
125
			}
126
127
			/**
128
			 * Date of Birth.
129
			 */
130
			$birth_date = $customer->get_birth_date();
131
132
			if ( $birth_date instanceof \DateTimeInterface ) {
133
				$end_user['dob'] = $birth_date->format( 'dmY' );
134
			}
135
		}
136
137
		/**
138
		 * End user - Address.
139
		 */
140
		$shipping_address = $payment->get_shipping_address();
141
142
		if ( null !== $shipping_address ) {
143
			$address = array(
144
				'streetName'            => $shipping_address->get_street_name(),
145
				'streetNumber'          => $shipping_address->get_house_number_base(),
146
				'streetNumberExtension' => $shipping_address->get_house_number_addition(),
147
				'zipCode'               => $shipping_address->get_postal_code(),
148
				'city'                  => $shipping_address->get_city(),
149
				'countryCode'           => $shipping_address->get_country_code(),
150
			);
151
152
			$end_user['address'] = $address;
153
		}
154
155
		/**
156
		 * End user - Invoice address.
157
		 */
158
		$billing_address = $payment->get_billing_address();
159
160
		if ( null !== $billing_address ) {
161
			$address = array(
162
				'streetName'            => $billing_address->get_street_name(),
163
				'streetNumber'          => $billing_address->get_house_number_base(),
164
				'streetNumberExtension' => $billing_address->get_house_number_addition(),
165
				'zipCode'               => $billing_address->get_postal_code(),
166
				'city'                  => $billing_address->get_city(),
167
				'countryCode'           => $billing_address->get_country_code(),
168
			);
169
170
			if ( \array_key_exists( 'gender', $end_user ) ) {
171
				$address['gender'] = $end_user['gender'];
172
			}
173
174
			if ( \array_key_exists( 'initials', $end_user ) ) {
175
				$address['initials'] = $end_user['initials'];
176
			}
177
178
			if ( \array_key_exists( 'lastName', $end_user ) ) {
179
				$address['lastName'] = $end_user['lastName'];
180
			}
181
182
			$end_user['invoiceAddress'] = $address;
183
		}
184
185
		/**
186
		 * Sale data.
187
		 */
188
		$sale_data = array(
189
			'invoiceDate'  => $payment->get_date()->format( 'd-m-Y' ),
190
			'deliveryDate' => $payment->get_date()->format( 'd-m-Y' ),
191
		);
192
193
		$payment_lines = $payment->get_lines();
194
195
		if ( null !== $payment_lines ) {
196
			$sale_data['order_data'] = array();
197
198
			foreach ( $payment_lines as $line ) {
199
				$order_data_item = array(
200
					'productId'   => $line->get_id(),
201
					'productType' => ProductTypes::transform( $line->get_type() ),
202
					'description' => $line->get_name(),
203
					'quantity'    => $line->get_quantity(),
204
				);
205
206
				$unit_price = $line->get_unit_price();
207
208
				if ( null !== $unit_price ) {
209
					$order_data_item['price'] = $unit_price->get_minor_units()->to_int();
210
				}
211
212
				$sale_data['order_data'][] = $order_data_item;
213
			}
214
		}
215
216
		/**
217
		 * Request.
218
		 *
219
		 * @link https://docs.pay.nl/developers?language=nl#transaction-process
220
		 */
221
		$request = array(
222
			'transaction' => array(
223
				'currency'    => $payment->get_total_amount()->get_currency()->get_alphabetic_code(),
224
				'description' => $payment->get_description(),
225
			),
226
			'enduser'     => $end_user,
227
			'saleData'    => $sale_data,
228
		);
229
230
		// Payment method.
231
		$method = Methods::transform( $payment_method );
232
233
		if ( null !== $method ) {
234
			$request['paymentOptionId'] = $method;
235
		}
236
237
		// Set payment method specific parameters.
238
		if ( PaymentMethods::IDEAL === $payment_method ) {
239
			$request['paymentOptionSubId'] = $payment->get_issuer();
240
		}
241
242
		// Start transaction.
243
		$result = $this->client->transaction_start(
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->client->transacti...return_url(), $request) targeting Pronamic\WordPress\Pay\G...nt::transaction_start() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
244
			$payment->get_total_amount()->get_minor_units()->to_int(),
245
			Util::get_ip_address(),
246
			$payment->get_return_url(),
247
			$request
248
		);
249
250
		// Update gateway results in payment.
251
		$payment->set_transaction_id( $result->transaction->transactionId );
252
		$payment->set_action_url( $result->transaction->paymentURL );
253
	}
254
255
	/**
256
	 * Update status of the specified payment.
257
	 *
258
	 * @param Payment $payment Payment.
259
	 */
260
	public function update_status( Payment $payment ) {
261
		try {
262
			// Get transaction info.
263
			$result = $this->client->transaction_info( $payment->get_transaction_id() );
264
		} catch ( \Exception $e ) {
265
			return;
266
		}
267
268
		if ( is_object( $result ) && isset( $result->paymentDetails ) ) {
0 ignored issues
show
introduced by
The condition is_object($result) is always false.
Loading history...
269
			$status = Statuses::transform( $result->paymentDetails->state );
270
271
			// Update payment status.
272
			$payment->set_status( $status );
273
		}
274
	}
275
}
276