BraintreeWebhookNotification

The PayPal for WooCommerce Braintree WebhookNotification class.

Defined (1)

The class is defined in the following location(s).

/classes/lib/Braintree/Braintree/WebhookNotification.php  
  1. class WebhookNotification extends Base 
  2. const SUBSCRIPTION_CANCELED = 'subscription_canceled'; 
  3. const SUBSCRIPTION_CHARGED_SUCCESSFULLY = 'subscription_charged_successfully'; 
  4. const SUBSCRIPTION_CHARGED_UNSUCCESSFULLY = 'subscription_charged_unsuccessfully'; 
  5. const SUBSCRIPTION_EXPIRED = 'subscription_expired'; 
  6. const SUBSCRIPTION_TRIAL_ENDED = 'subscription_trial_ended'; 
  7. const SUBSCRIPTION_WENT_ACTIVE = 'subscription_went_active'; 
  8. const SUBSCRIPTION_WENT_PAST_DUE = 'subscription_went_past_due'; 
  9. const SUB_MERCHANT_ACCOUNT_APPROVED = 'sub_merchant_account_approved'; 
  10. const SUB_MERCHANT_ACCOUNT_DECLINED = 'sub_merchant_account_declined'; 
  11. const TRANSACTION_DISBURSED = 'transaction_disbursed'; 
  12. const DISBURSEMENT_EXCEPTION = 'disbursement_exception'; 
  13. const DISBURSEMENT = 'disbursement'; 
  14. const DISPUTE_OPENED = 'dispute_opened'; 
  15. const DISPUTE_LOST = 'dispute_lost'; 
  16. const DISPUTE_WON = 'dispute_won'; 
  17. const PARTNER_MERCHANT_CONNECTED = 'partner_merchant_connected'; 
  18. const PARTNER_MERCHANT_DISCONNECTED = 'partner_merchant_disconnected'; 
  19. const PARTNER_MERCHANT_DECLINED = 'partner_merchant_declined'; 
  20. const CHECK = 'check'; 
  21.  
  22. public static function parse($signature, $payload) 
  23. if (preg_match("/[^A-Za-z0-9+=\/\n]/", $payload) === 1) { 
  24. throw new Exception\InvalidSignature("payload contains illegal characters"); 
  25.  
  26. Configuration::assertGlobalHasAccessTokenOrKeys(); 
  27. self::_validateSignature($signature, $payload); 
  28.  
  29. $xml = base64_decode($payload); 
  30. $attributes = Xml::buildArrayFromXml($xml); 
  31. return self::factory($attributes['notification']); 
  32.  
  33. public static function verify($challenge) 
  34. if (!preg_match('/^[a-f0-9]{20, 32}$/', $challenge)) { 
  35. throw new Exception\InvalidChallenge("challenge contains non-hex characters"); 
  36. Configuration::assertGlobalHasAccessTokenOrKeys(); 
  37. $publicKey = Configuration::publicKey(); 
  38. $digest = Digest::hexDigestSha1(Configuration::privateKey(), $challenge); 
  39. return "{$publicKey}|{$digest}"; 
  40.  
  41. public static function factory($attributes) 
  42. $instance = new self(); 
  43. $instance->_initialize($attributes); 
  44. return $instance; 
  45.  
  46. private static function _matchingSignature($signaturePairs) 
  47. foreach ($signaturePairs as $pair) 
  48. $components = preg_split("/\|/", $pair); 
  49. if ($components[0] == Configuration::publicKey()) { 
  50. return $components[1]; 
  51.  
  52. return null; 
  53.  
  54. private static function _payloadMatches($signature, $payload) 
  55. $payloadSignature = Digest::hexDigestSha1(Configuration::privateKey(), $payload); 
  56. return Digest::secureCompare($signature, $payloadSignature); 
  57.  
  58. private static function _validateSignature($signatureString, $payload) 
  59. $signaturePairs = preg_split("/&/", $signatureString); 
  60. $signature = self::_matchingSignature($signaturePairs); 
  61. if (!$signature) { 
  62. throw new Exception\InvalidSignature("no matching public key"); 
  63.  
  64. if (!(self::_payloadMatches($signature, $payload) || self::_payloadMatches($signature, $payload . "\n"))) { 
  65. throw new Exception\InvalidSignature("signature does not match payload - one has been modified"); 
  66.  
  67. protected function _initialize($attributes) 
  68. $this->_attributes = $attributes; 
  69.  
  70. if (isset($attributes['subject']['apiErrorResponse'])) { 
  71. $wrapperNode = $attributes['subject']['apiErrorResponse']; 
  72. } else { 
  73. $wrapperNode = $attributes['subject']; 
  74.  
  75. if (isset($wrapperNode['subscription'])) { 
  76. $this->_set('subscription', Subscription::factory($attributes['subject']['subscription'])); 
  77.  
  78. if (isset($wrapperNode['merchantAccount'])) { 
  79. $this->_set('merchantAccount', MerchantAccount::factory($wrapperNode['merchantAccount'])); 
  80.  
  81. if (isset($wrapperNode['transaction'])) { 
  82. $this->_set('transaction', Transaction::factory($wrapperNode['transaction'])); 
  83.  
  84. if (isset($wrapperNode['disbursement'])) { 
  85. $this->_set('disbursement', Disbursement::factory($wrapperNode['disbursement'])); 
  86.  
  87. if (isset($wrapperNode['partnerMerchant'])) { 
  88. $this->_set('partnerMerchant', PartnerMerchant::factory($wrapperNode['partnerMerchant'])); 
  89.  
  90. if (isset($wrapperNode['dispute'])) { 
  91. $this->_set('dispute', Dispute::factory($wrapperNode['dispute'])); 
  92.  
  93. if (isset($wrapperNode['errors'])) { 
  94. $this->_set('errors', new Error\ValidationErrorCollection($wrapperNode['errors'])); 
  95. $this->_set('message', $wrapperNode['message']);