MerchantGateway.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace Braintree;
  3. class MerchantGateway
  4. {
  5. private $_gateway;
  6. private $_config;
  7. private $_http;
  8. public function __construct($gateway)
  9. {
  10. $this->_gateway = $gateway;
  11. $this->_config = $gateway->config;
  12. $this->_config->assertHasClientCredentials();
  13. $this->_http = new Http($gateway->config);
  14. $this->_http->useClientCredentials();
  15. }
  16. public function create($attribs)
  17. {
  18. $response = $this->_http->post('/merchants/create_via_api', ['merchant' => $attribs]);
  19. return $this->_verifyGatewayResponse($response);
  20. }
  21. private function _verifyGatewayResponse($response)
  22. {
  23. if (isset($response['response']['merchant'])) {
  24. // return a populated instance of merchant
  25. return new Result\Successful([
  26. Merchant::factory($response['response']['merchant']),
  27. OAuthCredentials::factory($response['response']['credentials']),
  28. ]);
  29. } else if (isset($response['apiErrorResponse'])) {
  30. return new Result\Error($response['apiErrorResponse']);
  31. } else {
  32. throw new Exception\Unexpected(
  33. "Expected merchant or apiErrorResponse"
  34. );
  35. }
  36. }
  37. }
  38. class_alias('Braintree\MerchantGateway', 'Braintree_MerchantGateway');