Commit 619e2073 authored by Alessio Zampatti's avatar Alessio Zampatti Committed by Rik ter Beek

Added createAdyenBillingAgreement function in helper, calls...

Added createAdyenBillingAgreement function in helper, calls setCcBillingAgreement in billing agreement model
parent fa758982
...@@ -31,6 +31,11 @@ class RecurringDataBuilder implements BuilderInterface ...@@ -31,6 +31,11 @@ class RecurringDataBuilder implements BuilderInterface
*/ */
private $adyenHelper; private $adyenHelper;
/**
* @var \Adyen\Payment\Logger\AdyenLogger
*/
protected $_adyenLogger;
/** /**
* @var \Magento\Framework\App\State * @var \Magento\Framework\App\State
*/ */
...@@ -44,10 +49,12 @@ class RecurringDataBuilder implements BuilderInterface ...@@ -44,10 +49,12 @@ class RecurringDataBuilder implements BuilderInterface
*/ */
public function __construct( public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper, \Adyen\Payment\Helper\Data $adyenHelper,
\Magento\Framework\Model\Context $context \Magento\Framework\Model\Context $context,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger
) { ) {
$this->adyenHelper = $adyenHelper; $this->adyenHelper = $adyenHelper;
$this->appState = $context->getAppState(); $this->appState = $context->getAppState();
$this->_adyenLogger = $adyenLogger;
} }
/** /**
...@@ -93,14 +100,17 @@ class RecurringDataBuilder implements BuilderInterface ...@@ -93,14 +100,17 @@ class RecurringDataBuilder implements BuilderInterface
} }
} else { } else {
$recurringContractType = $recurringType; $recurringContractType = $recurringType;
$result['paymentMethod']['storeDetails'] = "true";
} }
} }
// $this->_adyenLogger->addAdyenDebug("recurring?: ". $recurringContractType);
// only when recurringContractType is set and when a customer is loggedIn
if ($recurringContractType && $customerId > 0) { // // only when recurringContractType is set and when a customer is loggedIn
$recurring = ['contract' => $recurringContractType]; // if ($recurringContractType && $customerId > 0) {
$result['recurring'] = $recurring; // $recurring = ['contract' => $recurringContractType];
} // $result['recurring'] = $recurring;
// }
return $result; return $result;
} }
......
...@@ -27,6 +27,14 @@ use Magento\Payment\Gateway\Response\HandlerInterface; ...@@ -27,6 +27,14 @@ use Magento\Payment\Gateway\Response\HandlerInterface;
class CheckoutPaymentsDetailsHandler implements HandlerInterface class CheckoutPaymentsDetailsHandler implements HandlerInterface
{ {
public function __construct(
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Adyen\Payment\Helper\Data $adyenHelper
) {
$this->_adyenLogger = $adyenLogger;
$this->_adyenHelper = $adyenHelper;
}
/** /**
* @param array $handlingSubject * @param array $handlingSubject
* @param array $response * @param array $response
...@@ -53,6 +61,12 @@ class CheckoutPaymentsDetailsHandler implements HandlerInterface ...@@ -53,6 +61,12 @@ class CheckoutPaymentsDetailsHandler implements HandlerInterface
$payment->setTransactionId($response['pspReference']); $payment->setTransactionId($response['pspReference']);
} }
if (!empty($response['additionalData']) && !empty($response['additionalData']['recurring.recurringDetailReference'])){
$order = $payment->getOrder();
$this->_adyenHelper->createAdyenBillingAgreement($order, $response['additionalData']);
}
// do not close transaction so you can do a cancel() and void // do not close transaction so you can do a cancel() and void
$payment->setIsTransactionClosed(false); $payment->setIsTransactionClosed(false);
$payment->setShouldCloseParentTransaction(false); $payment->setShouldCloseParentTransaction(false);
......
...@@ -108,6 +108,11 @@ class Data extends AbstractHelper ...@@ -108,6 +108,11 @@ class Data extends AbstractHelper
*/ */
protected $cache; protected $cache;
/**
* @var \Adyen\Payment\Model\Billing\AgreementFactory
*/
protected $_billingAgreementFactory;
/** /**
* Data constructor. * Data constructor.
* *
...@@ -143,6 +148,11 @@ class Data extends AbstractHelper ...@@ -143,6 +148,11 @@ class Data extends AbstractHelper
\Adyen\Payment\Logger\AdyenLogger $adyenLogger, \Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\CacheInterface $cache \Magento\Framework\App\CacheInterface $cache
\Magento\Framework\App\ProductMetadataInterface $productMetadata,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\CacheInterface $cache,
\Adyen\Payment\Model\Billing\AgreementFactory $billingAgreementFactory
) { ) {
parent::__construct($context); parent::__construct($context);
$this->_encryptor = $encryptor; $this->_encryptor = $encryptor;
...@@ -1441,4 +1451,53 @@ class Data extends AbstractHelper ...@@ -1441,4 +1451,53 @@ class Data extends AbstractHelper
return self::CHECKOUT_COMPONENT_JS_LIVE; return self::CHECKOUT_COMPONENT_JS_LIVE;
} }
public function createAdyenBillingAgreement($order, $additionalData){
$storeId = $order->getStoreId();
$customerReference = $order->getCustomerId();
$listRecurringContracts = null;
try {
// Get or create billing agreement
$billingAgreement = $this->_billingAgreementFactory->create();
$billingAgreement->load($additionalData['recurring.recurringDetailReference'], 'reference_id');
// check if BA exists
if (!($billingAgreement && $billingAgreement->getAgreementId() > 0 && $billingAgreement->isValid())) {
// create new
$this->adyenLogger->addNotificationLog("Creating new Billing Agreement");
$billingAgreement = $this->_billingAgreementFactory->create();
$billingAgreement->setStoreId($order->getStoreId());
$billingAgreement->importOrderPayment($order->getPayment());
$message = __('Created billing agreement #%1.', $additionalData['recurring.recurringDetailReference']);
} else {
$billingAgreement->setIsObjectChanged(true);
$message = __('Updated billing agreement #%1.', $additionalData['recurring.recurringDetailReference']);
}
// Populate billing agreement data
$billingAgreement->setCcBillingAgreement($additionalData);
if ($billingAgreement->isValid()) {
// save into sales_billing_agreement_order
$billingAgreement->addOrderRelation($order);
// add to order to save agreement
$order->addRelatedObject($billingAgreement);
} else {
$message = __('Failed to create billing agreement for this order.');
throw new \Exception($message);
}
} catch (\Exception $exception) {
$message = $exception->getMessage();
$this->adyenLogger->addAdyenDebug("exception: " . $message);
}
$comment = $order->addStatusHistoryComment($message);
$order->addRelatedObject($comment);
}
} }
...@@ -166,4 +166,58 @@ class Agreement extends \Magento\Paypal\Model\Billing\Agreement ...@@ -166,4 +166,58 @@ class Agreement extends \Magento\Paypal\Model\Billing\Agreement
{ {
return json_decode($this->getData('agreement_data'), true); return json_decode($this->getData('agreement_data'), true);
} }
public function setCcBillingAgreement($contractDetail)
{
$this
->setMethodCode('adyen_oneclick')
->setReferenceId($contractDetail['recurring.recurringDetailReference']);
// Billing agreement is CC
/**
* $contractDetail['cardBin'] = $cardBin;
* $contractDetail['recurringDetailReference'] = $recurringDetailReference;
* $contractDetail['cardHolderName'] = $cardHolderName;
* $contractDetail['cardSummary'] = $cardSummary;
* $contractDetail['expiryDate'] = $expiryDate;
* $contractDetail['paymentMethod'] = $paymentMethod;
*/
if (isset($contractDetail['cardBin']) &&
isset($contractDetail['cardHolderName']) &&
isset($contractDetail['cardSummary']) &&
isset($contractDetail['expiryDate']) &&
isset($contractDetail['paymentMethod'])) {
$ccType = $contractDetail['paymentMethod'];
$ccTypes = $this->_adyenHelper->getCcTypesAltData();
if (isset($ccTypes[$ccType])) {
$ccType = $ccTypes[$ccType]['name'];
}
$label = __('%1, %2, **** %3',
$ccType,
$contractDetail['cardHolderName'],
$contractDetail['cardSummary']
);
$this->setAgreementLabel($label);
}
$expiryDate = explode('/', $contractDetail['expiryDate']);
$recurringType = $this->_adyenHelper->getAdyenAbstractConfigData('recurring_type');
$agreementData = [
'card' => [
'holderName' => $contractDetail['cardHolderName'],
'number' => $contractDetail['cardSummary'],
'expiryMonth' => $expiryDate[0],
'expiryYear' => $expiryDate[1]
],
'variant' => $contractDetail['paymentMethod'],
'contractTypes' => explode(',', $recurringType)
];
$this->setAgreementData($agreementData);
return $this;
}
} }
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