-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReviewOrderPayment.php
89 lines (79 loc) · 2.81 KB
/
ReviewOrderPayment.php
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
<?php
namespace PayU\PaymentGateway\Model;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order\Payment;
use PayU\PaymentGateway\Api\OrderPaymentResolverInterface;
use PayU\PaymentGateway\Api\PayUUpdateOrderStatusInterface;
use PayU\PaymentGateway\Api\ReviewOrderPaymentInterface;
class ReviewOrderPayment implements ReviewOrderPaymentInterface
{
/**
* Error Message
*/
const REVIEW_ERROR = 'We can\'t update the payment right now.';
private Payment $payment;
private PayUUpdateOrderStatusInterface $updateOrderStatus;
private EventManager $eventManager;
private OrderPaymentResolverInterface $paymentResolver;
public function __construct(
PayUUpdateOrderStatusInterface $updateOrderStatus,
EventManager $eventManager,
OrderPaymentResolverInterface $paymentResolver
)
{
$this->updateOrderStatus = $updateOrderStatus;
$this->eventManager = $eventManager;
$this->paymentResolver = $paymentResolver;
}
/**
* {@inheritdoc}
*/
public function execute(OrderInterface $order, string $action): void
{
$this->payment = $this->paymentResolver->getLast($order);
$method = $action . 'Payment';
if (!method_exists($this, $method)) {
throw new LocalizedException(__('Action "%1" is not supported.', $action));
}
$this->{$method}($order->getStoreId());
}
/**
* Accept Payment Action
*
* @throws LocalizedException|\OpenPayU_Exception
*/
protected function acceptPayment(int $storeId): void
{
$response = $this->updateOrderStatus->update(
$this->payment->getMethod(),
$storeId,
$this->payment->getLastTransId(),
\OpenPayuOrderStatus::STATUS_COMPLETED
);
if ($response->getStatus() === \OpenPayU_Order::SUCCESS) {
$this->eventManager->dispatch('payu_payment_status_assign', ['payment' => $this->payment]);
} else {
throw new LocalizedException(__(static::REVIEW_ERROR));
}
}
/**
* Deny Payment Action
*
* @throws LocalizedException|\OpenPayU_Exception
*/
protected function denyPayment(int $storeId): void
{
$response = $this->updateOrderStatus->cancel(
$this->payment->getMethod(),
$storeId,
$this->payment->getLastTransId()
);
if ($response !== null && $response->getStatus() === \OpenPayU_Order::SUCCESS) {
$this->eventManager->dispatch('payu_payment_status_assign', ['payment' => $this->payment]);
} else {
throw new LocalizedException(__(static::REVIEW_ERROR));
}
}
}