68 'section_columns' => [
72 'PayPalReferenceID' => 3,
73 'PayPalReferenceIDType' => 4,
74 'TransactionEventCode' => 5,
75 'TransactionInitiationDate' => 6,
76 'TransactionCompletionDate' => 7,
77 'TransactionDebitOrCredit' => 8,
78 'GrossTransactionAmount' => 9,
79 'GrossTransactionCurrency' => 10,
80 'FeeDebitOrCredit' => 11,
87 'TransactionID' =>
'transaction_id',
88 'InvoiceID' =>
'invoice_id',
89 'PayPalReferenceID' =>
'paypal_reference_id',
90 'PayPalReferenceIDType' =>
'paypal_reference_id_type',
91 'TransactionEventCode' =>
'transaction_event_code',
92 'TransactionInitiationDate' =>
'transaction_initiation_date',
93 'TransactionCompletionDate' =>
'transaction_completion_date',
94 'TransactionDebitOrCredit' =>
'transaction_debit_or_credit',
95 'GrossTransactionAmount' =>
'gross_transaction_amount',
96 'GrossTransactionCurrency' =>
'gross_transaction_currency',
97 'FeeDebitOrCredit' =>
'fee_debit_or_credit',
98 'FeeAmount' =>
'fee_amount',
99 'FeeCurrency' =>
'fee_currency',
100 'CustomField' =>
'custom_field',
101 'ConsumerID' =>
'consumer_id',
105 'section_columns' => [
107 'Transaction ID' => 1,
109 'PayPal Reference ID' => 3,
110 'PayPal Reference ID Type' => 4,
111 'Transaction Event Code' => 5,
112 'Transaction Initiation Date' => 6,
113 'Transaction Completion Date' => 7,
114 'Transaction Debit or Credit' => 8,
115 'Gross Transaction Amount' => 9,
116 'Gross Transaction Currency' => 10,
117 'Fee Debit or Credit' => 11,
119 'Fee Currency' => 13,
120 'Custom Field' => 14,
122 'Payment Tracking ID' => 16,
126 'Transaction ID' =>
'transaction_id',
127 'Invoice ID' =>
'invoice_id',
128 'PayPal Reference ID' =>
'paypal_reference_id',
129 'PayPal Reference ID Type' =>
'paypal_reference_id_type',
130 'Transaction Event Code' =>
'transaction_event_code',
131 'Transaction Initiation Date' =>
'transaction_initiation_date',
132 'Transaction Completion Date' =>
'transaction_completion_date',
133 'Transaction Debit or Credit' =>
'transaction_debit_or_credit',
134 'Gross Transaction Amount' =>
'gross_transaction_amount',
135 'Gross Transaction Currency' =>
'gross_transaction_currency',
136 'Fee Debit or Credit' =>
'fee_debit_or_credit',
137 'Fee Amount' =>
'fee_amount',
138 'Fee Currency' =>
'fee_currency',
139 'Custom Field' =>
'custom_field',
140 'Consumer ID' =>
'consumer_id',
141 'Payment Tracking ID' =>
'payment_tracking_id',
142 'Store ID' =>
'store_id',
167 private $dateTimeColumns = [
'transaction_initiation_date',
'transaction_completion_date'];
174 private $amountColumns = [
'gross_transaction_amount',
'fee_amount'];
205 $this->_scopeConfig = $scopeConfig;
228 $this->_dataSaveAllowed =
true;
229 if ($this->
getId()) {
230 if ($this->getLastModified() == $this->getReportLastModified()) {
231 $this->_dataSaveAllowed =
false;
234 $this->setLastModified($this->getReportLastModified());
235 return parent::beforeSave();
251 $localCsv =
'PayPal_STL_' . uniqid(\
Magento\Framework\Math\Random::getRandomNumber()) .
time() .
'.csv';
252 if (
$connection->read($filename, $this->_tmpDirectory->getAbsolutePath($localCsv))) {
253 if (!$this->_tmpDirectory->isWritable($localCsv)) {
254 throw new \Magento\Framework\Exception\LocalizedException(
255 __(
'We cannot create a target file for reading reports.')
259 $encoded = $this->_tmpDirectory->readFile($localCsv);
262 $fileEncoding = mb_detect_encoding($encoded);
264 if (self::FILES_OUT_CHARSET != $fileEncoding) {
265 $decoded = @iconv($fileEncoding, self::FILES_OUT_CHARSET .
'//IGNORE', $encoded);
266 $this->_tmpDirectory->writeFile($localCsv, $decoded);
272 $date = new \DateTime();
273 $lastModified = $date->setTimestamp(
$attributes[
'mtime']);
274 $this->setReportLastModified(
275 $lastModified->format(
'Y-m-d H:i:s')
279 $this->setReportDate(
288 if ($this->getAccountId()) {
292 if ($this->_dataSaveAllowed) {
293 $fetched += count($this->_rows);
297 $this->_tmpDirectory->delete($localCsv);
312 if (!isset(
$config[
'hostname'])
317 throw new \InvalidArgumentException(
'Required config elements: hostname, username, password, path');
319 $connection = new \Magento\Framework\Filesystem\Io\Sftp();
321 [
'host' =>
$config[
'hostname'],
'username' =>
$config[
'username'],
'password' =>
$config[
'password']]
339 $sectionColumns = $this->_csvColumns[
$format][
'section_columns'];
340 $rowMap = $this->_csvColumns[
$format][
'rowmap'];
342 $flippedSectionColumns = array_flip($sectionColumns);
343 $stream = $this->_tmpDirectory->openFile($localCsv,
'r');
344 while ($line = $stream->readCsv()) {
349 $lineType = $line[0];
353 $lastModified = new \DateTime($line[1]);
354 $this->setReportLastModified(
355 $lastModified->format(
'Y-m-d H:i:s')
365 $this->setAccountId($line[3]);
374 $sectionColumns[$line[
$i]] =
$i;
376 $flippedSectionColumns = array_flip($sectionColumns);
380 $this->_rows[] = $this->getBodyItems($line, $flippedSectionColumns, $rowMap);
409 private function getBodyItems(array $line, array $sectionColumns, array $rowMap)
413 if (isset($rowMap[$sectionColumns[
$i]])) {
414 if (in_array($rowMap[$sectionColumns[
$i]], $this->dateTimeColumns)) {
415 $line[
$i] = $this->formatDateTimeColumns($line[
$i]);
417 if (in_array($rowMap[$sectionColumns[
$i]], $this->amountColumns)) {
418 $line[
$i] = $this->formatAmountColumn($line[
$i]);
420 $bodyItem[$rowMap[$sectionColumns[
$i]]] = $line[
$i];
432 private function formatDateTimeColumns($lineItem)
435 $date =
new DateTime($lineItem,
new \DateTimeZone(
'UTC'));
436 return $date->format(\
Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
447 private function formatAmountColumn($lineItem)
449 return intval($lineItem) / 100;
459 $this->
getResource()->loadByAccountAndDate($this, $this->getAccountId(), $this->getReportDate());
484 return __(
'Report Date');
486 return __(
'Merchant Account');
487 case 'transaction_id':
488 return __(
'Transaction ID');
490 return __(
'Invoice ID');
491 case 'paypal_reference_id':
492 return __(
'PayPal Reference ID');
493 case 'paypal_reference_id_type':
494 return __(
'PayPal Reference ID Type');
495 case 'transaction_event_code':
496 return __(
'Event Code');
497 case 'transaction_event':
499 case 'transaction_initiation_date':
500 return __(
'Start Date');
501 case 'transaction_completion_date':
502 return __(
'Finish Date');
503 case 'transaction_debit_or_credit':
504 return __(
'Debit or Credit');
505 case 'gross_transaction_amount':
506 return __(
'Gross Amount');
507 case 'fee_debit_or_credit':
508 return __(
'Fee Debit or Credit');
510 return __(
'Fee Amount');
531 foreach ($this->_storeManager->getStores() as
$store) {
533 $active = $this->_scopeConfig->isSetFlag(
534 'paypal/fetch_reports/active',
538 if (!$active && $automaticMode) {
542 'hostname' => $this->_scopeConfig->getValue(
543 'paypal/fetch_reports/ftp_ip',
547 'path' => $this->_scopeConfig->getValue(
548 'paypal/fetch_reports/ftp_path',
552 'username' => $this->_scopeConfig->getValue(
553 'paypal/fetch_reports/ftp_login',
557 'password' => $this->_scopeConfig->getValue(
558 'paypal/fetch_reports/ftp_password',
562 'sandbox' => $this->_scopeConfig->getValue(
563 'paypal/fetch_reports/ftp_sandbox',
568 if (empty($cfg[
'username']) || empty($cfg[
'password'])) {
571 if (empty($cfg[
'hostname']) || $cfg[
'sandbox']) {
574 if (empty($cfg[
'path']) || $cfg[
'sandbox']) {
578 if (in_array($this->serializer->serialize($cfg), $uniques)) {
581 $uniques[] = $this->serializer->serialize($cfg);
596 $dateSnippet = substr(basename($filename), 4, 8);
597 $result = substr($dateSnippet, 0, 4) .
'-' . substr($dateSnippet, 4, 2) .
'-' . substr($dateSnippet, 6, 2);
610 $pattern =
'/^STL-(\d{8,8})\.(\d{2,2})\.(.{3,3})\.CSV$/';
611 foreach ($list as $filename =>
$data) {
612 if (preg_match(
$pattern, $filename)) {
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Filesystem $filesystem, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], \Magento\Framework\Serialize\Serializer\Json $serializer=null)
_fileNameToDate($filename)
const SANDBOX_REPORTS_HOSTNAME
static createConnection(array $config)
fetchAndSave(\Magento\Framework\Filesystem\Io\Sftp $connection)
_filterReportsList($list)
getSftpCredentials($automaticMode=false)
parseCsv($localCsv, $format='new')