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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
<?php
class WCS_Braintree_Subscription_Controller extends WC_Braintree_Rest_Controller {
protected $namespace = '';
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}
public function register_routes() {
register_rest_route(
$this->deprecated_rest_uri(),
'webhooks/test',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'get_sample_notification' ),
'permission_callback' => '__return_true'
),
)
);
register_rest_route(
$this->deprecated_rest_uri(),
'webhooks',
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'process_webhook' ),
'permission_callback' => '__return_true',
'args' => array(
'wc_braintree_notification' => array(
'required' => true,
'validate_callback' => array(
$this,
'validate_notification',
),
),
),
)
);
$this->register_authenticated_route( $this->deprecated_rest_uri() . '/webhooks' );
}
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, 'shop_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 ) );
}
if ( 'braintree' !== get_post_meta( $id, '_subscription_type', true ) ) {
return new WP_Error( 'wc_braintree_webhook_error', sprintf( 'Subscription %1$s is not a Braintree Subscription.', $id ) );
}
return true;
}
return true;
}
public function process_webhook( $request ) {
$notification = $request->get_param( 'wc_braintree_notification' );
global $wpdb;
switch ( $notification->kind ) {
case \Braintree\WebhookNotification::SUBSCRIPTION_CHARGED_SUCCESSFULLY:
$transaction_id = $notification->subscription->transactions[0]->id;
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts as posts INNER JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE post_type = 'shop_order' AND meta.meta_key = '_transaction_id' AND meta.meta_value = %s", $transaction_id ) );
if ( ! $order_id ) {
$id = $notification->subscription->id;
$subscription = wcs_get_subscription( $id );
$renewal = wcs_create_renewal_order( $subscription );
$renewal->payment_complete( $transaction_id );
$renewal->save();
}
break;
case \Braintree\WebhookNotification::SUBSCRIPTION_CANCELED:
$id = $notification->subscription->id;
$subscription = wcs_get_subscription( $id );
if ( $subscription->can_be_updated_to( 'cancelled' ) ) {
$subscription->update_status( 'cancelled' );
}
break;
case \Braintree\WebhookNotification::SUBSCRIPTION_CHARGED_UNSUCCESSFULLY:
case \Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE:
$id = $notification->subscription->id;
$subscription = wcs_get_subscription( $id );
if ( $subscription->can_be_updated_to( 'on-hold' ) ) {
$subscription->add_order_note( __( 'Recurring payment for subscirption failed.', 'woo-payment-gateway' ) );
$subscription->update_status( 'on-hold' );
}
break;
case \Braintree\WebhookNotification::SUBSCRIPTION_EXPIRED:
$id = $notification->subscription->id;
$subscription = wcs_get_subscription( $id );
if ( $subscription->can_be_updated_to( 'expired' ) ) {
$subscription->update_status( 'expired' );
}
break;
}
return rest_ensure_response( array( 'success' => true ) );
}
public function get_sample_notification( $request ) {
$kind = $request->get_param( 'kind' );
$id = $request->get_param( 'id' );
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;
default:
throw new Exception( 'Invalid notification kind.' );
}
$subscription_xml = $this->build_object_xml( '/subscriptions', '<subscription>', '</subscription>', $id );
$notification = braintree()->gateway()->webhookTesting()->sampleNotification( $kind, $id, null, $subscription_xml );
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 ) );
}
}
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 );
}
}