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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Controller_Webhook extends WC_Braintree_Rest_Controller {
protected $namespace = 'webhook/';
public function register_routes() {
if ( wc_braintree_environment() === 'sandbox' ) {
register_rest_route(
$this->rest_uri(),
'test',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'get_sample_notification' ),
'permission_callback' => '__return_true'
),
)
);
}
register_rest_route(
$this->rest_uri(),
'notification',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'process_webhook' ),
'permission_callback' => '__return_true',
'args' => array(
'wc_braintree_notification' => array(
'required' => false,
'validate_callback' => array(
$this,
'validate_notification',
),
),
),
),
)
);
$this->register_authenticated_route( $this->rest_uri() . '/notification' );
}
public function validate_notification( $notification ) {
if ( strpos( $notification->kind, 'subscription' ) !== false ) {
$id = $notification->subscription->id;
global $wpdb;
$result = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d AND post_type = %s", $id, 'bfwc_subscription' ) );
if ( ! $result ) {
wc_braintree_log_info( sprintf( 'Subscription %1$d not found in your database.', 'woo-payment-gateway' ), $id );
return new WP_Error( 'wc_braintree_webhook_error', 'Invalid subscription ID.', array( 'status' => 400 ) );
}
return true;
}
return true;
}
public function get_sample_notification( $request ) {
$kind = $request->get_param( 'kind' );
$id = $request->get_param( 'id' );
$xml = null;
try {
switch ( $kind ) {
case \Braintree\WebhookNotification::SUBSCRIPTION_CANCELED:
$kind = \Braintree\WebhookNotification::SUBSCRIPTION_CANCELED;
break;
case \Braintree\WebhookNotification::SUBSCRIPTION_CHARGED_SUCCESSFULLY:
$kind = \Braintree\WebhookNotification::SUBSCRIPTION_CHARGED_SUCCESSFULLY;
break;
case \Braintree\WebhookNotification::SUBSCRIPTION_CHARGED_UNSUCCESSFULLY:
$kind = \Braintree\WebhookNotification::SUBSCRIPTION_CHARGED_UNSUCCESSFULLY;
break;
case \Braintree\WebhookNotification::SUBSCRIPTION_EXPIRED:
$kind = \Braintree\WebhookNotification::SUBSCRIPTION_EXPIRED;
break;
case \Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE:
$kind = \Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE;
break;
case \Braintree\WebhookNotification::LOCAL_PAYMENT_COMPLETED:
$kind = \Braintree\WebhookNotification::LOCAL_PAYMENT_COMPLETED;
break;
case \Braintree\WebhookNotification::CHECK:
$kind = \Braintree\WebhookNotification::CHECK;
break;
default:
throw new Exception( 'Invalid notification kind.' );
}
if ( strstr( $kind, 'subscription' ) ) {
$xml = $this->build_object_xml( '/subscriptions', '<subscription>', '</subscription>', $id );
}
$notification = braintree()->gateway()->webhookTesting()->sampleNotification( $kind, $id );
return rest_ensure_response(
array(
'bt_signature' => $notification['bt_signature'],
'bt_payload' => $notification['bt_payload'],
)
);
} catch ( Exception $e ) {
return new WP_Error( 'wc_braintree_sample_notification_error', $e->getMessage(), array( 'status' => 400 ) );
}
}
public function process_webhook( $request ) {
$notification = $request->get_param( 'wc_braintree_notification' );
try {
do_action( 'wc_braintree_webhook_notification_' . $notification->kind, $notification, $request );
return rest_ensure_response( array( 'success' => true ) );
} catch ( Exception $e ) {
return new WP_Error( 'wc_braintree_webhook_error', $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}
private function build_object_xml( $path, $start, $end, $id ) {
$config = braintree()->gateway()->config;
$http = new \Braintree\Http( $config );
$path = $config->baseUrl() . $config->merchantPath() . $path . '/' . $id;
$response = $http->_doUrlRequest( 'GET', $path );
if ( $response['status'] !== 200 ) {
Braintree_Util::throwStatusCodeException( $response['status'] );
}
$xml = $response['body'];
return wc_braintree_parse_xml_contents( $xml, $start, $end );
}
}