We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

Commit 8159aba3 authored by Alessio Zampatti's avatar Alessio Zampatti

Check if order-billingagreement relation exists in...

Check if order-billingagreement relation exists in paypal_billing_agreement_order table before adding it
parent e6319d80
......@@ -113,6 +113,11 @@ class Data extends AbstractHelper
*/
protected $billingAgreementFactory;
/**
* @var ResourceModel\Billing\Agreement
*/
private $agreementResourceModel;
/**
* Data constructor.
* @param \Magento\Framework\App\Helper\Context $context
......@@ -131,6 +136,7 @@ class Data extends AbstractHelper
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\CacheInterface $cache
* @param \Adyen\Payment\Model\Billing\AgreementFactory $billingAgreementFactory
* @param \Adyen\Payment\Model\ResourceModel\Billing\Agreement $agreementResourceModel
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
......@@ -148,7 +154,8 @@ class Data extends AbstractHelper
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\CacheInterface $cache,
\Adyen\Payment\Model\Billing\AgreementFactory $billingAgreementFactory
\Adyen\Payment\Model\Billing\AgreementFactory $billingAgreementFactory,
\Adyen\Payment\Model\ResourceModel\Billing\Agreement $agreementResourceModel
) {
parent::__construct($context);
$this->_encryptor = $encryptor;
......@@ -166,6 +173,7 @@ class Data extends AbstractHelper
$this->storeManager = $storeManager;
$this->cache = $cache;
$this->billingAgreementFactory = $billingAgreementFactory;
$this->agreementResourceModel = $agreementResourceModel;
}
/**
......@@ -1463,8 +1471,12 @@ class Data extends AbstractHelper
$billingAgreement->setCcBillingAgreement($additionalData);
if ($billingAgreement->isValid()) {
// save into sales_billing_agreement_order
$billingAgreement->addOrderRelation($order);
if (!$this->agreementResourceModel->getOrderRelation($billingAgreement->getAgreementId(),
$order->getId())) {
// save into sales_billing_agreement_order
$billingAgreement->addOrderRelation($order);
}
// add to order to save agreement
$order->addRelatedObject($billingAgreement);
} else {
......
......@@ -209,9 +209,13 @@ class Cron
*/
private $orderRepository;
/**
* @var ResourceModel\Billing\Agreement
*/
private $agreementResourceModel;
/**
* Cron constructor.
*
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
* @param ResourceModel\Notification\CollectionFactory $notificationFactory
......@@ -230,6 +234,7 @@ class Cron
* @param \Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory $orderStatusCollection
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param OrderRepository $orderRepository
* @param ResourceModel\Billing\Agreement $agreementResourceModel
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
......@@ -249,7 +254,8 @@ class Cron
AreaList $areaList,
\Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory $orderStatusCollection,
SearchCriteriaBuilder $searchCriteriaBuilder,
OrderRepository $orderRepository
OrderRepository $orderRepository,
\Adyen\Payment\Model\ResourceModel\Billing\Agreement $agreementResourceModel
) {
$this->_scopeConfig = $scopeConfig;
$this->_adyenLogger = $adyenLogger;
......@@ -269,6 +275,7 @@ class Cron
$this->_orderStatusCollection = $orderStatusCollection;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->orderRepository = $orderRepository;
$this->agreementResourceModel = $agreementResourceModel;
}
/**
......@@ -1019,8 +1026,13 @@ class Cron
// Populate billing agreement data
$billingAgreement->parseRecurringContractData($contractDetail);
if ($billingAgreement->isValid()) {
// save into sales_billing_agreement_order
$billingAgreement->addOrderRelation($this->_order);
if (!$this->agreementResourceModel->getOrderRelation($billingAgreement->getAgreementId(),
$this->_order->getId())) {
// save into sales_billing_agreement_order
$billingAgreement->addOrderRelation($this->_order);
}
// add to order to save agreement
$this->_order->addRelatedObject($billingAgreement);
......
......@@ -28,5 +28,14 @@ namespace Adyen\Payment\Model\ResourceModel\Billing;
*/
class Agreement extends \Magento\Paypal\Model\ResourceModel\Billing\Agreement
{
public function getOrderRelation($agreementId, $orderId)
{
$select = $this->getConnection()->select()
->from(['billingagreement_order' => $this->getTable('paypal_billing_agreement_order')])
->where('billingagreement_order.agreement_id=?', $agreementId)
->where('billingagreement_order.order_id=?', $orderId);
return $this->getConnection()->fetchAll($select);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment