Failed Conditions
Push — develop ( 2de1b1...49fae7 )
by Remco
12:57 queued 04:10
created

PushController::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Push 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\DigiWallet
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Buckaroo;
12
13
use Pronamic\WordPress\Pay\Plugin;
14
15
/**
16
 * Push Controller
17
 *
18
 * @author  Remco Tolsma
19
 * @version 1.0.0
20
 * @since   1.0.0
21
 */
22
class PushController {
23
	/**
24
	 * Setup.
25
	 *
26
	 * @return void
27
	 */
28
	public function setup() {
29
		\add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
30
31
		\add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
32
	}
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
	 * @return void
40
	 */
41
	public function rest_api_init() {
42
		\register_rest_route(
43
			Integration::REST_ROUTE_NAMESPACE,
44
			'/push',
45
			array(
46
				'methods'             => array(
47
					'GET',
48
					'POST',
49
				),
50
				'callback'            => array( $this, 'rest_api_buckaroo_push' ),
51
				'permission_callback' => '__return_true',
52
			)
53
		);
54
	}
55
56
	/**
57
	 * REST API Buckaroo push handler.
58
	 *
59
	 * @param \WP_REST_Request $request Request.
60
	 * @return object
61
	 * @throws \Exception Throws exception when something unexpected happens ;-).
62
	 */
63
	public function rest_api_buckaroo_push( \WP_REST_Request $request ) {
64
		if ( $request->is_json_content_type() ) {
65
			return $this->handle_json_push( $request );
66
		}
67
68
		$content_type = $request->get_content_type();
69
70
		if ( null !== $content_type && 'application/x-www-form-urlencoded' === $content_type['value'] ) {
71
			return $this->handle_http_post_push( $request );
72
		}
73
74
		return new \WP_Error(
75
			'pronamic_pay_buckaroo_push_unknown_content_type',
76
			\sprintf(
77
				'Unknown Buckaroo push request content type: %s.',
78
				$request->get_header( 'Content-Type' )
79
			),
80
			array( 'status' => 500 )
81
		);
82
	}
83
84
	/**
85
	 * Handle JSON push.
86
	 *
87
	 * @link https://dev.buckaroo.nl/PaymentMethods/Description/ideal
88
	 * @param \WP_REST_Request $request Request.
89
	 */
90
	private function handle_json_push( \WP_REST_Request $request ) {
91
		$json = $request->get_body();
92
93
		$data = \json_decode( $json );
94
95
		/**
96
		 * Process Refunds.
97
		 *
98
		 * @link https://support.buckaroo.nl/categorie%C3%ABn/integratie/transactietypes-overzicht
99
		 * @link https://dev.buckaroo.nl/PaymentMethods/Description/ideal
100
		 */
101
		foreach ( $data->Transaction->RelatedTransactions as $related_transaction ) {
102
			if ( 'refund' === $related_transaction->RelationType ) {
103
				$key = $related_transaction->RelatedTransactionKey;
0 ignored issues
show
Unused Code introduced by
The assignment to $key is dead and can be removed.
Loading history...
104
			}
105
		}
106
107
		$transaction_key = $data->Transaction->Key;
108
109
		return $this->handle_transcation_key( $transaction_key );
110
	}
111
112
	/**
113
	 * Handle HTTP POST push.
114
	 *
115
	 * @param \WP_REST_Request $request Request.
116
	 * @return object
117
	 */
118
	public function handle_http_post_push( \WP_REST_Request $request ) {
119
		$parameters = $request->get_params();
120
121
		$parameters = \array_change_key_case( $parameters, \CASE_LOWER );
122
123
		if ( ! \array_key_exists( 'brq_transactions', $parameters ) ) {
124
			return new \WP_Error(
125
				'rest_buckaroo_no_transactions_parameter',
126
				\__( 'The BRQ_TRANSACTIONS parameter is missing from the Buckaroo push request.', 'pronamic_ideal ' )
127
			);
128
		}
129
130
		/**
131
		 * The unique key for the transaction
132
		 * Important: the payment response also contains a parameter named
133
		 * brq_transactions, but may contain multiple transaction keys.
134
		 * The same field in the push response will always contain one single
135
		 * transaction key. For consistence, both fields have the same name.
136
		 *
137
		 * @link https://www.pronamic.nl/wp-content/uploads/2013/04/BPE-3.0-Gateway-HTML.1.02.pdf
138
		 */
139
		$transaction_key = $parameters['brq_transactions'];
140
141
		return $this->handle_transcation_key( $transaction_key );
142
	}
143
144
	/**
145
	 * Handle JSON request for specified transaction key.
146
	 *
147
	 * @param string $transaction_key Transaction key.
148
	 * @return object
149
	 */
150
	private function handle_transcation_key( $transaction_key ) {
151
		$payment = \get_pronamic_payment_by_meta( '_pronamic_payment_buckaroo_transaction_key', $transaction_key );
152
153
		if ( null === $payment ) {
154
			return new \WP_Error(
155
				'rest_buckaroo_unknown_transaction',
156
				\sprintf(
157
					/* translators: %s: Buckaroo transaction key. */
158
					\__( 'Unable to found payment for transaction key: %s.', 'pronamic_ideal ' ),
159
					$transaction_key
160
				),
161
				array(
162
					'status' => 400,
163
					'data'   => $data,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to be never defined.
Loading history...
164
				)
165
			);
166
		}
167
168
		// Add note.
169
		$note = \__( 'Push URL requested by Buckaroo.', 'pronamic_ideal' );
170
171
		$payment->add_note( $note );
172
173
		// Log webhook request.
174
		\do_action( 'pronamic_pay_webhook_log_payment', $payment );
175
176
		// Update payment.
177
		Plugin::update_payment( $payment, false );
178
179
		return \rest_ensure_response(
180
			array(
181
				'success'         => true,
182
				'transaction_key' => $transaction_key,
183
			)
184
		);
185
	}
186
187
	/**
188
	 * WordPress loaded, check for deprecated webhook call.
189
	 *
190
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309
191
	 * @return void
192
	 */
193
	public function wp_loaded() {
194
		if ( ! filter_has_var( INPUT_GET, 'buckaroo_push' ) ) {
195
			return;
196
		}
197
198
		\rest_get_server()->serve_request( '/pronamic-pay/buckaroo/v1/push' );
199
200
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
201
	}
202
}
203