Commit df8d2cb2 authored by stkams's avatar stkams Committed by Alessio Zampatti

Add setting to share billing agreements per store. Improved error log… (#372)

* Add setting to share billing agreements per store. Improved error logging. Add a possibility for UCD to retrieve the customer ID for the billing agreement in a different way.

* The originKey can depend on store-level settings (like demo/live mode) in rare cases like the UCD

* Fixed some minor code issues
parent e5de8538
......@@ -64,7 +64,7 @@ class Data extends AbstractHelper
protected $_billingAgreementCollectionFactory;
/**
* @var Repository
* @var \Magento\Framework\View\Asset\Repository
*/
protected $_assetRepo;
......@@ -353,8 +353,8 @@ class Data extends AbstractHelper
/**
* Fix this one string street + number
* @example street + number
* @param type $street
* @return type $street
* @param array $street
* @return array $street
*/
public static function formatStreet($street)
{
......@@ -858,7 +858,9 @@ class Data extends AbstractHelper
$baCollection = $this->_billingAgreementCollectionFactory->create();
$baCollection->addFieldToFilter('customer_id', $customerId);
$baCollection->addFieldToFilter('store_id', $storeId);
if ($this->isPerStoreBillingAgreement($storeId)) {
$baCollection->addFieldToFilter('store_id', $storeId);
}
$baCollection->addFieldToFilter('method_code', 'adyen_oneclick');
$baCollection->addActiveFilter();
......@@ -941,6 +943,9 @@ class Data extends AbstractHelper
return $billingAgreements;
}
public function isPerStoreBillingAgreement($storeId) {
return !$this->getAdyenOneclickConfigDataFlag('share_billing_agreement', $storeId);
}
/**
* @param $paymentMethod
......@@ -1330,7 +1335,7 @@ class Data extends AbstractHelper
}
/**
* @param \Adyen\Clien $client
* @param \Adyen\Client $client
* @return \Adyen\Service\PosPayment
* @throws \Adyen\AdyenException
*/
......@@ -1359,13 +1364,12 @@ class Data extends AbstractHelper
$baseUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
$parsed = parse_url($baseUrl);
$domain = $parsed['scheme'] . "://" . $parsed['host'];
$storeId = $this->storeManager->getStore()->getId();
$cacheKey = 'Adyen_origin_key_for_' . $domain . '_' . $storeId;
if (!$originKey = $this->cache->load("Adyen_origin_key_for_" . $domain)) {
$originKey = "";
$storeId = $this->storeManager->getStore()->getId();
if (!$originKey = $this->cache->load($cacheKey)) {
if ($originKey = $this->getOriginKeyForUrl($domain, $storeId)) {
$this->cache->save($originKey, "Adyen_origin_key_for_" . $domain, array(), 60 * 60 * 24);
$this->cache->save($originKey, $cacheKey, array(), 60 * 60 * 24);
}
}
......@@ -1416,7 +1420,7 @@ class Data extends AbstractHelper
}
/**
* @param \Adyen\Clien $client
* @param \Adyen\Client $client
* @return \Adyen\Service\CheckoutUtility
* @throws \Adyen\AdyenException
*/
......@@ -1448,6 +1452,7 @@ class Data extends AbstractHelper
$listRecurringContracts = null;
try {
// Get or create billing agreement
/** @var \Adyen\Payment\Model\Billing\Agreement $billingAgreement */
$billingAgreement = $this->billingAgreementFactory->create();
$billingAgreement->load($additionalData['recurring.recurringDetailReference'], 'reference_id');
......@@ -1458,6 +1463,9 @@ class Data extends AbstractHelper
$billingAgreement = $this->billingAgreementFactory->create();
$billingAgreement->setStoreId($order->getStoreId());
$billingAgreement->importOrderPayment($order->getPayment());
if ($billingAgreement->getCustomerId() === null) {
$billingAgreement->setCustomerId($this->getCustomerId($order));
}
$message = __('Created billing agreement #%1.',
$additionalData['recurring.recurringDetailReference']);
} else {
......@@ -1480,7 +1488,7 @@ class Data extends AbstractHelper
// add to order to save agreement
$order->addRelatedObject($billingAgreement);
} else {
$message = __('Failed to create billing agreement for this order.');
$message = __('Failed to create billing agreement for this order. Reason(s): ') . join(', ', $billingAgreement->getErrors());
throw new \Exception($message);
}
......@@ -1495,6 +1503,15 @@ class Data extends AbstractHelper
}
}
/**
* Method can be used by interceptors to provide the customer ID in a different way.
* @param \Magento\Sales\Model\Order $order
* @return int|null
*/
public function getCustomerId(\Magento\Sales\Model\Order $order) {
return $order->getCustomerId();
}
/**
* For backwards compatibility get the recurringType used for HPP + current billing agreements
*
......@@ -1515,6 +1532,5 @@ class Data extends AbstractHelper
} else {
return \Adyen\Payment\Model\RecurringType::NONE;
}
return null;
}
}
......@@ -235,4 +235,8 @@ class Agreement extends \Magento\Paypal\Model\Billing\Agreement
return $this;
}
public function getErrors() {
return $this->_errors;
}
}
......@@ -49,5 +49,14 @@
<source_model>Adyen\Payment\Model\Config\Source\RecurringPaymentType</source_model>
<config_path>payment/adyen_oneclick/recurring_payment_type</config_path>
</field>
<group id="adyen_oneclick_advanced_settings" translate="label" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="200">
<label>Advanced Settings</label>
<frontend_model>Magento\Config\Block\System\Config\Form\Fieldset</frontend_model>
<field id="title" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Share billing agreements between stores</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/adyen_oneclick/share_billing_agreement</config_path>
</field>
</group>
</group>
</include>
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