BraintreeAddressGateway

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

Defined (1)

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

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