|
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 WP_Error; |
|
16
|
|
|
use WP_REST_Request; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Notification controller |
|
20
|
|
|
* |
|
21
|
|
|
* @link https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequest |
|
22
|
|
|
* @author Remco Tolsma |
|
23
|
|
|
* @version 1.0.0 |
|
24
|
|
|
* @since 1.0.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class NotificationsController { |
|
27
|
|
|
/** |
|
28
|
|
|
* Setup. |
|
29
|
|
|
*/ |
|
30
|
2 |
|
public function setup() { |
|
31
|
2 |
|
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|
32
|
2 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* REST API init. |
|
36
|
|
|
* |
|
37
|
|
|
* @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ |
|
38
|
|
|
* @link https://developer.wordpress.org/reference/hooks/rest_api_init/ |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function rest_api_init() { |
|
41
|
1 |
|
register_rest_route( |
|
42
|
1 |
|
'adyen/v1', |
|
43
|
1 |
|
'/notifications', |
|
44
|
|
|
array( |
|
45
|
1 |
|
'methods' => 'POST', |
|
46
|
1 |
|
'callback' => array( $this, 'rest_api_adyen_notifications' ), |
|
47
|
|
|
) |
|
48
|
|
|
); |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* REST API Adyen notifications handler. |
|
53
|
|
|
* |
|
54
|
|
|
* @param WP_REST_Request $request Request. |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function rest_api_adyen_notifications( WP_REST_Request $request ) { |
|
57
|
2 |
|
$json = $request->get_body(); |
|
58
|
|
|
|
|
59
|
2 |
|
$data = json_decode( $json ); |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
2 |
|
$notification_request = NotificationRequest::from_object( $data ); |
|
63
|
1 |
|
} catch ( InvalidArgumentException $e ) { |
|
64
|
1 |
|
return new WP_Error( 'adyen_invalid_notification', __( 'Cannot parse JSON notification.' ), array( 'status' => 500 ) ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
foreach ( $notification_request->get_items() as $item ) { |
|
68
|
1 |
|
$payment = get_pronamic_payment( $item->get_merchant_reference() ); |
|
69
|
|
|
|
|
70
|
1 |
|
if ( empty( $payment ) ) { |
|
71
|
|
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// Store notification. |
|
75
|
1 |
|
$payment->set_meta( 'adyen_notification', $json ); |
|
76
|
|
|
|
|
77
|
|
|
// Add note. |
|
78
|
1 |
|
$note = sprintf( |
|
79
|
|
|
/* translators: %s: Adyen */ |
|
80
|
1 |
|
__( 'Webhook requested by %s.', 'pronamic_ideal' ), |
|
81
|
1 |
|
__( 'Adyen', 'pronamic_ideal' ) |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
1 |
|
$payment->add_note( $note ); |
|
85
|
|
|
|
|
86
|
|
|
// Authorization. |
|
87
|
1 |
|
if ( EventCode::AUTHORIZATION === $item->get_event_code() ) { |
|
88
|
1 |
|
$payment->set_status( $item->is_success() ? PaymentStatus::SUCCESS : PaymentStatus::FAILURE ); |
|
89
|
|
|
|
|
90
|
1 |
|
$payment->save(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$response = (object) array( |
|
95
|
1 |
|
'notificationResponse' => '[accepted]', |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
1 |
|
return $response; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|