Skip to content

Commit

Permalink
Merge pull request #339 from OXID-eSales/PSPAYPAL-825_LineItems
Browse files Browse the repository at this point in the history
Pspaypal 825 line items
  • Loading branch information
HonkDerHase authored Sep 6, 2024
2 parents 5798761 + bdeef64 commit 589b01f
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 115 deletions.
9 changes: 6 additions & 3 deletions src/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OxidSolutionCatalysts\PayPal\Controller;

use OxidEsales\Eshop\Core\Registry;
use OxidSolutionCatalysts\PayPal\Core\PayPalSession;
use OxidSolutionCatalysts\PayPal\Core\ServiceFactory;
use OxidSolutionCatalysts\PayPal\Exception\PayPalException;
use OxidSolutionCatalysts\PayPal\Service\Payment as PaymentService;
Expand Down Expand Up @@ -106,9 +107,9 @@ protected function paymentTypeExists($paymentType): bool
public function getPayPalPuiFraudnetCmId(): string
{

if (!($cmId = \OxidSolutionCatalysts\PayPal\Core\PayPalSession::getPayPalPuiCmId())) {
if (!($cmId = PayPalSession::getPayPalPuiCmId())) {
$cmId = Registry::getUtilsObject()->generateUId();
\OxidSolutionCatalysts\PayPal\Core\PayPalSession::storePayPalPuiCmId($cmId);
PayPalSession::storePayPalPuiCmId($cmId);
}
return $cmId;
}
Expand All @@ -129,6 +130,8 @@ public function getPaymentList()
$paymentListRaw = $paymentList;
$paymentList = [];
$payPalHealth = $this->getServiceFromContainer(ModuleSettings::class)->checkHealth();
$basket = Registry::getSession()->getBasket();
$isCalculationModeNetto = $basket ? $basket->isCalculationModeNetto() : false;

/*
* check:
Expand Down Expand Up @@ -158,7 +161,7 @@ public function getPaymentList()
(
$payPalDefinitions[$key]['onlybrutto'] === false ||
(
!$this->getServiceFromContainer(ModuleSettings::class)->isPriceViewModeNetto()
!$isCalculationModeNetto
)
)
)
Expand Down
15 changes: 3 additions & 12 deletions src/Controller/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,10 @@ public function createOrder()
null,
'',
'',
'',
Constants::PAYPAL_PARTNER_ATTRIBUTION_ID_PPCP,
null,
null,
false,
false,
null
false
);

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

if ($response->id) {
Expand Down Expand Up @@ -569,13 +563,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
99 changes: 50 additions & 49 deletions src/Core/OrderRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class OrderRequestFactory
* @param null|string $invoiceId custom invoice number
* @param null|string $returnUrl Return Url
* @param null|string $cancelUrl Cancel Url
* @param bool $withArticles Request with article information?
* @param bool $articlesWithTax Articles in Request with Tax information?
* @param bool $setProvidedAddress Address changeable in PayPal?
*
* @return OrderRequest
Expand All @@ -91,11 +91,11 @@ public function getRequest(
?string $invoiceId = null,
?string $returnUrl = null,
?string $cancelUrl = null,
bool $withArticles = true,
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, $withArticles);
$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 @@ -266,7 +265,7 @@ protected function getApplicationContext(
protected function getPurchaseUnits(
?string $transactionId,
?string $invoiceId,
bool $withArticles = true
bool $withItems = false
): array {
$moduleSettings = $this->getServiceFromContainer(ModuleSettings::class);
$purchaseUnit = new PurchaseUnitRequest();
Expand All @@ -281,12 +280,7 @@ protected function getPurchaseUnits(
$purchaseUnit->amount = $this->getAmount();
$purchaseUnit->reference_id = Constants::PAYPAL_ORDER_REFERENCE_ID;

// If it is planned to patch this PayPal order in the further course,
// then no items may be given, since PayPal cannot patch any items at the moment
// At the moment only the amount and the title of the article
// are relevant. However, no inventory.
// in this case get the purchase units without articles
if ($withArticles) {
if ($withItems) {
$purchaseUnit->items = $this->getItems();
}

Expand Down Expand Up @@ -315,6 +309,8 @@ public function getItems(): array
$itemCategory = $this->getItemCategoryByBasketContent();
$currency = $basket->getBasketCurrency();
$language = Registry::getLang();
$config = Registry::getConfig();
$netMode = $basket->isCalculationModeNetto();
$items = [];

/** @var BasketItem $basketItem */
Expand All @@ -329,98 +325,106 @@ public function getItems(): array
: Item::CATEGORY_PHYSICAL_GOODS;

// no zero price articles in the list
if ((float)$itemUnitPrice->getBruttoPrice() > 0) {
$item->unit_amount = PriceToMoney::convert((float)$itemUnitPrice->getBruttoPrice(), $currency);
if ($itemUnitPrice && $itemUnitPrice->getBruttoPrice() > 0) {
$item->unit_amount = PriceToMoney::convert(
$itemUnitPrice->getBruttoPrice(),
$currency
);
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert((float)0, $currency);
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
// TODO: There are usually still categories for digital products.
// But only with PHYSICAL_GOODS, Payments like PUI will work fine.
$item->category = $articleCategory;

$item->quantity = (string)$basketItem->getAmount();
$items[] = $item;
}
}

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

$item->unit_amount = PriceToMoney::convert((float)$wrapping, $currency);
$item->unit_amount = PriceToMoney::convert(
$wrapping->getBruttoPrice(),
$currency
);
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0, $currency);
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
// TODO: There are usually still categories for digital products.
// But only with PHYSICAL_GOODS, Payments like PUI will work fine.
$item->category = $itemCategory;

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

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

$item->unit_amount = PriceToMoney::convert((float)$giftCard, $currency);
$item->unit_amount = PriceToMoney::convert(
$giftCard->getBruttoPrice(),
$currency
);
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0, $currency);
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
// TODO: There are usually still categories for digital products.
// But only with PHYSICAL_GOODS, Payments like PUI will work fine.
$item->category = $itemCategory;

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

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

$item->unit_amount = PriceToMoney::convert((float)$payment, $currency);
$item->unit_amount = PriceToMoney::convert(
$payment->getBruttoPrice(),
$currency
);
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0, $currency);
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
// TODO: There are usually still categories for digital products.
// But only with PHYSICAL_GOODS, Payments like PUI will work fine.
$item->category = $itemCategory;

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

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

$item->unit_amount = PriceToMoney::convert((float)$delivery, $currency);
$item->unit_amount = PriceToMoney::convert(
$delivery->getBruttoPrice(),
$currency
);
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0, $currency);
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
// TODO: There are usually still categories for digital products.
// But only with PHYSICAL_GOODS, Payments like PUI will work fine.
$item->category = $itemCategory;

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

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

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

$item->unit_amount = PriceToMoney::convert($discount, $currency);
// tax - we use 0% and calculate with brutto to avoid rounding errors
$item->tax = PriceToMoney::convert(0, $currency);
$item->tax = PriceToMoney::convert(0.0, $currency);
$item->tax_rate = '0';
// TODO: There are usually still categories for digital products.
// But only with PHYSICAL_GOODS, Payments like PUI will work fine.
$item->category = $itemCategory;

$item->quantity = '1';
Expand All @@ -434,11 +438,8 @@ public function getItems(): array

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

// TODO: There are usually still categories for digital products.
// But only with PHYSICAL_GOODS, Payments like PUI will work fine.
$item->category = $itemCategory;

$item->quantity = '1';
Expand Down
Loading

0 comments on commit 589b01f

Please sign in to comment.