WebhookNotificationTest.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. namespace Test\Unit;
  3. require_once dirname(__DIR__) . '/Setup.php';
  4. use DateTime;
  5. use Test\Setup;
  6. use Braintree;
  7. class WebhookNotificationTest extends Setup
  8. {
  9. public function setup()
  10. {
  11. self::integrationMerchantConfig();
  12. }
  13. public function testVerify()
  14. {
  15. $verificationString = Braintree\WebhookNotification::verify('20f9f8ed05f77439fe955c977e4c8a53');
  16. $this->assertEquals('integration_public_key|d9b899556c966b3f06945ec21311865d35df3ce4', $verificationString);
  17. }
  18. /**
  19. * @expectedException Braintree\Exception\InvalidChallenge
  20. * @expectedExceptionMessage challenge contains non-hex characters
  21. */
  22. public function testVerifyRaisesErrorWithInvalidChallenge()
  23. {
  24. $this->setExpectedException('Braintree\Exception\InvalidChallenge', 'challenge contains non-hex characters');
  25. Braintree\WebhookNotification::verify('bad challenge');
  26. }
  27. /**
  28. * @expectedException Braintree\Exception\Configuration
  29. * @expectedExceptionMessage Braintree\Configuration::merchantId needs to be set (or accessToken needs to be passed to Braintree\Gateway)
  30. */
  31. public function testVerifyRaisesErrorWhenEnvironmentNotSet()
  32. {
  33. Braintree\Configuration::reset();
  34. Braintree\WebhookNotification::verify('20f9f8ed05f77439fe955c977e4c8a53');
  35. }
  36. public function testSampleNotificationReturnsAParsableNotification()
  37. {
  38. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  39. Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE,
  40. 'my_id'
  41. );
  42. $webhookNotification = Braintree\WebhookNotification::parse(
  43. $sampleNotification['bt_signature'],
  44. $sampleNotification['bt_payload']
  45. );
  46. $this->assertEquals(Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE, $webhookNotification->kind);
  47. $this->assertNotNull($webhookNotification->timestamp);
  48. $this->assertEquals("my_id", $webhookNotification->subscription->id);
  49. $this->assertNull($webhookNotification->sourceMerchantId);
  50. }
  51. public function testSampleNotificationContainsSourceMerchantIdIfSpecified()
  52. {
  53. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  54. Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE,
  55. 'my_id',
  56. 'my_source_merchant_id'
  57. );
  58. $webhookNotification = Braintree\WebhookNotification::parse(
  59. $sampleNotification['bt_signature'],
  60. $sampleNotification['bt_payload']
  61. );
  62. $this->assertEquals($webhookNotification->sourceMerchantId, 'my_source_merchant_id');
  63. }
  64. public function testParsingModifiedSignatureRaisesError()
  65. {
  66. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  67. Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE,
  68. 'my_id'
  69. );
  70. $this->setExpectedException('Braintree\Exception\InvalidSignature', 'signature does not match payload - one has been modified');
  71. $webhookNotification = Braintree\WebhookNotification::parse(
  72. $sampleNotification['bt_signature'] . "bad",
  73. $sampleNotification['bt_payload']
  74. );
  75. }
  76. /**
  77. * @expectedException Braintree\Exception\Configuration
  78. * @expectedExceptionMessage Braintree\Configuration::merchantId needs to be set (or accessToken needs to be passed to Braintree\Gateway)
  79. */
  80. public function testParsingWithNoKeysRaisesError()
  81. {
  82. Braintree\Configuration::reset();
  83. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  84. Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE,
  85. 'my_id'
  86. );
  87. $webhookNotification = Braintree\WebhookNotification::parse(
  88. $sampleNotification['bt_signature'],
  89. $sampleNotification['bt_payload']
  90. );
  91. }
  92. public function testParsingWebhookWithWrongKeysRaisesError()
  93. {
  94. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  95. Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE,
  96. 'my_id'
  97. );
  98. Braintree\Configuration::environment('development');
  99. Braintree\Configuration::merchantId('integration_merchant_id');
  100. Braintree\Configuration::publicKey('wrong_public_key');
  101. Braintree\Configuration::privateKey('wrong_private_key');
  102. $this->setExpectedException('Braintree\Exception\InvalidSignature', 'no matching public key');
  103. $webhookNotification = Braintree\WebhookNotification::parse(
  104. $sampleNotification['bt_signature'],
  105. "bad" . $sampleNotification['bt_payload']
  106. );
  107. }
  108. public function testParsingModifiedPayloadRaisesError()
  109. {
  110. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  111. Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE,
  112. 'my_id'
  113. );
  114. $this->setExpectedException('Braintree\Exception\InvalidSignature');
  115. $webhookNotification = Braintree\WebhookNotification::parse(
  116. $sampleNotification['bt_signature'],
  117. "bad" . $sampleNotification['bt_payload']
  118. );
  119. }
  120. public function testParsingUnknownPublicKeyRaisesError()
  121. {
  122. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  123. Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE,
  124. 'my_id'
  125. );
  126. $this->setExpectedException('Braintree\Exception\InvalidSignature');
  127. $webhookNotification = Braintree\WebhookNotification::parse(
  128. "bad" . $sampleNotification['bt_signature'],
  129. $sampleNotification['bt_payload']
  130. );
  131. }
  132. public function testParsingInvalidSignatureRaisesError()
  133. {
  134. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  135. Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE,
  136. 'my_id'
  137. );
  138. $this->setExpectedException('Braintree\Exception\InvalidSignature');
  139. $webhookNotification = Braintree\WebhookNotification::parse(
  140. "bad_signature",
  141. $sampleNotification['bt_payload']
  142. );
  143. }
  144. public function testParsingNullSignatureRaisesError()
  145. {
  146. $this->setExpectedException('Braintree\Exception\InvalidSignature', 'signature cannot be null');
  147. $webhookNotification = Braintree\WebhookNotification::parse(null, "payload");
  148. }
  149. public function testParsingNullPayloadRaisesError()
  150. {
  151. $this->setExpectedException('Braintree\Exception\InvalidSignature', 'payload cannot be null');
  152. $webhookNotification = Braintree\WebhookNotification::parse("signature", null);
  153. }
  154. public function testParsingInvalidCharactersRaisesError()
  155. {
  156. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  157. Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE,
  158. 'my_id'
  159. );
  160. $this->setExpectedException('Braintree\Exception\InvalidSignature', 'payload contains illegal characters');
  161. $webhookNotification = Braintree\WebhookNotification::parse(
  162. $sampleNotification['bt_signature'],
  163. "~*~*invalid*~*~"
  164. );
  165. }
  166. public function testParsingAllowsAllValidCharacters()
  167. {
  168. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  169. Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE,
  170. 'my_id'
  171. );
  172. $this->setExpectedException('Braintree\Exception\InvalidSignature', 'signature does not match payload - one has been modified');
  173. $webhookNotification = Braintree\WebhookNotification::parse(
  174. $sampleNotification['bt_signature'],
  175. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+=/\n"
  176. );
  177. }
  178. public function testParsingRetriesPayloadWithANewline()
  179. {
  180. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  181. Braintree\WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE,
  182. 'my_id'
  183. );
  184. $webhookNotification = Braintree\WebhookNotification::parse(
  185. $sampleNotification['bt_signature'],
  186. rtrim($sampleNotification['bt_payload'])
  187. );
  188. }
  189. public function testAllowsParsingUsingGateway()
  190. {
  191. $gateway = new Braintree\Gateway([
  192. 'privateKey' => 'integration_private_key',
  193. 'publicKey' => 'integration_public_key',
  194. 'merchantId' => 'integration_merchant_id',
  195. 'environment' => 'development'
  196. ]);
  197. $sampleNotification = $gateway->webhookTesting()->sampleNotification(
  198. Braintree\WebhookNotification::CHECK,
  199. "my_id"
  200. );
  201. $webhookNotification = $gateway->webhookNotification()->parse(
  202. $sampleNotification['bt_signature'],
  203. $sampleNotification['bt_payload']
  204. );
  205. $this->assertEquals(Braintree\WebhookNotification::CHECK, $webhookNotification->kind);
  206. }
  207. public function testAllowsParsingUsingStaticMethods()
  208. {
  209. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  210. Braintree\WebhookNotification::CHECK,
  211. "my_id"
  212. );
  213. $webhookNotification = Braintree\WebhookNotification::parse(
  214. $sampleNotification['bt_signature'],
  215. $sampleNotification['bt_payload']
  216. );
  217. $this->assertEquals(Braintree\WebhookNotification::CHECK, $webhookNotification->kind);
  218. }
  219. public function testBuildsASampleNotificationForASubscriptionChargedSuccessfullyWebhook()
  220. {
  221. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  222. Braintree\WebhookNotification::SUBSCRIPTION_CHARGED_SUCCESSFULLY,
  223. "my_id"
  224. );
  225. $webhookNotification = Braintree\WebhookNotification::parse(
  226. $sampleNotification['bt_signature'],
  227. $sampleNotification['bt_payload']
  228. );
  229. $this->assertEquals(Braintree\WebhookNotification::SUBSCRIPTION_CHARGED_SUCCESSFULLY, $webhookNotification->kind);
  230. $this->assertEquals("my_id", $webhookNotification->subscription->id);
  231. $this->assertEquals(new DateTime('2016-03-21'), $webhookNotification->subscription->billingPeriodStartDate);
  232. $this->assertEquals(new DateTime('2017-03-31'), $webhookNotification->subscription->billingPeriodEndDate);
  233. $this->assertEquals(1, count($webhookNotification->subscription->transactions));
  234. $transaction = $webhookNotification->subscription->transactions[0];
  235. $this->assertEquals('submitted_for_settlement', $transaction->status);
  236. $this->assertEquals('49.99', $transaction->amount);
  237. }
  238. public function testBuildsASampleNotificationForASubscriptionChargedUnsuccessfullyWebhook()
  239. {
  240. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  241. Braintree\WebhookNotification::SUBSCRIPTION_CHARGED_UNSUCCESSFULLY,
  242. "my_id"
  243. );
  244. $webhookNotification = Braintree\WebhookNotification::parse(
  245. $sampleNotification['bt_signature'],
  246. $sampleNotification['bt_payload']
  247. );
  248. $this->assertEquals(Braintree\WebhookNotification::SUBSCRIPTION_CHARGED_UNSUCCESSFULLY, $webhookNotification->kind);
  249. $this->assertEquals("my_id", $webhookNotification->subscription->id);
  250. $this->assertEquals(new DateTime('2016-03-21'), $webhookNotification->subscription->billingPeriodStartDate);
  251. $this->assertEquals(new DateTime('2017-03-31'), $webhookNotification->subscription->billingPeriodEndDate);
  252. $this->assertEquals(1, count($webhookNotification->subscription->transactions));
  253. $transaction = $webhookNotification->subscription->transactions[0];
  254. $this->assertEquals('failed', $transaction->status);
  255. $this->assertEquals('49.99', $transaction->amount);
  256. }
  257. public function testBuildsASampleNotificationForAMerchantAccountApprovedWebhook()
  258. {
  259. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  260. Braintree\WebhookNotification::SUB_MERCHANT_ACCOUNT_APPROVED,
  261. "my_id"
  262. );
  263. $webhookNotification = Braintree\WebhookNotification::parse(
  264. $sampleNotification['bt_signature'],
  265. $sampleNotification['bt_payload']
  266. );
  267. $this->assertEquals(Braintree\WebhookNotification::SUB_MERCHANT_ACCOUNT_APPROVED, $webhookNotification->kind);
  268. $this->assertEquals("my_id", $webhookNotification->merchantAccount->id);
  269. $this->assertEquals(Braintree\MerchantAccount::STATUS_ACTIVE, $webhookNotification->merchantAccount->status);
  270. $this->assertEquals("master_ma_for_my_id", $webhookNotification->merchantAccount->masterMerchantAccount->id);
  271. $this->assertEquals(Braintree\MerchantAccount::STATUS_ACTIVE, $webhookNotification->merchantAccount->masterMerchantAccount->status);
  272. }
  273. public function testBuildsASampleNotificationForAMerchantAccountDeclinedWebhook()
  274. {
  275. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  276. Braintree\WebhookNotification::SUB_MERCHANT_ACCOUNT_DECLINED,
  277. "my_id"
  278. );
  279. $webhookNotification = Braintree\WebhookNotification::parse(
  280. $sampleNotification['bt_signature'],
  281. $sampleNotification['bt_payload']
  282. );
  283. $this->assertEquals(Braintree\WebhookNotification::SUB_MERCHANT_ACCOUNT_DECLINED, $webhookNotification->kind);
  284. $this->assertEquals("my_id", $webhookNotification->merchantAccount->id);
  285. $this->assertEquals(Braintree\MerchantAccount::STATUS_SUSPENDED, $webhookNotification->merchantAccount->status);
  286. $this->assertEquals("master_ma_for_my_id", $webhookNotification->merchantAccount->masterMerchantAccount->id);
  287. $this->assertEquals(Braintree\MerchantAccount::STATUS_SUSPENDED, $webhookNotification->merchantAccount->masterMerchantAccount->status);
  288. $this->assertEquals("Credit score is too low", $webhookNotification->message);
  289. $errors = $webhookNotification->errors->forKey('merchantAccount')->onAttribute('base');
  290. $this->assertEquals(Braintree\Error\Codes::MERCHANT_ACCOUNT_DECLINED_OFAC, $errors[0]->code);
  291. }
  292. public function testBuildsASampleNotificationForATransactionDisbursedWebhook()
  293. {
  294. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  295. Braintree\WebhookNotification::TRANSACTION_DISBURSED,
  296. "my_id"
  297. );
  298. $webhookNotification = Braintree\WebhookNotification::parse(
  299. $sampleNotification['bt_signature'],
  300. $sampleNotification['bt_payload']
  301. );
  302. $this->assertEquals(Braintree\WebhookNotification::TRANSACTION_DISBURSED, $webhookNotification->kind);
  303. $this->assertEquals("my_id", $webhookNotification->transaction->id);
  304. $this->assertEquals(100, $webhookNotification->transaction->amount);
  305. $this->assertNotNull($webhookNotification->transaction->disbursementDetails->disbursementDate);
  306. }
  307. public function testBuildsASampleNotificationForATransactionSettledWebhook()
  308. {
  309. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  310. Braintree\WebhookNotification::TRANSACTION_SETTLED,
  311. "my_id"
  312. );
  313. $webhookNotification = Braintree\WebhookNotification::parse(
  314. $sampleNotification['bt_signature'],
  315. $sampleNotification['bt_payload']
  316. );
  317. $this->assertEquals(Braintree\WebhookNotification::TRANSACTION_SETTLED, $webhookNotification->kind);
  318. $transaction = $webhookNotification->transaction;
  319. $this->assertEquals("my_id", $transaction->id);
  320. $this->assertEquals("settled", $transaction->status);
  321. $this->assertEquals(100, $transaction->amount);
  322. $this->assertEquals('123456789', $transaction->usBankAccount->routingNumber);
  323. $this->assertEquals('1234', $transaction->usBankAccount->last4);
  324. $this->assertEquals('checking', $transaction->usBankAccount->accountType);
  325. $this->assertEquals('Dan Schulman', $transaction->usBankAccount->accountHolderName);
  326. }
  327. public function testBuildsASampleNotificationForATransactionSettlementDeclinedWebhook()
  328. {
  329. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  330. Braintree\WebhookNotification::TRANSACTION_SETTLEMENT_DECLINED,
  331. "my_id"
  332. );
  333. $webhookNotification = Braintree\WebhookNotification::parse(
  334. $sampleNotification['bt_signature'],
  335. $sampleNotification['bt_payload']
  336. );
  337. $this->assertEquals(Braintree\WebhookNotification::TRANSACTION_SETTLEMENT_DECLINED, $webhookNotification->kind);
  338. $transaction = $webhookNotification->transaction;
  339. $this->assertEquals("my_id", $transaction->id);
  340. $this->assertEquals("settlement_declined", $transaction->status);
  341. $this->assertEquals(100, $transaction->amount);
  342. $this->assertEquals('123456789', $transaction->usBankAccount->routingNumber);
  343. $this->assertEquals('1234', $transaction->usBankAccount->last4);
  344. $this->assertEquals('checking', $transaction->usBankAccount->accountType);
  345. $this->assertEquals('Dan Schulman', $transaction->usBankAccount->accountHolderName);
  346. }
  347. public function testBuildsASampleNotificationForADisputeOpenedWebhook()
  348. {
  349. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  350. Braintree\WebhookNotification::DISPUTE_OPENED,
  351. "my_id"
  352. );
  353. $webhookNotification = Braintree\WebhookNotification::parse(
  354. $sampleNotification['bt_signature'],
  355. $sampleNotification['bt_payload']
  356. );
  357. $this->assertEquals(Braintree\WebhookNotification::DISPUTE_OPENED, $webhookNotification->kind);
  358. $this->assertEquals("my_id", $webhookNotification->dispute->id);
  359. $this->assertEquals(Braintree\Dispute::OPEN, $webhookNotification->dispute->status);
  360. $this->assertEquals(Braintree\Dispute::CHARGEBACK, $webhookNotification->dispute->kind);
  361. $this->assertEquals(new DateTime('2014-03-21'), $webhookNotification->dispute->dateOpened);
  362. }
  363. public function testBuildsASampleNotificationForADisputeLostWebhook()
  364. {
  365. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  366. Braintree\WebhookNotification::DISPUTE_LOST,
  367. "my_id"
  368. );
  369. $webhookNotification = Braintree\WebhookNotification::parse(
  370. $sampleNotification['bt_signature'],
  371. $sampleNotification['bt_payload']
  372. );
  373. $this->assertEquals(Braintree\WebhookNotification::DISPUTE_LOST, $webhookNotification->kind);
  374. $this->assertEquals("my_id", $webhookNotification->dispute->id);
  375. $this->assertEquals(Braintree\Dispute::LOST, $webhookNotification->dispute->status);
  376. $this->assertEquals(Braintree\Dispute::CHARGEBACK, $webhookNotification->dispute->kind);
  377. $this->assertEquals(new DateTime('2014-03-21'), $webhookNotification->dispute->dateOpened);
  378. }
  379. public function testBuildsASampleNotificationForADisputeWonWebhook()
  380. {
  381. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  382. Braintree\WebhookNotification::DISPUTE_WON,
  383. "my_id"
  384. );
  385. $webhookNotification = Braintree\WebhookNotification::parse(
  386. $sampleNotification['bt_signature'],
  387. $sampleNotification['bt_payload']
  388. );
  389. $this->assertEquals(Braintree\WebhookNotification::DISPUTE_WON, $webhookNotification->kind);
  390. $this->assertEquals("my_id", $webhookNotification->dispute->id);
  391. $this->assertEquals(Braintree\Dispute::WON, $webhookNotification->dispute->status);
  392. $this->assertEquals(Braintree\Dispute::CHARGEBACK, $webhookNotification->dispute->kind);
  393. $this->assertEquals(new DateTime('2014-03-21'), $webhookNotification->dispute->dateOpened);
  394. $this->assertEquals(new DateTime('2014-03-22'), $webhookNotification->dispute->dateWon);
  395. }
  396. public function testBuildsASampleNotificationForADisbursementExceptionWebhook()
  397. {
  398. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  399. Braintree\WebhookNotification::DISBURSEMENT_EXCEPTION,
  400. "my_id"
  401. );
  402. $webhookNotification = Braintree\WebhookNotification::parse(
  403. $sampleNotification['bt_signature'],
  404. $sampleNotification['bt_payload']
  405. );
  406. $this->assertEquals(Braintree\WebhookNotification::DISBURSEMENT_EXCEPTION, $webhookNotification->kind);
  407. $this->assertEquals("my_id", $webhookNotification->disbursement->id);
  408. $this->assertEquals(false, $webhookNotification->disbursement->retry);
  409. $this->assertEquals(false, $webhookNotification->disbursement->success);
  410. $this->assertEquals("bank_rejected", $webhookNotification->disbursement->exceptionMessage);
  411. $this->assertEquals(100.00, $webhookNotification->disbursement->amount);
  412. $this->assertEquals("update_funding_information", $webhookNotification->disbursement->followUpAction);
  413. $this->assertEquals("merchant_account_token", $webhookNotification->disbursement->merchantAccount->id);
  414. $this->assertEquals(new DateTime("2014-02-10"), $webhookNotification->disbursement->disbursementDate);
  415. $this->assertEquals(["asdfg", "qwert"], $webhookNotification->disbursement->transactionIds);
  416. }
  417. public function testBuildsASampleNotificationForADisbursementWebhook()
  418. {
  419. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  420. Braintree\WebhookNotification::DISBURSEMENT,
  421. "my_id"
  422. );
  423. $webhookNotification = Braintree\WebhookNotification::parse(
  424. $sampleNotification['bt_signature'],
  425. $sampleNotification['bt_payload']
  426. );
  427. $this->assertEquals(Braintree\WebhookNotification::DISBURSEMENT, $webhookNotification->kind);
  428. $this->assertEquals("my_id", $webhookNotification->disbursement->id);
  429. $this->assertEquals(false, $webhookNotification->disbursement->retry);
  430. $this->assertEquals(true, $webhookNotification->disbursement->success);
  431. $this->assertEquals(NULL, $webhookNotification->disbursement->exceptionMessage);
  432. $this->assertEquals(100.00, $webhookNotification->disbursement->amount);
  433. $this->assertEquals(NULL, $webhookNotification->disbursement->followUpAction);
  434. $this->assertEquals("merchant_account_token", $webhookNotification->disbursement->merchantAccount->id);
  435. $this->assertEquals(new DateTime("2014-02-10"), $webhookNotification->disbursement->disbursementDate);
  436. $this->assertEquals(["asdfg", "qwert"], $webhookNotification->disbursement->transactionIds);
  437. }
  438. public function testBuildsASampleNotificationForAPartnerMerchantConnectedWebhook()
  439. {
  440. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  441. Braintree\WebhookNotification::PARTNER_MERCHANT_CONNECTED,
  442. "my_id"
  443. );
  444. $webhookNotification = Braintree\WebhookNotification::parse(
  445. $sampleNotification['bt_signature'],
  446. $sampleNotification['bt_payload']
  447. );
  448. $this->assertEquals(Braintree\WebhookNotification::PARTNER_MERCHANT_CONNECTED, $webhookNotification->kind);
  449. $this->assertEquals("public_id", $webhookNotification->partnerMerchant->merchantPublicId);
  450. $this->assertEquals("public_key", $webhookNotification->partnerMerchant->publicKey);
  451. $this->assertEquals("private_key", $webhookNotification->partnerMerchant->privateKey);
  452. $this->assertEquals("abc123", $webhookNotification->partnerMerchant->partnerMerchantId);
  453. $this->assertEquals("cse_key", $webhookNotification->partnerMerchant->clientSideEncryptionKey);
  454. }
  455. public function testBuildsASampleNotificationForAPartnerMerchantDisconnectedWebhook()
  456. {
  457. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  458. Braintree\WebhookNotification::PARTNER_MERCHANT_DISCONNECTED,
  459. "my_id"
  460. );
  461. $webhookNotification = Braintree\WebhookNotification::parse(
  462. $sampleNotification['bt_signature'],
  463. $sampleNotification['bt_payload']
  464. );
  465. $this->assertEquals(Braintree\WebhookNotification::PARTNER_MERCHANT_DISCONNECTED, $webhookNotification->kind);
  466. $this->assertEquals("abc123", $webhookNotification->partnerMerchant->partnerMerchantId);
  467. }
  468. public function testBuildsASampleNotificationForAPartnerMerchantDeclinedWebhook()
  469. {
  470. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  471. Braintree\WebhookNotification::PARTNER_MERCHANT_DECLINED,
  472. "my_id"
  473. );
  474. $webhookNotification = Braintree\WebhookNotification::parse(
  475. $sampleNotification['bt_signature'],
  476. $sampleNotification['bt_payload']
  477. );
  478. $this->assertEquals(Braintree\WebhookNotification::PARTNER_MERCHANT_DECLINED, $webhookNotification->kind);
  479. $this->assertEquals("abc123", $webhookNotification->partnerMerchant->partnerMerchantId);
  480. }
  481. public function testBuildsASampleNotificationForOAuthAccessRevokedWebhook()
  482. {
  483. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  484. Braintree\WebhookNotification::OAUTH_ACCESS_REVOKED,
  485. 'my_id'
  486. );
  487. $webhookNotification = Braintree\WebhookNotification::parse(
  488. $sampleNotification['bt_signature'],
  489. $sampleNotification['bt_payload']
  490. );
  491. $this->assertEquals(Braintree\WebhookNotification::OAUTH_ACCESS_REVOKED, $webhookNotification->kind);
  492. $this->assertEquals('my_id', $webhookNotification->oauthAccessRevocation->merchantId);
  493. $this->assertEquals("oauth_application_client_id", $webhookNotification->oauthAccessRevocation->oauthApplicationClientId);
  494. }
  495. public function testBuildsASampleNotificationForConnectedMerchantStatusTransitionedWebhook()
  496. {
  497. $gateway = new Braintree\Gateway([
  498. 'privateKey' => 'integration_private_key',
  499. 'publicKey' => 'integration_public_key',
  500. 'merchantId' => 'integration_merchant_id',
  501. 'environment' => 'development'
  502. ]);
  503. $sampleNotification = $gateway->webhookTesting()->sampleNotification(
  504. Braintree\WebhookNotification::CONNECTED_MERCHANT_STATUS_TRANSITIONED,
  505. "my_id"
  506. );
  507. $webhookNotification = $gateway->webhookNotification()->parse(
  508. $sampleNotification['bt_signature'],
  509. $sampleNotification['bt_payload']
  510. );
  511. $this->assertEquals(Braintree\WebhookNotification::CONNECTED_MERCHANT_STATUS_TRANSITIONED, $webhookNotification->kind);
  512. $this->assertEquals("my_id", $webhookNotification->connectedMerchantStatusTransitioned->merchantPublicId);
  513. $this->assertEquals("my_id", $webhookNotification->connectedMerchantStatusTransitioned->merchantId);
  514. $this->assertEquals("new_status", $webhookNotification->connectedMerchantStatusTransitioned->status);
  515. $this->assertEquals("oauth_application_client_id", $webhookNotification->connectedMerchantStatusTransitioned->oauthApplicationClientId);
  516. }
  517. public function testBuildsASampleNotificationForConnectedMerchantPayPalStatusChangedWebhook()
  518. {
  519. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  520. Braintree\WebhookNotification::CONNECTED_MERCHANT_PAYPAL_STATUS_CHANGED,
  521. "my_id"
  522. );
  523. $webhookNotification = Braintree\WebhookNotification::parse(
  524. $sampleNotification['bt_signature'],
  525. $sampleNotification['bt_payload']
  526. );
  527. $this->assertEquals(Braintree\WebhookNotification::CONNECTED_MERCHANT_PAYPAL_STATUS_CHANGED, $webhookNotification->kind);
  528. $this->assertEquals("my_id", $webhookNotification->connectedMerchantPayPalStatusChanged->merchantPublicId);
  529. $this->assertEquals("my_id", $webhookNotification->connectedMerchantPayPalStatusChanged->merchantId);
  530. $this->assertEquals("link", $webhookNotification->connectedMerchantPayPalStatusChanged->action);
  531. $this->assertEquals("oauth_application_client_id", $webhookNotification->connectedMerchantPayPalStatusChanged->oauthApplicationClientId);
  532. }
  533. public function testBuildsASampleNotificationForACheckWebhook()
  534. {
  535. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  536. Braintree\WebhookNotification::CHECK,
  537. ""
  538. );
  539. $webhookNotification = Braintree\WebhookNotification::parse(
  540. $sampleNotification["bt_signature"],
  541. $sampleNotification["bt_payload"]
  542. );
  543. $this->assertEquals(Braintree\WebhookNotification::CHECK, $webhookNotification->kind);
  544. }
  545. public function testAccountUpdaterDailyReportWebhook()
  546. {
  547. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  548. Braintree\WebhookNotification::ACCOUNT_UPDATER_DAILY_REPORT,
  549. "my_id"
  550. );
  551. $webhookNotification = Braintree\WebhookNotification::parse(
  552. $sampleNotification['bt_signature'],
  553. $sampleNotification['bt_payload']
  554. );
  555. $this->assertEquals(Braintree\WebhookNotification::ACCOUNT_UPDATER_DAILY_REPORT, $webhookNotification->kind);
  556. $this->assertEquals("link-to-csv-report", $webhookNotification->accountUpdaterDailyReport->reportUrl);
  557. $this->assertEquals(new DateTime("2016-01-14"), $webhookNotification->accountUpdaterDailyReport->reportDate);
  558. }
  559. public function testIdealPaymentCompleteWebhook()
  560. {
  561. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  562. Braintree\WebhookNotification::IDEAL_PAYMENT_COMPLETE,
  563. "my_id"
  564. );
  565. $webhookNotification = Braintree\WebhookNotification::parse(
  566. $sampleNotification['bt_signature'],
  567. $sampleNotification['bt_payload']
  568. );
  569. $this->assertEquals(Braintree\WebhookNotification::IDEAL_PAYMENT_COMPLETE, $webhookNotification->kind);
  570. $idealPayment = $webhookNotification->idealPayment;
  571. $this->assertEquals("my_id", $idealPayment->id);
  572. $this->assertEquals("COMPLETE", $idealPayment->status);
  573. $this->assertEquals("ORDERABC", $idealPayment->orderId);
  574. $this->assertEquals("10.00", $idealPayment->amount);
  575. $this->assertEquals("https://example.com", $idealPayment->approvalUrl);
  576. $this->assertEquals("1234567890", $idealPayment->idealTransactionId);
  577. }
  578. public function testIdealPaymentFailedWebhook()
  579. {
  580. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  581. Braintree\WebhookNotification::IDEAL_PAYMENT_FAILED,
  582. "my_id"
  583. );
  584. $webhookNotification = Braintree\WebhookNotification::parse(
  585. $sampleNotification['bt_signature'],
  586. $sampleNotification['bt_payload']
  587. );
  588. $this->assertEquals(Braintree\WebhookNotification::IDEAL_PAYMENT_FAILED, $webhookNotification->kind);
  589. $idealPayment = $webhookNotification->idealPayment;
  590. $this->assertEquals("my_id", $idealPayment->id);
  591. $this->assertEquals("FAILED", $idealPayment->status);
  592. $this->assertEquals("ORDERABC", $idealPayment->orderId);
  593. $this->assertEquals("10.00", $idealPayment->amount);
  594. $this->assertEquals("https://example.com", $idealPayment->approvalUrl);
  595. $this->assertEquals("1234567890", $idealPayment->idealTransactionId);
  596. }
  597. public function testGrantedPaymentInstrumentUpdateWebhook()
  598. {
  599. $sampleNotification = Braintree\WebhookTesting::sampleNotification(
  600. Braintree\WebhookNotification::GRANTED_PAYMENT_INSTRUMENT_UPDATE,
  601. "my_id"
  602. );
  603. $webhookNotification = Braintree\WebhookNotification::parse(
  604. $sampleNotification['bt_signature'],
  605. $sampleNotification['bt_payload']
  606. );
  607. $this->assertEquals(Braintree\WebhookNotification::GRANTED_PAYMENT_INSTRUMENT_UPDATE, $webhookNotification->kind);
  608. $update = $webhookNotification->grantedPaymentInstrumentUpdate;
  609. $this->assertEquals("vczo7jqrpwrsi2px", $update->grantOwnerMerchantId);
  610. $this->assertEquals("cf0i8wgarszuy6hc", $update->grantRecipientMerchantId);
  611. $this->assertEquals("ee257d98-de40-47e8-96b3-a6954ea7a9a4", $update->paymentMethodNonce->nonce);
  612. $this->assertEquals("abc123z", $update->token);
  613. $this->assertEquals(array("expiration-month", "expiration-year"), $update->updatedFields);
  614. }
  615. }