BraintreeTransparentRedirectGateway

Braintree Transparent Redirect Gateway module Static class providing methods to build Transparent Redirect urls.

Defined (1)

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

/includes/lib/Braintree/lib/Braintree/TransparentRedirectGateway.php  
  1. class TransparentRedirectGateway 
  2. private $_gateway; 
  3. private $_config; 
  4.  
  5. public function __construct($gateway) 
  6. $this->_gateway = $gateway; 
  7. $this->_config = $gateway->config; 
  8. $this->_config->assertHasAccessTokenOrKeys(); 
  9.  
  10. /** 
  11. * @ignore 
  12. */ 
  13. private static $_transparentRedirectKeys = 'redirectUrl'; 
  14. private static $_createCustomerSignature; 
  15. private static $_updateCustomerSignature; 
  16. private static $_transactionSignature; 
  17. private static $_createCreditCardSignature; 
  18. private static $_updateCreditCardSignature; 
  19.  
  20. /** 
  21. * create signatures for different call types 
  22. * @ignore 
  23. */ 
  24. public static function init() 
  25.  
  26. self::$_createCustomerSignature = [ 
  27. self::$_transparentRedirectKeys,  
  28. ['customer' => CustomerGateway::createSignature()],  
  29. ]; 
  30. self::$_updateCustomerSignature = [ 
  31. self::$_transparentRedirectKeys,  
  32. 'customerId',  
  33. ['customer' => CustomerGateway::updateSignature()],  
  34. ]; 
  35. self::$_transactionSignature = [ 
  36. self::$_transparentRedirectKeys,  
  37. ['transaction' => TransactionGateway::createSignature()],  
  38. ]; 
  39. self::$_createCreditCardSignature = [ 
  40. self::$_transparentRedirectKeys,  
  41. ['creditCard' => CreditCardGateway::createSignature()],  
  42. ]; 
  43. self::$_updateCreditCardSignature = [ 
  44. self::$_transparentRedirectKeys,  
  45. 'paymentMethodToken',  
  46. ['creditCard' => CreditCardGateway::updateSignature()],  
  47. ]; 
  48.  
  49. public function confirm($queryString) 
  50. $params = TransparentRedirect::parseAndValidateQueryString( 
  51. $queryString 
  52. ); 
  53. $confirmationKlasses = [ 
  54. TransparentRedirect::CREATE_TRANSACTION => 'Braintree\TransactionGateway',  
  55. TransparentRedirect::CREATE_CUSTOMER => 'Braintree\CustomerGateway',  
  56. TransparentRedirect::UPDATE_CUSTOMER => 'Braintree\CustomerGateway',  
  57. TransparentRedirect::CREATE_PAYMENT_METHOD => 'Braintree\CreditCardGateway',  
  58. TransparentRedirect::UPDATE_PAYMENT_METHOD => 'Braintree\CreditCardGateway',  
  59. ]; 
  60. $confirmationGateway = new $confirmationKlasses[$params["kind"]]($this->_gateway); 
  61. return $confirmationGateway->_doCreate('/transparent_redirect_requests/' . $params['id'] . '/confirm', []); 
  62.  
  63. /** 
  64. * returns the trData string for creating a credit card,  
  65. * @param array $params 
  66. * @return string 
  67. */ 
  68. public function createCreditCardData($params) 
  69. Util::verifyKeys( 
  70. self::$_createCreditCardSignature,  
  71. $params 
  72. ); 
  73. $params["kind"] = TransparentRedirect::CREATE_PAYMENT_METHOD; 
  74. return $this->_data($params); 
  75.  
  76. /** 
  77. * returns the trData string for creating a customer. 
  78. * @param array $params 
  79. * @return string 
  80. */ 
  81. public function createCustomerData($params) 
  82. Util::verifyKeys( 
  83. self::$_createCustomerSignature,  
  84. $params 
  85. ); 
  86. $params["kind"] = TransparentRedirect::CREATE_CUSTOMER; 
  87. return $this->_data($params); 
  88.  
  89.  
  90. public function url() 
  91. return $this->_config->baseUrl() . $this->_config->merchantPath() . '/transparent_redirect_requests'; 
  92.  
  93. /** 
  94. * returns the trData string for creating a transaction 
  95. * @param array $params 
  96. * @return string 
  97. */ 
  98. public function transactionData($params) 
  99. Util::verifyKeys( 
  100. self::$_transactionSignature,  
  101. $params 
  102. ); 
  103. $params["kind"] = TransparentRedirect::CREATE_TRANSACTION; 
  104. $transactionType = isset($params['transaction']['type']) ? 
  105. $params['transaction']['type'] : 
  106. null; 
  107. if ($transactionType != Transaction::SALE && $transactionType != Transaction::CREDIT) { 
  108. throw new InvalidArgumentException( 
  109. 'expected transaction[type] of sale or credit, was: ' . 
  110. $transactionType 
  111. ); 
  112.  
  113. return $this->_data($params); 
  114.  
  115. /** 
  116. * Returns the trData string for updating a credit card. 
  117. * The paymentMethodToken of the credit card to update is required. 
  118. * <code> 
  119. * $trData = TransparentRedirect::updateCreditCardData(array( 
  120. * 'redirectUrl' => 'http://example.com/redirect_here',  
  121. * 'paymentMethodToken' => 'token123',  
  122. * )); 
  123. * </code> 
  124. * @param array $params 
  125. * @return string 
  126. */ 
  127. public function updateCreditCardData($params) 
  128. Util::verifyKeys( 
  129. self::$_updateCreditCardSignature,  
  130. $params 
  131. ); 
  132. if (!isset($params['paymentMethodToken'])) { 
  133. throw new InvalidArgumentException( 
  134. 'expected params to contain paymentMethodToken.' 
  135. ); 
  136. $params["kind"] = TransparentRedirect::UPDATE_PAYMENT_METHOD; 
  137. return $this->_data($params); 
  138.  
  139. /** 
  140. * Returns the trData string for updating a customer. 
  141. * The customerId of the customer to update is required. 
  142. * <code> 
  143. * $trData = TransparentRedirect::updateCustomerData(array( 
  144. * 'redirectUrl' => 'http://example.com/redirect_here',  
  145. * 'customerId' => 'customer123',  
  146. * )); 
  147. * </code> 
  148. * @param array $params 
  149. * @return string 
  150. */ 
  151. public function updateCustomerData($params) 
  152. Util::verifyKeys( 
  153. self::$_updateCustomerSignature,  
  154. $params 
  155. ); 
  156. if (!isset($params['customerId'])) { 
  157. throw new InvalidArgumentException( 
  158. 'expected params to contain customerId of customer to update' 
  159. ); 
  160. $params["kind"] = TransparentRedirect::UPDATE_CUSTOMER; 
  161. return $this->_data($params); 
  162.  
  163. public function parseAndValidateQueryString($queryString) 
  164. // parse the params into an array 
  165. parse_str($queryString, $params); 
  166. // remove the hash 
  167. $queryStringWithoutHash = null; 
  168. if (preg_match('/^(.*)&hash=[a-f0-9]+$/', $queryString, $match)) { 
  169. $queryStringWithoutHash = $match[1]; 
  170.  
  171. if($params['http_status'] != '200') { 
  172. $message = null; 
  173. if(array_key_exists('bt_message', $params)) { 
  174. $message = $params['bt_message']; 
  175. Util::throwStatusCodeException(isset($params['http_status']) ? $params['http_status'] : null, $message); 
  176.  
  177. // recreate the hash and compare it 
  178. if ($this->_hash($queryStringWithoutHash) == $params['hash']) { 
  179. return $params; 
  180. } else { 
  181. throw new Exception\ForgedQueryString(); 
  182.  
  183.  
  184. /** 
  185. * @ignore 
  186. */ 
  187. private function _data($params) 
  188. if (!isset($params['redirectUrl'])) { 
  189. throw new InvalidArgumentException( 
  190. 'expected params to contain redirectUrl' 
  191. ); 
  192. $params = $this->_underscoreKeys($params); 
  193. $now = new DateTime('now', new DateTimeZone('UTC')); 
  194. $trDataParams = array_merge($params,  
  195. 'api_version' => Configuration::API_VERSION,  
  196. 'public_key' => $this->_config->publicKey(),  
  197. 'time' => $now->format('YmdHis'),  
  198. ); 
  199. ksort($trDataParams); 
  200. $urlEncodedData = http_build_query($trDataParams, null, "&"); 
  201. $signatureService = new SignatureService( 
  202. $this->_config->privateKey(),  
  203. "Braintree\Digest::hexDigestSha1" 
  204. ); 
  205. return $signatureService->sign($urlEncodedData); 
  206.  
  207. private function _underscoreKeys($array) 
  208. foreach($array as $key=>$value) 
  209. $newKey = Util::camelCaseToDelimiter($key, '_'); 
  210. unset($array[$key]); 
  211. if (is_array($value)) 
  212. $array[$newKey] = $this->_underscoreKeys($value); 
  213. else 
  214. $array[$newKey] = $value; 
  215. return $array; 
  216.  
  217. /** 
  218. * @ignore 
  219. */ 
  220. private function _hash($string) 
  221. return Digest::hexDigestSha1($this->_config->privateKey(), $string);