|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\VaultGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 11 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 12 | +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; |
| 13 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 14 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 15 | +use Magento\Vault\Api\PaymentTokenManagementInterface; |
| 16 | +use Magento\Vault\Api\PaymentTokenRepositoryInterface; |
| 17 | +use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount; |
| 18 | + |
| 19 | +/** |
| 20 | + * Delete Payment Token resolver, used for GraphQL mutation processing. |
| 21 | + */ |
| 22 | +class DeletePaymentToken implements ResolverInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var CheckCustomerAccount |
| 26 | + */ |
| 27 | + private $checkCustomerAccount; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var PaymentTokenManagementInterface |
| 31 | + */ |
| 32 | + private $paymentTokenManagement; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var PaymentTokenRepositoryInterface |
| 36 | + */ |
| 37 | + private $paymentTokenRepository; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param CheckCustomerAccount $checkCustomerAccount |
| 41 | + * @param PaymentTokenManagementInterface $paymentTokenManagement |
| 42 | + * @param PaymentTokenRepositoryInterface $paymentTokenRepository |
| 43 | + */ |
| 44 | + public function __construct( |
| 45 | + CheckCustomerAccount $checkCustomerAccount, |
| 46 | + PaymentTokenManagementInterface $paymentTokenManagement, |
| 47 | + PaymentTokenRepositoryInterface $paymentTokenRepository |
| 48 | + ) { |
| 49 | + $this->checkCustomerAccount = $checkCustomerAccount; |
| 50 | + $this->paymentTokenManagement = $paymentTokenManagement; |
| 51 | + $this->paymentTokenRepository = $paymentTokenRepository; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritdoc |
| 56 | + */ |
| 57 | + public function resolve( |
| 58 | + Field $field, |
| 59 | + $context, |
| 60 | + ResolveInfo $info, |
| 61 | + array $value = null, |
| 62 | + array $args = null |
| 63 | + ) { |
| 64 | + if (!isset($args['public_hash'])) { |
| 65 | + throw new GraphQlInputException(__('Specify the "public_hash" value.')); |
| 66 | + } |
| 67 | + |
| 68 | + $currentUserId = $context->getUserId(); |
| 69 | + $currentUserType = $context->getUserType(); |
| 70 | + |
| 71 | + $this->checkCustomerAccount->execute($currentUserId, $currentUserType); |
| 72 | + |
| 73 | + $token = $this->paymentTokenManagement->getByPublicHash($args['public_hash'], $currentUserId); |
| 74 | + if (!$token) { |
| 75 | + throw new GraphQlNoSuchEntityException( |
| 76 | + __('Could not find a token using public hash: %1', $args['public_hash']) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + return ['result' => $this->paymentTokenRepository->delete($token)]; |
| 81 | + } |
| 82 | +} |
0 commit comments