Skip to content

Commit

Permalink
Merge pull request #511 from Invertus/single-click-improvments
Browse files Browse the repository at this point in the history
Single click improvments
  • Loading branch information
margud authored Jan 20, 2022
2 parents 7648076 + 1531d48 commit 09c2ed3
Show file tree
Hide file tree
Showing 14 changed files with 400 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ cypress/screenshots
.php_cs.cache
.env
vendor
node_modules
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
+ Removed custom logic for local shops.
+ Added sentry validation to avoid crash if sentry breaks down.
+ Added check to avoid exception in path utility class.
+ Using single click payment without component now ask if you want to save it or use saved card.
+ Fixed issue where klarna status would be changed to klarna shipped and not completed when using default statuses for klarna.

## Changes in release 4.4.3 ##
Expand Down
1 change: 1 addition & 0 deletions config/handler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
arguments:
- '@Mollie\Provider\PaymentOption\BasePaymentOptionProvider'
- '@Mollie\Provider\PaymentOption\CreditCardPaymentOptionProvider'
- '@Mollie\Provider\PaymentOption\CreditCardSingleClickPaymentOptionProvider'
- '@Mollie\Provider\PaymentOption\IdealPaymentOptionProvider'
- '@Mollie\Repository\MolCustomerRepository'
- '@mollie.customer'
Expand Down
12 changes: 12 additions & 0 deletions config/payment_option_provider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ services:
- '@Mollie\Service\LanguageService'
- '@mollie.customer'
- '@Mollie\Repository\MolCustomerRepository'

Mollie\Provider\PaymentOption\CreditCardSingleClickPaymentOptionProvider:
class: Mollie\Provider\PaymentOption\CreditCardSingleClickPaymentOptionProvider
arguments:
- '@mollie'
- '@=service("mollie.adapter.context")'
- '@Mollie\Provider\CreditCardLogoProvider'
- '@Mollie\Provider\OrderTotalProvider'
- '@Mollie\Provider\PaymentFeeProvider'
- '@Mollie\Service\LanguageService'
- '@mollie.customer'
- '@Mollie\Repository\MolCustomerRepository'
1 change: 1 addition & 0 deletions mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ public function hookDisplayHeader(array $params)
['server' => 'remote', 'position' => 'bottom', 'priority' => 150]
);
$this->context->controller->addJS("{$this->_path}views/js/front/mollie_iframe.js");
$this->context->controller->addJS("{$this->_path}views/js/front/mollie_single_click.js");

