ExternalAccount.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Stripe;
  3. /**
  4. * Class ExternalAccount
  5. *
  6. * @package Stripe
  7. */
  8. abstract class ExternalAccount extends ApiResource
  9. {
  10. /**
  11. * @return string The instance URL for this resource. It needs to be special
  12. * cased because it doesn't fit into the standard resource pattern.
  13. */
  14. public function instanceUrl()
  15. {
  16. $id = $this['id'];
  17. if (!$id) {
  18. $class = get_class($this);
  19. $msg = "Could not determine which URL to request: $class instance "
  20. . "has invalid ID: $id";
  21. throw new Error\InvalidRequest($msg, null);
  22. }
  23. if ($this['customer']) {
  24. $parent = $this['customer'];
  25. $base = Customer::classUrl();
  26. $path = 'sources';
  27. } elseif ($this['account']) {
  28. $parent = $this['account'];
  29. $base = Account::classUrl();
  30. $path = 'external_accounts';
  31. } elseif ($this['recipient']) {
  32. $parent = $this['recipient'];
  33. $base = Recipient::classUrl();
  34. $path = 'cards';
  35. } else {
  36. return null;
  37. }
  38. $parent = Util\Util::utf8($parent);
  39. $id = Util\Util::utf8($id);
  40. $parentExtn = urlencode($parent);
  41. $extn = urlencode($id);
  42. return "$base/$parentExtn/$path/$extn";
  43. }
  44. /**
  45. * @param array|null $params
  46. * @param array|string|null $opts
  47. *
  48. * @return ExternalAccount The deleted external account.
  49. */
  50. public function delete($params = null, $opts = null)
  51. {
  52. return $this->_delete($params, $opts);
  53. }
  54. /**
  55. * @param array|string|null $opts
  56. *
  57. * @return ExternalAccount The saved external account.
  58. */
  59. public function save($opts = null)
  60. {
  61. return $this->_save($opts);
  62. }
  63. /**
  64. * @param array|null $params
  65. * @param array|string|null $opts
  66. *
  67. * @return ExternalAccount The verified (or not) external account.
  68. */
  69. public function verify($params = null, $opts = null)
  70. {
  71. if ($this['customer']) {
  72. $url = $this->instanceUrl() . '/verify';
  73. list($response, $options) = $this->_request('post', $url, $params, $opts);
  74. $this->refreshFrom($response, $options);
  75. return $this;
  76. } else {
  77. $message = 'Only customer external accounts can be verified in this manner.';
  78. throw new Error\Api($message);
  79. }
  80. }
  81. }