Failed Conditions
Push — develop ( 99f409...718770 )
by Remco
03:27
created

rest_api_adyen_permissions_check()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 8.125

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 3
nop 1
dl 0
loc 16
ccs 5
cts 10
cp 0.5
crap 8.125
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * Notifications controller
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use InvalidArgumentException;
14
use Pronamic\WordPress\Pay\Core\Statuses as PaymentStatus;
15
use Pronamic\WordPress\Pay\Core\Server;
16
use WP_Error;
17
use WP_REST_Request;
18
19
/**
20
 * Notification controller
21
 *
22
 * @link    https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequest
23
 * @author  Remco Tolsma
24
 * @version 1.0.0
25
 * @since   1.0.0
26
 */
27
class NotificationsController {
28
	/**
29
	 * REST route namespace.
30
	 *
31
	 * @var string
32
	 */
33
	const REST_ROUTE_NAMESPACE = 'pronamic-pay/adyen/v1';
34
35
	/**
36
	 * Setup.
37
	 */
38 3
	public function setup() {
39 3
		add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
40 3
	}
41
42
	/**
43
	 * REST API init.
44
	 *
45
	 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
46
	 * @link https://developer.wordpress.org/reference/hooks/rest_api_init/
47
	 */
48 1
	public function rest_api_init() {
49 1
		register_rest_route(
50 1
			self::REST_ROUTE_NAMESPACE,
51 1
			'/notifications',
52
			array(
53 1
				'methods'             => 'POST',
54 1
				'callback'            => array( $this, 'rest_api_adyen_notifications' ),
55 1
				'permission_callback' => array( $this, 'rest_api_adyen_permissions_check' ),
56
			)
57
		);
58 1
	}
59
60
	/**
61
	 * REST API Adyen permissions check.
62
	 *
63
	 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/#permissions-callback
64
	 *
65
	 * @param WP_REST_Request $request Request.
66
	 */
67 2
	public function rest_api_adyen_permissions_check( WP_REST_Request $request ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

67
	public function rest_api_adyen_permissions_check( /** @scrutinizer ignore-unused */ WP_REST_Request $request ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68 2
		$username = get_option( 'pronamic_pay_adyen_notification_authentication_username' );
69 2
		$password = get_option( 'pronamic_pay_adyen_notification_authentication_password' );
70
71 2
		if ( empty( $username ) && empty( $password ) ) {
72 2
			return true;
73
		}
74
75
		$username_input = Server::get( 'PHP_AUTH_USER' );	
76
		$password_input = Server::get( 'PHP_AUTH_PW' );
77
78
		if ( $username === $username_input && $password === $password_input ) {
79
			return true;
80
		}
81
82
		return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to post Adyen notifications.' ), array( 'status' => rest_authorization_required_code() ) );
83
	}
84
85
	/**
86
	 * REST API Adyen notifications handler.
87
	 *
88
	 * @param WP_REST_Request $request Request.
89
	 */
90 2
	public function rest_api_adyen_notifications( WP_REST_Request $request ) {
91 2
		$json = $request->get_body();
92
93 2
		$data = json_decode( $json );
94
95
		try {
96 2
			$notification_request = NotificationRequest::from_object( $data );
97 1
		} catch ( InvalidArgumentException $e ) {
98 1
			return new WP_Error( 'adyen_invalid_notification', __( 'Cannot parse JSON notification.' ), array( 'status' => 500 ) );
99
		}
100
101 1
		foreach ( $notification_request->get_items() as $item ) {
102 1
			$payment = get_pronamic_payment( $item->get_merchant_reference() );
103
104 1
			if ( null === $payment ) {
105
				continue;
106
			}
107
108
			// Store notification.
109 1
			$payment->set_meta( 'adyen_notification', $json );
110
111
			// Add note.
112 1
			$note = sprintf(
113
				/* translators: %s: Adyen */
114 1
				__( 'Webhook requested by %s.', 'pronamic_ideal' ),
115 1
				__( 'Adyen', 'pronamic_ideal' )
116
			);
117
118 1
			$payment->add_note( $note );
119
120
			// Authorization.
121 1
			if ( EventCode::AUTHORIZATION === $item->get_event_code() ) {
122 1
				$payment->set_status( $item->is_success() ? PaymentStatus::SUCCESS : PaymentStatus::FAILURE );
123
124 1
				$payment->save();
125
			}
126
		}
127
128
		$response = (object) array(
129 1
			'notificationResponse' => '[accepted]',
130
		);
131
132 1
		return $response;
133
	}
134
}
135