SubjectReader.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Gateway;
  7. use Braintree\Transaction;
  8. use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
  9. use Magento\Payment\Gateway\Helper;
  10. use Magento\Vault\Api\Data\PaymentTokenInterface;
  11. /**
  12. * Class SubjectReader
  13. */
  14. class SubjectReader
  15. {
  16. /**
  17. * Reads response object from subject
  18. *
  19. * @param array $subject
  20. * @return object
  21. */
  22. public function readResponseObject(array $subject)
  23. {
  24. $response = Helper\SubjectReader::readResponse($subject);
  25. if (!isset($response['object']) || !is_object($response['object'])) {
  26. throw new \InvalidArgumentException('Response object does not exist');
  27. }
  28. return $response['object'];
  29. }
  30. /**
  31. * Reads payment from subject
  32. *
  33. * @param array $subject
  34. * @return PaymentDataObjectInterface
  35. */
  36. public function readPayment(array $subject)
  37. {
  38. return Helper\SubjectReader::readPayment($subject);
  39. }
  40. /**
  41. * Reads transaction from the subject.
  42. *
  43. * @param array $subject
  44. * @return Transaction
  45. * @throws \InvalidArgumentException if the subject doesn't contain transaction details.
  46. */
  47. public function readTransaction(array $subject)
  48. {
  49. if (!isset($subject['object']) || !is_object($subject['object'])) {
  50. throw new \InvalidArgumentException('Response object does not exist.');
  51. }
  52. if (!isset($subject['object']->transaction)
  53. || !$subject['object']->transaction instanceof Transaction
  54. ) {
  55. throw new \InvalidArgumentException('The object is not a class \Braintree\Transaction.');
  56. }
  57. return $subject['object']->transaction;
  58. }
  59. /**
  60. * Reads amount from subject
  61. *
  62. * @param array $subject
  63. * @return mixed
  64. */
  65. public function readAmount(array $subject)
  66. {
  67. return Helper\SubjectReader::readAmount($subject);
  68. }
  69. /**
  70. * Reads customer id from subject
  71. *
  72. * @param array $subject
  73. * @return int
  74. */
  75. public function readCustomerId(array $subject)
  76. {
  77. if (!isset($subject['customer_id'])) {
  78. throw new \InvalidArgumentException('The "customerId" field does not exists');
  79. }
  80. return (int) $subject['customer_id'];
  81. }
  82. /**
  83. * Reads public hash from subject
  84. *
  85. * @param array $subject
  86. * @return string
  87. */
  88. public function readPublicHash(array $subject)
  89. {
  90. if (empty($subject[PaymentTokenInterface::PUBLIC_HASH])) {
  91. throw new \InvalidArgumentException('The "public_hash" field does not exists');
  92. }
  93. return $subject[PaymentTokenInterface::PUBLIC_HASH];
  94. }
  95. /**
  96. * Reads PayPal details from transaction object
  97. *
  98. * @param Transaction $transaction
  99. * @return array
  100. */
  101. public function readPayPal(Transaction $transaction)
  102. {
  103. if (!isset($transaction->paypal)) {
  104. throw new \InvalidArgumentException('Transaction has\'t paypal attribute');
  105. }
  106. return $transaction->paypal;
  107. }
  108. /**
  109. * Reads store's ID, otherwise returns null.
  110. *
  111. * @param array $subject
  112. * @return int|null
  113. */
  114. public function readStoreId(array $subject)
  115. {
  116. return $subject['store_id'] ?? null;
  117. }
  118. }