First upload
This commit is contained in:
135
shop/source/modules/oe/oepaypal/GraphQL/Controller/Payment.php
Normal file
135
shop/source/modules/oe/oepaypal/GraphQL/Controller/Payment.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Controller;
|
||||
|
||||
use OxidEsales\GraphQL\Storefront\Basket\Service\Basket as StorefrontBasketService;
|
||||
use OxidEsales\PayPalModule\GraphQL\DataType\PayPalCommunicationInformation;
|
||||
use OxidEsales\PayPalModule\GraphQL\DataType\PayPalTokenStatus;
|
||||
use OxidEsales\PayPalModule\GraphQL\Exception\GraphQLServiceNotFound;
|
||||
use OxidEsales\PayPalModule\GraphQL\Service\Basket as BasketService;
|
||||
use OxidEsales\PayPalModule\GraphQL\Service\Payment as PaymentService;
|
||||
use TheCodingMachine\GraphQLite\Annotations\Logged;
|
||||
use TheCodingMachine\GraphQLite\Annotations\Query;
|
||||
use TheCodingMachine\GraphQLite\Annotations\Right;
|
||||
use TheCodingMachine\GraphQLite\Types\ID;
|
||||
|
||||
final class Payment
|
||||
{
|
||||
/** @var PaymentService */
|
||||
private $paymentService;
|
||||
|
||||
/** @var BasketService */
|
||||
private $basketService;
|
||||
|
||||
/** @var StorefrontBasketService */
|
||||
private $storefrontBasketService;
|
||||
|
||||
public function __construct(
|
||||
PaymentService $paymentService,
|
||||
BasketService $basketService,
|
||||
StorefrontBasketService $storefrontBasketService = null
|
||||
) {
|
||||
$this->paymentService = $paymentService;
|
||||
$this->basketService = $basketService;
|
||||
$this->storefrontBasketService = $storefrontBasketService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Query
|
||||
* @Logged()
|
||||
*/
|
||||
public function paypalApprovalProcess(
|
||||
ID $basketId,
|
||||
string $returnUrl,
|
||||
string $cancelUrl,
|
||||
bool $displayBasketInPayPal
|
||||
): PayPalCommunicationInformation {
|
||||
$this->validateState();
|
||||
|
||||
$basket = $this->storefrontBasketService->getAuthenticatedCustomerBasket($basketId);
|
||||
|
||||
// validate if basket payment method is correct
|
||||
$this->basketService->validateBasketPaymentMethod($basket);
|
||||
|
||||
$communicationInformation = $this->paymentService->getPayPalCommunicationInformation(
|
||||
$basket,
|
||||
$returnUrl,
|
||||
$cancelUrl,
|
||||
$displayBasketInPayPal
|
||||
);
|
||||
|
||||
$this->basketService->updateBasketToken($basket, $communicationInformation->getToken());
|
||||
|
||||
return $communicationInformation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Query
|
||||
* @Right("PAYPAL_EXPRESS_APPROVAL")
|
||||
*/
|
||||
public function paypalExpressApprovalProcess(
|
||||
ID $basketId,
|
||||
string $returnUrl,
|
||||
string $cancelUrl,
|
||||
bool $displayBasketInPayPal
|
||||
): PayPalCommunicationInformation
|
||||
{
|
||||
$this->validateState();
|
||||
|
||||
$basket = $this->storefrontBasketService->getAuthenticatedCustomerBasket($basketId);
|
||||
|
||||
// validate if pp express payment is available
|
||||
$this->basketService->validateBasketExpressPaymentMethod();
|
||||
|
||||
$communicationInformation = $this->paymentService->getPayPalExpressCommunicationInformation(
|
||||
$basket,
|
||||
$returnUrl,
|
||||
$cancelUrl,
|
||||
$displayBasketInPayPal
|
||||
);
|
||||
|
||||
$this->basketService->updateExpressBasketInformation(
|
||||
$basket,
|
||||
$communicationInformation->getToken()
|
||||
);
|
||||
|
||||
return $communicationInformation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Query()
|
||||
* @Right("PAYPAL_TOKEN_STATUS")
|
||||
*/
|
||||
public function paypalTokenStatus(string $paypalToken): PayPalTokenStatus
|
||||
{
|
||||
return $this->paymentService->getPayPalTokenStatus($paypalToken);
|
||||
}
|
||||
|
||||
protected function validateState(): void
|
||||
{
|
||||
if (is_null($this->storefrontBasketService)) {
|
||||
throw GraphQLServiceNotFound::byServiceName(StorefrontBasketService::class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\DataType;
|
||||
|
||||
use TheCodingMachine\GraphQLite\Annotations\Field;
|
||||
use TheCodingMachine\GraphQLite\Annotations\Type;
|
||||
|
||||
/**
|
||||
* @Type()
|
||||
*/
|
||||
final class PayPalCommunicationInformation
|
||||
{
|
||||
/** @var string */
|
||||
private $token;
|
||||
|
||||
/** @var string */
|
||||
private $communicationUrl;
|
||||
|
||||
public function __construct(string $token, string $communicationUrl)
|
||||
{
|
||||
$this->token = $token;
|
||||
$this->communicationUrl = $communicationUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Field()
|
||||
*/
|
||||
public function getToken(): string
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Field()
|
||||
*/
|
||||
public function getCommunicationUrl(): string
|
||||
{
|
||||
return $this->communicationUrl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\DataType;
|
||||
|
||||
use TheCodingMachine\GraphQLite\Annotations\Field;
|
||||
use TheCodingMachine\GraphQLite\Annotations\Type;
|
||||
|
||||
/**
|
||||
* @Type()
|
||||
*/
|
||||
final class PayPalTokenStatus
|
||||
{
|
||||
/** @var string */
|
||||
private $token;
|
||||
|
||||
/** @var bool */
|
||||
private $status;
|
||||
|
||||
/** @var string */
|
||||
private $payerId;
|
||||
|
||||
public function __construct(string $token, bool $status, string $payerId)
|
||||
{
|
||||
$this->token = $token;
|
||||
$this->status = $status;
|
||||
$this->payerId = $payerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Field()
|
||||
*/
|
||||
public function getToken(): string
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Field()
|
||||
*/
|
||||
public function isTokenApproved(): bool
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
// for internal user only
|
||||
public function getPayerId(): string
|
||||
{
|
||||
return $this->payerId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Exception;
|
||||
|
||||
use OxidEsales\GraphQL\Base\Exception\InvalidRequest;
|
||||
|
||||
final class BasketCommunication extends InvalidRequest
|
||||
{
|
||||
public static function notStarted(string $basketId): self
|
||||
{
|
||||
return new self(sprintf("PayPal communication for basket %s is not started", $basketId));
|
||||
}
|
||||
|
||||
public static function notConfirmed(string $basketId): self
|
||||
{
|
||||
return new self(sprintf("PayPal communication for basket %s is not confirmed", $basketId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Exception;
|
||||
|
||||
use OxidEsales\GraphQL\Base\Exception\InvalidRequest;
|
||||
|
||||
final class BasketValidation extends InvalidRequest
|
||||
{
|
||||
public static function basketChange(string $basketId): self
|
||||
{
|
||||
return new self(sprintf("Content change discovered for basket %s. Please run paypal approval process again.", $basketId));
|
||||
}
|
||||
|
||||
public static function basketAddressChange(string $basketId): self
|
||||
{
|
||||
return new self(sprintf("Address change discovered for basket %s. Please run paypal approval process again.", $basketId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class GraphQLServiceNotFound extends Exception
|
||||
{
|
||||
public static function byServiceName(string $name): self
|
||||
{
|
||||
return new self(sprintf("GraphQL service %s is not available.", $name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Exception;
|
||||
|
||||
use OxidEsales\GraphQL\Base\Exception\InvalidRequest;
|
||||
|
||||
final class PaymentValidation extends InvalidRequest
|
||||
{
|
||||
public static function paymentMethodIsNotPaypal(): self
|
||||
{
|
||||
return new self('Payment method should be PayPal.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Infrastructure;
|
||||
|
||||
use OxidEsales\PayPalModule\Core\Config as PayPalConfig;
|
||||
use OxidEsales\PayPalModule\Core\PayPalService;
|
||||
use OxidEsales\PayPalModule\Model\PaymentManager;
|
||||
|
||||
final class Request
|
||||
{
|
||||
public function getPaymentManager(): PaymentManager
|
||||
{
|
||||
$payPalCheckoutService = oxNew(PayPalService::class);
|
||||
return oxNew(PaymentManager::class, $payPalCheckoutService);
|
||||
}
|
||||
|
||||
public function getPayPalConfig(): PayPalConfig
|
||||
{
|
||||
return oxNew(PayPalConfig::class);
|
||||
}
|
||||
}
|
||||
144
shop/source/modules/oe/oepaypal/GraphQL/Service/Basket.php
Normal file
144
shop/source/modules/oe/oepaypal/GraphQL/Service/Basket.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Service;
|
||||
|
||||
use OxidEsales\Eshop\Core\Registry as EshopRegistry;
|
||||
use OxidEsales\GraphQL\Storefront\Basket\DataType\Basket as BasketDataType;
|
||||
use OxidEsales\GraphQL\Storefront\Basket\Service\BasketRelationService;
|
||||
use OxidEsales\GraphQL\Storefront\Payment\DataType\Payment as PaymentDataType;
|
||||
use OxidEsales\GraphQL\Storefront\Payment\Exception\PaymentNotFound;
|
||||
use OxidEsales\GraphQL\Storefront\Payment\Exception\UnavailablePayment;
|
||||
use OxidEsales\GraphQL\Storefront\Payment\Service\Payment as StorefrontPaymentService;
|
||||
use OxidEsales\PayPalModule\Core\Config as PayPalConfig;
|
||||
use OxidEsales\PayPalModule\GraphQL\Exception\GraphQLServiceNotFound;
|
||||
use OxidEsales\PayPalModule\GraphQL\Exception\PaymentValidation;
|
||||
use OxidEsales\PayPalModule\Model\PaymentManager;
|
||||
|
||||
final class Basket
|
||||
{
|
||||
/** @var BasketRelationService */
|
||||
private $basketRelationService;
|
||||
|
||||
/** @var StorefrontPaymentService */
|
||||
private $storefrontPaymentService;
|
||||
|
||||
/** @var PayPalConfig */
|
||||
private $paypalConfig;
|
||||
|
||||
public function __construct(
|
||||
BasketRelationService $basketRelationService = null,
|
||||
StorefrontPaymentService $storefrontPaymentService = null,
|
||||
PayPalConfig $paypalConfig
|
||||
) {
|
||||
$this->basketRelationService = $basketRelationService;
|
||||
$this->storefrontPaymentService = $storefrontPaymentService;
|
||||
$this->paypalConfig = $paypalConfig;
|
||||
}
|
||||
|
||||
public function checkBasketPaymentMethodIsPayPal(BasketDataType $basket): bool
|
||||
{
|
||||
$this->validateState();
|
||||
|
||||
$paymentMethod = $this->basketRelationService->payment($basket);
|
||||
$result = false;
|
||||
|
||||
if (!is_null($paymentMethod) && $paymentMethod->getId()->val() === 'oxidpaypal') {
|
||||
$result = true;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws PaymentValidation
|
||||
*/
|
||||
public function validateBasketPaymentMethod(BasketDataType $basket): void
|
||||
{
|
||||
if (!$this->checkBasketPaymentMethodIsPayPal($basket)) {
|
||||
throw PaymentValidation::paymentMethodIsNotPaypal();
|
||||
}
|
||||
|
||||
if (!$this->paypalConfig->isStandardCheckoutEnabled()) {
|
||||
throw UnavailablePayment::byId('oxidpaypal');
|
||||
}
|
||||
}
|
||||
|
||||
public function validateBasketExpressPaymentMethod(): void
|
||||
{
|
||||
if (!$this->paypalConfig->isExpressCheckoutEnabled()) {
|
||||
throw UnavailablePayment::byId('oxidpaypal');
|
||||
}
|
||||
|
||||
try {
|
||||
$payment = $this->storefrontPaymentService->payment('oxidpaypal');
|
||||
} catch (PaymentNotFound $e) {
|
||||
throw UnavailablePayment::byId('oxidpaypal');
|
||||
}
|
||||
|
||||
if (!$payment instanceof PaymentDataType) {
|
||||
throw UnavailablePayment::byId('oxidpaypal');
|
||||
}
|
||||
}
|
||||
|
||||
public function updateBasketToken(BasketDataType $basket, string $token): void
|
||||
{
|
||||
/**
|
||||
* @TODO: check if we can/need to revoke the old token.
|
||||
*/
|
||||
|
||||
$userBasketModel = $basket->getEshopModel();
|
||||
$userBasketModel->assign([
|
||||
'OEPAYPAL_PAYMENT_TOKEN' => $token
|
||||
]);
|
||||
$userBasketModel->save();
|
||||
}
|
||||
|
||||
public function updateExpressBasketInformation(BasketDataType $basket, string $token): void
|
||||
{
|
||||
$userBasketModel = $basket->getEshopModel();
|
||||
$userBasketModel->assign(
|
||||
[
|
||||
'OEPAYPAL_PAYMENT_TOKEN' => $token,
|
||||
'OEPAYPAL_SERVICE_TYPE' => PaymentManager::PAYPAL_SERVICE_TYPE_EXPRESS,
|
||||
'OEGQL_PAYMENTID' => 'oxidpaypal'
|
||||
]
|
||||
);
|
||||
$userBasketModel->save();
|
||||
|
||||
EshopRegistry::getSession()->setVariable(
|
||||
PayPalConfig::OEPAYPAL_TRIGGER_NAME,
|
||||
PaymentManager::PAYPAL_SERVICE_TYPE_EXPRESS
|
||||
);
|
||||
}
|
||||
|
||||
protected function validateState(): void
|
||||
{
|
||||
if (is_null($this->basketRelationService)) {
|
||||
throw GraphQLServiceNotFound::byServiceName(BasketRelationService::class);
|
||||
}
|
||||
|
||||
if (is_null($this->storefrontPaymentService)) {
|
||||
throw GraphQLServiceNotFound::byServiceName(StorefrontPaymentService::class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Service;
|
||||
|
||||
use OxidEsales\GraphQL\Storefront\Basket\DataType\Basket as BasketDataType;
|
||||
use TheCodingMachine\GraphQLite\Annotations\ExtendType;
|
||||
use TheCodingMachine\GraphQLite\Annotations\Field;
|
||||
|
||||
/**
|
||||
* @ExtendType(class=BasketDataType::class)
|
||||
*/
|
||||
final class BasketExtendType
|
||||
{
|
||||
/**
|
||||
* @Field()
|
||||
*/
|
||||
public function paypalToken(BasketDataType $basket): string
|
||||
{
|
||||
return $basket->getEshopModel()->getFieldData('OEPAYPAL_PAYMENT_TOKEN');
|
||||
}
|
||||
|
||||
/**
|
||||
* @Field()
|
||||
*/
|
||||
public function paypalServiceType(BasketDataType $basket): int
|
||||
{
|
||||
return (int) $basket->getEshopModel()->getFieldData('OEPAYPAL_SERVICE_TYPE');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Service;
|
||||
|
||||
use OxidEsales\Eshop\Core\Registry as EshopRegistry;
|
||||
use OxidEsales\GraphQL\Storefront\Basket\Event\BeforePlaceOrder as BeforePlaceOrderEvent;
|
||||
use OxidEsales\PayPalModule\Core\Config as PayPalConfig;
|
||||
use OxidEsales\PayPalModule\GraphQL\Exception\BasketCommunication;
|
||||
use OxidEsales\PayPalModule\GraphQL\Service\Basket as BasketService;
|
||||
use OxidEsales\PayPalModule\GraphQL\Service\Payment as PaymentService;
|
||||
use OxidEsales\PayPalModule\Model\Response\ResponseGetExpressCheckoutDetails;
|
||||
use OxidEsales\GraphQL\Storefront\Basket\Service\Basket as StorefrontBasketService;
|
||||
use OxidEsales\PayPalModule\GraphQL\Exception\GraphQLServiceNotFound;
|
||||
|
||||
final class BeforePlaceOrder
|
||||
{
|
||||
/** @var PaymentService */
|
||||
private $paymentService;
|
||||
|
||||
/** @var BasketService */
|
||||
private $basketService;
|
||||
|
||||
/** @var StorefrontBasketService */
|
||||
private $storefrontBasketService;
|
||||
|
||||
public function __construct(
|
||||
PaymentService $paymentService,
|
||||
BasketService $basketService,
|
||||
StorefrontBasketService $storefrontBasketService = null
|
||||
) {
|
||||
$this->paymentService = $paymentService;
|
||||
$this->basketService = $basketService;
|
||||
$this->storefrontBasketService = $storefrontBasketService;
|
||||
}
|
||||
|
||||
public function handle(BeforePlaceOrderEvent $event): void
|
||||
{
|
||||
$this->validateState();
|
||||
|
||||
$userBasket = $this->storefrontBasketService->getAuthenticatedCustomerBasket($event->getBasketId());
|
||||
if ($this->basketService->checkBasketPaymentMethodIsPayPal($userBasket)) {
|
||||
$extendUserBasket = new BasketExtendType();
|
||||
|
||||
$token = $extendUserBasket->paypalToken($userBasket);
|
||||
if (!$token) {
|
||||
throw BasketCommunication::notStarted($userBasket->id()->val());
|
||||
}
|
||||
|
||||
//call PayPal API once for ExpressCheckoutDetails
|
||||
/** @var ResponseGetExpressCheckoutDetails $expressCheckoutDetails */
|
||||
$expressCheckoutDetails = $this->paymentService->getExpressCheckoutDetails($token);
|
||||
|
||||
$tokenStatus = $this->paymentService->getPayPalTokenStatus($token, $expressCheckoutDetails);
|
||||
if (!$tokenStatus->isTokenApproved()) {
|
||||
throw BasketCommunication::notConfirmed($userBasket->id()->val());
|
||||
}
|
||||
|
||||
$sessionBasket = $this->paymentService->getValidEshopBasketModel($userBasket, $expressCheckoutDetails);
|
||||
|
||||
// In order to be able to finalize order, using PayPal as payment method,
|
||||
// we need to prepare the following session variables.
|
||||
$session = EshopRegistry::getSession();
|
||||
$session->setBasket($sessionBasket);
|
||||
$session->setVariable('oepaypal-token', $token);
|
||||
$session->setVariable("oepaypal-userId", $sessionBasket->getUser()->getId());
|
||||
$session->setVariable('oepaypal-payerId', $tokenStatus->getPayerId());
|
||||
$session->setVariable(
|
||||
PayPalConfig::OEPAYPAL_TRIGGER_NAME,
|
||||
$extendUserBasket->paypalServiceType($userBasket)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected function validateState(): void
|
||||
{
|
||||
if (is_null($this->storefrontBasketService)) {
|
||||
throw GraphQLServiceNotFound::byServiceName(StorefrontBasketService::class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Service;
|
||||
|
||||
final class NamespaceMapper
|
||||
{
|
||||
public function getControllerNamespaceMapping(): array
|
||||
{
|
||||
return [
|
||||
'\\OxidEsales\\PayPalModule\\GraphQL\\Controller' => __DIR__ . '/../Controller/',
|
||||
];
|
||||
}
|
||||
|
||||
public function getTypeNamespaceMapping(): array
|
||||
{
|
||||
return [
|
||||
'\\OxidEsales\\PayPalModule\\GraphQL\\DataType' => __DIR__ . '/../DataType/',
|
||||
'\\OxidEsales\\PayPalModule\\GraphQL\\Service' => __DIR__ . '/../Service/',
|
||||
];
|
||||
}
|
||||
}
|
||||
297
shop/source/modules/oe/oepaypal/GraphQL/Service/Payment.php
Normal file
297
shop/source/modules/oe/oepaypal/GraphQL/Service/Payment.php
Normal file
@@ -0,0 +1,297 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Service;
|
||||
|
||||
use OxidEsales\Eshop\Core\Model\BaseModel;
|
||||
use OxidEsales\Eshop\Core\Registry as EshopRegistry;
|
||||
use OxidEsales\GraphQL\Base\Exception\InvalidLogin;
|
||||
use OxidEsales\GraphQL\Storefront\Basket\DataType\Basket as BasketDataType;
|
||||
use OxidEsales\GraphQL\Storefront\Basket\DataType\BasketOwner as BasketOwnerDataType;
|
||||
use OxidEsales\GraphQL\Storefront\Customer\Exception\CustomerNotFound;
|
||||
use OxidEsales\GraphQL\Storefront\Shared\Infrastructure\Basket as SharedBasketInfrastructure;
|
||||
use OxidEsales\GraphQL\Storefront\Basket\Service\BasketRelationService;
|
||||
use OxidEsales\PayPalModule\GraphQL\DataType\PayPalCommunicationInformation;
|
||||
use OxidEsales\PayPalModule\GraphQL\DataType\PayPalTokenStatus;
|
||||
use OxidEsales\PayPalModule\GraphQL\Exception\BasketValidation;
|
||||
use OxidEsales\PayPalModule\GraphQL\Exception\GraphQLServiceNotFound;
|
||||
use OxidEsales\PayPalModule\GraphQL\Infrastructure\Request as RequestInfrastructure;
|
||||
use OxidEsales\PayPalModule\Model\PaymentManager;
|
||||
use OxidEsales\PayPalModule\Model\Response\ResponseGetExpressCheckoutDetails;
|
||||
use OxidEsales\Eshop\Application\Model\Basket as EshopBasketModel;
|
||||
use OxidEsales\Eshop\Application\Model\User as EshopUserModel;
|
||||
use OxidEsales\Eshop\Application\Model\Address as EshopAddressModel;
|
||||
|
||||
final class Payment
|
||||
{
|
||||
/** @var RequestInfrastructure */
|
||||
private $requestInfrastructure;
|
||||
|
||||
/** @var SharedBasketInfrastructure */
|
||||
private $sharedBasketInfrastructure;
|
||||
|
||||
/** @var BasketRelationService */
|
||||
private $basketRelationService;
|
||||
|
||||
public function __construct(
|
||||
RequestInfrastructure $requestInfrastructure,
|
||||
SharedBasketInfrastructure $sharedBasketInfrastructure = null,
|
||||
BasketRelationService $basketRelationService = null
|
||||
) {
|
||||
$this->requestInfrastructure = $requestInfrastructure;
|
||||
$this->sharedBasketInfrastructure = $sharedBasketInfrastructure;
|
||||
$this->basketRelationService = $basketRelationService;
|
||||
}
|
||||
|
||||
public function getPayPalTokenStatus(
|
||||
string $token,
|
||||
ResponseGetExpressCheckoutDetails $details = null
|
||||
): PayPalTokenStatus {
|
||||
//NOTE: only when the approval was finished on PayPal site
|
||||
//(payment channel and delivery address registered with PayPal)
|
||||
//the getExpressCheckoutResponse will contain the PayerId. So we can use this to get information
|
||||
//about token status. If anything is amiss, PayPal will no let the order pass.
|
||||
|
||||
if (is_null($details)) {
|
||||
$details = $this->getExpressCheckoutDetails($token);
|
||||
}
|
||||
|
||||
$payerId = $details->getPayerId();
|
||||
|
||||
return new PayPalTokenStatus(
|
||||
$token,
|
||||
$payerId ? true : false,
|
||||
$payerId
|
||||
);
|
||||
}
|
||||
|
||||
public function getExpressCheckoutDetails(string $token): ResponseGetExpressCheckoutDetails
|
||||
{
|
||||
$paymentManager = $this->requestInfrastructure->getPaymentManager();
|
||||
return $paymentManager->getExpressCheckoutDetails($token);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BasketValidation
|
||||
*/
|
||||
public function getValidEshopBasketModel(
|
||||
BasketDataType $userBasket,
|
||||
ResponseGetExpressCheckoutDetails $expressCheckoutDetails
|
||||
): EshopBasketModel {
|
||||
|
||||
$this->validateState();
|
||||
|
||||
//At this point we need to check if we have PayPal Express or paypal as standard payment method checkout
|
||||
// and act accordingly
|
||||
if (PaymentManager::PAYPAL_SERVICE_TYPE_EXPRESS == $userBasket->getEshopModel()->getFieldData('OEPAYPAL_SERVICE_TYPE')) {
|
||||
$paymentManager = $this->requestInfrastructure->getPaymentManager();
|
||||
$user = $paymentManager->initializeUserData($expressCheckoutDetails, (string) $userBasket->getUserId());
|
||||
|
||||
$userBasket->getEshopModel()->setUser($user);
|
||||
|
||||
if (is_null($user->getSelectedAddressId())) {
|
||||
EshopRegistry::getSession()->deleteVariable('deladrid');
|
||||
}
|
||||
|
||||
EshopRegistry::getSession()->setVariable('usr', $user->getId());
|
||||
EshopRegistry::getSession()->setUser($user);
|
||||
|
||||
$deliveryMethodId = $paymentManager->extractShippingId($expressCheckoutDetails->getShippingOptionName(), $user);
|
||||
$userBasket->getEshopModel()->assign([
|
||||
'OEGQL_DELIVERYMETHODID' => $deliveryMethodId,
|
||||
'OEGQL_DELADDRESSID' => $user->getSelectedAddressId()
|
||||
]);
|
||||
$userBasket->getEshopModel()->save();
|
||||
}
|
||||
|
||||
$sessionBasket = $this->sharedBasketInfrastructure->getCalculatedBasket($userBasket);
|
||||
|
||||
$this->validateApprovedBasketAmount($sessionBasket, $expressCheckoutDetails, $userBasket);
|
||||
$this->validateApprovedBasketAddress($sessionBasket, $expressCheckoutDetails, $userBasket);
|
||||
|
||||
return $sessionBasket;
|
||||
}
|
||||
|
||||
private function validateApprovedBasketAddress(
|
||||
EshopBasketModel $sessionBasket,
|
||||
ResponseGetExpressCheckoutDetails $expressCheckoutDetails,
|
||||
BasketDataType $userBasket
|
||||
): void {
|
||||
$modelWithAddress = $this->calculateDeliveryAddressModel($sessionBasket, $userBasket);
|
||||
|
||||
//Ensure delivery address registered with PayPal is the same as shop will use
|
||||
$paypalAddressModel = oxNew(EshopAddressModel::class);
|
||||
$paypalAddressData = $paypalAddressModel->prepareDataPayPalAddress($expressCheckoutDetails);
|
||||
|
||||
$compareWith = [];
|
||||
foreach ($paypalAddressData as $key => $value) {
|
||||
$compareWith[$key] = $modelWithAddress->getFieldData($key);
|
||||
}
|
||||
|
||||
$diff = array_diff($paypalAddressData, $compareWith);
|
||||
if (!empty($diff)) {
|
||||
throw BasketValidation::basketAddressChange($userBasket->id()->val());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delivery address is currently related to user basket
|
||||
* if that one is null, the user's invoice address is used as delivery address
|
||||
*/
|
||||
private function calculateDeliveryAddressModel(
|
||||
EshopBasketModel $sessionBasket,
|
||||
BasketDataType $userBasket
|
||||
): BaseModel {
|
||||
$this->validateState();
|
||||
|
||||
$shipToAddress = $this->basketRelationService->deliveryAddress($userBasket);
|
||||
if (!is_null($shipToAddress)) {
|
||||
/** @var EshopAddressModel $eshopModel */
|
||||
$modelWithAddress = $shipToAddress->getEshopModel();
|
||||
} else {
|
||||
/** @var EshopUserModel $modelWithAddress */
|
||||
$modelWithAddress = $sessionBasket->getUser();
|
||||
}
|
||||
|
||||
return $modelWithAddress;
|
||||
}
|
||||
|
||||
private function validateApprovedBasketAmount(
|
||||
EshopBasketModel $sessionBasket,
|
||||
ResponseGetExpressCheckoutDetails $expressCheckoutDetails,
|
||||
BasketDataType $userBasket
|
||||
): void {
|
||||
$paymentManager = $this->requestInfrastructure->getPaymentManager();
|
||||
|
||||
/** @var \OxidEsales\Eshop\Core\Price $price */
|
||||
$price = $sessionBasket->getPrice();
|
||||
if (
|
||||
!$price ||
|
||||
!$paymentManager->validateApprovedBasketAmount(
|
||||
$price->getBruttoPrice(),
|
||||
$expressCheckoutDetails->getAmount()
|
||||
)
|
||||
) {
|
||||
throw BasketValidation::basketChange($userBasket->id()->val());
|
||||
}
|
||||
}
|
||||
|
||||
public function getPayPalCommunicationInformation(
|
||||
BasketDataType $basket,
|
||||
string $returnUrl,
|
||||
string $cancelUrl,
|
||||
bool $displayBasketInPayPal
|
||||
): PayPalCommunicationInformation {
|
||||
$this->validateState();
|
||||
|
||||
$paymentManager = $this->requestInfrastructure->getPaymentManager();
|
||||
|
||||
$shipToAddress = $this->basketRelationService->deliveryAddress($basket);
|
||||
$shipToAddressId = $shipToAddress ? (string) $shipToAddress->id(): '';
|
||||
|
||||
$response = $paymentManager->setStandardCheckout(
|
||||
$this->sharedBasketInfrastructure->getBasket($basket),
|
||||
$this->basketRelationService->owner($basket)->getEshopModel(),
|
||||
$returnUrl,
|
||||
$cancelUrl,
|
||||
$displayBasketInPayPal,
|
||||
$shipToAddressId
|
||||
);
|
||||
|
||||
$token = (string) $response->getToken();
|
||||
|
||||
return new PayPalCommunicationInformation(
|
||||
$token,
|
||||
$this->getPayPalCommunicationUrl($token)
|
||||
);
|
||||
}
|
||||
|
||||
public function getPayPalExpressCommunicationInformation(
|
||||
BasketDataType $basket,
|
||||
string $returnUrl,
|
||||
string $cancelUrl,
|
||||
bool $displayBasketInPayPal
|
||||
): PayPalCommunicationInformation {
|
||||
$this->validateState();
|
||||
|
||||
$paymentManager = $this->requestInfrastructure->getPaymentManager();
|
||||
$shipToAddressId = $this->getDeliveryAddressId($basket);
|
||||
$deliveryMethod = $this->basketRelationService->deliveryMethod($basket);
|
||||
$shippingMethodId = $deliveryMethod ? (string) $deliveryMethod->id() : 'oxidstandard';
|
||||
|
||||
//for Express checkout, the user might not yet exist (anonymous user)
|
||||
try {
|
||||
/** @var BasketOwnerDataType $customer */
|
||||
$owner = $this->basketRelationService->owner($basket);
|
||||
$user = $owner->getEshopModel();
|
||||
$user->setSelectedAddressId($shipToAddressId);
|
||||
} catch (CustomerNotFound $e) {
|
||||
$user = null;
|
||||
}
|
||||
|
||||
$response = $paymentManager->setExpressCheckout(
|
||||
$this->sharedBasketInfrastructure->getBasket($basket),
|
||||
$user,
|
||||
$returnUrl,
|
||||
$cancelUrl,
|
||||
$paymentManager->getGraphQLCallBackUrl((string) $basket->id()),
|
||||
$displayBasketInPayPal,
|
||||
$shippingMethodId
|
||||
);
|
||||
|
||||
$token = (string) $response->getToken();
|
||||
|
||||
return new PayPalCommunicationInformation(
|
||||
$token,
|
||||
$this->getPayPalCommunicationUrl($token)
|
||||
);
|
||||
}
|
||||
|
||||
public function getPayPalCommunicationUrl($token): string
|
||||
{
|
||||
$payPalConfig = $this->requestInfrastructure->getPayPalConfig();
|
||||
return $payPalConfig->getPayPalCommunicationUrl($token);
|
||||
}
|
||||
|
||||
protected function validateState(): void
|
||||
{
|
||||
if (is_null($this->sharedBasketInfrastructure)) {
|
||||
throw GraphQLServiceNotFound::byServiceName(SharedBasketInfrastructure::class);
|
||||
}
|
||||
|
||||
if (is_null($this->basketRelationService)) {
|
||||
throw GraphQLServiceNotFound::byServiceName(BasketRelationService::class);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getDeliveryAddressId(BasketDataType $basket): String
|
||||
{
|
||||
try {
|
||||
//use might be logged in to his existing shop account
|
||||
$shipToAddress = $this->basketRelationService->deliveryAddress($basket);
|
||||
} catch (InvalidLogin $e) {
|
||||
//in case of anonymous user access of delivery address is forbidden
|
||||
}
|
||||
|
||||
return $shipToAddress ? (string) $shipToAddress->id(): '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Service;
|
||||
|
||||
final class PermissionProvider
|
||||
{
|
||||
public function getPermissions(): array
|
||||
{
|
||||
return [
|
||||
'oxidcustomer' => [
|
||||
'PAYPAL_EXPRESS_APPROVAL',
|
||||
'PAYPAL_TOKEN_STATUS'
|
||||
],
|
||||
'oxidnotyetordered' => [
|
||||
'PAYPAL_EXPRESS_APPROVAL',
|
||||
'PAYPAL_TOKEN_STATUS'
|
||||
],
|
||||
'oxidanonymous' => [
|
||||
'CREATE_BASKET',
|
||||
'VIEW_BASKET',
|
||||
'ADD_PRODUCT_TO_BASKET',
|
||||
'REMOVE_BASKET_PRODUCT',
|
||||
'ADD_VOUCHER',
|
||||
'REMOVE_VOUCHER',
|
||||
'PLACE_ORDER',
|
||||
'PAYPAL_EXPRESS_APPROVAL',
|
||||
'PAYPAL_TOKEN_STATUS'
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Subscriber;
|
||||
|
||||
use OxidEsales\Eshop\Core\Registry as EshopRegistry;
|
||||
use OxidEsales\EshopCommunity\Internal\Framework\Event\AbstractShopAwareEventSubscriber;
|
||||
use OxidEsales\GraphQL\Storefront\Basket\Event\BeforeBasketPayments as BeforeBasketPaymentsEvent;
|
||||
use OxidEsales\GraphQL\Storefront\Basket\Service\Basket as StorefrontBasketService;
|
||||
use OxidEsales\PayPalModule\Core\Config as PayPalConfig;
|
||||
use OxidEsales\PayPalModule\GraphQL\Exception\GraphQLServiceNotFound;
|
||||
use OxidEsales\PayPalModule\GraphQL\Service\Basket as BasketService;
|
||||
use OxidEsales\PayPalModule\GraphQL\Service\BasketExtendType;
|
||||
|
||||
class BeforeBasketPayments extends AbstractShopAwareEventSubscriber
|
||||
{
|
||||
/** @var BasketService */
|
||||
private $basketService;
|
||||
|
||||
/** @var StorefrontBasketService */
|
||||
private $storefrontBasketService;
|
||||
|
||||
public function __construct(
|
||||
BasketService $basketService,
|
||||
StorefrontBasketService $storefrontBasketService = null
|
||||
) {
|
||||
$this->basketService = $basketService;
|
||||
$this->storefrontBasketService = $storefrontBasketService;
|
||||
}
|
||||
|
||||
public function handle(BeforeBasketPaymentsEvent $event): BeforeBasketPaymentsEvent
|
||||
{
|
||||
$this->validateState();
|
||||
|
||||
$basketDataType = $this->storefrontBasketService->getAuthenticatedCustomerBasket($event->getBasketId());
|
||||
if ($this->basketService->checkBasketPaymentMethodIsPayPal($basketDataType)) {
|
||||
$extendUserBasket = new BasketExtendType();
|
||||
$session = EshopRegistry::getSession();
|
||||
$session->setVariable(
|
||||
PayPalConfig::OEPAYPAL_TRIGGER_NAME,
|
||||
$extendUserBasket->paypalServiceType($basketDataType)
|
||||
);
|
||||
}
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'OxidEsales\GraphQL\Storefront\Basket\Event\BeforeBasketPayments' => 'handle'
|
||||
];
|
||||
}
|
||||
|
||||
protected function validateState(): void
|
||||
{
|
||||
if (is_null($this->storefrontBasketService)) {
|
||||
throw GraphQLServiceNotFound::byServiceName(StorefrontBasketService::class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of O3-Shop Paypal module.
|
||||
*
|
||||
* O3-Shop is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* O3-Shop is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with O3-Shop. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @copyright Copyright (c) 2022 OXID eSales AG (https://www.oxid-esales.com)
|
||||
* @copyright Copyright (c) 2022 O3-Shop (https://www.o3-shop.com)
|
||||
* @license https://www.gnu.org/licenses/gpl-3.0 GNU General Public License 3 (GPLv3)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OxidEsales\PayPalModule\GraphQL\Subscriber;
|
||||
|
||||
use OxidEsales\PayPalModule\GraphQL\Exception\GraphQLServiceNotFound;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use OxidEsales\EshopCommunity\Internal\Framework\Event\AbstractShopAwareEventSubscriber;
|
||||
use OxidEsales\PayPalModule\GraphQL\Service\BeforePlaceOrder as BeforePlaceOrderService;
|
||||
|
||||
class BeforePlaceOrder extends AbstractShopAwareEventSubscriber
|
||||
{
|
||||
/** @var BeforePlaceOrderService */
|
||||
private $beforePlaceOrderService;
|
||||
|
||||
public function __construct(BeforePlaceOrderService $beforePlaceOrderService)
|
||||
{
|
||||
$this->beforePlaceOrderService = $beforePlaceOrderService;
|
||||
}
|
||||
|
||||
public function handle(Event $event): Event
|
||||
{
|
||||
$this->validateState();
|
||||
|
||||
$this->beforePlaceOrderService->handle($event);
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'OxidEsales\GraphQL\Storefront\Basket\Event\BeforePlaceOrder' => 'handle'
|
||||
];
|
||||
}
|
||||
|
||||
protected function validateState(): void
|
||||
{
|
||||
if (is_null($this->beforePlaceOrderService)) {
|
||||
throw GraphQLServiceNotFound::byServiceName(BeforePlaceOrderService::class);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user