PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/CORP-RESELLER/shopping-cart-lite
PHP | 323 lines | 194 code | 25 blank | 104 comment | 15 complexity | ece0e255d4363e7834f58939cbb86acc MD5 | raw file
  1. <?php
  2. /**
  3. * Braintree Transparent Redirect module
  4. * Static class providing methods to build Transparent Redirect urls
  5. *
  6. * The TransparentRedirect module provides methods to build the tr_data param
  7. * that must be submitted when using the transparent redirect API.
  8. * For more information
  9. * about transparent redirect, see (TODO).
  10. *
  11. * You must provide a redirectUrl to which the gateway will redirect the
  12. * user the action is complete.
  13. *
  14. * <code>
  15. * $trData = Braintree_TransparentRedirect::createCustomerData(array(
  16. * 'redirectUrl => 'http://example.com/redirect_back_to_merchant_site',
  17. * ));
  18. * </code>
  19. *
  20. * In addition to the redirectUrl, any data that needs to be protected
  21. * from user tampering should be included in the trData.
  22. * For example, to prevent the user from tampering with the transaction
  23. * amount, include the amount in the trData.
  24. *
  25. * <code>
  26. * $trData = Braintree_TransparentRedirect::transactionData(array(
  27. * 'redirectUrl' => 'http://example.com/complete_transaction',
  28. * 'transaction' => array('amount' => '100.00'),
  29. * ));
  30. *
  31. * </code>
  32. *
  33. * @package Braintree
  34. * @category Resources
  35. * @copyright 2014 Braintree, a division of PayPal, Inc.
  36. */
  37. class Braintree_TransparentRedirect
  38. {
  39. // Request Kinds
  40. const CREATE_TRANSACTION = 'create_transaction';
  41. const CREATE_PAYMENT_METHOD = 'create_payment_method';
  42. const UPDATE_PAYMENT_METHOD = 'update_payment_method';
  43. const CREATE_CUSTOMER = 'create_customer';
  44. const UPDATE_CUSTOMER = 'update_customer';
  45. /**
  46. *
  47. * @ignore
  48. */
  49. private static $_transparentRedirectKeys = 'redirectUrl';
  50. private static $_createCustomerSignature;
  51. private static $_updateCustomerSignature;
  52. private static $_transactionSignature;
  53. private static $_createCreditCardSignature;
  54. private static $_updateCreditCardSignature;
  55. /**
  56. * @ignore
  57. * don't permit an explicit call of the constructor!
  58. * (like $t = new Braintree_TransparentRedirect())
  59. */
  60. protected function __construct()
  61. {
  62. }
  63. /**
  64. * create signatures for different call types
  65. * @ignore
  66. */
  67. public static function init()
  68. {
  69. self::$_createCustomerSignature = array(
  70. self::$_transparentRedirectKeys,
  71. array('customer' => Braintree_Customer::createSignature()),
  72. );
  73. self::$_updateCustomerSignature = array(
  74. self::$_transparentRedirectKeys,
  75. 'customerId',
  76. array('customer' => Braintree_Customer::updateSignature()),
  77. );
  78. self::$_transactionSignature = array(
  79. self::$_transparentRedirectKeys,
  80. array('transaction' => Braintree_Transaction::createSignature()),
  81. );
  82. self::$_createCreditCardSignature = array(
  83. self::$_transparentRedirectKeys,
  84. array('creditCard' => Braintree_CreditCard::createSignature()),
  85. );
  86. self::$_updateCreditCardSignature = array(
  87. self::$_transparentRedirectKeys,
  88. 'paymentMethodToken',
  89. array('creditCard' => Braintree_CreditCard::updateSignature()),
  90. );
  91. }
  92. public static function confirm($queryString)
  93. {
  94. $params = Braintree_TransparentRedirect::parseAndValidateQueryString(
  95. $queryString
  96. );
  97. $confirmationKlasses = array(
  98. Braintree_TransparentRedirect::CREATE_TRANSACTION => 'Braintree_Transaction',
  99. Braintree_TransparentRedirect::CREATE_CUSTOMER => 'Braintree_Customer',
  100. Braintree_TransparentRedirect::UPDATE_CUSTOMER => 'Braintree_Customer',
  101. Braintree_TransparentRedirect::CREATE_PAYMENT_METHOD => 'Braintree_CreditCard',
  102. Braintree_TransparentRedirect::UPDATE_PAYMENT_METHOD => 'Braintree_CreditCard'
  103. );
  104. return call_user_func(array($confirmationKlasses[$params["kind"]], '_doCreate'),
  105. '/transparent_redirect_requests/' . $params['id'] . '/confirm',
  106. array()
  107. );
  108. }
  109. /**
  110. * returns the trData string for creating a credit card,
  111. * @param array $params
  112. * @return string
  113. */
  114. public static function createCreditCardData($params)
  115. {
  116. Braintree_Util::verifyKeys(
  117. self::$_createCreditCardSignature,
  118. $params
  119. );
  120. $params["kind"] = Braintree_TransparentRedirect::CREATE_PAYMENT_METHOD;
  121. return self::_data($params);
  122. }
  123. /**
  124. * returns the trData string for creating a customer.
  125. * @param array $params
  126. * @return string
  127. */
  128. public static function createCustomerData($params)
  129. {
  130. Braintree_Util::verifyKeys(
  131. self::$_createCustomerSignature,
  132. $params
  133. );
  134. $params["kind"] = Braintree_TransparentRedirect::CREATE_CUSTOMER;
  135. return self::_data($params);
  136. }
  137. public static function url()
  138. {
  139. return Braintree_Configuration::merchantUrl() . "/transparent_redirect_requests";
  140. }
  141. /**
  142. * returns the trData string for creating a transaction
  143. * @param array $params
  144. * @return string
  145. */
  146. public static function transactionData($params)
  147. {
  148. Braintree_Util::verifyKeys(
  149. self::$_transactionSignature,
  150. $params
  151. );
  152. $params["kind"] = Braintree_TransparentRedirect::CREATE_TRANSACTION;
  153. $transactionType = isset($params['transaction']['type']) ?
  154. $params['transaction']['type'] :
  155. null;
  156. if ($transactionType != Braintree_Transaction::SALE && $transactionType != Braintree_Transaction::CREDIT) {
  157. throw new InvalidArgumentException(
  158. 'expected transaction[type] of sale or credit, was: ' .
  159. $transactionType
  160. );
  161. }
  162. return self::_data($params);
  163. }
  164. /**
  165. * Returns the trData string for updating a credit card.
  166. *
  167. * The paymentMethodToken of the credit card to update is required.
  168. *
  169. * <code>
  170. * $trData = Braintree_TransparentRedirect::updateCreditCardData(array(
  171. * 'redirectUrl' => 'http://example.com/redirect_here',
  172. * 'paymentMethodToken' => 'token123',
  173. * ));
  174. * </code>
  175. *
  176. * @param array $params
  177. * @return string
  178. */
  179. public static function updateCreditCardData($params)
  180. {
  181. Braintree_Util::verifyKeys(
  182. self::$_updateCreditCardSignature,
  183. $params
  184. );
  185. if (!isset($params['paymentMethodToken'])) {
  186. throw new InvalidArgumentException(
  187. 'expected params to contain paymentMethodToken.'
  188. );
  189. }
  190. $params["kind"] = Braintree_TransparentRedirect::UPDATE_PAYMENT_METHOD;
  191. return self::_data($params);
  192. }
  193. /**
  194. * Returns the trData string for updating a customer.
  195. *
  196. * The customerId of the customer to update is required.
  197. *
  198. * <code>
  199. * $trData = Braintree_TransparentRedirect::updateCustomerData(array(
  200. * 'redirectUrl' => 'http://example.com/redirect_here',
  201. * 'customerId' => 'customer123',
  202. * ));
  203. * </code>
  204. *
  205. * @param array $params
  206. * @return string
  207. */
  208. public static function updateCustomerData($params)
  209. {
  210. Braintree_Util::verifyKeys(
  211. self::$_updateCustomerSignature,
  212. $params
  213. );
  214. if (!isset($params['customerId'])) {
  215. throw new InvalidArgumentException(
  216. 'expected params to contain customerId of customer to update'
  217. );
  218. }
  219. $params["kind"] = Braintree_TransparentRedirect::UPDATE_CUSTOMER;
  220. return self::_data($params);
  221. }
  222. public static function parseAndValidateQueryString($queryString)
  223. {
  224. // parse the params into an array
  225. parse_str($queryString, $params);
  226. // remove the hash
  227. $queryStringWithoutHash = null;
  228. if(preg_match('/^(.*)&hash=[a-f0-9]+$/', $queryString, $match)) {
  229. $queryStringWithoutHash = $match[1];
  230. }
  231. if($params['http_status'] != '200') {
  232. $message = null;
  233. if(array_key_exists('bt_message', $params)) {
  234. $message = $params['bt_message'];
  235. }
  236. Braintree_Util::throwStatusCodeException($params['http_status'], $message);
  237. }
  238. // recreate the hash and compare it
  239. if(self::_hash($queryStringWithoutHash) == $params['hash']) {
  240. return $params;
  241. } else {
  242. throw new Braintree_Exception_ForgedQueryString();
  243. }
  244. }
  245. /**
  246. *
  247. * @ignore
  248. */
  249. private static function _data($params)
  250. {
  251. if (!isset($params['redirectUrl'])) {
  252. throw new InvalidArgumentException(
  253. 'expected params to contain redirectUrl'
  254. );
  255. }
  256. $params = self::_underscoreKeys($params);
  257. $now = new DateTime('now', new DateTimeZone('UTC'));
  258. $trDataParams = array_merge($params,
  259. array(
  260. 'api_version' => Braintree_Configuration::API_VERSION,
  261. 'public_key' => Braintree_Configuration::publicKey(),
  262. 'time' => $now->format('YmdHis'),
  263. )
  264. );
  265. ksort($trDataParams);
  266. $urlEncodedData = http_build_query($trDataParams, null, "&");
  267. $signatureService = new Braintree_SignatureService(
  268. Braintree_Configuration::privateKey(),
  269. "Braintree_Digest::hexDigestSha1"
  270. );
  271. return $signatureService->sign($urlEncodedData);
  272. }
  273. private static function _underscoreKeys($array)
  274. {
  275. foreach($array as $key=>$value)
  276. {
  277. $newKey = Braintree_Util::camelCaseToDelimiter($key, '_');
  278. unset($array[$key]);
  279. if (is_array($value))
  280. {
  281. $array[$newKey] = self::_underscoreKeys($value);
  282. }
  283. else
  284. {
  285. $array[$newKey] = $value;
  286. }
  287. }
  288. return $array;
  289. }
  290. /**
  291. * @ignore
  292. */
  293. private static function _hash($string)
  294. {
  295. return Braintree_Digest::hexDigestSha1(Braintree_Configuration::privateKey(), $string);
  296. }
  297. }
  298. Braintree_TransparentRedirect::init();