Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TransactionMap.php
Go to the documentation of this file.
1 <?php
7 
8 use Braintree\Transaction;
9 use DateTime;
12 
17 {
19 
23  private $attributeValueFactory;
24 
28  private $transaction;
29 
33  public static $simpleFieldsMap = [
34  'id',
35  'merchantAccountId',
36  'orderId',
37  'paymentInstrumentType',
38  'paypalDetails_paymentId',
39  'type',
40  'createdAt',
41  'amount',
42  'processorSettlementResponseCode',
43  'status',
44  'processorSettlementResponseText',
45  'refundIds',
46  'settlementBatchId',
47  'currencyIsoCode'
48  ];
49 
54  public function __construct(
55  AttributeValueFactory $attributeValueFactory,
56  Transaction $transaction
57  ) {
58  $this->attributeValueFactory = $attributeValueFactory;
59  $this->transaction = $transaction;
60  }
61 
67  public function getId()
68  {
69  return $this->getMappedValue('id');
70  }
71 
79  public function setId($id)
80  {
81  }
82 
89  public function getCustomAttribute($attributeCode)
90  {
92  $attributeValue = $this->attributeValueFactory->create();
93  $attributeValue->setAttributeCode($attributeCode);
94  $attributeValue->setValue($this->getMappedValue($attributeCode));
95  return $attributeValue;
96  }
97 
106  public function setCustomAttribute($attributeCode, $attributeValue)
107  {
108  return $this;
109  }
110 
116  public function getCustomAttributes()
117  {
118  $shouldBeLocalized = ['paymentInstrumentType', 'type', 'status'];
119  $output = [];
120  foreach ($this->getMappedValues() as $key => $value) {
121  $attribute = $this->attributeValueFactory->create();
122  if (in_array($key, $shouldBeLocalized)) {
123  $value = __($value);
124  }
125  $output[] = $attribute->setAttributeCode($key)->setValue($value);
126  }
127  return $output;
128  }
129 
138  public function setCustomAttributes(array $attributes)
139  {
140  return $this;
141  }
142 
149  private function getMappedValue($key)
150  {
151  if (!in_array($key, static::$simpleFieldsMap)) {
152  return null;
153  }
154 
155  $val = $this->getTransactionFieldValue($key);
156  $val = $this->convertToText($val);
157  return $val;
158  }
159 
163  private function getMappedValues()
164  {
165  $result = [];
166 
167  foreach (static::$simpleFieldsMap as $key) {
168  $val = $this->getTransactionFieldValue($key);
169  $val = $this->convertToText($val);
170  $result[$key] = $val;
171  }
172 
173  return $result;
174  }
175 
182  private function getTransactionFieldValue($key)
183  {
184  $keys = explode(self::TRANSACTION_FIELD_MAP_DELIMITER, $key);
185  $result = $this->transaction;
186  foreach ($keys as $k) {
187  if (!isset($result->$k)) {
188  $result = null;
189  break;
190  }
191  $result = $result->$k;
192  }
193  return $result;
194  }
195 
202  private function convertToText($val)
203  {
204  if (is_object($val)) {
205  switch (get_class($val)) {
206  case 'DateTime':
208  $val = $val->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
209  }
210  } elseif (is_array($val)) {
211  $val = implode(', ', $val);
212  }
213 
214  return (string) $val;
215  }
216 }
$transaction
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
setCustomAttribute($attributeCode, $attributeValue)
__()
Definition: __.php:13
__construct(AttributeValueFactory $attributeValueFactory, Transaction $transaction)
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$attributes
Definition: matrix.phtml:13