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.3 |
||
| 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 | $results = array(); |
||
| 94 | |||
| 95 | foreach ( $query->posts as $post ) { |
||
| 96 | $id = \get_post_field( 'ID', $post ); |
||
| 97 | |||
| 98 | $request->set_param( 'id', $id ); |
||
| 99 | |||
| 100 | $results[] = $this->rest_api_omnikassa_2_webhook_item( $request ); |
||
| 101 | } |
||
| 102 | |||
| 103 | // Response. |
||
| 104 | $response = new \WP_REST_Response( |
||
| 105 | array( |
||
| 106 | 'success' => true, |
||
| 107 | 'results' => $results, |
||
| 108 | ) |
||
| 109 | ); |
||
| 110 | |||
| 111 | $response->add_link( 'self', \rest_url( $request->get_route() ) ); |
||
| 112 | |||
| 113 | return $response; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * REST API OmniKassa 2.0 webhook handler. |
||
| 118 | * |
||
| 119 | * @param \WP_REST_Request $request Request. |
||
| 120 | * @return object |
||
| 121 | * @throws \Exception Throws exception when something unexpected happens ;-). |
||
| 122 | */ |
||
| 123 | public function rest_api_omnikassa_2_webhook_item( \WP_REST_Request $request ) { |
||
| 124 | // Input. |
||
| 125 | $json = $request->get_body(); |
||
| 126 | |||
| 127 | try { |
||
| 128 | $notification = Notification::from_json( $json ); |
||
| 129 | } catch ( \JsonSchema\Exception\ValidationException $e ) { |
||
| 130 | // Invalid input data. |
||
| 131 | return new \WP_Error( |
||
| 132 | 'rest_omnikassa_2_notification_invalid', |
||
| 133 | \__( 'Invalid OmniKassa 2.0 notification.', 'pronamic_ideal ' ), |
||
| 134 | array( |
||
| 135 | 'status' => 400, |
||
| 136 | 'notification' => $json, |
||
| 137 | ) |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | |||
| 141 | // Gateway configuration. |
||
| 142 | $id = $request->get_param( 'id' ); |
||
| 143 | |||
| 144 | if ( null === $id ) { |
||
| 145 | return new \WP_Error( |
||
| 146 | 'rest_omnikassa_2_gateway_no_id', |
||
| 147 | \__( 'No gateway ID given in `id` parameter.', 'pronamic_ideal' ) |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | |||
| 151 | $gateway = Plugin::get_gateway( $id ); |
||
|
0 ignored issues
–
show
|
|||
| 152 | |||
| 153 | if ( ! $gateway instanceof Gateway ) { |
||
| 154 | // Invalid gateway. |
||
| 155 | return new \WP_Error( |
||
| 156 | 'rest_omnikassa_2_gateway_invalid', |
||
| 157 | \__( 'Invalid OmniKassa 2.0 gateway.', 'pronamic_ideal ' ), |
||
| 158 | array( |
||
| 159 | 'status' => 400, |
||
| 160 | 'id' => $id, |
||
| 161 | ) |
||
| 162 | ); |
||
| 163 | } |
||
| 164 | |||
| 165 | try { |
||
| 166 | $gateway->handle_notification( $notification ); |
||
| 167 | // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
||
| 168 | } catch ( \Pronamic\WordPress\Pay\Gateways\OmniKassa2\UnknownOrderIdsException $e ) { |
||
| 169 | /** |
||
| 170 | * We don't return an error for unknown order IDs, since OmniKassa |
||
| 171 | * otherwise assumes that the notification could not be processed. |
||
| 172 | */ |
||
| 173 | } catch ( \Exception $e ) { |
||
| 174 | return new \WP_Error( |
||
| 175 | 'rest_omnikassa_2_exception', |
||
| 176 | $e->getMessage(), |
||
| 177 | array( |
||
| 178 | 'status' => 400, |
||
| 179 | 'notification' => $json, |
||
| 180 | 'id' => $id, |
||
| 181 | ) |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | // Response. |
||
| 186 | $response = new \WP_REST_Response( array( 'success' => true ) ); |
||
| 187 | |||
| 188 | $response->add_link( 'self', \rest_url( $request->get_route() ) ); |
||
| 189 | |||
| 190 | return $response; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * WordPress loaded, check for deprecated webhook call. |
||
| 195 | * |
||
| 196 | * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309 |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | public function wp_loaded() { |
||
| 200 | if ( ! \filter_has_var( \INPUT_GET, 'omnikassa2_webhook' ) ) { |
||
| 201 | return; |
||
| 202 | } |
||
| 203 | |||
| 204 | \rest_get_server()->serve_request( '/pronamic-pay/omnikassa-2/v1/webhook' ); |
||
| 205 | |||
| 206 | exit; |
||
| 207 | } |
||
| 208 | } |
||
| 209 |
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.