Skip to content

Commit

Permalink
Items only in brut shopping carts and then without VAT.
Browse files Browse the repository at this point in the history
After many tests, it has become clear that the calculation problems
still exist in Netto shops. That is why we can only transfer the items
in brut shopping carts. And there we transfer the VAT as 0.
  • Loading branch information
mariolorenz committed Sep 6, 2024
1 parent df70ef2 commit bdeef64
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 139 deletions.
15 changes: 3 additions & 12 deletions src/Controller/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,10 @@ public function createOrder()
null,
'',
'',
'',
Constants::PAYPAL_PARTNER_ATTRIBUTION_ID_PPCP,
null,
null,
false,
false,
null
false
);

if ($response->id) {
Expand Down Expand Up @@ -196,13 +193,10 @@ public function createGooglepayOrder()
null,
'',
'',
'',
Constants::PAYPAL_PARTNER_ATTRIBUTION_ID_PPCP,
null,
null,
false,
false,
null
false
);

if ($response->id) {
Expand Down Expand Up @@ -564,13 +558,10 @@ public function createApplepayOrder()
null,
'',
'',
'',
Constants::PAYPAL_PARTNER_ATTRIBUTION_ID_PPCP,
null,
null,
false,
false,
null
false
);
if ($response->id) {
PayPalSession::storePayPalOrderId($response->id);
Expand Down
96 changes: 44 additions & 52 deletions src/Core/OrderRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public function getRequest(
?string $invoiceId = null,
?string $returnUrl = null,
?string $cancelUrl = null,
bool $articlesWithTax = false,
bool $setProvidedAddress = true
): OrderRequest {
$request = $this->request = new OrderRequest();
$this->basket = $basket;
$withItems = !$this->basket->isCalculationModeNetto();

$moduleSettings = $this->getServiceFromContainer(ModuleSettings::class);
$setVaulting = $moduleSettings->getIsVaultingActive();
Expand All @@ -108,7 +108,7 @@ public function getRequest(
$request->payment_source = $this->getApplePayPaymentSource($basket, 'apple_pay');
}
$request->intent = $intent;
$request->purchase_units = $this->getPurchaseUnits($customId, $invoiceId, $articlesWithTax);
$request->purchase_units = $this->getPurchaseUnits($customId, $invoiceId, $withItems);

$useVaultedPayment = $setVaulting && !is_null($selectedVaultPaymentSourceIndex);
if ($useVaultedPayment) {
Expand Down Expand Up @@ -177,7 +177,6 @@ public function getRequest(
}
protected function getApplePayPaymentSource($basket, $requestName)
{

$user = $basket->getBasketUser();

$userName = $user->getFieldData('oxfname') . ' ' . $user->getFieldData('oxlname');
Expand Down Expand Up @@ -217,12 +216,12 @@ protected function getGooglePayPaymentSource($basket, $requestName)
if ($deliveryId && $deliveryAddress->load($deliveryId)) {
$country->load($deliveryAddress->getFieldData('oxcountryid'));
}
$paymentSource = new \stdClass();
$paymentSource = new stdClass();

// Dynamically adding properties to the stdClass object
$paymentSource->$requestName = new \stdClass();
$paymentSource->$requestName->attributes = new \stdClass();
$paymentSource->$requestName->attributes->verification = new \stdClass();
// Dynamically adding properties to the stdClass object
$paymentSource->$requestName = new stdClass();
$paymentSource->$requestName->attributes = new stdClass();
$paymentSource->$requestName->attributes->verification = new stdClass();
$paymentSource->$requestName->attributes->verification->method = 'SCA_ALWAYS';
return $paymentSource;
}
Expand Down Expand Up @@ -265,7 +264,7 @@ protected function getApplicationContext(
protected function getPurchaseUnits(
?string $transactionId,
?string $invoiceId,
bool $articlesWithTax = false
bool $withItems = false
): array {
$purchaseUnit = new PurchaseUnitRequest();
$shopName = Registry::getConfig()->getActiveShop()->getFieldData('oxname');
Expand All @@ -279,7 +278,9 @@ protected function getPurchaseUnits(
$purchaseUnit->amount = $this->getAmount();
$purchaseUnit->reference_id = Constants::PAYPAL_ORDER_REFERENCE_ID;

$purchaseUnit->items = $this->getItems($articlesWithTax);
if ($withItems) {
$purchaseUnit->items = $this->getItems();
}

if ($this->basket->getBasketUser()) {
$purchaseUnit->shipping = $this->getShippingAddress();
Expand All @@ -300,17 +301,15 @@ protected function getAmount(): AmountWithBreakdown
* @return array
* @psalm-suppress UndefinedDocblockClass
*/
public function getItems(
bool $articlesWithTax = false
): array
public function getItems(): array
{
$basket = $this->basket;
$itemCategory = $this->getItemCategoryByBasketContent();
$currency = $basket->getBasketCurrency();
$language = Registry::getLang();
$items = [];
$config = Registry::getConfig();
$netMode = $basket->isCalculationModeNetto();
$items = [];

/** @var BasketItem $basketItem */
foreach ($basket->getContents() as $basketItem) {
Expand All @@ -326,13 +325,12 @@ public function getItems(
// no zero price articles in the list
if ($itemUnitPrice && $itemUnitPrice->getBruttoPrice() > 0) {
$item->unit_amount = PriceToMoney::convert(
$netMode ? $itemUnitPrice->getNettoPrice() : $itemUnitPrice->getBruttoPrice(),
$itemUnitPrice->getBruttoPrice(),
$currency
);
if ($articlesWithTax) {
$item->tax = PriceToMoney::convert($itemUnitPrice->getVat(), $currency);
$item->tax_rate = $itemUnitPrice->getVatValue();
}
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
$item->category = $articleCategory;

$item->quantity = (string)$basketItem->getAmount();
Expand All @@ -341,56 +339,53 @@ public function getItems(
}

$wrapping = $basket->getPayPalCheckoutWrapping();
if ($wrapping) {
if ($wrapping && $wrapping->getBruttoPrice()) {
$item = new Item();
$item->name = $language->translateString('GIFT_WRAPPING');

$item->unit_amount = PriceToMoney::convert(
$netMode ? $wrapping->getNettoPrice() : $wrapping->getBruttoPrice(),
$wrapping->getBruttoPrice(),
$currency
);
if ($articlesWithTax) {
$item->tax = PriceToMoney::convert(0, $currency);
$item->tax_rate = '0';
}
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
$item->category = $itemCategory;

$item->quantity = '1';
$items[] = $item;
}

$giftCard = $basket->getPayPalCheckoutGiftCard();
if ($giftCard) {
if ($giftCard && $giftCard->getBruttoPrice()) {
$item = new Item();
$item->name = $language->translateString('GREETING_CARD');

$item->unit_amount = PriceToMoney::convert(
$netMode ? $giftCard->getNettoPrice() : $giftCard->getBruttoPrice(),
$giftCard->getBruttoPrice(),
$currency
);
if ($articlesWithTax) {
$item->tax = PriceToMoney::convert(0, $currency);
$item->tax_rate = '0';
}
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
$item->category = $itemCategory;

$item->quantity = '1';
$items[] = $item;
}

$payment = $basket->getPayPalCheckoutPayment();
if ($payment) {
if ($payment && $payment->getBruttoPrice()) {
$item = new Item();
$item->name = $language->translateString('PAYMENT_METHOD');

$item->unit_amount = PriceToMoney::convert(
$netMode ? $payment->getNettoPrice() : $payment->getBruttoPrice(),
$payment->getBruttoPrice(),
$currency
);
if ($articlesWithTax) {
$item->tax = PriceToMoney::convert(0, $currency);
$item->tax_rate = '0';
}
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
$item->category = $itemCategory;

$item->quantity = '1';
Expand All @@ -399,37 +394,35 @@ public function getItems(

//Shipping cost
$delivery = $basket->getPayPalCheckoutDeliveryCosts();
if ($delivery) {
if ($delivery && $delivery->getBruttoPrice()) {
$item = new Item();
$item->name = $language->translateString('SHIPPING_COST');

$item->unit_amount = PriceToMoney::convert(
$netMode ? $delivery->getNettoPrice() : $delivery->getBruttoPrice(),
$delivery->getBruttoPrice(),
$currency
);
if ($articlesWithTax) {
$item->tax = PriceToMoney::convert(0, $currency);
$item->tax_rate = '0';
}
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
$item->category = $itemCategory;

$item->quantity = '1';
$items[] = $item;
}

// possible price surcharge
$discount = $basket->getPayPalCheckoutDiscount($netMode);
$discount = $basket->getPayPalCheckoutDiscount();

if ($discount < 0) {
$discount *= -1;
$item = new Item();
$item->name = $language->translateString('SURCHARGE');

$item->unit_amount = PriceToMoney::convert($discount, $currency);
if ($articlesWithTax) {
$item->tax = PriceToMoney::convert($basket->getPayPalCheckoutDiscountVat($netMode), $currency);
$item->tax_rate = $config->getConfigParam('dDefaultVAT');
}
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
$item->category = $itemCategory;

$item->quantity = '1';
Expand All @@ -442,10 +435,9 @@ public function getItems(
$item->name = $language->translateString('OSC_PAYPAL_VAT_CORRECTION');

$item->unit_amount = PriceToMoney::convert((float)$roundDiff, $currency);
if ($articlesWithTax) {
$item->tax = PriceToMoney::convert(0, $currency);
$item->tax_rate = '0';
}
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
$item->category = $itemCategory;

$item->quantity = '1';
Expand Down
28 changes: 13 additions & 15 deletions src/Core/PatchRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getRequest(
string $orderId = ''
): array {
$this->basket = $basket;
$netMode = $basket->isCalculationModeNetto();
$withItems = !$this->basket->isCalculationModeNetto();
$currency = $basket->getBasketCurrency();

$this->getShippingNamePatch();
Expand All @@ -55,12 +55,12 @@ public function getRequest(
if ($orderId) {
$this->getCustomIdPatch($orderId);
}

$this->getPurchaseUnitsPatch(
$this->basket,
$netMode,
$currency
);
if ($withItems) {
$this->getPurchaseUnitsPatch(
$this->basket,
$currency
);
}

return $this->request;
}
Expand Down Expand Up @@ -138,12 +138,10 @@ protected function getAmountPatch(): void

/**
* @param Basket $basket
* @param bool $netMode
* @param $currency
*/
protected function getPurchaseUnitsPatch(
Basket $basket,
bool $netMode,
$currency
): void {

Expand All @@ -162,7 +160,7 @@ protected function getPurchaseUnitsPatch(
$itemUnitPrice = $basketItem->getUnitPrice();
if ($itemUnitPrice) {
$item->unit_amount = PriceToMoney::convert(
$netMode ? $itemUnitPrice->getNettoPrice() : $itemUnitPrice->getBruttoPrice(),
$itemUnitPrice->getBruttoPrice(),
$currency
);
// We provide no tax, because Tax is in 99% not necessary.
Expand All @@ -178,7 +176,7 @@ protected function getPurchaseUnitsPatch(
$item->name = $language->translateString('GIFT_WRAPPING');

$item->unit_amount = PriceToMoney::convert(
$netMode ? $wrapping->getNettoPrice() : $wrapping->getBruttoPrice(),
$wrapping->getBruttoPrice(),
$currency
);

Expand All @@ -192,7 +190,7 @@ protected function getPurchaseUnitsPatch(
$item->name = $language->translateString('GREETING_CARD');

$item->unit_amount = PriceToMoney::convert(
$netMode ? $giftCard->getNettoPrice() : $giftCard->getBruttoPrice(),
$giftCard->getBruttoPrice(),
$currency
);

Expand All @@ -206,7 +204,7 @@ protected function getPurchaseUnitsPatch(
$item->name = $language->translateString('PAYMENT_METHOD');

$item->unit_amount = PriceToMoney::convert(
$netMode ? $payment->getNettoPrice() : $payment->getBruttoPrice(),
$payment->getBruttoPrice(),
$currency
);

Expand All @@ -221,7 +219,7 @@ protected function getPurchaseUnitsPatch(
$item->name = $language->translateString('SHIPPING_COST');

$item->unit_amount = PriceToMoney::convert(
$netMode ? $delivery->getNettoPrice() : $delivery->getBruttoPrice(),
$delivery->getBruttoPrice(),
$currency
);

Expand All @@ -230,7 +228,7 @@ protected function getPurchaseUnitsPatch(
}

// possible price surcharge
$discount = $basket->getPayPalCheckoutDiscount($netMode);
$discount = $basket->getPayPalCheckoutDiscount();

if ($discount < 0) {
$discount *= -1;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/PayPalRequestAmountFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getAmount(Basket $basket): AmountWithBreakdown
$currency = $basket->getBasketCurrency();

//Discount
$discount = $basket->getPayPalCheckoutDiscount($netMode);
$discount = $basket->getPayPalCheckoutDiscount();
//Item total cost
$itemTotal = $basket->getPayPalCheckoutItems();

Expand Down
Loading

0 comments on commit bdeef64

Please sign in to comment.