wp-pay-gateways /
omnikassa-2
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Webhook 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\OmniKassa2 |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2; |
||
| 12 | |||
| 13 | use Pronamic\WordPress\Pay\GatewayPostType; |
||
| 14 | use Pronamic\WordPress\Pay\Plugin; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Webhook controller |
||
| 18 | * |
||
| 19 | * @author Remco Tolsma |
||
| 20 | * @version 2.3.4 |
||
| 21 | * @since 2.3.0 |
||
| 22 | */ |
||
| 23 | class WebhookController { |
||
| 24 | /** |
||
| 25 | * Setup. |
||
| 26 | * |
||
| 27 | * @return void |
||
| 28 | */ |
||
| 29 | public function setup() { |
||
| 30 | \add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
||
| 31 | |||
| 32 | \add_action( 'wp_loaded', array( $this, 'wp_loaded' ) ); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * REST API init. |
||
| 37 | * |
||
| 38 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ |
||
| 39 | * @link https://developer.wordpress.org/reference/hooks/rest_api_init/ |
||
| 40 | * @return void |
||
| 41 | */ |
||
| 42 | public function rest_api_init() { |
||
| 43 | \register_rest_route( |
||
| 44 | Integration::REST_ROUTE_NAMESPACE, |
||
| 45 | '/webhook', |
||
| 46 | array( |
||
| 47 | 'methods' => 'POST', |
||
| 48 | 'callback' => array( $this, 'rest_api_omnikassa_2_webhook' ), |
||
| 49 | 'permission_callback' => '__return_true', |
||
| 50 | ) |
||
| 51 | ); |
||
| 52 | |||
| 53 | \register_rest_route( |
||
| 54 | Integration::REST_ROUTE_NAMESPACE, |
||
| 55 | '/webhook/(?P<id>[\d]+)', |
||
| 56 | array( |
||
| 57 | 'args' => array( |
||
| 58 | 'id' => array( |
||
| 59 | 'description' => \__( 'Unique identifier for the gateway configuration post.', 'pronamic_ideal' ), |
||
| 60 | 'type' => 'integer', |
||
| 61 | ), |
||
| 62 | ), |
||
| 63 | 'methods' => 'POST', |
||
| 64 | 'callback' => array( $this, 'rest_api_omnikassa_2_webhook_item' ), |
||
| 65 | 'permission_callback' => '__return_true', |
||
| 66 | ) |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * REST API OmniKassa 2.0 webhook handler. |
||
| 72 | * |
||
| 73 | * @param \WP_REST_Request $request Request. |
||
| 74 | * @return object |
||
| 75 | * @throws \Exception Throws exception when something unexpected happens ;-). |
||
| 76 | */ |
||
| 77 | public function rest_api_omnikassa_2_webhook( \WP_REST_Request $request ) { |
||
| 78 | // Query. |
||
| 79 | $query = new \WP_Query( |
||
| 80 | array( |
||
| 81 | 'post_type' => GatewayPostType::POST_TYPE, |
||
| 82 | 'post_status' => 'publish', |
||
| 83 | 'nopaging' => true, |
||
| 84 | 'meta_query' => array( |
||
| 85 | array( |
||
| 86 | 'key' => '_pronamic_gateway_id', |
||
| 87 | 'value' => 'rabobank-omnikassa-2', |
||
| 88 | ), |
||
| 89 | ), |
||
| 90 | ) |
||
| 91 | ); |
||
| 92 | |||
| 93 | $data = array( |
||
| 94 | 'success' => true, |
||
| 95 | 'results' => array(), |
||
| 96 | ); |
||
| 97 | |||
| 98 | foreach ( $query->posts as $post ) { |
||
| 99 | $id = \get_post_field( 'ID', $post ); |
||
| 100 | |||
| 101 | $request->set_param( 'id', $id ); |
||
| 102 | |||
| 103 | $data['results'][] = $this->rest_api_omnikassa_2_webhook_item( $request ); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Response. |
||
| 107 | $response = new \WP_REST_Response( $data ); |
||
| 108 | |||
| 109 | $response->add_link( 'self', \rest_url( $request->get_route() ) ); |
||
| 110 | |||
| 111 | return $response; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * REST API OmniKassa 2.0 webhook handler. |
||
| 116 | * |
||
| 117 | * @param \WP_REST_Request $request Request. |
||
| 118 | * @return object |
||
| 119 | * @throws \Exception Throws exception when something unexpected happens ;-). |
||
| 120 | */ |
||
| 121 | public function rest_api_omnikassa_2_webhook_item( \WP_REST_Request $request ) { |
||
| 122 | // Input. |
||
| 123 | $json = $request->get_body(); |
||
| 124 | |||
| 125 | try { |
||
| 126 | $notification = Notification::from_json( $json ); |
||
| 127 | } catch ( \JsonSchema\Exception\ValidationException $e ) { |
||
| 128 | // Invalid input data. |
||
| 129 | return new \WP_Error( |
||
| 130 | 'rest_omnikassa_2_notification_invalid', |
||
| 131 | \__( 'Invalid OmniKassa 2.0 notification.', 'pronamic_ideal ' ), |
||
| 132 | array( |
||
| 133 | 'status' => 400, |
||
| 134 | 'notification' => $json, |
||
| 135 | ) |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | |||
| 139 | // Gateway configuration. |
||
| 140 | $id = $request->get_param( 'id' ); |
||
| 141 | |||
| 142 | if ( null === $id ) { |
||
| 143 | return new \WP_Error( |
||
| 144 | 'rest_omnikassa_2_gateway_no_id', |
||
| 145 | \__( 'No gateway ID given in `id` parameter.', 'pronamic_ideal' ) |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | $gateway = Plugin::get_gateway( $id ); |
||
|
0 ignored issues
–
show
|
|||
| 150 | |||
| 151 | if ( ! $gateway instanceof Gateway ) { |
||
| 152 | // Invalid gateway. |
||
| 153 | return new \WP_Error( |
||
| 154 | 'rest_omnikassa_2_gateway_invalid', |
||
| 155 | \__( 'Invalid OmniKassa 2.0 gateway.', 'pronamic_ideal ' ), |
||
| 156 | array( |
||
| 157 | 'status' => 400, |
||
| 158 | 'id' => $id, |
||
| 159 | ) |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Data. |
||
| 165 | */ |
||
| 166 | $data = array( |
||
| 167 | 'success' => true, |
||
| 168 | ); |
||
| 169 | |||
| 170 | try { |
||
| 171 | $gateway->handle_notification( $notification ); |
||
| 172 | // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
||
| 173 | } catch ( \Pronamic\WordPress\Pay\Gateways\OmniKassa2\UnknownOrderIdsException $e ) { |
||
| 174 | /** |
||
| 175 | * We don't return an error for unknown order IDs, since OmniKassa |
||
| 176 | * otherwise assumes that the notification could not be processed. |
||
| 177 | */ |
||
| 178 | $data['uknown_order_ids'] = true; |
||
| 179 | } catch ( \Exception $e ) { |
||
| 180 | return new \WP_Error( |
||
| 181 | 'rest_omnikassa_2_exception', |
||
| 182 | $e->getMessage(), |
||
| 183 | array( |
||
| 184 | 'status' => 400, |
||
| 185 | 'notification' => $json, |
||
| 186 | 'id' => $id, |
||
| 187 | ) |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | // Response. |
||
| 192 | $response = new \WP_REST_Response( $data ); |
||
| 193 | |||
| 194 | $response->add_link( 'self', \rest_url( $request->get_route() ) ); |
||
| 195 | |||
| 196 | return $response; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * WordPress loaded, check for deprecated webhook call. |
||
| 201 | * |
||
| 202 | * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309 |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function wp_loaded() { |
||
| 206 | if ( ! \filter_has_var( \INPUT_GET, 'omnikassa2_webhook' ) ) { |
||
| 207 | return; |
||
| 208 | } |
||
| 209 | |||
| 210 | \rest_get_server()->serve_request( '/pronamic-pay/omnikassa-2/v1/webhook' ); |
||
| 211 | |||
| 212 | exit; |
||
| 213 | } |
||
| 214 | } |
||
| 215 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.