1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Rest_Webook_Authentication {
public function __construct() {
add_filter( 'rest_pre_dispatch', array( $this, 'check_authentication' ), 10, 3 );
}
public function check_authentication( $response, $server, $request ) {
if ( ! empty( $response ) ) {
return $response;
}
if ( $this->is_request_to_webook_api( $request->get_route() ) ) {
return $this->authenticate_request( $request );
}
return $response;
}
private function authenticate_request( $request ) {
$post = $request->get_body_params();
$signature = isset( $post['bt_signature'] ) ? $post['bt_signature'] : '';
$payload = isset( $post['bt_payload'] ) ? str_replace( array( "\\n", "\\r" ), '', $post['bt_payload'] ) : '';
try {
$env = wc_braintree_environment();
$notification = braintree()->gateway()->webhookNotification()->parse( $signature, $payload );
$request->set_param( 'wc_braintree_notification', $notification );
wc_braintree_log_info( sprintf( __( 'Webhook received. Kind: %1$s.', 'woo-payment-gateway' ), $notification->kind ) );
return null;
} catch ( Exception $e ) {
wc_braintree_log_error( sprintf( __( 'Error authenticating Braintree webhook request. Environment: %1$s. Reason: %2$s', 'woo-payment-gateway' ), $env, $e->getMessage() ) );
return new WP_Error( 'wc_braintree_webook_authentication_error', __( 'Invalid webhook request.', 'woo-payment-gateway' ), array( 'status' => 401 ) );
}
}
private function is_request_to_webook_api( $route ) {
$routes = get_option( 'wc_braintree_authenticated_routes', array() );
foreach ( $routes as $auth_route ) {
if ( preg_match( '@^' . $auth_route . '$@i', $route ) ) {
return true;
}
}
return false;
}
}
new WC_Braintree_Rest_Webook_Authentication();