-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathPaymentMethodCommand.php
More file actions
120 lines (96 loc) · 4.63 KB
/
Copy pathPaymentMethodCommand.php
File metadata and controls
120 lines (96 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace App\Command;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Payment\PaymentMethod\PaymentMethodCreateStruct;
use Ibexa\Contracts\Payment\PaymentMethod\PaymentMethodQuery;
use Ibexa\Contracts\Payment\PaymentMethod\PaymentMethodUpdateStruct;
use Ibexa\Contracts\Payment\PaymentMethod\Query\Criterion\Enabled;
use Ibexa\Contracts\Payment\PaymentMethod\Query\Criterion\LogicalAnd;
use Ibexa\Contracts\Payment\PaymentMethod\Query\Criterion\Type;
use Ibexa\Contracts\Payment\PaymentMethodServiceInterface;
use Ibexa\Payment\Values\PaymentMethodType;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(
name: 'doc:paymentMethod'
)]
final class PaymentMethodCommand extends Command
{
public function __construct(
private readonly PermissionResolver $permissionResolver,
private readonly UserService $userService,
private readonly PaymentMethodServiceInterface $paymentMethodService
) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$currentUser = $this->userService->loadUserByLogin('admin');
$this->permissionResolver->setCurrentUserReference($currentUser);
// Get a single payment method by ID
$paymentMethodId = 1;
$paymentMethod = $this->paymentMethodService->getPaymentMethod($paymentMethodId);
$output->writeln(sprintf('Payment method %d has type "%s"', $paymentMethodId, $paymentMethod->getType()->getIdentifier()));
// Get a single payment method by identifier
$paymentMethodIdentifier = 'cash';
$paymentMethod = $this->paymentMethodService->getPaymentMethodByIdentifier($paymentMethodIdentifier);
$output->writeln(sprintf('Availability status of payment method "%s" is "%s".', $paymentMethodIdentifier, $paymentMethod->isEnabled()));
// Find payment methods
$offlinePaymentType = new PaymentMethodType('offline', 'Offline');
$paymentMethodCriterions = [
new Type($offlinePaymentType),
new Enabled(true),
];
$paymentMethodQuery = new PaymentMethodQuery((new LogicalAnd(...$paymentMethodCriterions)));
$paymentMethodQuery->setLimit(10);
$paymentMethods = $this->paymentMethodService->findPaymentMethods($paymentMethodQuery);
$paymentMethods->getPaymentMethods();
$paymentMethods->getTotalCount();
foreach ($paymentMethods as $paymentMethod) {
$output->writeln($paymentMethod->getIdentifier() . ': ' . $paymentMethod->getName() . ' - ' . $paymentMethod->getDescription());
}
// Create a new payment method
$paymentMethodCreateStruct = new PaymentMethodCreateStruct(
'bank_transfer_EUR',
$offlinePaymentType,
);
$paymentMethodCreateStruct->setName('eng-GB', 'Bank transfer EUR');
$paymentMethodCreateStruct->setEnabled(false);
$paymentMethod = $this->paymentMethodService->createPaymentMethod($paymentMethodCreateStruct);
$output->writeln(sprintf('Created payment method with name %s', $paymentMethod->getName()));
// Update the payment method
$paymentMethodUpdateStruct = new PaymentMethodUpdateStruct();
$paymentMethodUpdateStruct->setEnabled(true);
$this->paymentMethodService->updatePaymentMethod($paymentMethod, $paymentMethodUpdateStruct);
$output->writeln(sprintf(
'Updated payment method "%s" by changing its availability status to "%s".',
$paymentMethod->getName(),
$paymentMethod->isEnabled()
));
// Delete the payment method
$this->paymentMethodService->deletePaymentMethod($paymentMethod);
$output->writeln(sprintf(
'Deleted payment method with ID %d and identifier "%s".',
$paymentMethod->getId(),
$paymentMethod->getIdentifier()
));
// Check whether the payment method is used
$isUsed = $this->paymentMethodService->isPaymentMethodUsed($paymentMethod);
if ($isUsed) {
$output->writeln(sprintf(
'Payment method with ID %d is currently used.',
$paymentMethod->getId()
));
} else {
$output->writeln(sprintf(
'Payment method with ID %d is not used.',
$paymentMethod->getId()
));
}
return self::SUCCESS;
}
}