diff --git a/buckaroo3.php b/buckaroo3.php index 5ef561b4..a6ba5532 100644 --- a/buckaroo3.php +++ b/buckaroo3.php @@ -383,6 +383,7 @@ public function hookPaymentOptions($params) 'in3Method' => $this->get('buckaroo.classes.issuers.capayableIn3')->getMethod(), 'showIdealIssuers' => $buckarooConfigService->getConfigValue('ideal', 'show_issuers') ?? true, 'buckaroo_idin_test' => $buckarooConfigService->getConfigValue('idin', 'mode'), + 'houseNumbersAreValid' => $buckarooPaymentService->areHouseNumberValidForCountryDE($cart) ] ); } catch (Exception $e) { diff --git a/controllers/front/request.php b/controllers/front/request.php index a5baf012..72f76b83 100644 --- a/controllers/front/request.php +++ b/controllers/front/request.php @@ -151,10 +151,11 @@ public function postProcess() $id_order_cart = Order::getIdByCartId($cart->id); $order = new Order($id_order_cart); $this->checkout->setReference($order->reference); - $this->checkout->setCheckout(); - $logger->logDebug('Set checkout info: '); + try { + $this->checkout->setCheckout(); + $logger->logDebug('Set checkout info: '); if ($this->checkout->isVerifyRequired()) { $logger->logInfo('Start verify process'); $this->checkout->startVerify(['cid' => $cart->id_customer]); diff --git a/library/checkout/afterpaycheckout.php b/library/checkout/afterpaycheckout.php index 0c55a0d5..1c4d68f7 100644 --- a/library/checkout/afterpaycheckout.php +++ b/library/checkout/afterpaycheckout.php @@ -90,6 +90,12 @@ public function getBillingAddress() : (($this->customerType == self::CUSTOMER_TYPE_B2B) ? RecipientCategory::COMPANY : ($this->companyExists($this->invoice_address->company) ? self::CUSTOMER_TYPE_B2B : RecipientCategory::PERSON)); + $countryIso = Tools::strtoupper($country->iso_code); + + if ($countryIso === 'DE' && empty(trim($address_components['house_number']))) { + throw new Exception('Invalid billing address, cannot find house number'); + } + $payload = [ 'recipient' => [ 'category' => $category, @@ -115,7 +121,7 @@ public function getBillingAddress() 'houseNumberAdditional' => $address_components['number_addition'], 'zipcode' => $this->invoice_address->postcode, 'city' => $this->invoice_address->city, - 'country' => Tools::strtoupper($country->iso_code), + 'country' => $countryIso, ], 'email' => !empty($this->customer->email) ? $this->customer->email : '', ]; @@ -198,10 +204,15 @@ public function getShippingAddress() $country = $sendCloudData['country']; } + $countryIso = Tools::strtoupper($country->iso_code); + if ($countryIso === 'DE' && empty(trim($houseNumber))) { + throw new Exception('Invalid shipping address, cannot find house number'); + } + $payload = [ 'recipient' => [ 'category' => (self::CUSTOMER_TYPE_B2C == $this->customerType) ? RecipientCategory::PERSON : RecipientCategory::COMPANY, - 'conversationLanguage' => Tools::strtoupper($country->iso_code), + 'conversationLanguage' => $countryIso, 'careOf' => $this->shipping_address->firstname . ' ' . $this->shipping_address->lastname, 'firstName' => $this->shipping_address->firstname, 'lastName' => $this->shipping_address->lastname, @@ -213,7 +224,7 @@ public function getShippingAddress() 'houseNumberAdditional' => $houseNumberSuffix, 'zipcode' => $zipcode, 'city' => $city, - 'country' => Tools::strtoupper($country->iso_code), + 'country' => $countryIso, ], 'phone' => [ 'mobile' => $phone, diff --git a/library/checkout/checkout.php b/library/checkout/checkout.php index e5cc5787..9499a3f0 100644 --- a/library/checkout/checkout.php +++ b/library/checkout/checkout.php @@ -15,6 +15,7 @@ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) */ +use Buckaroo\PrestaShop\Src\AddressComponents; use Buckaroo\PrestaShop\Src\Service\BuckarooConfigService; use Buckaroo\PrestaShop\Src\Service\BuckarooFeeService; use PrestaShop\Decimal\DecimalNumber; @@ -327,27 +328,7 @@ final public static function getInstanceRefund($payment_method) */ protected function getAddressComponents($address) { - $result = []; - $result['house_number'] = ''; - $result['number_addition'] = ''; - - $address = str_replace(['?', '*', '[', ']', ',', '!'], ' ', $address); - $address = preg_replace('/\s\s+/', ' ', $address); - - preg_match('/^([0-9]*)(.*?)([0-9]+)(.*)/', $address, $matches); - - if (!empty($matches[2])) { - $result['street'] = trim($matches[1] . $matches[2]); - $result['house_number'] = trim($matches[3]); - $result['number_addition'] = trim($matches[4]); - } else { - $result['street'] = $address; - } - - $logger = new \Logger(CoreLogger::INFO, ''); - $logger->logInfo(json_encode($result) . '-----------' . json_encode($matches)); - - return $result; + return AddressComponents::getAddressComponents($address); } /** diff --git a/src/AddressComponents.php b/src/AddressComponents.php new file mode 100644 index 00000000..1022eaee --- /dev/null +++ b/src/AddressComponents.php @@ -0,0 +1,51 @@ + + * @copyright Copyright (c) Buckaroo B.V. + * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) + */ + +namespace Buckaroo\PrestaShop\Src; + + +class AddressComponents +{ + /** + * Split address to parts + * + * @param string $address + * + * @return array + */ + public static function getAddressComponents($address) + { + $result = []; + $result['house_number'] = ''; + $result['number_addition'] = ''; + + $address = str_replace(['?', '*', '[', ']', ',', '!'], ' ', $address); + $address = preg_replace('/\s\s+/', ' ', $address); + + preg_match('/^([0-9]*)(.*?)([0-9]+)(.*)/', $address, $matches); + + if (!empty($matches[2])) { + $result['street'] = trim($matches[1] . $matches[2]); + $result['house_number'] = trim($matches[3]); + $result['number_addition'] = trim($matches[4]); + } else { + $result['street'] = $address; + } + + return $result; + } +} \ No newline at end of file diff --git a/src/Repository/PaymentMethodRepository.php b/src/Repository/PaymentMethodRepository.php index 8d5dd1e1..da17805a 100644 --- a/src/Repository/PaymentMethodRepository.php +++ b/src/Repository/PaymentMethodRepository.php @@ -143,9 +143,8 @@ private function isCountryInConfig(?array $configValue, int $countryId): bool if (isset($country['id']) && $country['id'] == $countryId) { return true; } - - return false; } + return false; } return true; diff --git a/src/Service/BuckarooPaymentService.php b/src/Service/BuckarooPaymentService.php index fe83bf37..0aad1914 100644 --- a/src/Service/BuckarooPaymentService.php +++ b/src/Service/BuckarooPaymentService.php @@ -21,6 +21,7 @@ require_once dirname(__FILE__) . '/../../library/checkout/afterpaycheckout.php'; include_once _PS_MODULE_DIR_ . 'buckaroo3/library/logger.php'; +use Buckaroo\PrestaShop\Src\AddressComponents; use Buckaroo\PrestaShop\Src\Entity\BkOrdering; use Buckaroo\PrestaShop\Src\Entity\BkPaymentMethods; use Doctrine\ORM\EntityManager; @@ -72,7 +73,6 @@ public function getPaymentOptions($cart) $positions = $this->bkOrderingRepository->fetchPositions($country['id'], $activeMethodIds); $positions = array_flip($positions); - foreach ($paymentMethods as $details) { $method = $details->getName(); $isMethodValid = $this->module->isPaymentModeActive($method) @@ -364,6 +364,22 @@ public function showBillinkCoc($cart) \BillinkCheckout::CUSTOMER_TYPE_B2C); } + public function areHouseNumberValidForCountryDE($cart) { + list($billingAddress, $billingCountry, $shippingAddress, $shippingCountry) = $this->getAddressDetails($cart); + return [ + "billing" =>$this->isHouseNumberValid($billingAddress) || $billingCountry !== 'DE', + "shipping" => $this->isHouseNumberValid($shippingAddress) || $shippingCountry !== 'DE' + ]; + } + + private function isHouseNumberValid($address) { + if (is_string($address->address1)) { + $address = AddressComponents::getAddressComponents($address->address1); + return is_string($address['house_number']) && !empty(trim($address['house_number'])); + } + return false; + } + private function shouldShowCoc($cart, $customer_type, $typeB2B, $typeB2C) { list($billingAddress, $billingCountry, $shippingAddress, $shippingCountry) = $this->getAddressDetails($cart); @@ -399,8 +415,8 @@ private function getAddressDetails($cart) */ protected function getAddressById($id) { - if (is_int($id)) { - return new \Address($id); + if (is_scalar($id)) { + return new \Address((int)$id); } } diff --git a/views/templates/hook/payment_afterpay.tpl b/views/templates/hook/payment_afterpay.tpl index 5a586aa6..85dbaaeb 100644 --- a/views/templates/hook/payment_afterpay.tpl +++ b/views/templates/hook/payment_afterpay.tpl @@ -17,8 +17,19 @@
+ {if (isset($houseNumbersAreValid['billing']) && $houseNumbersAreValid['billing'] === false)} + + {/if} + + {if (isset($houseNumbersAreValid['shipping']) && $houseNumbersAreValid['shipping'] === false)} + + {/if} {l s='Please provide additional data for Riverty | AfterPay.' mod='buckaroo3'}

-
+
+ +