Skip to content

Commit

Permalink
Update core, add payment mean id when calculating cart amount
Browse files Browse the repository at this point in the history
ADCRSET18-38
  • Loading branch information
MarijaIv committed Sep 25, 2024
1 parent 3c2d900 commit 24179cd
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 36 deletions.
24 changes: 19 additions & 5 deletions Components/BasketHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Adyen\Core\BusinessLogic\Domain\Checkout\PaymentRequest\Models\Amount\Amount;
use Adyen\Core\BusinessLogic\Domain\Checkout\PaymentRequest\Models\Amount\Currency;
use Adyen\Core\BusinessLogic\Domain\Integration\Payment\ShopPaymentService;
use Adyen\Core\Infrastructure\ServiceRegister;
use AdyenPayment\Components\Integration\PaymentMethodService;
use Shopware\Components\Model\ModelManager;
use Doctrine\DBAL\Connection;
use Enlight_Components_Session_Namespace;
Expand Down Expand Up @@ -56,12 +59,13 @@ public function forceBasketContentFor(string $articleOrderNumber): void
public function getTotalAmountFor(
Shopware_Controllers_Frontend_Checkout $coController,
?string $articleOrderNumber = null,
$address = null
$address = null,
?string $paymentMethod = null
): Amount
{
if (!$articleOrderNumber) {
if ($address) {
$this->setDispatchForAddress($address);
$this->setDispatchForAddress($address, $paymentMethod);
}

return $this->getCurrentCartAmount($coController);
Expand All @@ -71,7 +75,7 @@ public function getTotalAmountFor(
$this->forceBasketContentFor($articleOrderNumber);

if ($address) {
$this->setDispatchForAddress($address);
$this->setDispatchForAddress($address, $paymentMethod);
} elseif (empty($this->session['sDispatch'])) {
$coController->getSelectedCountry();
$userData = Shopware()->Modules()->Admin()->sGetUserData();
Expand Down Expand Up @@ -128,15 +132,25 @@ private function restoreBasketFromBackup(): void
* Sets dispatch for given address country.
*
* @param $address
* @param string|null $paymentMethod
*
* @return void
*/
private function setDispatchForAddress($address)
private function setDispatchForAddress($address, ?string $paymentMethod = null)
{
$id = null;

if ($paymentMethod) {
/** @var PaymentMethodService $service */
$service = ServiceRegister::getService(ShopPaymentService::class);
$paymentMean = $service->getPaymentMeanByName($paymentMethod);
$id = $paymentMean->getId();
}

$countryData = $this->getCountryData($address);
$dispatches = Shopware()->Modules()->Admin()->sGetPremiumDispatches(
$countryData['id'],
null,
$id,
$countryData['areaId']
);
$dispatch = reset($dispatches);
Expand Down
9 changes: 8 additions & 1 deletion Components/CheckoutConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public function getExpressCheckoutConfig(Amount $forceAmount): Response
private function buildConfigRequest(?Amount $forceAmount = null): PaymentCheckoutConfigRequest
{
$country = null;
$isGuest = false;

if ($this->getUser() && isset($this->getUser()['additional']['country']['countryiso'])) {
$country = Country::fromIsoCode($this->getUser()['additional']['country']['countryiso']);
}
Expand All @@ -118,6 +120,10 @@ private function buildConfigRequest(?Amount $forceAmount = null): PaymentCheckou
$shopperReference = ($userId !== 0) ? $shop->getHost() . '_' . Shop::getShopId() . '_' . $userId : null;
$shopperEmail = null;

if (!$userId) {
$isGuest = true;
}

if (
($sAdmin = Shopware()->Modules()->Admin()) &&
($userData = $sAdmin->sGetUserData()) &&
Expand All @@ -132,7 +138,8 @@ private function buildConfigRequest(?Amount $forceAmount = null): PaymentCheckou
Shopware()->Shop()->getLocale()->getLocale(),
$shopperReference,
$shopperEmail,
$shop->getName()
$shop->getName(),
$isGuest
);
}

Expand Down
16 changes: 10 additions & 6 deletions Controllers/Frontend/AdyenExpressCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ public function getCheckoutConfigAction(): void

$productNumber = $this->Request()->get('adyen_article_number');
$shippingAddress = $this->Request()->get('adyenShippingAddress');
$paymentMethod = $this->Request()->get('adyen_payment_method');

if ($shippingAddress) {
$this->handleNewShippingAddress($shippingAddress, $productNumber);
$this->handleNewShippingAddress($shippingAddress, $productNumber, $paymentMethod);

return;
}

$this->Response()->setBody(json_encode(
$this->checkoutConfigProvider->getExpressCheckoutConfig(
$this->basketHelper->getTotalAmountFor($this->prepareCheckoutController(), $productNumber)
$this->basketHelper->getTotalAmountFor($this->prepareCheckoutController(), $productNumber, $paymentMethod)
)->toArray()
));
}
Expand All @@ -84,7 +85,8 @@ public function paypalUpdateOrderAction(): void
$amount = $this->basketHelper->getTotalAmountFor(
$this->prepareCheckoutController(),
$productNumber,
$shippingAddress
$shippingAddress,
'paypal'
);

$response = CheckoutAPI::get()
Expand Down Expand Up @@ -183,7 +185,7 @@ public function finishAction(): void
*
* @throws InvalidCurrencyCode
*/
private function handleNewShippingAddress($shippingAddress, ?string $productNumber = null): void
private function handleNewShippingAddress($shippingAddress, ?string $productNumber = null, ?string $paymentMethod = null): void
{
$shippingAddress = json_decode($shippingAddress, false);

Expand All @@ -200,7 +202,8 @@ private function handleNewShippingAddress($shippingAddress, ?string $productNumb
$this->basketHelper->getTotalAmountFor(
$this->prepareCheckoutController(),
$productNumber,
$shippingAddress
$shippingAddress,
$paymentMethod
)
);
$this->Response()->setBody(json_encode(
Expand Down Expand Up @@ -238,7 +241,8 @@ private function startGuestPayPalPaymentTransaction()
'paypal',
$this->basketHelper->getTotalAmountFor(
$this->prepareCheckoutController(),
!empty($productNumber) ? $productNumber : null
!empty($productNumber) ? $productNumber : null,
'paypal'
),
$reference,
Url::getFrontUrl(
Expand Down
14 changes: 0 additions & 14 deletions Resources/frontend/js/adyen-checkout-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,6 @@
return config.onShippingContactSelected(resolve, reject, event);
}

const handleShopperDetails = (shopperDetails, rawData, actions) => {
return config.onShopperDetails(shopperDetails, rawData, actions);
}

const shippingAddressChanged = (data, actions, component) => {
return config.onShippingAddressChanged(data, actions, component);
}

let checkout,
activeComponent,
isStateValid = true,
Expand Down Expand Up @@ -203,8 +195,6 @@
paymentDataCallbacks: googlePaymentDataCallbacks
},
"paypal": {
isExpress: true,
onShopperDetails: handleShopperDetails,
blockPayPalCreditButton: true,
blockPayPalPayLaterButton: true,
onClick: (source, event, self) => {
Expand All @@ -213,10 +203,6 @@
}
};

if (config.onShippingAddressChanged) {
paymentMethodSpecificConfig.paypal.onShippingAddressChange = shippingAddressChanged
}

if (config.requireAddress) {
paymentMethodSpecificConfig.applepay = {
isExpress: true,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"php": "^7.2|^7.4|^8.0",
"ext-json": "*",
"ext-zip": "*",
"adyen/integration-core": "dev-cr-12-14-release",
"adyen/integration-core": "1.2.0",
"ext-simplexml": "*"
},
"minimum-stability": "dev",
Expand Down
16 changes: 7 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 24179cd

Please sign in to comment.