PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/braintree/braintree_php/lib/Braintree/WebhookTesting.php

https://gitlab.com/cividesk-civicrm/com.cividesk.payment.braintree
PHP | 391 lines | 369 code | 22 blank | 0 comment | 1 complexity | 4fa3fb7c1c4591a30cb073cbd4c2ec5a MD5 | raw file
  1. <?php
  2. namespace Braintree;
  3. class WebhookTesting
  4. {
  5. public static function sampleNotification($kind, $id)
  6. {
  7. $payload = base64_encode(self::_sampleXml($kind, $id)) . "\n";
  8. $signature = Configuration::publicKey() . "|" . Digest::hexDigestSha1(Configuration::privateKey(), $payload);
  9. return [
  10. 'bt_signature' => $signature,
  11. 'bt_payload' => $payload
  12. ];
  13. }
  14. private static function _sampleXml($kind, $id)
  15. {
  16. switch ($kind) {
  17. case WebhookNotification::SUB_MERCHANT_ACCOUNT_APPROVED:
  18. $subjectXml = self::_merchantAccountApprovedSampleXml($id);
  19. break;
  20. case WebhookNotification::SUB_MERCHANT_ACCOUNT_DECLINED:
  21. $subjectXml = self::_merchantAccountDeclinedSampleXml($id);
  22. break;
  23. case WebhookNotification::TRANSACTION_DISBURSED:
  24. $subjectXml = self::_transactionDisbursedSampleXml($id);
  25. break;
  26. case WebhookNotification::TRANSACTION_SETTLED:
  27. $subjectXml = self::_transactionSettledSampleXml($id);
  28. break;
  29. case WebhookNotification::TRANSACTION_SETTLEMENT_DECLINED:
  30. $subjectXml = self::_transactionSettlementDeclinedSampleXml($id);
  31. break;
  32. case WebhookNotification::DISBURSEMENT_EXCEPTION:
  33. $subjectXml = self::_disbursementExceptionSampleXml($id);
  34. break;
  35. case WebhookNotification::DISBURSEMENT:
  36. $subjectXml = self::_disbursementSampleXml($id);
  37. break;
  38. case WebhookNotification::PARTNER_MERCHANT_CONNECTED:
  39. $subjectXml = self::_partnerMerchantConnectedSampleXml($id);
  40. break;
  41. case WebhookNotification::PARTNER_MERCHANT_DISCONNECTED:
  42. $subjectXml = self::_partnerMerchantDisconnectedSampleXml($id);
  43. break;
  44. case WebhookNotification::PARTNER_MERCHANT_DECLINED:
  45. $subjectXml = self::_partnerMerchantDeclinedSampleXml($id);
  46. break;
  47. case WebhookNotification::DISPUTE_OPENED:
  48. $subjectXml = self::_disputeOpenedSampleXml($id);
  49. break;
  50. case WebhookNotification::DISPUTE_LOST:
  51. $subjectXml = self::_disputeLostSampleXml($id);
  52. break;
  53. case WebhookNotification::DISPUTE_WON:
  54. $subjectXml = self::_disputeWonSampleXml($id);
  55. break;
  56. case WebhookNotification::SUBSCRIPTION_CHARGED_SUCCESSFULLY:
  57. $subjectXml = self::_subscriptionChargedSuccessfullySampleXml($id);
  58. break;
  59. case WebhookNotification::CHECK:
  60. $subjectXml = self::_checkSampleXml();
  61. break;
  62. case WebhookNotification::ACCOUNT_UPDATER_DAILY_REPORT:
  63. $subjectXml = self::_accountUpdaterDailyReportSampleXml($id);
  64. break;
  65. default:
  66. $subjectXml = self::_subscriptionSampleXml($id);
  67. break;
  68. }
  69. $timestamp = self::_timestamp();
  70. return "
  71. <notification>
  72. <timestamp type=\"datetime\">{$timestamp}</timestamp>
  73. <kind>{$kind}</kind>
  74. <subject>{$subjectXml}</subject>
  75. </notification>
  76. ";
  77. }
  78. private static function _merchantAccountApprovedSampleXml($id)
  79. {
  80. return "
  81. <merchant_account>
  82. <id>{$id}</id>
  83. <master_merchant_account>
  84. <id>master_ma_for_{$id}</id>
  85. <status>active</status>
  86. </master_merchant_account>
  87. <status>active</status>
  88. </merchant_account>
  89. ";
  90. }
  91. private static function _merchantAccountDeclinedSampleXml($id)
  92. {
  93. return "
  94. <api-error-response>
  95. <message>Credit score is too low</message>
  96. <errors>
  97. <errors type=\"array\"/>
  98. <merchant-account>
  99. <errors type=\"array\">
  100. <error>
  101. <code>82621</code>
  102. <message>Credit score is too low</message>
  103. <attribute type=\"symbol\">base</attribute>
  104. </error>
  105. </errors>
  106. </merchant-account>
  107. </errors>
  108. <merchant-account>
  109. <id>{$id}</id>
  110. <status>suspended</status>
  111. <master-merchant-account>
  112. <id>master_ma_for_{$id}</id>
  113. <status>suspended</status>
  114. </master-merchant-account>
  115. </merchant-account>
  116. </api-error-response>
  117. ";
  118. }
  119. private static function _transactionDisbursedSampleXml($id)
  120. {
  121. return "
  122. <transaction>
  123. <id>${id}</id>
  124. <amount>100</amount>
  125. <disbursement-details>
  126. <disbursement-date type=\"date\">2013-07-09</disbursement-date>
  127. </disbursement-details>
  128. </transaction>
  129. ";
  130. }
  131. private static function _transactionSettledSampleXml($id)
  132. {
  133. return "
  134. <transaction>
  135. <id>${id}</id>
  136. <status>settled</status>
  137. <type>sale</type>
  138. <currency-iso-code>USD</currency-iso-code>
  139. <amount>100.00</amount>
  140. <merchant-account-id>ogaotkivejpfayqfeaimuktty</merchant-account-id>
  141. <payment-instrument-type>us_bank_account</payment-instrument-type>
  142. <us-bank-account>
  143. <routing-number>123456789</routing-number>
  144. <last-4>1234</last-4>
  145. <account-type>checking</account-type>
  146. <account-description>PayPal Checking - 1234</account-description>
  147. <account-holder-name>Dan Schulman</account-holder-name>
  148. </us-bank-account>
  149. </transaction>
  150. ";
  151. }
  152. private static function _transactionSettlementDeclinedSampleXml($id)
  153. {
  154. return "
  155. <transaction>
  156. <id>${id}</id>
  157. <status>settlement_declined</status>
  158. <type>sale</type>
  159. <currency-iso-code>USD</currency-iso-code>
  160. <amount>100.00</amount>
  161. <merchant-account-id>ogaotkivejpfayqfeaimuktty</merchant-account-id>
  162. <payment-instrument-type>us_bank_account</payment-instrument-type>
  163. <us-bank-account>
  164. <routing-number>123456789</routing-number>
  165. <last-4>1234</last-4>
  166. <account-type>checking</account-type>
  167. <account-description>PayPal Checking - 1234</account-description>
  168. <account-holder-name>Dan Schulman</account-holder-name>
  169. </us-bank-account>
  170. </transaction>
  171. ";
  172. }
  173. private static function _disbursementExceptionSampleXml($id)
  174. {
  175. return "
  176. <disbursement>
  177. <id>${id}</id>
  178. <transaction-ids type=\"array\">
  179. <item>asdfg</item>
  180. <item>qwert</item>
  181. </transaction-ids>
  182. <success type=\"boolean\">false</success>
  183. <retry type=\"boolean\">false</retry>
  184. <merchant-account>
  185. <id>merchant_account_token</id>
  186. <currency-iso-code>USD</currency-iso-code>
  187. <sub-merchant-account type=\"boolean\">false</sub-merchant-account>
  188. <status>active</status>
  189. </merchant-account>
  190. <amount>100.00</amount>
  191. <disbursement-date type=\"date\">2014-02-10</disbursement-date>
  192. <exception-message>bank_rejected</exception-message>
  193. <follow-up-action>update_funding_information</follow-up-action>
  194. </disbursement>
  195. ";
  196. }
  197. private static function _disbursementSampleXml($id)
  198. {
  199. return "
  200. <disbursement>
  201. <id>${id}</id>
  202. <transaction-ids type=\"array\">
  203. <item>asdfg</item>
  204. <item>qwert</item>
  205. </transaction-ids>
  206. <success type=\"boolean\">true</success>
  207. <retry type=\"boolean\">false</retry>
  208. <merchant-account>
  209. <id>merchant_account_token</id>
  210. <currency-iso-code>USD</currency-iso-code>
  211. <sub-merchant-account type=\"boolean\">false</sub-merchant-account>
  212. <status>active</status>
  213. </merchant-account>
  214. <amount>100.00</amount>
  215. <disbursement-date type=\"date\">2014-02-10</disbursement-date>
  216. <exception-message nil=\"true\"/>
  217. <follow-up-action nil=\"true\"/>
  218. </disbursement>
  219. ";
  220. }
  221. private static function _disputeOpenedSampleXml($id)
  222. {
  223. return "
  224. <dispute>
  225. <amount>250.00</amount>
  226. <currency-iso-code>USD</currency-iso-code>
  227. <received-date type=\"date\">2014-03-01</received-date>
  228. <reply-by-date type=\"date\">2014-03-21</reply-by-date>
  229. <kind>chargeback</kind>
  230. <status>open</status>
  231. <reason>fraud</reason>
  232. <id>${id}</id>
  233. <transaction>
  234. <id>${id}</id>
  235. <amount>250.00</amount>
  236. </transaction>
  237. <date-opened type=\"date\">2014-03-21</date-opened>
  238. </dispute>
  239. ";
  240. }
  241. private static function _disputeLostSampleXml($id)
  242. {
  243. return "
  244. <dispute>
  245. <amount>250.00</amount>
  246. <currency-iso-code>USD</currency-iso-code>
  247. <received-date type=\"date\">2014-03-01</received-date>
  248. <reply-by-date type=\"date\">2014-03-21</reply-by-date>
  249. <kind>chargeback</kind>
  250. <status>lost</status>
  251. <reason>fraud</reason>
  252. <id>${id}</id>
  253. <transaction>
  254. <id>${id}</id>
  255. <amount>250.00</amount>
  256. <next_billing-date type=\"date\">2020-02-10</next_billing-date>
  257. </transaction>
  258. <date-opened type=\"date\">2014-03-21</date-opened>
  259. </dispute>
  260. ";
  261. }
  262. private static function _disputeWonSampleXml($id)
  263. {
  264. return "
  265. <dispute>
  266. <amount>250.00</amount>
  267. <currency-iso-code>USD</currency-iso-code>
  268. <received-date type=\"date\">2014-03-01</received-date>
  269. <reply-by-date type=\"date\">2014-03-21</reply-by-date>
  270. <kind>chargeback</kind>
  271. <status>won</status>
  272. <reason>fraud</reason>
  273. <id>${id}</id>
  274. <transaction>
  275. <id>${id}</id>
  276. <amount>250.00</amount>
  277. </transaction>
  278. <date-opened type=\"date\">2014-03-21</date-opened>
  279. <date-won type=\"date\">2014-03-22</date-won>
  280. </dispute>
  281. ";
  282. }
  283. private static function _subscriptionSampleXml($id)
  284. {
  285. return "
  286. <subscription>
  287. <id>{$id}</id>
  288. <transactions type=\"array\">
  289. </transactions>
  290. <add_ons type=\"array\">
  291. </add_ons>
  292. <discounts type=\"array\">
  293. </discounts>
  294. </subscription>
  295. ";
  296. }
  297. private static function _subscriptionChargedSuccessfullySampleXml($id)
  298. {
  299. return "
  300. <subscription>
  301. <id>{$id}</id>
  302. <billing-period-start-date type=\"date\">2016-03-21</billing-period-start-date>
  303. <billing-period-end-date type=\"date\">2017-03-31</billing-period-end-date>
  304. <transactions type=\"array\">
  305. <transaction>
  306. <status>submitted_for_settlement</status>
  307. <amount>49.99</amount>
  308. </transaction>
  309. </transactions>
  310. <add_ons type=\"array\">
  311. </add_ons>
  312. <discounts type=\"array\">
  313. </discounts>
  314. </subscription>
  315. ";
  316. }
  317. private static function _checkSampleXml()
  318. {
  319. return "
  320. <check type=\"boolean\">true</check>
  321. ";
  322. }
  323. private static function _partnerMerchantConnectedSampleXml($id)
  324. {
  325. return "
  326. <partner-merchant>
  327. <merchant-public-id>public_id</merchant-public-id>
  328. <public-key>public_key</public-key>
  329. <private-key>private_key</private-key>
  330. <partner-merchant-id>abc123</partner-merchant-id>
  331. <client-side-encryption-key>cse_key</client-side-encryption-key>
  332. </partner-merchant>
  333. ";
  334. }
  335. private static function _partnerMerchantDisconnectedSampleXml($id)
  336. {
  337. return "
  338. <partner-merchant>
  339. <partner-merchant-id>abc123</partner-merchant-id>
  340. </partner-merchant>
  341. ";
  342. }
  343. private static function _partnerMerchantDeclinedSampleXml($id)
  344. {
  345. return "
  346. <partner-merchant>
  347. <partner-merchant-id>abc123</partner-merchant-id>
  348. </partner-merchant>
  349. ";
  350. }
  351. private static function _accountUpdaterDailyReportSampleXml($id)
  352. {
  353. return "
  354. <account-updater-daily-report>
  355. <report-date type=\"date\">2016-01-14</report-date>
  356. <report-url>link-to-csv-report</report-url>
  357. </account-updater-daily-report>
  358. ";
  359. }
  360. private static function _timestamp()
  361. {
  362. $originalZone = date_default_timezone_get();
  363. date_default_timezone_set('UTC');
  364. $timestamp = strftime('%Y-%m-%dT%TZ');
  365. date_default_timezone_set($originalZone);
  366. return $timestamp;
  367. }
  368. }
  369. class_alias('Braintree\WebhookTesting', 'Braintree_WebhookTesting');