Failed Conditions
Push — develop ( d33c63...c20c13 )
by Remco
06:11
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;
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
36
		// Client.
37
		$this->client = new Client();
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
39
		$url = Client::URL_PRODUCTION;
40
41
		if ( self::MODE_TEST === $config->mode ) {
42
			$url = Client::URL_SANDBOX;
43
		}
44
45
		$this->client->set_url( $url );
46
		$this->client->set_refresh_token( $config->refresh_token );
47
		$this->client->set_signing_key( $config->signing_key );
48
	}
49
50
	/**
51
	 * Get supported payment methods.
52
	 *
53
	 * @see \Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
54
	 * @return array
55
	 */
56
	public function get_supported_payment_methods() {
57
		return array(
58
			PaymentMethods::BANCONTACT,
59
			PaymentMethods::CREDIT_CARD,
60
			PaymentMethods::IDEAL,
61
			PaymentMethods::PAYPAL,
62
		);
63
	}
64
65
	/**
66
	 * Start.
67
	 *
68
	 * @see Core_Gateway::start()
69
	 *
70
	 * @param Payment $payment Payment.
71
	 */
72
	public function start( Payment $payment ) {
73
		$merchant_order_id = $payment->format_string( $this->config->order_id );
74
75
		$payment->set_meta( 'omnikassa_2_merchant_order_id', $merchant_order_id );
76
77
		$amount = new Money(
78
			$payment->get_currency(),
0 ignored issues
show
Bug introduced by
$payment->get_currency() of type Pronamic\WordPress\Money\Currency is incompatible with the type string expected by parameter $currency of Pronamic\WordPress\Pay\G...a2\Money::__construct(). ( Ignorable by Annotation )

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

78
			/** @scrutinizer ignore-type */ $payment->get_currency(),
Loading history...
79
			Core_Util::amount_to_cents( $payment->get_amount()->get_amount() )
80
		);
81
82
		$merchant_return_url = $payment->get_return_url();
83
84
		$order = new Order( $merchant_order_id, $amount, $merchant_return_url );
85
86
		$order->set_description( $payment->get_description() );
87
		$order->set_language( $payment->get_language() );
88
89
		// Payment brand.
90
		$payment_brand = PaymentBrands::transform( $payment->get_method() );
91
92
		$order->set_payment_brand( $payment_brand );
93
94
		if ( null !== $payment_brand ) {
0 ignored issues
show
introduced by
The condition null !== $payment_brand is always true.
Loading history...
95
			// Payment brand force should only be set if payment brand is not empty.
96
			$order->set_payment_brand_force( PaymentBrandForce::FORCE_ONCE );
97
		}
98
99
		if ( ! $this->config->is_access_token_valid() ) {
100
			$data = $this->client->get_access_token_data();
101
102
			$error = $this->client->get_error();
103
104
			if ( is_wp_error( $error ) ) {
105
				$this->error = $error;
106
107
				return;
108
			}
109
110
			$this->config->access_token             = $data->token;
0 ignored issues
show
Bug introduced by
The property token does not exist on string.
Loading history...
111
			$this->config->access_token_valid_until = $data->validUntil;
0 ignored issues
show
Bug introduced by
The property validUntil does not exist on string.
Loading history...
112
113
			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...
114
			update_post_meta( $this->config->post_id, '_pronamic_gateway_omnikassa_2_access_token_valid_until', $data->validUntil );
115
		}
116
117
		$result = $this->client->order_announce( $this->config, $order );
118
119
		$error = $this->client->get_error();
120
121
		if ( is_wp_error( $error ) ) {
122
			$this->error = $error;
123
124
			return;
125
		}
126
127
		if ( $result ) {
128
			$payment->set_action_url( $result->redirectUrl );
129
		}
130
	}
131
132
	/**
133
	 * Update status of the specified payment.
134
	 *
135
	 * @param Payment $payment Payment.
136
	 */
137
	public function update_status( Payment $payment ) {
138
		if ( ! ReturnParameters::contains( $_GET ) ) { // WPCS: CSRF ok.
139
			return;
140
		}
141
142
		$parameters = ReturnParameters::from_array( $_GET ); // WPCS: CSRF ok.
143
144
		// Note.
145
		$note_values = array(
146
			'order_id'  => $parameters->get_order_id(),
147
			'status'    => $parameters->get_status(),
148
			'signature' => $parameters->get_signature(),
149
			'valid'     => $parameters->is_valid( $this->config->signing_key ) ? 'true' : 'false',
150
		);
151
152
		$note = '';
153
154
		$note .= '<p>';
155
		$note .= __( 'OmniKassa 2.0 return URL requested:', 'pronamic_ideal' );
156
		$note .= '</p>';
157
158
		$note .= '<dl>';
159
160
		foreach ( $note_values as $key => $value ) {
161
			$note .= sprintf( '<dt>%s</dt>', esc_html( $key ) );
162
			$note .= sprintf( '<dd>%s</dd>', esc_html( $value ) );
163
		}
164
165
		$note .= '</dl>';
166
167
		$payment->add_note( $note );
168
169
		// Validate.
170
		if ( ! $parameters->is_valid( $this->config->signing_key ) ) {
171
			return;
172
		}
173
174
		// Status.
175
		$payment->set_status( Statuses::transform( $parameters->get_status() ) );
176
	}
177
178
	/**
179
	 * Handle notification.
180
	 *
181
	 * @param Notification $notification Notification.
182
	 */
183
	public function handle_notification( Notification $notification ) {
184
		if ( ! $notification->is_valid( $this->config->signing_key ) ) {
185
			return;
186
		}
187
188
		switch ( $notification->get_event_name() ) {
189
			case 'merchant.order.status.changed':
190
				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...
191
		}
192
	}
193
194
	/**
195
	 * Handle `merchant.order.status.changed` event.
196
	 *
197
	 * @param Notification $notification Notification.
198
	 */
199
	private function handle_merchant_order_staus_changed( Notification $notification ) {
200
		do {
201
			$order_results = $this->client->get_order_results( $notification->get_authentication() );
202
203
			if ( ! $order_results->is_valid( $this->config->signing_key ) ) {
204
				return;
205
			}
206
207
			foreach ( $order_results as $order_result ) {
208
				$payment = get_pronamic_payment_by_meta( '_pronamic_payment_omnikassa_2_merchant_order_id', $order_result->get_merchant_order_id() );
209
210
				if ( empty( $payment ) ) {
211
					continue;
212
				}
213
214
				$payment->set_transaction_id( $order_result->get_omnikassa_order_id() );
215
				$payment->set_status( Statuses::transform( $order_result->get_order_status() ) );
216
217
				// Note.
218
				$note = '';
219
220
				$note .= '<p>';
221
				$note .= __( 'OmniKassa 2.0 webhook URL requested:', 'pronamic_ideal' );
222
				$note .= '</p>';
223
				$note .= '<pre>';
224
				$note .= wp_json_encode( $order_result->get_json(), JSON_PRETTY_PRINT );
225
				$note .= '</pre>';
226
227
				$payment->add_note( $note );
228
229
				$payment->save();
230
			}
231
		} while ( $order_results->more_available() );
232
	}
233
}
234