Skip to content

Commit

Permalink
format, removed uneccessary code, coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
jeppekroghitk committed Oct 12, 2023
1 parent 64c180b commit 4d2ead9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
61 changes: 42 additions & 19 deletions src/Helper/PaymentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PaymentHelper {

const VALIDATION_ERROR_NO_PAYMENT = 'VALIDATION_ERROR_NO_PAYMENT';
const VALIDATION_ERROR_INVALID_AMOUNT = 'VALIDATION_ERROR_INVALID_AMOUNT';
const AMOUNT_TO_PAY = 'AMOUNT_TO_PAY';

/**
* Private temp store.
Expand Down Expand Up @@ -122,9 +123,9 @@ public function validatePayment(array &$element, FormStateInterface $formState):

if (!$paymentId) {
$formState->setError(
$element,
'No payment found.'
);
$element,
'No payment found.'
);
return;
}

Expand All @@ -143,7 +144,7 @@ public function validatePayment(array &$element, FormStateInterface $formState):
);
$result = $this->responseToObject($response);

$amountToPay = floatval($this->privateTempStore->get(NetsEasyPaymentElement::AMOUNT_TO_PAY) * 100);
$amountToPay = floatval($this->getAmountToPayTemp() * 100);
$reservedAmount = floatval($result->payment->summary->reservedAmount);
$chargedAmount = floatval($result->payment->summary->chargedAmount);

Expand All @@ -169,24 +170,23 @@ public function validatePayment(array &$element, FormStateInterface $formState):

$chargeEndpoint = $this->getChargeEndpoint() . $paymentChargeId;
$response = $this->httpClient->request(
'GET',
$chargeEndpoint,
[
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => $this->getSecretKey(),
],
]
'GET',
$chargeEndpoint,
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => $this->getSecretKey(),
],
]
);
$result = $this->responseToObject($response);
$chargedAmount = $result->amount;

if (!$reservedAmount && !$chargedAmount) {
// Payment amount mismatch.
$formState->setError(
$element,
'Payment amount mismatch'
$element,
'Payment amount mismatch'
);
return;
}
Expand Down Expand Up @@ -286,8 +286,8 @@ public function getTestMode(): bool {
*/
public function getPaymentEndpoint(): string {
return $this->getTestMode()
? 'https://test.api.dibspayment.eu/v1/payments/'
: 'https://api.dibspayment.eu/v1/payments/';
? 'https://test.api.dibspayment.eu/v1/payments/'
: 'https://api.dibspayment.eu/v1/payments/';
}

/**
Expand All @@ -298,8 +298,8 @@ public function getPaymentEndpoint(): string {
*/
public function getChargeEndpoint(): string {
return $this->getTestMode()
? 'https://test.api.dibspayment.eu/v1/charges/'
: 'https://api.dibspayment.eu/v1/charges/';
? 'https://test.api.dibspayment.eu/v1/charges/'
: 'https://api.dibspayment.eu/v1/charges/';
}

/**
Expand All @@ -312,4 +312,27 @@ public function responseToObject(ResponseInterface $response): object {
return json_decode($response->getBody()->getContents());
}

/**
* Sets the amount to pay in private tempoary storage.
*
* @param float $amountToPay
* The amount to pay.
*
* @return void
* Sets the tempoary storage.
*/
public function setAmountToPayTemp(float $amountToPay): void {
$this->privateTempStore->set(self::AMOUNT_TO_PAY, $amountToPay);
}

/**
* Gets the amount to pay in private tempoary storage.
*
* @return float
* The amount to pay.
*/
public function getAmountToPayTemp(): float {
return $this->privateTempStore->get(self::AMOUNT_TO_PAY);
}

}
16 changes: 2 additions & 14 deletions src/Plugin/WebformElement/NetsEasyPaymentElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Drupal\os2forms_payment\Plugin\WebformElement;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TempStore\PrivateTempStore;
use Drupal\Core\Url;
use Drupal\os2forms_payment\Helper\PaymentHelper;
use Drupal\webform\Plugin\WebformElementBase;
Expand All @@ -28,15 +27,9 @@ class NetsEasyPaymentElement extends WebformElementBase {
*/
private PaymentHelper $paymentHelper;

/**
* Private temp store.
*
* @var \Drupal\Core\TempStore\PrivateTempStore
*/
private PrivateTempStore $tempStore;


const PAYMENT_REFERENCE_NAME = 'os2forms_payment_reference_field';
const AMOUNT_TO_PAY = 'AMOUNT_TO_PAY';

/**
* {@inheritdoc}
Expand All @@ -46,7 +39,6 @@ class NetsEasyPaymentElement extends WebformElementBase {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->paymentHelper = $container->get(PaymentHelper::class);
$instance->tempStore = $container->get('tempstore.private')->get('os2forms_payment');
return $instance;
}

Expand Down Expand Up @@ -137,7 +129,7 @@ public function alterForm(array &$element, array &$form, FormStateInterface $for
// Check if we are on the preview page.
if ($webformCurrentPage === "webform_preview") {
$amountToPay = $this->paymentHelper->getAmountToPay($formState->getUserInput(), $this->getElementProperty($element, 'amount_to_pay'));
$this->tempStore->set(self::AMOUNT_TO_PAY, $amountToPay);
$this->paymentHelper->setAmountToPayTemp($amountToPay);
/*
* If amount to pay is present,
* inject placeholder for nets gateway containing amount to pay.
Expand Down Expand Up @@ -189,10 +181,6 @@ public static function validatePayment(array &$element, FormStateInterface $form

$paymentHelper->validatePayment($element, $formState);

if ($formState->hasAnyErrors()) {
return FALSE;
}

return TRUE;
}

Expand Down

0 comments on commit 4d2ead9

Please sign in to comment.