Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PW-1661 Implement local payment methods #39

Merged
merged 34 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
5aa040a
Use the PrestaShop Service Locator
Oct 17, 2019
49da1ec
Clean up code
Oct 17, 2019
6ef4617
Add a wrapper for ServiceLocator
Oct 25, 2019
1fe78eb
Use the wrapped ServiceLocator
Oct 25, 2019
5801d68
Show up Local Payment Methods
Oct 25, 2019
8a51f0b
PW-1637 (#27)
msilvagarcia Oct 21, 2019
c901a18
Add PHP Code Sniffer (#29)
msilvagarcia Oct 24, 2019
487b551
Create CODEOWNERS (#28)
KadoBOT Oct 24, 2019
0c18d9f
Updated copyright (#30)
Aleffio Oct 24, 2019
01e9821
Merge remote-tracking branch 'remotes/origin/develop' into PW-1761
Oct 25, 2019
ff8c253
Rename variable
Oct 30, 2019
09a9b22
Extract methods
Oct 30, 2019
bda7a4e
PW-1761 Display Local Payment Methods (#32)
msilvagarcia Oct 31, 2019
713e722
Fix condition with `empty`
Nov 1, 2019
6f41611
Redirect shopper to external authorisation
Nov 1, 2019
1ce72f2
Merge branch 'PW-1661' into PW-1762
msilvagarcia Nov 1, 2019
bf8778f
Display and log error on uncaught response codes
Nov 5, 2019
9a0a059
Merge remote-tracking branch 'origin/PW-1762' into PW-1762
Nov 5, 2019
8f50d4a
Remove recurring data
msilvagarcia Nov 6, 2019
143648e
PW-1762 Redirect shopper to external authorization (#35)
msilvagarcia Nov 6, 2019
1dc930f
Merge remote-tracking branch 'remotes/origin/develop' into PW-1661
Nov 6, 2019
1db8c8b
Fix ServiceLocator calls
Nov 6, 2019
f78649f
Setup a default value for `ADYEN_MODE`
Nov 6, 2019
780d377
Merge remote-tracking branch 'remotes/origin/develop' into PW-1661
Nov 6, 2019
c0473d2
Fix escaping on JSON string
Nov 7, 2019
1cf700e
Restrict Local Payment Methods to iDeal
Nov 7, 2019
c557935
Use ServiceLocator
Nov 7, 2019
3444b78
Start session by default
Nov 7, 2019
25b87b3
Refactor to ServiceLocator
Nov 7, 2019
5174b5a
Implement iDeal processing
Nov 7, 2019
8df65a0
Instantiate `helperData`
msilvagarcia Nov 8, 2019
a8bb9e4
Fix problem with 3DS
Nov 8, 2019
e5f4475
Remove restriction with iDeal
Nov 11, 2019
ec0add0
Merge remote-tracking branch 'origin/PW-1661' into PW-1661
Nov 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 172 additions & 52 deletions adyen.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Adyen extends PaymentModule
/**
* Adyen constructor.
*
* @throws Adyen\AdyenException
* @throws \PrestaShop\PrestaShop\Adapter\CoreException
*/
public function __construct()
{
Expand All @@ -77,18 +77,12 @@ public function __construct()
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->currencies = true;

$adyenRunningMode = \Adyen\Environment::TEST;
if (!empty(\Configuration::get('ADYEN_MODE'))) {
$adyenRunningMode = \Configuration::get('ADYEN_MODE');
}

$adyenHelperFactory = new Adyen\PrestaShop\service\helper\DataFactory();
$this->helper_data = $adyenHelperFactory->createAdyenHelperData(
$adyenRunningMode,
_COOKIE_KEY_
$this->helper_data = \Adyen\PrestaShop\service\adapter\classes\ServiceLocator::get(
'Adyen\PrestaShop\helper\Data'
);
$this->hashing = \Adyen\PrestaShop\service\adapter\classes\ServiceLocator::get(
'Adyen\PrestaShop\model\Hashing'
);

$this->hashing = new Adyen\PrestaShop\model\Hashing();

// start for 1.6
$this->is_eu_compatible = 1;
Expand Down Expand Up @@ -627,6 +621,52 @@ public function hookPaymentOptions()
}
}

if (!empty($paymentMethods['paymentMethods'])) {
foreach ($paymentMethods['paymentMethods'] as $paymentMethod) {
$issuerList = [];
// restrict to iDeal only for now
if (empty($paymentMethod['type']) || $paymentMethod['type'] == 'scheme' || $paymentMethod['type'] != 'ideal') {
continue;
}
if (!empty($paymentMethod['details'])) {
foreach ($paymentMethod['details'] as $paymentMethodDetails) {
if (key_exists('key', $paymentMethodDetails) && $paymentMethodDetails['key'] == 'issuer') {
$issuerList = $paymentMethodDetails['items'];
break;
}
}
}
$this->context->smarty->assign(
array(
'locale' => $this->helper_data->getLocale($this->context->language),
'originKey' => $this->helper_data->getOriginKeyForOrigin(),
'environment' => Configuration::get('ADYEN_MODE'),
'issuerList' => json_encode($issuerList),
'paymentMethodType' => $paymentMethod['type'],
'paymentMethodName' => $paymentMethod['name'],
'paymentProcessUrl' => $this->context->link->getModuleLink(
$this->name,
'Payment',
array(),
true
),
'renderPayButton' => false,
)
);
$localPaymentMethod = new PrestaShop\PrestaShop\Core\Payment\PaymentOption();
$localPaymentMethod->setCallToActionText($this->l('Pay by ' . $paymentMethod['name']))
->setForm(
$this->context->smarty->fetch(
_PS_MODULE_DIR_ . $this->name . '/views/templates/front/local-payment-method.tpl'
)
)
->setAction(
$this->context->link->getModuleLink($this->name, 'Payment', array(), true)
);

$payment_options[] = $localPaymentMethod;
}
}

$embeddedOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption();

Expand Down Expand Up @@ -668,43 +708,14 @@ public function hookPayment()
$payments = "";
$paymentMethods = $this->helper_data->fetchPaymentMethods($this->context->cart, $this->context->language);
if (!$this->context->customer->is_guest && !empty($paymentMethods['oneClickPaymentMethods'])) {
$oneClickPaymentMethods = $paymentMethods['oneClickPaymentMethods'];
foreach ($oneClickPaymentMethods as $storedCard) {
if (!empty($storedCard["storedDetails"]["card"])) {
$this->context->smarty->assign(
array(
'locale' => $this->helper_data->getLocale($this->context->language),
'originKey' => $this->helper_data->getOriginKeyForOrigin(),
'environment' => Configuration::get('ADYEN_MODE'),
'paymentProcessUrl' => $this->context->link->getModuleLink($this->name, 'Payment', array(),
true),
'threeDSProcessUrl' => $this->context->link->getModuleLink($this->name, 'ThreeDSProcess',
array(), true),
'prestashop16' => true,
'oneClickPaymentMethod' => json_encode($storedCard),
'recurringDetailReference' => $storedCard['recurringDetailReference'],
'name' => $storedCard['name'],
'number' => $storedCard['storedDetails']['card']['number']
)
);
}
$payments .= $this->display(__FILE__, '/views/templates/front/oneclick.tpl');
}
$payments .= $this->getOneClickPaymentMethods($paymentMethods);
}

$this->context->smarty->assign(
array(
'locale' => $this->helper_data->getLocale($this->context->language), // no locale in PrestaShop1.6 only languageCode that is en-en but we need en_EN
'originKey' => $this->helper_data->getOriginKeyForOrigin(),
'environment' => Configuration::get('ADYEN_MODE'),
'paymentProcessUrl' => $this->context->link->getModuleLink($this->name, 'Payment', array(), true),
'threeDSProcessUrl' => $this->context->link->getModuleLink($this->name, 'ThreeDSProcess', array(), true),
'prestashop16' => true,
'loggedInUser' => !$this->context->customer->is_guest
)
);
if (!empty($paymentMethods['paymentMethods'])) {
$payments .= $this->getLocalPaymentMethods($paymentMethods);
}

$payments .= $this->display(__FILE__, '/views/templates/front/payment.tpl');
$payments .= $this->getStandardPaymentMethod();

return $payments;
}
Expand Down Expand Up @@ -773,16 +784,16 @@ public function hookActionOrderSlipAdd(array $params)
}

try {
$client = $this->helper_data->initializeAdyenClient();
$modificationService = \Adyen\PrestaShop\service\adapter\classes\ServiceLocator::get(
'Adyen\Service\ResourceModel\Modification'
);
} catch (Adyen\AdyenException $e) {
$this->addMessageToOrderForOrderSlipAndLogErrorMessage(
'Error initializing Adyen Client in actionOrderSlipAdd hook:' . PHP_EOL . $e->getMessage()
'Error initializing Adyen Modification Service in actionOrderSlipAdd hook:'
. PHP_EOL . $e->getMessage()
);
return;
}
try {
$modificationService = new Adyen\Service\Modification($client);
} catch (Adyen\AdyenException $e) {
} catch (\PrestaShop\PrestaShop\Adapter\CoreException $e) {
$this->addMessageToOrderForOrderSlipAndLogErrorMessage(
'Error initializing Adyen Modification Service in actionOrderSlipAdd hook:'
. PHP_EOL . $e->getMessage()
Expand Down Expand Up @@ -924,4 +935,113 @@ private function createCustomerMessage($message, CustomerThread $customerThread)
);
}
}

/**
* @param array $paymentMethods
*
* @return string
*/
private function getOneClickPaymentMethods(array $paymentMethods)
{
$payments = '';
$oneClickPaymentMethods = $paymentMethods['oneClickPaymentMethods'];
foreach ($oneClickPaymentMethods as $storedCard) {
if (!empty($storedCard["storedDetails"]["card"])) {
$this->context->smarty->assign(
array(
'locale' => $this->helper_data->getLocale($this->context->language),
'originKey' => $this->helper_data->getOriginKeyForOrigin(),
'environment' => Configuration::get('ADYEN_MODE'),
'paymentProcessUrl' => $this->context->link->getModuleLink(
$this->name, 'Payment', array(),
true
),
'threeDSProcessUrl' => $this->context->link->getModuleLink(
$this->name, 'ThreeDSProcess',
array(), true
),
'prestashop16' => true,
'oneClickPaymentMethod' => json_encode($storedCard),
'recurringDetailReference' => $storedCard['recurringDetailReference'],
'name' => $storedCard['name'],
'number' => $storedCard['storedDetails']['card']['number']
)
);
}
$payments .= $this->display(__FILE__, '/views/templates/front/oneclick.tpl');
}
return $payments;
}

/**
* @param array $paymentMethods
*
* @return string
*/
private function getLocalPaymentMethods(array $paymentMethods)
{
$payments = '';
foreach ($paymentMethods['paymentMethods'] as $paymentMethod) {
// restrict to iDeal only for now
if ($paymentMethod['type'] != 'ideal') {
continue;
}
$issuerList = [];
if (empty($paymentMethod['type']) || $paymentMethod['type'] == 'scheme') {
continue;
}
if (isset($paymentMethod['details'])) {
foreach ($paymentMethod['details'] as $paymentMethodDetails) {
if (key_exists('key', $paymentMethodDetails) && $paymentMethodDetails['key'] == 'issuer') {
$issuerList = $paymentMethodDetails['items'];
break;
}
}
}
$this->context->smarty->assign(
array(
'locale' => $this->helper_data->getLocale($this->context->language),
'originKey' => $this->helper_data->getOriginKeyForOrigin(),
'environment' => Configuration::get('ADYEN_MODE'),
'issuerList' => json_encode($issuerList),
'paymentMethodType' => $paymentMethod['type'],
'paymentMethodName' => $paymentMethod['name'],
'paymentProcessUrl' => $this->context->link->getModuleLink(
$this->name,
'Payment',
array(),
true
),
'renderPayButton' => true,
)
);
$payments .= $this->display(__FILE__, '/views/templates/front/local-payment-method.tpl');
}
return $payments;
}

/**
* @return string
*/
private function getStandardPaymentMethod()
{
$payments = '';
$this->context->smarty->assign(
array(
'locale' => $this->helper_data->getLocale($this->context->language),
// no locale in PrestaShop1.6 only languageCode that is en-en but we need en_EN
'originKey' => $this->helper_data->getOriginKeyForOrigin(),
'environment' => Configuration::get('ADYEN_MODE'),
'paymentProcessUrl' => $this->context->link->getModuleLink($this->name, 'Payment', array(), true),
'threeDSProcessUrl' => $this->context->link->getModuleLink(
$this->name, 'ThreeDSProcess', array(), true
),
'prestashop16' => true,
'loggedInUser' => !$this->context->customer->is_guest
)
);

$payments .= $this->display(__FILE__, '/views/templates/front/payment.tpl');
return $payments;
}
}
7 changes: 7 additions & 0 deletions controllers/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace Adyen\PrestaShop\controllers;

use Adyen\PrestaShop\service\adapter\classes\ServiceLocator;
use PrestaShopException;

abstract class FrontController extends \ModuleFrontController
Expand All @@ -31,6 +32,12 @@ abstract class FrontController extends \ModuleFrontController
*/
protected $helperData;

public function __construct()
{
parent::__construct();
ServiceLocator::get('Adyen\PrestaShop\helper\Data')->startSession();
msilvagarcia marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param null $value
* @param null $controller
Expand Down
9 changes: 3 additions & 6 deletions controllers/admin/AdminAdyenPrestashopCronController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// Controllers, which breaks a PSR1 element.
// phpcs:disable PSR1.Classes.ClassDeclaration

use Adyen\PrestaShop\service\adapter\classes\ServiceLocator;

class AdminAdyenPrestashopCronController extends \ModuleAdminController
{
/**
Expand All @@ -38,12 +40,7 @@ class AdminAdyenPrestashopCronController extends \ModuleAdminController
*/
public function __construct()
{

$adyenHelperFactory = new \Adyen\PrestaShop\service\helper\DataFactory();
$this->helperData = $adyenHelperFactory->createAdyenHelperData(
\Configuration::get('ADYEN_MODE'),
_COOKIE_KEY_
);
$this->helperData = ServiceLocator::get('Adyen\PrestaShop\helper\Data');

if (\Tools::getValue('token') != $this->helperData->decrypt(\Configuration::get('ADYEN_CRONJOB_TOKEN'))) {
die('Invalid token');
Expand Down
7 changes: 2 additions & 5 deletions controllers/front/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// phpcs:disable PSR1.Classes.ClassDeclaration

use Adyen\PrestaShop\controllers\FrontController;
use Adyen\PrestaShop\service\adapter\classes\ServiceLocator;
use Adyen\PrestaShop\service\notification\NotificationReceiver;

class AdyenNotificationsModuleFrontController extends FrontController
Expand All @@ -37,11 +38,7 @@ class AdyenNotificationsModuleFrontController extends FrontController
public function __construct()
{
parent::__construct();
$adyenHelperFactory = new \Adyen\PrestaShop\service\helper\DataFactory();
$this->helperData = $adyenHelperFactory->createAdyenHelperData(
Configuration::get('ADYEN_MODE'),
_COOKIE_KEY_
);
$this->helperData = ServiceLocator::get('Adyen\PrestaShop\helper\Data');
}

/**
Expand Down
Loading