diff --git a/src/Helper/PaymentHelper.php b/src/Helper/PaymentHelper.php index caacd71..15a7d37 100644 --- a/src/Helper/PaymentHelper.php +++ b/src/Helper/PaymentHelper.php @@ -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. @@ -122,9 +123,9 @@ public function validatePayment(array &$element, FormStateInterface $formState): if (!$paymentId) { $formState->setError( - $element, - 'No payment found.' - ); + $element, + 'No payment found.' + ); return; } @@ -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); @@ -169,15 +170,14 @@ 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; @@ -185,8 +185,8 @@ public function validatePayment(array &$element, FormStateInterface $formState): if (!$reservedAmount && !$chargedAmount) { // Payment amount mismatch. $formState->setError( - $element, - 'Payment amount mismatch' + $element, + 'Payment amount mismatch' ); return; } @@ -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/'; } /** @@ -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/'; } /** @@ -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); + } + } diff --git a/src/Plugin/WebformElement/NetsEasyPaymentElement.php b/src/Plugin/WebformElement/NetsEasyPaymentElement.php index 2e80685..b11585b 100644 --- a/src/Plugin/WebformElement/NetsEasyPaymentElement.php +++ b/src/Plugin/WebformElement/NetsEasyPaymentElement.php @@ -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; @@ -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} @@ -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; } @@ -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. @@ -189,10 +181,6 @@ public static function validatePayment(array &$element, FormStateInterface $form $paymentHelper->validatePayment($element, $formState); - if ($formState->hasAnyErrors()) { - return FALSE; - } - return TRUE; }