1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Notification controller |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2021 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\IDealBasic |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\IDealBasic; |
12
|
|
|
|
13
|
|
|
use Pronamic\WordPress\Pay\Plugin; |
14
|
|
|
use Pronamic\WordPress\Pay\Core\Util as Core_Util; |
15
|
|
|
use Pronamic\WordPress\Pay\Gateways\IDealBasic\XML\NotificationParser; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Notification controller |
19
|
|
|
* |
20
|
|
|
* @author Remco Tolsma |
21
|
|
|
* @version 1.0.0 |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
*/ |
24
|
|
|
class NotificationController { |
25
|
|
|
/** |
26
|
|
|
* Setup. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function setup() { |
31
|
|
|
\add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
32
|
|
|
|
33
|
|
|
\add_action( 'wp_loaded', array( $this, 'wp_loaded' ) ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* REST API init. |
38
|
|
|
* |
39
|
|
|
* @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ |
40
|
|
|
* @link https://developer.wordpress.org/reference/hooks/rest_api_init/ |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function rest_api_init() { |
44
|
|
|
\register_rest_route( |
45
|
|
|
Integration::REST_ROUTE_NAMESPACE, |
46
|
|
|
'/notification', |
47
|
|
|
array( |
48
|
|
|
'methods' => 'POST', |
49
|
|
|
'callback' => array( $this, 'rest_api_notification' ), |
50
|
|
|
'permission_callback' => '__return_true', |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* REST API iDEAL XML notification handler. |
57
|
|
|
* |
58
|
|
|
* @param \WP_REST_Request $request Request. |
59
|
|
|
* @return object |
60
|
|
|
* @throws \Exception Throws exception when something unexpected happens ;-). |
61
|
|
|
*/ |
62
|
|
|
public function rest_api_notification( \WP_REST_Request $request ) { |
63
|
|
|
$body = $request->get_body(); |
64
|
|
|
|
65
|
|
|
if ( empty( $body ) ) { |
66
|
|
|
return new \WP_Error( |
67
|
|
|
'ideal_basic_empty_notification', |
68
|
|
|
__( 'The iDEAL Basic notification is empty.', 'pronamic_ideal' ), |
69
|
|
|
array( 'status' => 400 ) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$xml = Core_Util::simplexml_load_string( $body ); |
74
|
|
|
|
75
|
|
|
if ( \is_wp_error( $xml ) ) { |
76
|
|
|
return $xml; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$notification = NotificationParser::parse( $xml ); |
80
|
|
|
|
81
|
|
|
// Get payment for purchase ID. |
82
|
|
|
$purchase_id = $notification->get_purchase_id(); |
83
|
|
|
|
84
|
|
|
$payment = get_pronamic_payment_by_purchase_id( $purchase_id ); |
85
|
|
|
|
86
|
|
|
if ( null === $payment ) { |
87
|
|
|
return new \WP_Error( |
88
|
|
|
'ideal_basic_no_payment', |
89
|
|
|
\sprintf( |
90
|
|
|
/* translators: %s: Purchase ID. */ |
91
|
|
|
__( 'Could not find iDEAL Basic payment by purchase ID: %s.', 'pronamic_ideal' ), |
92
|
|
|
$purchase_id |
93
|
|
|
), |
94
|
|
|
array( 'status' => 404 ) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Add note. |
99
|
|
|
$note = sprintf( |
100
|
|
|
/* translators: %s: payment provider name */ |
101
|
|
|
__( 'Webhook requested by %s.', 'pronamic_ideal' ), |
102
|
|
|
__( 'iDEAL Basic', 'pronamic_ideal' ) |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$payment->add_note( $note ); |
106
|
|
|
|
107
|
|
|
// Update payment with notification data. |
108
|
|
|
$payment->set_status( $notification->get_status() ); |
109
|
|
|
$payment->set_transaction_id( $notification->get_transaction_id() ); |
110
|
|
|
|
111
|
|
|
// Log webhook request. |
112
|
|
|
\do_action( 'pronamic_pay_webhook_log_payment', $payment ); |
113
|
|
|
|
114
|
|
|
// Update payment. |
115
|
|
|
Plugin::update_payment( $payment ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* WordPress loaded, check for deprecated webhook call. |
120
|
|
|
* |
121
|
|
|
* @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309 |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function wp_loaded() { |
125
|
|
|
if ( ! \filter_has_var( \INPUT_GET, 'xml_notification' ) ) { |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
\rest_get_server()->serve_request( '/pronamic-pay/ideal-basic/v1/notification' ); |
130
|
|
|
|
131
|
|
|
exit; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.