Error.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Braintree\Result;
  3. use Braintree\Base;
  4. use Braintree\Transaction;
  5. use Braintree\Subscription;
  6. use Braintree\MerchantAccount;
  7. use Braintree\Util;
  8. use Braintree\Error\ErrorCollection;
  9. /**
  10. * Braintree Error Result
  11. *
  12. * An Error Result will be returned from gateway methods when
  13. * the gateway responds with an error. It will provide access
  14. * to the original request.
  15. * For example, when voiding a transaction, Error Result will
  16. * respond to the void request if it failed:
  17. *
  18. * <code>
  19. * $result = Transaction::void('abc123');
  20. * if ($result->success) {
  21. * // Successful Result
  22. * } else {
  23. * // Result\Error
  24. * }
  25. * </code>
  26. *
  27. * @package Braintree
  28. * @subpackage Result
  29. *
  30. * @property-read array $params original passed params
  31. * @property-read \Braintree\Error\ErrorCollection $errors
  32. * @property-read \Braintree\Result\CreditCardVerification $creditCardVerification credit card verification data
  33. */
  34. class Error extends Base
  35. {
  36. /**
  37. * @var bool always false
  38. */
  39. public $success = false;
  40. /**
  41. * return original value for a field
  42. * For example, if a user tried to submit 'invalid-email' in the html field transaction[customer][email],
  43. * $result->valueForHtmlField("transaction[customer][email]") would yield "invalid-email"
  44. *
  45. * @param string $field
  46. * @return string
  47. */
  48. public function valueForHtmlField($field)
  49. {
  50. $pieces = preg_split("/[\[\]]+/", $field, 0, PREG_SPLIT_NO_EMPTY);
  51. $params = $this->params;
  52. foreach(array_slice($pieces, 0, -1) as $key) {
  53. $params = $params[Util::delimiterToCamelCase($key)];
  54. }
  55. if ($key != 'custom_fields') {
  56. $finalKey = Util::delimiterToCamelCase(end($pieces));
  57. } else {
  58. $finalKey = end($pieces);
  59. }
  60. $fieldValue = isset($params[$finalKey]) ? $params[$finalKey] : null;
  61. return $fieldValue;
  62. }
  63. /**
  64. * overrides default constructor
  65. * @ignore
  66. * @param array $response gateway response array
  67. */
  68. public function __construct($response)
  69. {
  70. $this->_attributes = $response;
  71. $this->_set('errors', new ErrorCollection($response['errors']));
  72. if(isset($response['verification'])) {
  73. $this->_set('creditCardVerification', new CreditCardVerification($response['verification']));
  74. } else {
  75. $this->_set('creditCardVerification', null);
  76. }
  77. if(isset($response['transaction'])) {
  78. $this->_set('transaction', Transaction::factory($response['transaction']));
  79. } else {
  80. $this->_set('transaction', null);
  81. }
  82. if(isset($response['subscription'])) {
  83. $this->_set('subscription', Subscription::factory($response['subscription']));
  84. } else {
  85. $this->_set('subscription', null);
  86. }
  87. if(isset($response['merchantAccount'])) {
  88. $this->_set('merchantAccount', MerchantAccount::factory($response['merchantAccount']));
  89. } else {
  90. $this->_set('merchantAccount', null);
  91. }
  92. if(isset($response['verification'])) {
  93. $this->_set('verification', new CreditCardVerification($response['verification']));
  94. } else {
  95. $this->_set('verification', null);
  96. }
  97. }
  98. /**
  99. * create a printable representation of the object as:
  100. * ClassName[property=value, property=value]
  101. * @ignore
  102. * @return string
  103. */
  104. public function __toString()
  105. {
  106. $output = Util::attributesToString($this->_attributes);
  107. if (isset($this->_creditCardVerification)) {
  108. $output .= sprintf('%s', $this->_creditCardVerification);
  109. }
  110. return __CLASS__ .'[' . $output . ']';
  111. }
  112. }
  113. class_alias('Braintree\Result\Error', 'Braintree_Result_Error');