Passed
Push — master ( b8e26c...2f113f )
by Remco
05:42
created

Gateway::handle_merchant_order_staus_changed()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 4
nop 1
dl 0
loc 31
rs 9.2888
c 0
b 0
f 0
1
<?php
2
/**
3
 * Gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
14
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
15
use Pronamic\WordPress\Pay\Core\PaymentMethods;
16
use Pronamic\WordPress\Pay\Payments\Payment;
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\Payments\Payment 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...
17
18
/**
19
 * Gateway
20
 *
21
 * @author  Remco Tolsma
22
 * @version 2.0.2
23
 * @since   1.0.0
24
 */
25
class Gateway extends Core_Gateway {
26
	/**
27
	 * Constructs and initializes an OmniKassa 2.0 gateway.
28
	 *
29
	 * @param \Pronamic\WordPress\Pay\Gateways\OmniKassa2\Config $config Config.
30
	 */
31
	public function __construct( Config $config ) {
32
		parent::__construct( $config );
33
34
		$this->set_method( self::METHOD_HTTP_REDIRECT );
35
		$this->set_has_feedback( true );
36
		$this->set_amount_minimum( 0.01 );
37
38
		// Client.
39
		$this->client = new Client();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Pronamic\WordPress\P...ays\OmniKassa2\Client() of type Pronamic\WordPress\Pay\Gateways\OmniKassa2\Client is incompatible with the declared type Pronamic\WordPress\Pay\Core\Client of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
41
		$url = Client::URL_PRODUCTION;
42
43
		if ( self::MODE_TEST === $config->mode ) {
44
			$url = Client::URL_SANDBOX;
45
		}
46
47
		$this->client->set_url( $url );
48
		$this->client->set_refresh_token( $config->refresh_token );
49
		$this->client->set_signing_key( $config->signing_key );
50
	}
51
52
	/**
53
	 * Get supported payment methods.
54
	 *
55
	 * @see \Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
56
	 * @return array
57
	 */
58
	public function get_supported_payment_methods() {
59
		return array(
60
			PaymentMethods::BANCONTACT,
61
			PaymentMethods::CREDIT_CARD,
62
			PaymentMethods::IDEAL,
63
			PaymentMethods::PAYPAL,
64
		);
65
	}
66
67
	/**
68
	 * Start.
69
	 *
70
	 * @see Core_Gateway::start()
71
	 *
72
	 * @param Payment $payment Payment.
73
	 */
74
	public function start( Payment $payment ) {
75
		$merchant_order_id = $payment->format_string( $this->config->order_id );
76
77
		$payment->set_meta( 'omnikassa_2_merchant_order_id', $merchant_order_id );
78
79
		$amount = new Money(
80
			$payment->get_currency(),
81
			Core_Util::amount_to_cents( $payment->get_amount()->get_amount() )
82
		);
83
84
		$merchant_return_url = $payment->get_return_url();
85
86
		$order = new Order( $merchant_order_id, $amount, $merchant_return_url );
87
88
		$order->set_description( $payment->get_description() );
89
		$order->set_language( $payment->get_language() );
90
91
		// Payment brand.
92
		$payment_brand = PaymentBrands::transform( $payment->get_method() );
93
94
		$order->set_payment_brand( $payment_brand );
95
96
		if ( null !== $payment_brand ) {
0 ignored issues
show
introduced by
The condition null !== $payment_brand is always true.
Loading history...
97
			// Payment brand force should only be set if payment brand is not empty.
98
			$order->set_payment_brand_force( PaymentBrandForce::FORCE_ONCE );
99
		}
100
101
		if ( ! $this->config->is_access_token_valid() ) {
102
			$data = $this->client->get_access_token_data();
103
104
			$error = $this->client->get_error();
105
106
			if ( is_wp_error( $error ) ) {
107
				$this->error = $error;
108
109
				return;
110
			}
111
112
			$this->config->access_token             = $data->token;
113
			$this->config->access_token_valid_until = $data->validUntil;
114
115
			update_post_meta( $this->config->post_id, '_pronamic_gateway_omnikassa_2_access_token', $data->token );
0 ignored issues
show
Bug introduced by
The property post_id does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
116
			update_post_meta( $this->config->post_id, '_pronamic_gateway_omnikassa_2_access_token_valid_until', $data->validUntil );
117
		}
118
119
		$result = $this->client->order_announce( $this->config, $order );
120
121
		$error = $this->client->get_error();
122
123
		if ( is_wp_error( $error ) ) {
124
			$this->error = $error;
125
126
			return;
127
		}
128
129
		if ( $result ) {
130
			$payment->set_action_url( $result->redirectUrl );
131
		}
132
	}
133
134
	/**
135
	 * Update status of the specified payment.
136
	 *
137
	 * @param Payment $payment Payment.
138
	 */
139
	public function update_status( Payment $payment ) {
140
		if ( ! ReturnParameters::contains( $_GET ) ) { // WPCS: CSRF ok.
141
			return;
142
		}
143
144
		$parameters = ReturnParameters::from_array( $_GET ); // WPCS: CSRF ok.
145
146
		// Note.
147
		$note_values = array(
148
			'order_id'  => $parameters->get_order_id(),
149
			'status'    => $parameters->get_status(),
150
			'signature' => $parameters->get_signature(),
151
			'valid'     => $parameters->is_valid( $this->config->signing_key ) ? 'true' : 'false',
152
		);
153
154
		$note = '';
155
156
		$note .= '<p>';
157
		$note .= __( 'OmniKassa 2.0 return URL requested:', 'pronamic_ideal' );
158
		$note .= '</p>';
159
160
		$note .= '<dl>';
161
162
		foreach ( $note_values as $key => $value ) {
163
			$note .= sprintf( '<dt>%s</dt>', esc_html( $key ) );
164
			$note .= sprintf( '<dd>%s</dd>', esc_html( $value ) );
165
		}
166
167
		$note .= '</dl>';
168
169
		$payment->add_note( $note );
170
171
		// Validate.
172
		if ( ! $parameters->is_valid( $this->config->signing_key ) ) {
173
			return;
174
		}
175
176
		// Status.
177
		$payment->set_status( Statuses::transform( $parameters->get_status() ) );
178
	}
179
180
	/**
181
	 * Handle notification.
182
	 *
183
	 * @param Notification $notification Notification.
184
	 */
185
	public function handle_notification( Notification $notification ) {
186
		if ( ! $notification->is_valid( $this->config->signing_key ) ) {
187
			return;
188
		}
189
190
		switch ( $notification->get_event_name() ) {
191
			case 'merchant.order.status.changed':
192
				return $this->handle_merchant_order_staus_changed( $notification );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->handle_merchant_o..._changed($notification) targeting Pronamic\WordPress\Pay\G...t_order_staus_changed() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
193
		}
194
	}
195
196
	/**
197
	 * Handle `merchant.order.status.changed` event.
198
	 *
199
	 * @param Notification $notification Notification.
200
	 */
201
	private function handle_merchant_order_staus_changed( Notification $notification ) {
202
		do {
203
			$order_results = $this->client->get_order_results( $notification->get_authentication() );
204
205
			if ( ! $order_results->is_valid( $this->config->signing_key ) ) {
206
				return;
207
			}
208
209
			foreach ( $order_results as $order_result ) {
210
				$payment = get_pronamic_payment_by_meta( '_pronamic_payment_omnikassa_2_merchant_order_id', $order_result->get_merchant_order_id() );
0 ignored issues
show
Bug introduced by
The function get_pronamic_payment_by_meta 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

210
				$payment = /** @scrutinizer ignore-call */ get_pronamic_payment_by_meta( '_pronamic_payment_omnikassa_2_merchant_order_id', $order_result->get_merchant_order_id() );
Loading history...
211
212
				if ( empty( $payment ) ) {
213
					continue;
214
				}
215
216
				$payment->set_transaction_id( $order_result->get_omnikassa_order_id() );
217
				$payment->set_status( Statuses::transform( $order_result->get_order_status() ) );
218
219
				// Note.
220
				$note = '';
221
222
				$note .= '<p>';
223
				$note .= __( 'OmniKassa 2.0 webhook URL requested:', 'pronamic_ideal' );
224
				$note .= '</p>';
225
				$note .= '<pre>';
226
				$note .= wp_json_encode( $order_result->get_json(), JSON_PRETTY_PRINT );
227
				$note .= '</pre>';
228
229
				$payment->add_note( $note );
230
231
				$payment->save();
232
			}
233
		} while ( $order_results->more_available() );
234
	}
235
}
236