Test Failed
Push — develop ( 19a0d9...9b1653 )
by Reüel
03:31
created

Gateway::start()   D

Complexity

Conditions 13
Paths 320

Size

Total Lines 138
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 7
Bugs 0 Features 0
Metric Value
cc 13
eloc 73
c 7
b 0
f 0
nc 320
nop 1
dl 0
loc 138
ccs 0
cts 94
cp 0
crap 182
rs 4.0157

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-2019 Pronamic
13
 * Company: Pronamic
14
 *
15
 * @author  Remco Tolsma
16
 * @version 2.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 Pronamic_WP_Pay_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 Pronamic_WP_Pay_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
		);
91
	}
92
93
	/**
94
	 * Start.
95
	 *
96
	 * @see Core_Gateway::start()
97
	 *
98
	 * @param Payment $payment Payment.
99
	 */
100
	public function start( Payment $payment ) {
101
		$payment_method = $payment->get_method();
102
103
		/*
104
		 * New transaction request.
105
		 * @link https://www.pay.nl/docs/developers.php#transactions
106
		 */
107
		$customer         = $payment->get_customer();
108
		$billing_address  = $payment->get_billing_address();
109
		$shipping_address = $payment->get_shipping_address();
110
111
		// Payment lines.
112
		$order_data = array();
113
114
		if ( null !== $payment->get_lines() ) {
115
			foreach ( $payment->get_lines() as $line ) {
116
				$price = null;
117
118
				if ( null !== $line->get_unit_price() ) {
119
					$price = $line->get_unit_price()->get_including_tax()->get_cents();
120
				}
121
122
				$order_data[] = array(
123
					'productId'   => $line->get_id(),
124
					'productType' => ProductTypes::transform( $line->get_type() ),
125
					'description' => $line->get_name(),
126
					'price'       => $price,
127
					'quantity'    => $line->get_quantity(),
128
				);
129
			}
130
		}
131
132
		// End user.
133
		$end_user = array();
134
135
		// End user - Address.
136
		if ( null !== $shipping_address ) {
137
			$end_user['address'] = array(
138
				'streetName'            => $shipping_address->get_street_name(),
139
				'streetNumber'          => $shipping_address->get_house_number_base(),
140
				'streetNumberExtension' => $shipping_address->get_house_number_addition(),
141
				'zipCode'               => $shipping_address->get_postal_code(),
142
				'city'                  => $shipping_address->get_city(),
143
				'countryCode'           => $shipping_address->get_country_code(),
144
			);
145
		}
146
147
		// End user - Invoice address.
148
		if ( null !== $billing_address ) {
149
			$end_user['invoiceAddress'] = array(
150
				'streetName'            => $billing_address->get_street_name(),
151
				'streetNumber'          => $billing_address->get_house_number_base(),
152
				'streetNumberExtension' => $billing_address->get_house_number_addition(),
153
				'zipCode'               => $billing_address->get_postal_code(),
154
				'city'                  => $billing_address->get_city(),
155
				'countryCode'           => $billing_address->get_country_code(),
156
			);
157
		}
158
159
		// Request.
160
		$request = array(
161
			// Transaction.
162
			'transaction' => array(
163
				'currency'    => $payment->get_total_amount()->get_currency()->get_alphabetic_code(),
164
				'description' => $payment->get_description(),
165
			),
166
167
			// End user.
168
			'enduser'     => $end_user,
169
170
			// Sale data.
171
			'saleData'    => array(
172
				'invoiceDate'  => $payment->get_date()->format( 'd-m-Y' ),
173
				'deliveryDate' => $payment->get_date()->format( 'd-m-Y' ),
174
				'orderData'    => $order_data,
175
			),
176
		);
177
178
		// Payment method.
179
		$method = Methods::transform( $payment_method );
180
181
		if ( null !== $method ) {
182
			$request['paymentOptionId'] = $method;
183
		}
184
185
		if ( null !== $payment->get_customer() ) {
186
			$enduser = array(
187
				'gender'       => $customer->get_gender(),
188
				'phoneNumber'  => $customer->get_phone(),
189
				'emailAddress' => $customer->get_email(),
190
				'language'     => $customer->get_language(),
191
			);
192
193
			$invoice_address = array(
194
				'gender' => $customer->get_gender(),
195
			);
196
197
			// Set name from customer.
198
			if ( null !== $customer->get_name() ) {
199
				$enduser['initials'] = $customer->get_name()->get_first_name();
200
				$enduser['lastName'] = $customer->get_name()->get_last_name();
201
202
				$invoice_address['initials'] = $customer->get_name()->get_first_name();
203
				$invoice_address['lastName'] = $customer->get_name()->get_last_name();
204
			}
205
206
			// Set date of birth.
207
			if ( $customer->get_birth_date() instanceof \DateTime ) {
208
				$enduser['dob'] = $customer->get_birth_date()->format( 'dmY' );
209
			}
210
211
			$request['enduser'] = array_merge( $request['enduser'], $enduser );
212
213
			$request['enduser']['invoiceAddress'] = array_merge( $request['enduser']['invoiceAddress'], $invoice_address );
214
		}
215
216
		// Check payment method.
217
		if ( null === $request['paymentOptionId'] && ! empty( $payment_method ) ) {
218
			// Leap of faith if the WordPress payment method could not transform to a Pay.nl method?
219
			$request['paymentOptionId'] = $payment_method;
220
		}
221
222
		// Set payment method specific parameters.
223
		if ( PaymentMethods::IDEAL === $payment_method ) {
224
			$request['paymentOptionSubId'] = $payment->get_issuer();
225
		}
226
227
		// Start transaction.
228
		$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...
229
			$payment->get_total_amount()->get_cents(),
0 ignored issues
show
Deprecated Code introduced by
The function Pronamic\WordPress\Money\Money::get_cents() has been deprecated: 1.2.2 Use `Money::get_minor_units()` instead. ( Ignorable by Annotation )

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

229
			/** @scrutinizer ignore-deprecated */ $payment->get_total_amount()->get_cents(),

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

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

Loading history...
230
			Util::get_ip_address(),
231
			$payment->get_return_url(),
232
			$request
233
		);
234
235
		// Update gateway results in payment.
236
		$payment->set_transaction_id( $result->transaction->transactionId );
237
		$payment->set_action_url( $result->transaction->paymentURL );
238
	}
239
240
	/**
241
	 * Update status of the specified payment.
242
	 *
243
	 * @param Payment $payment Payment.
244
	 */
245
	public function update_status( Payment $payment ) {
246
		try {
247
			// Get transaction info.
248
			$result = $this->client->transaction_info( $payment->get_transaction_id() );
249
		} catch ( \Exception $e ) {
250
			return;
251
		}
252
253
		if ( is_object( $result ) && isset( $result->paymentDetails ) ) {
0 ignored issues
show
introduced by
The condition is_object($result) is always false.
Loading history...
254
			$status = Statuses::transform( $result->paymentDetails->state );
255
256
			// Update payment status.
257
			$payment->set_status( $status );
258
		}
259
	}
260
}
261