BraintreeAddressGateway

Braintree AddressGateway module PHP Version 5 Creates and manages Braintree Addresses.

Defined (1)

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

/includes/lib/Braintree/lib/Braintree/AddressGateway.php  
  1. class AddressGateway 
  2. /** 
  3. * @var Gateway 
  4. */ 
  5. private $_gateway; 
  6.  
  7. /** 
  8. * @var Configuration 
  9. */ 
  10. private $_config; 
  11.  
  12. /** 
  13. * @var Http 
  14. */ 
  15. private $_http; 
  16.  
  17. /** 
  18. * @param Gateway $gateway 
  19. */ 
  20. public function __construct($gateway) 
  21. $this->_gateway = $gateway; 
  22. $this->_config = $gateway->config; 
  23. $this->_config->assertHasAccessTokenOrKeys(); 
  24. $this->_http = new Http($gateway->config); 
  25.  
  26.  
  27. /** public class methods */ 
  28. /** 
  29. * @access public 
  30. * @param array $attribs 
  31. * @return Result\Successful|Result\Error 
  32. */ 
  33. public function create($attribs) 
  34. Util::verifyKeys(self::createSignature(), $attribs); 
  35. $customerId = isset($attribs['customerId']) ? 
  36. $attribs['customerId'] : 
  37. null; 
  38.  
  39. $this->_validateCustomerId($customerId); 
  40. unset($attribs['customerId']); 
  41. try { 
  42. return $this->_doCreate( 
  43. '/customers/' . $customerId . '/addresses',  
  44. ['address' => $attribs] 
  45. ); 
  46. } catch (Exception\NotFound $e) { 
  47. throw new Exception\NotFound( 
  48. 'Customer ' . $customerId . ' not found.' 
  49. ); 
  50.  
  51. /** 
  52. * attempts the create operation assuming all data will validate 
  53. * returns a Address object instead of a Result 
  54. * @access public 
  55. * @param array $attribs 
  56. * @return self 
  57. * @throws Exception\ValidationError 
  58. */ 
  59. public function createNoValidate($attribs) 
  60. $result = $this->create($attribs); 
  61. return Util::returnObjectOrThrowException(__CLASS__, $result); 
  62.  
  63.  
  64. /** 
  65. * delete an address by id 
  66. * @param mixed $customerOrId 
  67. * @param string $addressId 
  68. */ 
  69. public function delete($customerOrId = null, $addressId = null) 
  70. $this->_validateId($addressId); 
  71. $customerId = $this->_determineCustomerId($customerOrId); 
  72. $path = $this->_config->merchantPath() . '/customers/' . $customerId . '/addresses/' . $addressId; 
  73. $this->_http->delete($path); 
  74. return new Result\Successful(); 
  75.  
  76. /** 
  77. * find an address by id 
  78. * Finds the address with the given <b>addressId</b> that is associated 
  79. * to the given <b>customerOrId</b>. 
  80. * If the address cannot be found, a NotFound exception will be thrown. 
  81. * @access public 
  82. * @param mixed $customerOrId 
  83. * @param string $addressId 
  84. * @return Address 
  85. * @throws Exception\NotFound 
  86. */ 
  87. public function find($customerOrId, $addressId) 
  88.  
  89. $customerId = $this->_determineCustomerId($customerOrId); 
  90. $this->_validateId($addressId); 
  91.  
  92. try { 
  93. $path = $this->_config->merchantPath() . '/customers/' . $customerId . '/addresses/' . $addressId; 
  94. $response = $this->_http->get($path); 
  95. return Address::factory($response['address']); 
  96. } catch (Exception\NotFound $e) { 
  97. throw new Exception\NotFound( 
  98. 'address for customer ' . $customerId . 
  99. ' with id ' . $addressId . ' not found.' 
  100. ); 
  101.  
  102.  
  103. /** 
  104. * updates the address record 
  105. * if calling this method in context,  
  106. * customerOrId is the 2nd attribute, addressId 3rd. 
  107. * customerOrId & addressId are not sent in object context. 
  108. * @access public 
  109. * @param array $attributes 
  110. * @param mixed $customerOrId (only used in call) 
  111. * @param string $addressId (only used in call) 
  112. * @return Result\Successful|Result\Error 
  113. */ 
  114. public function update($customerOrId, $addressId, $attributes) 
  115. $this->_validateId($addressId); 
  116. $customerId = $this->_determineCustomerId($customerOrId); 
  117. Util::verifyKeys(self::updateSignature(), $attributes); 
  118.  
  119. $path = $this->_config->merchantPath() . '/customers/' . $customerId . '/addresses/' . $addressId; 
  120. $response = $this->_http->put($path, ['address' => $attributes]); 
  121.  
  122. return $this->_verifyGatewayResponse($response); 
  123.  
  124.  
  125. /** 
  126. * update an address record, assuming validations will pass 
  127. * if calling this method in context,  
  128. * customerOrId is the 2nd attribute, addressId 3rd. 
  129. * customerOrId & addressId are not sent in object context. 
  130. * @access public 
  131. * @param array $transactionAttribs 
  132. * @param string $customerId 
  133. * @return Transaction 
  134. * @throws Exception\ValidationsFailed 
  135. * @see Address::update() 
  136. */ 
  137. public function updateNoValidate($customerOrId, $addressId, $attributes) 
  138. $result = $this->update($customerOrId, $addressId, $attributes); 
  139. return Util::returnObjectOrThrowException(__CLASS__, $result); 
  140.  
  141. /** 
  142. * creates a full array signature of a valid create request 
  143. * @return array gateway create request format 
  144. */ 
  145. public static function createSignature() 
  146. return [ 
  147. 'company', 'countryCodeAlpha2', 'countryCodeAlpha3', 'countryCodeNumeric',  
  148. 'countryName', 'customerId', 'extendedAddress', 'firstName',  
  149. 'lastName', 'locality', 'postalCode', 'region', 'streetAddress' 
  150. ]; 
  151.  
  152. /** 
  153. * creates a full array signature of a valid update request 
  154. * @return array gateway update request format 
  155. */ 
  156. public static function updateSignature() 
  157. // TODO: remove customerId from update signature 
  158. return self::createSignature(); 
  159.  
  160.  
  161. /** 
  162. * verifies that a valid address id is being used 
  163. * @ignore 
  164. * @param string $id address id 
  165. * @throws InvalidArgumentException 
  166. */ 
  167. private function _validateId($id = null) 
  168. if (empty($id) || trim($id) == "") { 
  169. throw new InvalidArgumentException( 
  170. 'expected address id to be set' 
  171. ); 
  172. if (!preg_match('/^[0-9A-Za-z_-]+$/', $id)) { 
  173. throw new InvalidArgumentException( 
  174. $id . ' is an invalid address id.' 
  175. ); 
  176.  
  177. /** 
  178. * verifies that a valid customer id is being used 
  179. * @ignore 
  180. * @param string $id customer id 
  181. * @throws InvalidArgumentException 
  182. */ 
  183. private function _validateCustomerId($id = null) 
  184. if (empty($id) || trim($id) == "") { 
  185. throw new InvalidArgumentException( 
  186. 'expected customer id to be set' 
  187. ); 
  188. if (!preg_match('/^[0-9A-Za-z_-]+$/', $id)) { 
  189. throw new InvalidArgumentException( 
  190. $id . ' is an invalid customer id.' 
  191. ); 
  192.  
  193.  
  194. /** 
  195. * determines if a string id or Customer object was passed 
  196. * @ignore 
  197. * @param mixed $customerOrId 
  198. * @return string customerId 
  199. */ 
  200. private function _determineCustomerId($customerOrId) 
  201. $customerId = ($customerOrId instanceof Customer) ? $customerOrId->id : $customerOrId; 
  202. $this->_validateCustomerId($customerId); 
  203. return $customerId; 
  204.  
  205.  
  206. /** private class methods */ 
  207. /** 
  208. * sends the create request to the gateway 
  209. * @ignore 
  210. * @param string $subPath 
  211. * @param array $params 
  212. * @return Result\Successful|Result\Error 
  213. */ 
  214. private function _doCreate($subPath, $params) 
  215. $fullPath = $this->_config->merchantPath() . $subPath; 
  216. $response = $this->_http->post($fullPath, $params); 
  217.  
  218. return $this->_verifyGatewayResponse($response); 
  219.  
  220. /** 
  221. * generic method for validating incoming gateway responses 
  222. * creates a new Address object and encapsulates 
  223. * it inside a Result\Successful object, or 
  224. * encapsulates an Errors object inside a Result\Error 
  225. * alternatively, throws an Unexpected exception if the response is invalid 
  226. * @ignore 
  227. * @param array $response gateway response values 
  228. * @return Result\Successful|Result\Error 
  229. * @throws Exception\Unexpected 
  230. */ 
  231. private function _verifyGatewayResponse($response) 
  232. if (isset($response['address'])) { 
  233. // return a populated instance of Address 
  234. return new Result\Successful( 
  235. Address::factory($response['address']) 
  236. ); 
  237. } else if (isset($response['apiErrorResponse'])) { 
  238. return new Result\Error($response['apiErrorResponse']); 
  239. } else { 
  240. throw new Exception\Unexpected( 
  241. "Expected address or apiErrorResponse" 
  242. ); 
  243.