Commit fc46d74a authored by Rik ter Beek's avatar Rik ter Beek

oneclick based on the listRecurringDetails api call for demo purposes

parent 331edac8
......@@ -90,6 +90,11 @@ class PaymentMethods extends AbstractHelper
*/
protected $_themeProvider;
/**
* @var \Adyen\Payment\Model\Api\PaymentRequest
*/
protected $_adyenPaymentRequest;
/**
* PaymentMethods constructor.
*
......@@ -118,7 +123,8 @@ class PaymentMethods extends AbstractHelper
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\View\Asset\Source $assetSource,
\Magento\Framework\View\DesignInterface $design,
\Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider
\Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider,
\Adyen\Payment\Model\Api\PaymentRequest $paymentRequest
) {
$this->_quoteRepository = $quoteRepository;
$this->_quoteIdMaskFactory = $quoteIdMaskFactory;
......@@ -132,6 +138,7 @@ class PaymentMethods extends AbstractHelper
$this->_assetSource = $assetSource;
$this->_design = $design;
$this->_themeProvider = $themeProvider;
$this->_adyenPaymentRequest = $paymentRequest;
}
/**
......@@ -454,4 +461,109 @@ class PaymentMethods extends AbstractHelper
{
return $this->_session->getQuote();
}
/**
* Get payment methods from the listRecurringDetails API
*
* @param $customerId
* @param $storeId
* @param $grandTotal
* @param $recurringType
* @return array
* @throws \Exception
*/
public function getOneClickPaymentMethods($customerId, $storeId, $grandTotal, $recurringType)
{
$billingAgreements = [];
// get the billing agreements from the listRecurringDetails call
$recurringContracts = $this->_adyenPaymentRequest->getRecurringContractsForShopper($customerId,$storeId);
// print_r($recurringContracts);
foreach ($recurringContracts as $recurringDetailReference => $agreementData) {
// $agreementData = $billingAgreement->getAgreementData();
// no agreementData and contractType then ignore
if ((!is_array($agreementData)) || (!isset($agreementData['contractTypes']))) {
continue;
}
// check if contractType is supporting the selected contractType for OneClick payments
$allowedContractTypes = $agreementData['contractTypes'];
if (in_array($recurringType, $allowedContractTypes)) {
// allow only cards
if(isset($agreementData['card']['number'])) {
// only cards
$label = __('%1, %2, **** %3',
$agreementData['variant'],
$agreementData['card']['holderName'],
$agreementData['card']['number'],
$agreementData['card']['expiryMonth'],
$agreementData['card']['expiryYear']
);
$data = [
'reference_id' => $recurringDetailReference,
'agreement_label' => $label,
'agreement_data' => $agreementData
];
if ($this->_adyenHelper->showLogos()) {
$logoName = $agreementData['variant'];
$asset = $this->_adyenHelper->createAsset(
'Adyen_Payment::images/logos/' . $logoName . '.png'
);
$icon = null;
$placeholder = $this->_assetSource->findSource($asset);
if ($placeholder) {
list($width, $height) = getimagesize($asset->getSourceFile());
$icon = [
'url' => $asset->getUrl(),
'width' => $width,
'height' => $height
];
}
$data['logo'] = $icon;
}
/**
* Check if there are installments for this creditcard type defined
*/
$data['number_of_installments'] = 0;
$ccType = $this->_adyenHelper->getMagentoCreditCartType($agreementData['variant']);
$installments = null;
$installmentsValue = $this->_adyenHelper->getAdyenCcConfigData('installments');
if ($installmentsValue) {
$installments = unserialize($installmentsValue);
}
if ($installments) {
$numberOfInstallments = null;
foreach ($installments as $ccTypeInstallment => $installment) {
if ($ccTypeInstallment == $ccType) {
foreach ($installment as $amount => $installments) {
if ($grandTotal <= $amount) {
$numberOfInstallments = $installments;
}
}
}
}
if ($numberOfInstallments) {
$data['number_of_installments'] = $numberOfInstallments;
}
}
$billingAgreements[] = $data;
}
}
}
return $billingAgreements;
}
}
\ No newline at end of file
......@@ -36,12 +36,16 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
*/
protected $config;
/**
* @var \Adyen\Payment\Helper\Data
*/
protected $_adyenHelper;
/**
* @var \Adyen\Payment\Helper\PaymentMethods
*/
protected $_adyenPaymentMethodsHelper;
/**
* @var \Magento\Framework\App\RequestInterface
*/
......@@ -76,14 +80,17 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
* AdyenOneclickConfigProvider constructor.
*
* @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Adyen\Payment\Helper\PaymentMethods $adyenPaymentMethodsHelper
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Checkout\Model\Session $session
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Payment\Model\CcConfig $ccConfig
*/
public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Helper\PaymentMethods $adyenPaymentMethodsHelper,
\Magento\Framework\App\RequestInterface $request,
\Magento\Customer\Model\Session $customerSession,
\Magento\Checkout\Model\Session $session,
......@@ -98,6 +105,7 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
$this->_storeManager = $storeManager;
$this->_urlBuilder = $urlBuilder;
$this->ccConfig = $ccConfig;
$this->_adyenPaymentMethodsHelper = $adyenPaymentMethodsHelper;
}
/**
......@@ -177,7 +185,7 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
$grandTotal = $this->_getQuote()->getGrandTotal();
$recurringType = $this->_getRecurringContractType();
$billingAgreements = $this->_adyenHelper->getOneClickPaymentMethods(
$billingAgreements = $this->_adyenPaymentMethodsHelper->getOneClickPaymentMethods(
$customerId,
$storeId,
$grandTotal,
......
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