Media::addJsDef([
'ajaxUrl' => $this->context->link->getModuleLink('mollie', 'ajax'),
Expand Down
27 changes: 26 additions & 1 deletion src/Handler/PaymentOption/PaymentOptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Mollie\Config\Config;
use Mollie\Provider\PaymentOption\BasePaymentOptionProvider;
use Mollie\Provider\PaymentOption\CreditCardPaymentOptionProvider;
use Mollie\Provider\PaymentOption\CreditCardSingleClickPaymentOptionProvider;
use Mollie\Provider\PaymentOption\IdealPaymentOptionProvider;
use Mollie\Repository\MolCustomerRepository;
use MolPaymentMethod;
Expand Down Expand Up @@ -70,10 +71,15 @@ class PaymentOptionHandler implements PaymentOptionHandlerInterface
* @var Customer
*/
private $customer;
/**
* @var CreditCardSingleClickPaymentOptionProvider
*/
private $cardSingleClickPaymentOptionProvider;

public function __construct(
BasePaymentOptionProvider $basePaymentOptionProvider,
CreditCardPaymentOptionProvider $creditCardPaymentOptionProvider,
CreditCardSingleClickPaymentOptionProvider $cardSingleClickPaymentOptionProvider,
IdealPaymentOptionProvider $idealPaymentOptionProvider,
MolCustomerRepository $customerRepository,
Customer $customer
Expand All @@ -83,6 +89,7 @@ public function __construct(
$this->idealPaymentOptionProvider = $idealPaymentOptionProvider;
$this->customerRepository = $customerRepository;
$this->customer = $customer;
$this->cardSingleClickPaymentOptionProvider = $cardSingleClickPaymentOptionProvider;
}

/**
Expand All @@ -95,7 +102,11 @@ public function handle(MolPaymentMethod $paymentMethod)
}

if ($this->isCreditCardPaymentMethod($paymentMethod)) {
return $this->creditCardPaymentOptionProvider->getPaymentOption($paymentMethod);
if ($this->isIFrame()) {
return $this->creditCardPaymentOptionProvider->getPaymentOption($paymentMethod);
} elseif ($this->isSingleClick()) {
return $this->cardSingleClickPaymentOptionProvider->getPaymentOption($paymentMethod);
}
}

return $this->basePaymentOptionProvider->getPaymentOption($paymentMethod);
Expand Down Expand Up @@ -132,10 +143,24 @@ private function isCreditCardPaymentMethod(MolPaymentMethod $paymentMethod)
return false;
}

return true;
}

private function isIFrame()
{
if (!Configuration::get(Config::MOLLIE_IFRAME)) {
return false;
}

return true;
}

private function isSingleClick()
{
if (!Configuration::get(Config::MOLLIE_SINGLE_CLICK_PAYMENT)) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php
/**
* Copyright (c) 2012-2020, Mollie B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license Berkeley Software Distribution License (BSD-License 2) http://www.opensource.org/licenses/bsd-license.php
*
* @category Mollie
*
* @see https://www.mollie.nl
* @codingStandardsIgnoreStart
*/

namespace Mollie\Provider\PaymentOption;

use Configuration;
use Customer;
use MolCustomer;
use Mollie;
use Mollie\Adapter\LegacyContext;
use Mollie\Config\Config;
use Mollie\Provider\CreditCardLogoProvider;
use Mollie\Provider\OrderTotalProviderInterface;
use Mollie\Provider\PaymentFeeProviderInterface;
use Mollie\Repository\MolCustomerRepository;
use Mollie\Service\LanguageService;
use Mollie\Utility\CustomerUtility;
use MolPaymentMethod;
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
use Tools;

class CreditCardSingleClickPaymentOptionProvider implements PaymentOptionProviderInterface
{
/**
* @var Mollie
*/
private $module;

/**
* @var LegacyContext
*/
private $context;

/**
* @var CreditCardLogoProvider
*/
private $creditCardLogoProvider;

/**
* @var OrderTotalProviderInterface
*/
private $orderTotalProvider;

/**
* @var PaymentFeeProviderInterface
*/
private $paymentFeeProvider;

/**
* @var LanguageService
*/
private $languageService;
/**
* @var Customer
*/
private $customer;
/**
* @var MolCustomerRepository
*/
private $customerRepository;

public function __construct(
Mollie $module,
LegacyContext $context,
CreditCardLogoProvider $creditCardLogoProvider,
OrderTotalProviderInterface $orderTotalProvider,
PaymentFeeProviderInterface $paymentFeeProvider,
LanguageService $languageService,
Customer $customer,
MolCustomerRepository $customerRepository
) {
$this->module = $module;
$this->context = $context;
$this->creditCardLogoProvider = $creditCardLogoProvider;
$this->orderTotalProvider = $orderTotalProvider;
$this->paymentFeeProvider = $paymentFeeProvider;
$this->languageService = $languageService;
$this->customer = $customer;
$this->customerRepository = $customerRepository;
}

/**
* {@inheritDoc}
*/
public function getPaymentOption(MolPaymentMethod $paymentMethod)
{
$paymentOption = new PaymentOption();
$paymentOption->setCallToActionText(
$paymentMethod->title ?:
$this->languageService->lang($paymentMethod->method_name)
);
$paymentOption->setModuleName($this->module->name);
$paymentOption->setAction($this->context->getLink()->getModuleLink(
'mollie',
'payScreen',
['method' => $paymentMethod->getPaymentMethodName(), 'rand' => Mollie\Utility\TimeUtility::getCurrentTimeStamp()],
true
));
$fullName = CustomerUtility::getCustomerFullName($this->customer->id);

/** @var MolCustomer|null $molCustomer */
$molCustomer = $this->customerRepository->findOneBy(
[
'name' => $fullName,
'email' => $this->customer->email,
]
);

$paymentOption->setInputs([
[
'type' => 'hidden',
'name' => 'mollieCardToken',
'value' => '',
],
[
'type' => 'hidden',
'name' => 'mollieSaveCard',
'value' => '',
],
[
'type' => 'hidden',
'name' => 'mollieCustomerExists',
'value' => (bool) $molCustomer,
],
[
'type' => 'hidden',
'name' => 'mollieUseSavedCard',
'value' => '',
],
]);

$this->context->getSmarty()->assign([
'price' => $this->orderTotalProvider->getOrderTotal(),
'priceSign' => $this->context->getCurrencySign(),
'methodId' => $paymentMethod->getPaymentMethodName(),
'isSingleClickPayment' => (bool) Configuration::get(Mollie\Config\Config::MOLLIE_SINGLE_CLICK_PAYMENT),
'mollieUseSavedCard' => (bool) (Configuration::get(Config::MOLLIE_SINGLE_CLICK_PAYMENT) && $molCustomer),
'isGuest' => $this->customer->isGuest(),
]);
$paymentOption->setLogo($this->creditCardLogoProvider->getMethodOptionLogo($paymentMethod));

$paymentOption->setAdditionalInformation($this->module->display(
$this->module->getPathUri(), 'views/templates/hook/mollie_single_click.tpl'
));

$paymentFee = $this->paymentFeeProvider->getPaymentFee($paymentMethod);

if ($paymentFee) {
$paymentOption->setInputs([
[
'type' => 'hidden',
'name' => 'payment-fee-price',
'value' => $paymentFee,
],
[
'type' => 'hidden',
'name' => 'payment-fee-price-display',
'value' => sprintf($this->module->l('Payment Fee: %1s'), Tools::displayPrice($paymentFee)),
],
]);
}

return $paymentOption;
}
}
15 changes: 5 additions & 10 deletions src/Service/CustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@

namespace Mollie\Service;

use Cart;
use MolCustomer;
use Mollie;
use Mollie\Api\Types\PaymentMethod;
use Mollie\Config\Config;
use Mollie\Exception\MollieException;
use Mollie\Repository\MolCustomerRepository;
Expand All @@ -39,17 +37,17 @@ public function __construct(Mollie $mollie, MolCustomerRepository $customerRepos
$this->customerRepository = $customerRepository;
}

public function processCustomerCreation(Cart $cart, $method): ?MolCustomer
public function processCustomerCreation($customerId): ?MolCustomer
{
if (!$this->isSingleCLickPaymentEnabled($method)) {
if (!$this->isSingleClickPaymentEnabled()) {
return null;
}

$customer = new \Customer($cart->id_customer);
$customer = new \Customer($customerId);

$fullName = CustomerUtility::getCustomerFullName($customer->id);
/** @var MolCustomer|null $molCustomer */
$molCustomer = $this->getCustomer($cart->id_customer);
$molCustomer = $this->getCustomer($customerId);

if ($molCustomer) {
return $molCustomer;
Expand Down Expand Up @@ -97,11 +95,8 @@ public function createCustomer($name, $email)
}
}

public function isSingleCLickPaymentEnabled($method)
public function isSingleClickPaymentEnabled()
{
if (PaymentMethod::CREDITCARD !== $method) {
return false;
}
$isSingleClickPaymentEnabled = \Configuration::get(Config::MOLLIE_SINGLE_CLICK_PAYMENT);
if ($isSingleClickPaymentEnabled) {
return true;
Expand Down
Loading

0 comments on commit 09c2ed3

Please sign in to comment.