201 lines
7.2 KiB
PHP
201 lines
7.2 KiB
PHP
<?php
|
|
/**
|
|
* Integrates BTCPay Server with Magento 2 for online payments
|
|
* Copyright (C) 2019 Storefront BVBA
|
|
*
|
|
* This file is part of Storefront/BTCPay.
|
|
*
|
|
* Storefront/BTCPay is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace Storefront\BTCPay\Model;
|
|
|
|
use Storefront\BTCPay\Api\TransactionRepositoryInterface;
|
|
use Storefront\BTCPay\Api\Data\TransactionSearchResultsInterfaceFactory;
|
|
use Storefront\BTCPay\Api\Data\TransactionInterfaceFactory;
|
|
use Magento\Framework\Api\DataObjectHelper;
|
|
use Magento\Framework\Exception\CouldNotDeleteException;
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Magento\Framework\Exception\CouldNotSaveException;
|
|
use Magento\Framework\Reflection\DataObjectProcessor;
|
|
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
|
|
use Storefront\BTCPay\Model\ResourceModel\Transaction as ResourceTransaction;
|
|
use Storefront\BTCPay\Model\ResourceModel\Transaction\CollectionFactory as TransactionCollectionFactory;
|
|
use Magento\Store\Model\StoreManagerInterface;
|
|
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
|
|
use Magento\Framework\Api\ExtensibleDataObjectConverter;
|
|
|
|
class TransactionRepository implements TransactionRepositoryInterface
|
|
{
|
|
|
|
protected $resource;
|
|
|
|
protected $transactionFactory;
|
|
|
|
protected $transactionCollectionFactory;
|
|
|
|
protected $searchResultsFactory;
|
|
|
|
protected $dataObjectHelper;
|
|
|
|
protected $dataObjectProcessor;
|
|
|
|
protected $dataTransactionFactory;
|
|
|
|
protected $extensionAttributesJoinProcessor;
|
|
|
|
private $storeManager;
|
|
|
|
private $collectionProcessor;
|
|
|
|
protected $extensibleDataObjectConverter;
|
|
|
|
/**
|
|
* @param ResourceTransaction $resource
|
|
* @param TransactionFactory $transactionFactory
|
|
* @param TransactionInterfaceFactory $dataTransactionFactory
|
|
* @param TransactionCollectionFactory $transactionCollectionFactory
|
|
* @param TransactionSearchResultsInterfaceFactory $searchResultsFactory
|
|
* @param DataObjectHelper $dataObjectHelper
|
|
* @param DataObjectProcessor $dataObjectProcessor
|
|
* @param StoreManagerInterface $storeManager
|
|
* @param CollectionProcessorInterface $collectionProcessor
|
|
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
|
|
* @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
|
|
*/
|
|
public function __construct(
|
|
ResourceTransaction $resource,
|
|
TransactionFactory $transactionFactory,
|
|
TransactionInterfaceFactory $dataTransactionFactory,
|
|
TransactionCollectionFactory $transactionCollectionFactory,
|
|
TransactionSearchResultsInterfaceFactory $searchResultsFactory,
|
|
DataObjectHelper $dataObjectHelper,
|
|
DataObjectProcessor $dataObjectProcessor,
|
|
StoreManagerInterface $storeManager,
|
|
CollectionProcessorInterface $collectionProcessor,
|
|
JoinProcessorInterface $extensionAttributesJoinProcessor,
|
|
ExtensibleDataObjectConverter $extensibleDataObjectConverter
|
|
) {
|
|
$this->resource = $resource;
|
|
$this->transactionFactory = $transactionFactory;
|
|
$this->transactionCollectionFactory = $transactionCollectionFactory;
|
|
$this->searchResultsFactory = $searchResultsFactory;
|
|
$this->dataObjectHelper = $dataObjectHelper;
|
|
$this->dataTransactionFactory = $dataTransactionFactory;
|
|
$this->dataObjectProcessor = $dataObjectProcessor;
|
|
$this->storeManager = $storeManager;
|
|
$this->collectionProcessor = $collectionProcessor;
|
|
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
|
|
$this->extensibleDataObjectConverter = $extensibleDataObjectConverter;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function save(
|
|
\Storefront\BTCPay\Api\Data\TransactionInterface $transaction
|
|
) {
|
|
/* if (empty($transaction->getStoreId())) {
|
|
$storeId = $this->storeManager->getStore()->getId();
|
|
$transaction->setStoreId($storeId);
|
|
} */
|
|
|
|
$transactionData = $this->extensibleDataObjectConverter->toNestedArray(
|
|
$transaction,
|
|
[],
|
|
\Storefront\BTCPay\Api\Data\TransactionInterface::class
|
|
);
|
|
|
|
$transactionModel = $this->transactionFactory->create()->setData($transactionData);
|
|
|
|
try {
|
|
$this->resource->save($transactionModel);
|
|
} catch (\Exception $exception) {
|
|
throw new CouldNotSaveException(__(
|
|
'Could not save the BTC Pay transaction: %1',
|
|
$exception->getMessage()
|
|
));
|
|
}
|
|
return $transactionModel->getDataModel();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getById($transactionId)
|
|
{
|
|
$transaction = $this->transactionFactory->create();
|
|
$this->resource->load($transaction, $transactionId);
|
|
if (!$transaction->getId()) {
|
|
throw new NoSuchEntityException(__('BTCPay transaction with id "%1" does not exist.', $transactionId));
|
|
}
|
|
return $transaction->getDataModel();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getList(
|
|
\Magento\Framework\Api\SearchCriteriaInterface $criteria
|
|
) {
|
|
$collection = $this->transactionCollectionFactory->create();
|
|
|
|
$this->extensionAttributesJoinProcessor->process(
|
|
$collection,
|
|
\Storefront\BTCPay\Api\Data\TransactionInterface::class
|
|
);
|
|
|
|
$this->collectionProcessor->process($criteria, $collection);
|
|
|
|
$searchResults = $this->searchResultsFactory->create();
|
|
$searchResults->setSearchCriteria($criteria);
|
|
|
|
$items = [];
|
|
foreach ($collection as $model) {
|
|
$items[] = $model->getDataModel();
|
|
}
|
|
|
|
$searchResults->setItems($items);
|
|
$searchResults->setTotalCount($collection->getSize());
|
|
return $searchResults;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function delete(
|
|
\Storefront\BTCPay\Api\Data\TransactionInterface $transaction
|
|
) {
|
|
try {
|
|
$transactionModel = $this->transactionFactory->create();
|
|
$this->resource->load($transactionModel, $transaction->getTransactionId());
|
|
$this->resource->delete($transactionModel);
|
|
} catch (\Exception $exception) {
|
|
throw new CouldNotDeleteException(__(
|
|
'Could not delete the BTCPay transaction ID: %1',
|
|
$exception->getMessage()
|
|
));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function deleteById($transactionId)
|
|
{
|
|
return $this->delete($this->getById($transactionId));
|
|
}
|
|
}
|