-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathPaymentCommand.php
More file actions
119 lines (89 loc) · 4.22 KB
/
PaymentCommand.php
File metadata and controls
119 lines (89 loc) · 4.22 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
<?php
declare(strict_types=1);
namespace App\Command;
use Ibexa\Contracts\Core\Collection\ArrayMap;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\OrderManagement\OrderServiceInterface;
use Ibexa\Contracts\Payment\Payment\PaymentCreateStruct;
use Ibexa\Contracts\Payment\Payment\PaymentQuery;
use Ibexa\Contracts\Payment\Payment\PaymentUpdateStruct;
use Ibexa\Contracts\Payment\Payment\Query\Criterion\Currency;
use Ibexa\Contracts\Payment\Payment\Query\Criterion\LogicalOr;
use Ibexa\Contracts\Payment\PaymentMethodServiceInterface;
use Ibexa\Contracts\Payment\PaymentServiceInterface;
use Money;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
final class PaymentCommand extends Command
{
private PermissionResolver $permissionResolver;
private UserService $userService;
private PaymentServiceInterface $paymentService;
private OrderServiceInterface $orderService;
private PaymentMethodServiceInterface $paymentMethodService;
public function __construct(
PermissionResolver $permissionResolver,
UserService $userService,
PaymentServiceInterface $paymentService,
OrderServiceInterface $orderService,
PaymentMethodServiceInterface $paymentMethodService
) {
$this->paymentService = $paymentService;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;
$this->orderService = $orderService;
$this->paymentMethodService = $paymentMethodService;
parent::__construct('doc:payment');
}
public function configure(): void
{
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$currentUser = $this->userService->loadUserByLogin('admin');
$this->permissionResolver->setCurrentUserReference($currentUser);
// Get a single payment
$paymentId = 1;
$payment = $this->paymentService->getPayment($paymentId);
$output->writeln(sprintf('Payment %d has status %s', $paymentId, $payment->getStatus()));
// Get a single payment by identifier
$paymentIdentifier = '4ac4b8a0-eed8-496d-87d9-32a960a10629';
$payment = $this->paymentService->getPaymentByIdentifier($paymentIdentifier);
$output->writeln(sprintf('Your payment for transaction has status %s', $payment->getStatus()));
// Query for payments
$paymentCriterions = [
new Currency('USD'),
new Currency('CZK'),
];
$paymentQuery = new PaymentQuery(new LogicalOr(...$paymentCriterions));
$paymentQuery->setLimit(10);
$paymentsList = $this->paymentService->findPayments($paymentQuery);
$paymentsList->getPayments();
$paymentsList->getTotalCount();
foreach ($paymentsList as $payment) {
$output->writeln($payment->getIdentifier() . ': ' . $payment->getOrder()->getIdentifier() . ': ' . $payment->getOrder()->getValue()->getTotalGross()->getAmount());
}
// Create a new payment
$context = [
'transaction_id' => '5e5fe187-c865-49£2-b407-a946fd7b5be0',
];
$paymentCreateStruct = new PaymentCreateStruct(
$this->paymentMethodService->getPaymentMethodByIdentifier('bank_transfer_EUR'),
$this->orderService->getOrder(135),
new Money\Money(100, new Money\Currency('EUR'))
);
$paymentCreateStruct->setContext(new ArrayMap($context));
$payment = $this->paymentService->createPayment($paymentCreateStruct);
$output->writeln(sprintf('Created payment %s for order %s', $payment->getIdentifier(), $payment->getOrder()->getIdentifier()));
// Update existing payment
$paymentUpdateStruct = new PaymentUpdateStruct();
$paymentUpdateStruct->setTransition('pay');
$this->paymentService->updatePayment($payment, $paymentUpdateStruct);
$output->writeln(sprintf('Changed payment status to %s', $payment->getStatus()));
// Delete existing payment permanently
$this->paymentService->deletePayment($payment);
return self::SUCCESS;
}
}