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
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* REST API init. |
34
|
|
|
* |
35
|
|
|
* @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ |
36
|
|
|
* @link https://developer.wordpress.org/reference/hooks/rest_api_init/ |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function rest_api_init() { |
40
|
|
|
\register_rest_route( |
41
|
|
|
Integration::REST_ROUTE_NAMESPACE, |
42
|
|
|
'/push', |
43
|
|
|
array( |
44
|
|
|
'methods' => array( |
45
|
|
|
'GET', |
46
|
|
|
'POST', |
47
|
|
|
), |
48
|
|
|
'callback' => array( $this, 'rest_api_buckaroo_push' ), |
49
|
|
|
'permission_callback' => '__return_true', |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* REST API Buckaroo push handler. |
56
|
|
|
* |
57
|
|
|
* @param \WP_REST_Request $request Request. |
58
|
|
|
* @return object |
59
|
|
|
* @throws \Exception Throws exception when something unexpected happens ;-). |
60
|
|
|
*/ |
61
|
|
|
public function rest_api_buckaroo_push( \WP_REST_Request $request ) { |
62
|
|
|
if ( $request->is_json_content_type() ) { |
63
|
|
|
return $this->handle_json_push( $request ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$content_type = $request->get_header( 'Content-Type' ); |
67
|
|
|
|
68
|
|
|
if ( 'application/x-www-form-urlencoded' === $content_type ) { |
69
|
|
|
return $this->handle_http_post_push( $request ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return new \WP_Error( |
73
|
|
|
'pronamic_pay_buckaroo_push_unknown_content_type', |
74
|
|
|
\sprintf( |
75
|
|
|
'Unknown Buckaroo push request: %s.', |
76
|
|
|
$content_type |
77
|
|
|
), |
78
|
|
|
array( 'status' => 500 ) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Handle JSON push. |
84
|
|
|
* |
85
|
|
|
* @link https://dev.buckaroo.nl/PaymentMethods/Description/ideal |
86
|
|
|
* @param \WP_REST_Request $request Request. |
87
|
|
|
*/ |
88
|
|
|
private function handle_json_push( \WP_REST_Request $request ) { |
89
|
|
|
$json = $request->get_body(); |
90
|
|
|
|
91
|
|
|
$data = \json_decode( $json ); |
92
|
|
|
|
93
|
|
|
$transaction_key = $data->Transaction->Key; |
94
|
|
|
|
95
|
|
|
return $this->handle_transcation_key( $transaction_key ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Handle HTTP POST push. |
100
|
|
|
* |
101
|
|
|
* @param \WP_REST_Request $request Request. |
102
|
|
|
* @return object |
103
|
|
|
*/ |
104
|
|
|
public function handle_http_post_push( \WP_REST_Request $request ) { |
105
|
|
|
$parameters = $request->get_params(); |
106
|
|
|
|
107
|
|
|
$parameters = \array_change_key_case( $parameters, \CASE_LOWER ); |
108
|
|
|
|
109
|
|
|
if ( ! \array_key_exists( 'brq_transactions', $parameters ) ) { |
110
|
|
|
return new \WP_Error( |
111
|
|
|
'rest_buckaroo_no_transactions_parameter', |
112
|
|
|
\__( 'The BRQ_TRANSACTIONS parameter is missing from the Buckaroo push request.', 'pronamic_ideal ' ) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* The unique key for the transaction |
118
|
|
|
* Important: the payment response also contains a parameter named |
119
|
|
|
* brq_transactions, but may contain multiple transaction keys. |
120
|
|
|
* The same field in the push response will always contain one single |
121
|
|
|
* transaction key. For consistence, both fields have the same name. |
122
|
|
|
* |
123
|
|
|
* @link https://www.pronamic.nl/wp-content/uploads/2013/04/BPE-3.0-Gateway-HTML.1.02.pdf |
124
|
|
|
*/ |
125
|
|
|
$transaction_key = $parameters['brq_transactions']; |
126
|
|
|
|
127
|
|
|
return $this->handle_transcation_key( $transaction_key ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Handle JSON request for specified transaction key. |
132
|
|
|
* |
133
|
|
|
* @param string $transaction_key Transaction key. |
134
|
|
|
* @return object |
135
|
|
|
*/ |
136
|
|
|
private function handle_transcation_key( $transaction_key ) { |
137
|
|
|
$payment = \get_pronamic_payment_by_meta( '_pronamic_payment_buckaroo_transaction_key', $transaction_key ); |
138
|
|
|
|
139
|
|
|
if ( null === $payment ) { |
140
|
|
|
return new \WP_Error( |
141
|
|
|
'rest_buckaroo_unknown_transaction', |
142
|
|
|
\sprintf( |
143
|
|
|
\__( 'Unable to found payment for transaction key: %s.', 'pronamic_ideal ' ), |
144
|
|
|
$transaction_key |
145
|
|
|
), |
146
|
|
|
array( |
147
|
|
|
'status' => 400, |
148
|
|
|
'data' => $data, |
|
|
|
|
149
|
|
|
) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Add note. |
154
|
|
|
$note = \__( 'Push URL requested by Buckaroo.', 'pronamic_ideal' ); |
155
|
|
|
|
156
|
|
|
$payment->add_note( $note ); |
157
|
|
|
|
158
|
|
|
// Log webhook request. |
159
|
|
|
\do_action( 'pronamic_pay_webhook_log_payment', $payment ); |
160
|
|
|
|
161
|
|
|
// Update payment. |
162
|
|
|
Plugin::update_payment( $payment, false ); |
163
|
|
|
|
164
|
|
|
return \rest_ensure_response( |
165
|
|
|
array( |
166
|
|
|
'success' => true, |
167
|
|
|
'transaction_key' => $transaction_key, |
168
|
|
|
) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|