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

BP-3326 Change new required fields for Riverty DE #127

Merged
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
1 change: 1 addition & 0 deletions buckaroo3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions controllers/front/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
17 changes: 14 additions & 3 deletions library/checkout/afterpaycheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 : '',
];
Expand Down Expand Up @@ -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,
Expand All @@ -213,7 +224,7 @@ public function getShippingAddress()
'houseNumberAdditional' => $houseNumberSuffix,
'zipcode' => $zipcode,
'city' => $city,
'country' => Tools::strtoupper($country->iso_code),
'country' => $countryIso,
],
'phone' => [
'mobile' => $phone,
Expand Down
23 changes: 2 additions & 21 deletions library/checkout/checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
51 changes: 51 additions & 0 deletions src/AddressComponents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* It is available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this file
*
* @author Buckaroo.nl <[email protected]>
* @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;
}
}
3 changes: 1 addition & 2 deletions src/Repository/PaymentMethodRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 19 additions & 3 deletions src/Service/BuckarooPaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down
15 changes: 14 additions & 1 deletion views/templates/hook/payment_afterpay.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@
<form class="mb-1" name="booAfterPayForm_digi" id="booAfterPayForm_digi"
action="{$link->getModuleLink('buckaroo3', 'request', ['method' => 'afterpay', 'service' => 'digi'])|escape:'quotes':'UTF-8'}"
method="post">
{if (isset($houseNumbersAreValid['billing']) && $houseNumbersAreValid['billing'] === false)}
<div class="alert alert-danger" role="alert" data-alert="danger">
{l s='Invalid billing address, cannot find house number' mod='buckaroo3'}
</div>
{/if}

{if (isset($houseNumbersAreValid['shipping']) && $houseNumbersAreValid['shipping'] === false)}
<div class="alert alert-danger" role="alert" data-alert="danger">
{l s='Invalid shipping address, cannot find house number' mod='buckaroo3'}
</div>
{/if}
{l s='Please provide additional data for Riverty | AfterPay.' mod='buckaroo3'}<br/><br/>
<div class="row row-padding">
<div class="row row-padding">
<div class="col-sm-5">
<label for="phone_afterpay_billing_digi"
class="required">
Expand All @@ -34,6 +45,8 @@
>
</div>
</div>



<div class="row row-padding">
<div class="col-xs-5">
Expand Down