123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Braintree\Gateway;
- use Braintree\Transaction;
- use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
- use Magento\Payment\Gateway\Helper;
- use Magento\Vault\Api\Data\PaymentTokenInterface;
- /**
- * Class SubjectReader
- */
- class SubjectReader
- {
- /**
- * Reads response object from subject
- *
- * @param array $subject
- * @return object
- */
- public function readResponseObject(array $subject)
- {
- $response = Helper\SubjectReader::readResponse($subject);
- if (!isset($response['object']) || !is_object($response['object'])) {
- throw new \InvalidArgumentException('Response object does not exist');
- }
- return $response['object'];
- }
- /**
- * Reads payment from subject
- *
- * @param array $subject
- * @return PaymentDataObjectInterface
- */
- public function readPayment(array $subject)
- {
- return Helper\SubjectReader::readPayment($subject);
- }
- /**
- * Reads transaction from the subject.
- *
- * @param array $subject
- * @return Transaction
- * @throws \InvalidArgumentException if the subject doesn't contain transaction details.
- */
- public function readTransaction(array $subject)
- {
- if (!isset($subject['object']) || !is_object($subject['object'])) {
- throw new \InvalidArgumentException('Response object does not exist.');
- }
- if (!isset($subject['object']->transaction)
- || !$subject['object']->transaction instanceof Transaction
- ) {
- throw new \InvalidArgumentException('The object is not a class \Braintree\Transaction.');
- }
- return $subject['object']->transaction;
- }
- /**
- * Reads amount from subject
- *
- * @param array $subject
- * @return mixed
- */
- public function readAmount(array $subject)
- {
- return Helper\SubjectReader::readAmount($subject);
- }
- /**
- * Reads customer id from subject
- *
- * @param array $subject
- * @return int
- */
- public function readCustomerId(array $subject)
- {
- if (!isset($subject['customer_id'])) {
- throw new \InvalidArgumentException('The "customerId" field does not exists');
- }
- return (int) $subject['customer_id'];
- }
- /**
- * Reads public hash from subject
- *
- * @param array $subject
- * @return string
- */
- public function readPublicHash(array $subject)
- {
- if (empty($subject[PaymentTokenInterface::PUBLIC_HASH])) {
- throw new \InvalidArgumentException('The "public_hash" field does not exists');
- }
- return $subject[PaymentTokenInterface::PUBLIC_HASH];
- }
- /**
- * Reads PayPal details from transaction object
- *
- * @param Transaction $transaction
- * @return array
- */
- public function readPayPal(Transaction $transaction)
- {
- if (!isset($transaction->paypal)) {
- throw new \InvalidArgumentException('Transaction has\'t paypal attribute');
- }
- return $transaction->paypal;
- }
- /**
- * Reads store's ID, otherwise returns null.
- *
- * @param array $subject
- * @return int|null
- */
- public function readStoreId(array $subject)
- {
- return $subject['store_id'] ?? null;
- }
- }
|