Helper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace Test;
  3. require_once __DIR__ . '/Setup.php';
  4. use DateTime;
  5. use DateTimeZone;
  6. use Braintree;
  7. class Helper
  8. {
  9. public static $valid_nonce_characters = 'bcdfghjkmnpqrstvwxyz23456789';
  10. public static function testMerchantConfig()
  11. {
  12. Braintree\Configuration::reset();
  13. Braintree\Configuration::environment('development');
  14. Braintree\Configuration::merchantId('test_merchant_id');
  15. Braintree\Configuration::publicKey('test_public_key');
  16. Braintree\Configuration::privateKey('test_private_key');
  17. }
  18. public static function integrationMerchantGateway()
  19. {
  20. return new Braintree\Gateway([
  21. 'environment' => 'development',
  22. 'merchantId' => 'integration_merchant_id',
  23. 'publicKey' => 'integration_public_key',
  24. 'privateKey' => 'integration_private_key'
  25. ]);
  26. }
  27. public static function advancedFraudIntegrationMerchantGateway()
  28. {
  29. return new Braintree\Gateway([
  30. 'environment' => 'development',
  31. 'merchantId' => 'advanced_fraud_integration_merchant_id',
  32. 'publicKey' => 'advanced_fraud_integration_public_key',
  33. 'privateKey' => 'advanced_fraud_integration_private_key'
  34. ]);
  35. }
  36. public static function defaultMerchantAccountId()
  37. {
  38. return 'sandbox_credit_card';
  39. }
  40. public static function nonDefaultMerchantAccountId()
  41. {
  42. return 'sandbox_credit_card_non_default';
  43. }
  44. public static function nonDefaultSubMerchantAccountId()
  45. {
  46. return 'sandbox_sub_merchant_account';
  47. }
  48. public static function threeDSecureMerchantAccountId()
  49. {
  50. return 'three_d_secure_merchant_account';
  51. }
  52. public static function fakeAmexDirectMerchantAccountId()
  53. {
  54. return 'fake_amex_direct_usd';
  55. }
  56. public static function fakeVenmoAccountMerchantAccountId()
  57. {
  58. return 'fake_first_data_venmo_account';
  59. }
  60. public static function usBankMerchantAccount() {
  61. return 'us_bank_merchant_account';
  62. }
  63. public static function anotherUsBankMerchantAccount() {
  64. return 'another_us_bank_merchant_account';
  65. }
  66. public static function createViaTr($regularParams, $trParams)
  67. {
  68. $trData = Braintree\TransparentRedirect::transactionData(
  69. array_merge($trParams, ["redirectUrl" => "http://www.example.com"])
  70. );
  71. return self::submitTrRequest(
  72. Braintree\TransparentRedirect::url(),
  73. $regularParams,
  74. $trData
  75. );
  76. }
  77. public static function submitTrRequest($url, $regularParams, $trData)
  78. {
  79. $curl = curl_init();
  80. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
  81. curl_setopt($curl, CURLOPT_URL, $url);
  82. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
  83. curl_setopt($curl, CURLOPT_HEADER, true);
  84. // curl_setopt($curl, CURLOPT_VERBOSE, true);
  85. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array_merge($regularParams, ['tr_data' => $trData])));
  86. curl_setopt($curl, CURLOPT_HTTPHEADER, [
  87. 'Content-Type: application/x-www-form-urlencoded'
  88. ]);
  89. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  90. $response = curl_exec($curl);
  91. curl_close($curl);
  92. preg_match('/Location: .*\?(.*)/i', $response, $match);
  93. return trim($match[1]);
  94. }
  95. public static function suppressDeprecationWarnings()
  96. {
  97. set_error_handler("Test\Helper::_errorHandler", E_USER_NOTICE);
  98. }
  99. public static function _errorHandler($errno, $errstr, $errfile, $errline)
  100. {
  101. if (preg_match('/^DEPRECATED/', $errstr) == 0) {
  102. trigger_error('Unknown error received: ' . $errstr, E_USER_ERROR);
  103. }
  104. }
  105. public static function includes($collection, $targetItem)
  106. {
  107. foreach ($collection AS $item) {
  108. if ($item->id == $targetItem->id) {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. public static function assertPrintable($object)
  115. {
  116. " " . $object;
  117. }
  118. public static function escrow($transactionId)
  119. {
  120. $http = new Braintree\Http(Braintree\Configuration::$global);
  121. $path = Braintree\Configuration::$global->merchantPath() . '/transactions/' . $transactionId . '/escrow';
  122. $http->put($path);
  123. }
  124. public static function create3DSVerification($merchantAccountId, $params)
  125. {
  126. $http = new Braintree\Http(Braintree\Configuration::$global);
  127. $path = Braintree\Configuration::$global->merchantPath() . '/three_d_secure/create_verification/' . $merchantAccountId;
  128. $response = $http->post($path, ['threeDSecureVerification' => $params]);
  129. return $response['threeDSecureVerification']['threeDSecureToken'];
  130. }
  131. public static function generate3DSNonce($params)
  132. {
  133. $http = new Braintree\Http(Braintree\Configuration::$global);
  134. $path = Braintree\Configuration::$global->merchantPath() . '/three_d_secure/create_nonce/' . self::threeDSecureMerchantAccountId();
  135. $response = $http->post($path, $params);
  136. return $response['paymentMethodNonce']['nonce'];
  137. }
  138. public static function nowInEastern()
  139. {
  140. $eastern = new DateTimeZone('America/New_York');
  141. $now = new DateTime('now', $eastern);
  142. return $now->format('Y-m-d');
  143. }
  144. public static function decodedClientToken($params=[]) {
  145. $encodedClientToken = Braintree\ClientToken::generate($params);
  146. return base64_decode($encodedClientToken);
  147. }
  148. public static function generateValidUsBankAccountNonce($accountNumber = '567891234') {
  149. $client_token = json_decode(Helper::decodedClientToken(), true);
  150. $url = $client_token['braintree_api']['url'] . '/tokens';
  151. $token = $client_token['braintree_api']['access_token'];
  152. $curl = curl_init();
  153. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  154. curl_setopt($curl, CURLOPT_URL, $url);
  155. $headers[] = 'Content-Type: application/json';
  156. $headers[] = 'Braintree-Version: 2016-10-07';
  157. $headers[] = 'Authorization: Bearer ' . $token;
  158. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  159. $requestBody = [
  160. 'type' => 'us_bank_account',
  161. 'billing_address' => [
  162. 'street_address' => '123 Ave',
  163. 'region' => 'CA',
  164. 'locality' => 'San Francisco',
  165. 'postal_code' => '94112'
  166. ],
  167. 'account_type' => 'checking',
  168. 'ownership_type' => 'personal',
  169. 'routing_number' => '021000021',
  170. 'account_number' => $accountNumber,
  171. 'first_name' => 'Dan',
  172. 'last_name' => 'Schulman',
  173. 'ach_mandate' => [
  174. 'text' => 'cl mandate text'
  175. ]
  176. ];
  177. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($requestBody));
  178. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  179. $response = curl_exec($curl);
  180. $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  181. $error_code = curl_errno($curl);
  182. curl_close($curl);
  183. $jsonResponse = json_decode($response, true);
  184. return $jsonResponse['data']['id'];
  185. }
  186. public static function generatePlaidUsBankAccountNonce() {
  187. $client_token = json_decode(Helper::decodedClientToken(), true);
  188. $url = $client_token['braintree_api']['url'] . '/tokens';
  189. $token = $client_token['braintree_api']['access_token'];
  190. $curl = curl_init();
  191. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  192. curl_setopt($curl, CURLOPT_URL, $url);
  193. $headers[] = 'Content-Type: application/json';
  194. $headers[] = 'Braintree-Version: 2016-10-07';
  195. $headers[] = 'Authorization: Bearer ' . $token;
  196. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  197. $requestBody = [
  198. 'type' => 'plaid_public_token',
  199. 'public_token' => 'good',
  200. 'account_id' => 'plaid_account_id',
  201. 'billing_address' => [
  202. 'street_address' => '123 Ave',
  203. 'region' => 'CA',
  204. 'locality' => 'San Francisco',
  205. 'postal_code' => '94112'
  206. ],
  207. 'ownership_type' => 'personal',
  208. 'first_name' => 'Dan',
  209. 'last_name' => 'Schulman',
  210. 'ach_mandate' => [
  211. 'text' => 'cl mandate text'
  212. ]
  213. ];
  214. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($requestBody));
  215. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  216. $response = curl_exec($curl);
  217. $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  218. $error_code = curl_errno($curl);
  219. curl_close($curl);
  220. $jsonResponse = json_decode($response, true);
  221. return $jsonResponse['data']['id'];
  222. }
  223. public static function generateInvalidUsBankAccountNonce() {
  224. $valid_characters = str_split(self::$valid_nonce_characters);
  225. $nonce = 'tokenusbankacct';
  226. for($i=0; $i<4; $i++) {
  227. $nonce = $nonce . '_';
  228. for($j=0; $j<6; $j++) {
  229. $t = rand(0, sizeof($valid_characters)-1);
  230. $nonce = $nonce . $valid_characters[$t];
  231. }
  232. }
  233. return $nonce . "_xxx";
  234. }
  235. public static function generateValidIdealPaymentId($amount = null) {
  236. if (null === $amount) {
  237. $amount = '100.00';
  238. }
  239. $client_token = json_decode(Helper::decodedClientToken([
  240. 'merchantAccountId' => 'ideal_merchant_account'
  241. ]), true);
  242. $client = new Integration\HttpClientApi(Braintree\Configuration::$global);
  243. $configuration = $client->get_configuration([
  244. "authorization_fingerprint" => $client_token['authorizationFingerprint'],
  245. ]);
  246. $url = $client_token['braintree_api']['url'] . '/ideal-payments';
  247. $token = $client_token['braintree_api']['access_token'];
  248. $curl = curl_init();
  249. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  250. curl_setopt($curl, CURLOPT_URL, $url);
  251. $headers[] = 'Content-Type: application/json';
  252. $headers[] = 'Braintree-Version: 2015-11-01';
  253. $headers[] = 'Authorization: Bearer ' . $token;
  254. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  255. $requestBody = [
  256. 'issuer' => 'RABONL2U',
  257. 'order_id' => 'ABC123',
  258. 'amount' => $amount,
  259. 'currency' => 'EUR',
  260. 'redirect_url' => 'https://braintree-api.com',
  261. 'route_id' => $configuration->ideal->routeId,
  262. ];
  263. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($requestBody));
  264. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  265. $response = curl_exec($curl);
  266. $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  267. $error_code = curl_errno($curl);
  268. curl_close($curl);
  269. $jsonResponse = json_decode($response, true);
  270. return $jsonResponse['data']['id'];
  271. }
  272. public static function generateInvalidIdealPaymentId() {
  273. $valid_characters = str_split(self::$valid_nonce_characters);
  274. $ideal_payment_id = 'idealpayment';
  275. for($i=0; $i<4; $i++) {
  276. $ideal_payment_id = $ideal_payment_id . '_';
  277. for($j=0; $j<6; $j++) {
  278. $t = rand(0, sizeof($valid_characters)-1);
  279. $ideal_payment_id = $ideal_payment_id . $valid_characters[$t];
  280. }
  281. }
  282. return $ideal_payment_id . "_xxx";
  283. }
  284. }