Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractApi.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Magento\Payment\Helper\Formatter;
12 
17 {
18  use Formatter;
19 
25  protected $_config;
26 
31  protected $_globalMap = [];
32 
38  protected $_exportToRequestFilters = [];
39 
46 
53 
58  'name' => 'strval'
59  ];
60 
64  protected $_lineItemTotalExportMap = [];
65 
71  protected $_cart;
72 
79 
86 
92  protected $_customerAddress;
93 
97  protected $_logger;
98 
102  protected $customLogger;
103 
107  protected $_localeResolver;
108 
112  protected $_regionFactory;
113 
125  public function __construct(
126  \Magento\Customer\Helper\Address $customerAddress,
127  \Psr\Log\LoggerInterface $logger,
129  \Magento\Framework\Locale\ResolverInterface $localeResolver,
130  \Magento\Directory\Model\RegionFactory $regionFactory,
131  array $data = []
132  ) {
133  $this->_customerAddress = $customerAddress;
134  $this->_logger = $logger;
135  $this->customLogger = $customLogger;
136  $this->_localeResolver = $localeResolver;
137  $this->_regionFactory = $regionFactory;
138  parent::__construct($data);
139  }
140 
146  public function getApiUsername()
147  {
148  return $this->_config->getValue('apiUsername');
149  }
150 
156  public function getApiPassword()
157  {
158  return $this->_config->getValue('apiPassword');
159  }
160 
166  public function getApiSignature()
167  {
168  return $this->_config->getValue('apiSignature');
169  }
170 
176  public function getApiCertificate()
177  {
178  return $this->_config->getApiCertificate();
179  }
180 
186  public function getBuildNotationCode()
187  {
188  return $this->_config->getBuildNotationCode();
189  }
190 
197  public function getUseProxy()
198  {
199  return $this->_getDataOrConfig('use_proxy', false);
200  }
201 
207  public function getProxyHost()
208  {
209  return $this->_getDataOrConfig('proxy_host', '127.0.0.1');
210  }
211 
217  public function getProxyPort()
218  {
219  return $this->_getDataOrConfig('proxy_port', '808');
220  }
221 
227  public function getPageStyle()
228  {
229  return $this->_getDataOrConfig('page_style');
230  }
231 
237  public function getHdrimg()
238  {
239  return $this->_getDataOrConfig('paypal_hdrimg');
240  }
241 
247  public function getHdrbordercolor()
248  {
249  return $this->_getDataOrConfig('paypal_hdrbordercolor');
250  }
251 
257  public function getHdrbackcolor()
258  {
259  return $this->_getDataOrConfig('paypal_hdrbackcolor');
260  }
261 
267  public function getPayflowcolor()
268  {
269  return $this->_getDataOrConfig('paypal_payflowcolor');
270  }
271 
277  public function getPaymentAction()
278  {
279  return $this->_getDataOrConfig('payment_action');
280  }
281 
287  public function getBusinessAccount()
288  {
289  return $this->_getDataOrConfig('business_account');
290  }
291 
299  public function import($to, array $publicMap = [])
300  {
301  return \Magento\Framework\DataObject\Mapper::accumulateByMap([$this, 'getDataUsingMethod'], $to, $publicMap);
302  }
303 
311  public function export($from, array $publicMap = [])
312  {
313  \Magento\Framework\DataObject\Mapper::accumulateByMap($from, [$this, 'setDataUsingMethod'], $publicMap);
314  return $this;
315  }
316 
323  public function setPaypalCart(\Magento\Paypal\Model\Cart $cart)
324  {
325  $this->_cart = $cart;
326  return $this;
327  }
328 
335  public function setConfigObject(\Magento\Paypal\Model\Config $config)
336  {
337  $this->_config = $config;
338  return $this;
339  }
340 
346  public function getLocale()
347  {
348  return $this->_localeResolver->getLocale();
349  }
350 
357  {
358  return 1;
359  }
360 
368  protected function &_exportToRequest(array $privateRequestMap, array $request = [])
369  {
370  $map = [];
371  foreach ($privateRequestMap as $key) {
372  if (isset($this->_globalMap[$key])) {
373  $map[$this->_globalMap[$key]] = $key;
374  }
375  }
376  $result = \Magento\Framework\DataObject\Mapper::accumulateByMap([$this, 'getDataUsingMethod'], $request, $map);
377  foreach ($privateRequestMap as $key) {
378  if (isset($this->_exportToRequestFilters[$key]) && isset($result[$key])) {
379  $callback = $this->_exportToRequestFilters[$key];
380  $privateKey = $result[$key];
381  $publicKey = $map[$this->_globalMap[$key]];
382  $result[$key] = call_user_func([$this, $callback], $privateKey, $publicKey);
383  }
384  }
385  return $result;
386  }
387 
395  protected function _importFromResponse(array $privateResponseMap, array $response)
396  {
397  $map = [];
398  foreach ($privateResponseMap as $key) {
399  if (isset($this->_globalMap[$key])) {
400  $map[$key] = $this->_globalMap[$key];
401  }
402  if (isset($response[$key]) && isset($this->_importFromRequestFilters[$key])) {
403  $callback = $this->_importFromRequestFilters[$key];
404  $response[$key] = call_user_func([$this, $callback], $response[$key], $key, $map[$key]);
405  }
406  }
407  \Magento\Framework\DataObject\Mapper::accumulateByMap($response, [$this, 'setDataUsingMethod'], $map);
408  }
409 
420  protected function _exportLineItems(array &$request, $i = 0)
421  {
422  if (!$this->_cart) {
423  return;
424  }
425 
426  // always add cart totals, even if line items are not requested
427  if ($this->_lineItemTotalExportMap) {
428  foreach ($this->_cart->getAmounts() as $key => $total) {
429  if (isset($this->_lineItemTotalExportMap[$key])) {
430  // !empty($total)
431  $privateKey = $this->_lineItemTotalExportMap[$key];
432  $total = round($total, 2);
433  $request[$privateKey] = $this->formatPrice($total);
434  }
435  }
436  }
437 
438  // add cart line items
439  $items = $this->_cart->getAllItems();
440  if (empty($items) || !$this->getIsLineItemsEnabled()) {
441  return;
442  }
443  $result = null;
444  foreach ($items as $item) {
445  foreach ($this->_lineItemExportItemsFormat as $publicKey => $privateFormat) {
446  $result = true;
447  $value = $item->getDataUsingMethod($publicKey);
448  $request[sprintf($privateFormat, $i)] = $this->formatValue($value, $publicKey);
449  }
450  $i++;
451  }
452  return $result;
453  }
454 
463  protected function _exportShippingOptions(array &$request, $i = 0)
464  {
465  $options = $this->getShippingOptions();
466  if (empty($options)) {
467  return false;
468  }
469  foreach ($options as $option) {
470  foreach ($this->_shippingOptionsExportItemsFormat as $publicKey => $privateFormat) {
471  $value = $option->getDataUsingMethod($publicKey);
472  if (is_float($value)) {
473  $value = $this->formatPrice($value);
474  }
475  if (is_bool($value)) {
476  $value = $this->_filterBool($value);
477  }
478  $request[sprintf($privateFormat, $i)] = $value;
479  }
480  $i++;
481  }
482  return true;
483  }
484 
491  protected function _filterBool($value)
492  {
493  return $value ? 'true' : 'false';
494  }
495 
502  protected function _filterInt($value)
503  {
504  return (int)$value;
505  }
506 
514  protected function _getDataOrConfig($key, $default = null)
515  {
516  if ($this->hasData($key)) {
517  return $this->getData($key);
518  }
519  return $this->_config->getValue($key) ? $this->_config->getValue($key) : $default;
520  }
521 
529  {
530  $regionId = $address->getData('region_id');
531  if ($regionId) {
532  $region = $this->_regionFactory->create()->load($regionId);
533  if ($region->getId()) {
534  return $region->getCode();
535  }
536  }
537  return '';
538  }
539 
548  protected function _importStreetFromAddress(\Magento\Framework\DataObject $address, array &$to)
549  {
550  $keys = func_get_args();
551  array_shift($keys);
552  array_shift($keys);
553  $street = $address->getStreet();
554  if (!$keys || !$street || !is_array($street)) {
555  return;
556  }
557 
558  $street = $this->_customerAddress->convertStreetLines($address->getStreet(), count($keys));
559 
560  $i = 0;
561  foreach ($keys as $key) {
562  $to[$key] = isset($street[$i]) ? $street[$i] : '';
563  $i++;
564  }
565  }
566 
573  protected function _buildQuery($request)
574  {
575  return http_build_query($request);
576  }
577 
585  protected function _filterQty($value)
586  {
587  return intval($value);
588  }
589 
596  protected function _debug($debugData)
597  {
598  $this->customLogger->debug(
599  $debugData,
600  (array)$this->getDebugReplacePrivateDataKeys(),
601  (bool)$this->getDebugFlag()
602  );
603  }
604 
611  public function getDebugFlag()
612  {
613  return $this->_config->getValue('debug');
614  }
615 
622  public function getUseCertAuthentication()
623  {
624  return (bool)$this->_config->getValue('apiAuthentication');
625  }
626 
633  {
635  }
636 
644  private function formatValue($value, $publicKey)
645  {
646  if (!empty($this->_lineItemExportItemsFilters[$publicKey])) {
647  $callback = $this->_lineItemExportItemsFilters[$publicKey];
648  $value = method_exists($this, $callback) ? $this->{$callback}($value) : $callback($value);
649  }
650 
651  if (is_float($value)) {
652  $value = $this->formatPrice($value);
653  }
654 
655  return $value;
656  }
657 }
_exportShippingOptions(array &$request, $i=0)
$response
Definition: 404.php:11
_exportLineItems(array &$request, $i=0)
export($from, array $publicMap=[])
getData($key='', $index=null)
Definition: DataObject.php:119
$config
Definition: fraud_order.php:17
_lookupRegionCodeFromAddress(\Magento\Framework\DataObject $address)
& _exportToRequest(array $privateRequestMap, array $request=[])
__construct(\Magento\Customer\Helper\Address $customerAddress, \Psr\Log\LoggerInterface $logger, Logger $customLogger, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Directory\Model\RegionFactory $regionFactory, array $data=[])
$logger
$address
Definition: customer.php:38
$value
Definition: gender.phtml:16
_importStreetFromAddress(\Magento\Framework\DataObject $address, array &$to)
setConfigObject(\Magento\Paypal\Model\Config $config)
_importFromResponse(array $privateResponseMap, array $response)
$i
Definition: gallery.phtml:31
_getDataOrConfig($key, $default=null)
$customerAddress
setPaypalCart(\Magento\Paypal\Model\Cart $cart)
$items