You are here

class Braintree_WebhookNotification in Commerce Braintree 7

Hierarchy

Expanded class hierarchy of Braintree_WebhookNotification

File

braintree_php/lib/Braintree/WebhookNotification.php, line 2

View source
class Braintree_WebhookNotification extends Braintree {
  const SUBSCRIPTION_CANCELED = 'subscription_canceled';
  const SUBSCRIPTION_CHARGED_SUCCESSFULLY = 'subscription_charged_successfully';
  const SUBSCRIPTION_CHARGED_UNSUCCESSFULLY = 'subscription_charged_unsuccessfully';
  const SUBSCRIPTION_EXPIRED = 'subscription_expired';
  const SUBSCRIPTION_TRIAL_ENDED = 'subscription_trial_ended';
  const SUBSCRIPTION_WENT_ACTIVE = 'subscription_went_active';
  const SUBSCRIPTION_WENT_PAST_DUE = 'subscription_went_past_due';
  public static function parse($signature, $payload) {
    self::_validateSignature($signature, $payload);
    $xml = base64_decode($payload);
    $attributes = Braintree_Xml::buildArrayFromXml($xml);
    return self::factory($attributes['notification']);
  }
  public static function verify($challenge) {
    $publicKey = Braintree_Configuration::publicKey();
    $digest = Braintree_Digest::hexDigest($challenge);
    return "{$publicKey}|{$digest}";
  }
  public static function factory($attributes) {
    $instance = new self();
    $instance
      ->_initialize($attributes);
    return $instance;
  }
  private static function _matchingSignature($signaturePairs) {
    foreach ($signaturePairs as $pair) {
      $components = preg_split("/\\|/", $pair);
      if ($components[0] == Braintree_Configuration::publicKey()) {
        return $components[1];
      }
    }
    return null;
  }
  private static function _validateSignature($signature, $payload) {
    $signaturePairs = preg_split("/&/", $signature);
    $matchingSignature = self::_matchingSignature($signaturePairs);
    $payloadSignature = Braintree_Digest::hexDigest($payload);
    if (!Braintree_Digest::secureCompare($matchingSignature, $payloadSignature)) {
      throw new Braintree_Exception_InvalidSignature("webhook notification signature invalid");
    }
  }
  protected function _initialize($attributes) {
    $this->_attributes = $attributes;
    if (isset($attributes['subject']) && isset($attributes['subject']['subscription'])) {
      $this
        ->_set('subscription', Braintree_Subscription::factory($attributes['subject']['subscription']));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Braintree::returnObjectOrThrowException public static function
Braintree::_set public function
Braintree::__clone protected function @ignore don't permit cloning the instances (like $x = clone $v)
Braintree::__construct protected function @ignore don't permit an explicit call of the constructor! (like $t = new Braintree_Transaction()) 1
Braintree::__get public function returns private/nonexistent instance properties @ignore @access public
Braintree::__isset public function used by isset() and empty() @access public
Braintree_WebhookNotification::factory public static function
Braintree_WebhookNotification::parse public static function
Braintree_WebhookNotification::SUBSCRIPTION_CANCELED constant
Braintree_WebhookNotification::SUBSCRIPTION_CHARGED_SUCCESSFULLY constant
Braintree_WebhookNotification::SUBSCRIPTION_CHARGED_UNSUCCESSFULLY constant
Braintree_WebhookNotification::SUBSCRIPTION_EXPIRED constant
Braintree_WebhookNotification::SUBSCRIPTION_TRIAL_ENDED constant
Braintree_WebhookNotification::SUBSCRIPTION_WENT_ACTIVE constant
Braintree_WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE constant
Braintree_WebhookNotification::verify public static function
Braintree_WebhookNotification::_initialize protected function
Braintree_WebhookNotification::_matchingSignature private static function
Braintree_WebhookNotification::_validateSignature private static function