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

add checks for switching payment method in myaccount #24

Merged
merged 3 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions meta/documents/changelog_de.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes für PAYONE

## 2.1.X

### Behoben
- Bei der Prüfung, ob eine Zahlungsart für die nachträgliche Bezahlung zur Verfügung steht, werden die entsprechenden Regeln angewendet, die auch für den Checkout gelten.

## 2.1.2

### Behoben
Expand Down
5 changes: 5 additions & 0 deletions meta/documents/changelog_en.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for PAYONE

## 2.1.X

### Fixed
- When checking whether a payment method is available for subsequent payment, the same rules that are applied to the checkout als apply here.

## 2.1.2

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions meta/documents/user_guide_de.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ https://www.psg-projektmanagement.de/payone-plentymarkets/

Termin vereinbaren:
<a href="https://calendly.com/payone-plentymarkets/payone-plentymarkets">hier klicken</a>

Die PSG Projektmanagement GmbH ist ein langjähriger Partner der PAYONE, sowie ein langjähriger Partner von plentymarkets und ist somit Dein zentraler Ansprechpartner der optimal mit allen Partnern vernetzt ist. Bitte wende dich nicht direkt an PAYONE, um den reibungslosen Ablauf nicht zu gefährden.

Hier der offizielle Link zur Partnerschaft mit PAYONE: [https://payone.com/DE-de/plentymarkets-payone-plugin](https://payone.com/DE-de/plentymarkets-payone-plugin)
estafilarakis marked this conversation as resolved.
Show resolved Hide resolved
42 changes: 30 additions & 12 deletions src/Methods/PaymentAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function __construct(
Application $application,
PaymentValidator $paymentValidator,
SettingsService $settingsService
) {
)
{
$this->paymentValidator = $paymentValidator;
$this->app = $app = $application;
$this->settingsService = $settingsService;
Expand All @@ -63,7 +64,7 @@ public function getName(string $lang = 'de'): string
{
/** @var Translator $translator */
$translator = pluginApp(Translator::class);
return $translator->trans('Payone::PaymentMethods.'.$this::PAYMENT_CODE, [], $lang);
return $translator->trans('Payone::PaymentMethods.' . $this::PAYMENT_CODE, [], $lang);
}

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ public function getDescription(string $lang = 'de'): string
{
/** @var Translator $translator */
$translator = pluginApp(Translator::class);
return $translator->trans('Payone::PaymentMethods.'.$this::PAYMENT_CODE.'_DESCRIPTION', [], $lang);
return $translator->trans('Payone::PaymentMethods.' . $this::PAYMENT_CODE . '_DESCRIPTION', [], $lang);
}

/**
Expand All @@ -109,19 +110,19 @@ public function getCode(): string
*/
public function getMaxCartAmount(): float
{
$amount = $this->settingsService->getPaymentSettingsValue('MaximumAmount',$this::PAYMENT_CODE);
$amount = $this->settingsService->getPaymentSettingsValue('MaximumAmount', $this::PAYMENT_CODE);

return $amount ? (float) $amount : 0.;
return $amount ? (float)$amount : 0.;
}

/**
* @return float
*/
public function getMinCartAmount(): float
{
$amount = $this->settingsService->getPaymentSettingsValue('MinimumAmount',$this::PAYMENT_CODE);
$amount = $this->settingsService->getPaymentSettingsValue('MinimumAmount', $this::PAYMENT_CODE);

return $amount ? (float) $amount : 0.;
return $amount ? (float)$amount : 0.;
}

/**
Expand Down Expand Up @@ -155,7 +156,7 @@ public function isBackendActive(): bool
/**
* Get name for the backend
*
* @param string $lang
* @param string $lang
* @return string
*/
public function getBackendName(string $lang = 'de'): string
Expand All @@ -181,13 +182,13 @@ public function canHandleSubscriptions(): bool
public function getBackendIcon(): string
{
$app = pluginApp(Application::class);
$icon = $app->getUrlPath(PluginConstants::NAME).'/images/logos/'.strtolower($this::PAYMENT_CODE).'_backend_icon.svg';
$icon = $app->getUrlPath(PluginConstants::NAME) . '/images/logos/' . strtolower($this::PAYMENT_CODE) . '_backend_icon.svg';
return $icon;
}

/**
* Can the delivery address be different from the invoice address?
*
*
* @return bool
*/
public function canHandleDifferingDeliveryAddress(): bool
Expand All @@ -197,7 +198,7 @@ public function canHandleDifferingDeliveryAddress(): bool

/**
* Check if all settings for the payment method are set.
*
*
* @param SettingsService $settingsService
* @return bool
*/
Expand All @@ -208,12 +209,29 @@ public function validateSettings(SettingsService $settingsService): bool

/**
* Is the payment method active for the given currency?
*
*
* @param $currency
* @return bool
*/
public function isActiveForCurrency($currency): bool
{
return true;
}

/**
* @param int|null $orderId
* @return bool
*/
public function isSwitchableTo($orderId = null): bool
{
if($orderId > 0) {
/** @var PaymentOrderValidator $paymentOrderValidator */
$paymentOrderValidator = pluginApp(PaymentOrderValidator::class);

return (bool)$this->settingsService->getPaymentSettingsValue('active', $this::PAYMENT_CODE)
&& $paymentOrderValidator->validate($this, $this->settingsService, $orderId);
}

return false;
}
}
110 changes: 110 additions & 0 deletions src/Methods/PaymentOrderValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Payone\Methods;

use Payone\Adapter\Logger;
use Payone\Helpers\AddressHelper;
use Payone\Services\SettingsService;
use Plenty\Modules\Authorization\Services\AuthHelper;
use Plenty\Modules\Order\Contracts\OrderRepositoryContract;
use Plenty\Modules\Order\Models\Order;

class PaymentOrderValidator
{
/**
* @var AddressHelper
*/
private $addressHelper;

/**
* @var Logger
*/
private $logger;

/**
* PaymentValidator constructor.
*
* @param AddressHelper $addressHelper
* @param Logger $logger
*/
public function __construct(AddressHelper $addressHelper, Logger $logger)
{
$this->addressHelper = $addressHelper;
$this->logger = $logger;
}

/**
* @param PaymentAbstract $payment
*
* @return bool
*/
public function validate(PaymentAbstract $payment, SettingsService $settingsService, int $orderId)
{
/** @var OrderRepositoryContract $orderRepositoryContract */
$orderRepositoryContract = pluginApp(OrderRepositoryContract::class);
/** @var AuthHelper $authHelper */
$authHelper = pluginApp(AuthHelper::class);
/** @var Order $order */
$order = $authHelper->processUnguarded(
function () use ($orderRepositoryContract, $orderId) {
return $orderRepositoryContract->findById($orderId, ['amounts', 'addresses']);
}
);

$orderAmount = $order->amount->invoiceTotal;
if ($payment->getMinCartAmount() && $orderAmount < $payment->getMinCartAmount()) {
$this->log($payment->getName(), 'Payment.minCartAmount', $orderAmount);
return false;
}

if ($payment->getMaxCartAmount() && $orderAmount > $payment->getMaxCartAmount()) {
$this->log($payment->getName(), 'Payment.maxCartAmount', $orderAmount);
return false;
}

$billingAddress = $order->billingAddress;
$deliveryAddress = $order->deliveryAddress;
if (!$billingAddress) {
// TODO: shouldn't this be 'return false'?
return true;
}

if (!in_array($billingAddress->countryId, $payment->getAllowedCountries())) {
$this->log($payment->getName(), 'Payment.countryNotAllowed', $billingAddress->countryId);
return false;
}

if (!$payment->canHandleDifferingDeliveryAddress() && $deliveryAddress && $billingAddress->id != $deliveryAddress->id) {
return false;
}

if (!$payment->validateSettings($settingsService)) {
return false;
}

if (!$payment->isActiveForCurrency($order->amount->currency)) {
return false;
}

return true;
}

/**
* @param string $payment
* @param string $code
* @param string $value
*/
protected function log($payment, $code, $value)
{
$logger = $this->logger->setIdentifier(__METHOD__);
$logger->debug(
$code,
[
'basketID' => $this->basket->id,
'customer' => $this->basket->customerId,
'payment' => $payment,
'value' => $value,
]
);
}
}