Skip to content

Commit

Permalink
add module code
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad-konieczny committed Oct 17, 2023
1 parent 115721f commit 2566ea5
Show file tree
Hide file tree
Showing 10 changed files with 549 additions and 0 deletions.
78 changes: 78 additions & 0 deletions Model/Resolver/PaymentUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Paytrail\PaymentServiceGraphQl\Model\Resolver;

use Magento\Checkout\Model\Session;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Sales\Model\Order;
use Paytrail\PaymentService\Exceptions\CheckoutException;
use Paytrail\PaymentService\Gateway\Config\Config;
use Paytrail\PaymentService\Helper\ApiData;
use Paytrail\PaymentService\Helper\Data as paytrailHelper;
use Paytrail\SDK\Response\PaymentResponse;
use Psr\Log\LoggerInterface;

class PaymentUrl implements ResolverInterface
{
public function __construct(
private readonly Session $checkoutSession,
private readonly ApiData $apiData,
private readonly paytrailHelper $paytrailHelper,
private readonly LoggerInterface $logger
) {
}

/**
* @param Field $field
* @param $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
*
* @return string[]
* @throws CheckoutException
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): array
{
$result = [
'payment_url' => '',
'error' => ''
];

try {
$order = $this->checkoutSession->getLastRealOrder();
if ($order->getPayment()->getMethod() == Config::CODE) {
$responseData = $this->getResponseData($order);
$result['payment_url'] = $responseData->getHref();
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
$result['error'] = $e->getMessage();
}

return $result;
}

/**
* @param Order $order
*
* @return PaymentResponse
* @throws CheckoutException
*/
private function getResponseData(
Order $order
): PaymentResponse {
$response = $this->apiData->processApiRequest('payment', $order);

$errorMsg = $response['error'];

if (isset($errorMsg)) {
$this->errorMsg = ($errorMsg);
$this->paytrailHelper->processError($errorMsg);
}

return $response["data"];
}
}
121 changes: 121 additions & 0 deletions Model/Resolver/PaytrailCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types=1);

namespace Paytrail\PaymentServiceGraphQl\Model\Resolver;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\Quote\Model\Quote;

/**
* @inheritdoc
*/
class PaytrailCart implements ResolverInterface
{

/**
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
private readonly MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
private readonly CartRepositoryInterface $cartRepository
) {
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (empty($args['cart_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}

$maskedCartId = $args['cart_id'];

$currentUserId = $context->getUserId();
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
$cart = $this->getPaytrailCart($maskedCartId, $currentUserId, $storeId);

return [
'model' => $cart,
];
}

/**
* Get cart for user
*
* @param string $cartHash
* @param int|null $customerId
* @param int $storeId
*
* @return Quote
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
* @throws NoSuchEntityException
*/
public function getPaytrailCart(string $cartHash, ?int $customerId, int $storeId): Quote
{
try {
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
);
}

try {
/** @var Quote $cart */
$cart = $this->cartRepository->get($cartId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
);
}

if ($cart->getPayment()->getMethod() && !str_contains($cart->getPayment()->getMethod(), 'paytrail')) {
throw new GraphQlNoSuchEntityException(
__(
'The cart paid "%masked_cart_id" isn\'t with Paytrail payment method. ',
['masked_cart_id' => $cartHash]
)
);
}

if ((int)$cart->getStoreId() !== $storeId) {
throw new GraphQlNoSuchEntityException(
__(
'Wrong store code specified for cart "%masked_cart_id"',
['masked_cart_id' => $cartHash]
)
);
}

$cartCustomerId = (int)$cart->getCustomerId();

/* Guest cart, allow operations */
if (0 === $cartCustomerId && (null === $customerId || 0 === $customerId)) {
return $cart;
}

if ($cartCustomerId !== $customerId) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot perform operations on cart "%masked_cart_id"',
['masked_cart_id' => $cartHash]
)
);
}

return $cart;
}
}
123 changes: 123 additions & 0 deletions Model/Resolver/RestoreQuote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is provided with Magento in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* See DISCLAIMER.md for disclaimer details.
*/

declare(strict_types=1);

namespace Paytrail\PaymentServiceGraphQl\Model\Resolver;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;

class RestoreQuote implements ResolverInterface
{

/**
* RestoreQuote constructor.
*
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
private readonly MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
private readonly CartRepositoryInterface $cartRepository
) {
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): string {
if (empty($args['input']['cart_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing.'));
}

$maskedCartId = $args['input']['cart_id'];
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
$cart = $this->getQuoteByHash($maskedCartId, $context->getUserId(), $storeId);

if (!$cart->getIsActive()) {
$this->restoreQuote($cart);
}

return $maskedCartId;
}

/**
* @param string $cartHash
* @param int|null $customerId
* @param int $storeId
*
* @return CartInterface
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
private function getQuoteByHash(string $cartHash, ?int $customerId, int $storeId): CartInterface
{
try {
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
$cart = $this->cartRepository->get($cartId);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%1"', $cartHash)
);
}

if ($cart->getPayment()->getMethod() && !str_contains($cart->getPayment()->getMethod(), 'paytrail')) {
throw new GraphQlNoSuchEntityException(
__('This cart "%1" is not using a Paytrail payment method', $cartHash)
);
}

$cartCustomerId = (int)$cart->getCustomerId();

if ($cartCustomerId === 0 && (null === $customerId || 0 === $customerId)) {
return $cart;
}

if ($cartCustomerId !== $customerId) {
throw new GraphQlAuthorizationException(
__('The current user cannot perform operations on cart "%1"', $cartHash)
);
}

if ((int)$cart->getStoreId() !== $storeId) {
throw new GraphQlNoSuchEntityException(
__('Wrong store code specified for cart "%1"', $cartHash)
);
}

return $cart;
}

/**
* @param CartInterface $cart
*/
private function restoreQuote(CartInterface $cart): void
{
$cart->setIsActive(1)->setReservedOrderId(null);
$this->cartRepository->save($cart);
}
}
Loading

0 comments on commit 2566ea5

Please sign in to comment.