-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCaptureAction.php
More file actions
177 lines (148 loc) · 6 KB
/
Copy pathCaptureAction.php
File metadata and controls
177 lines (148 loc) · 6 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/
declare(strict_types=1);
namespace BitBag\SyliusPayUPlugin\Action;
use BitBag\SyliusPayUPlugin\Bridge\OpenPayUBridgeInterface;
use BitBag\SyliusPayUPlugin\Exception\PayUException;
use OpenPayU_Configuration;
use Payum\Core\Action\ActionInterface;
use Payum\Core\ApiAwareInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Exception\UnsupportedApiException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Reply\HttpRedirect;
use Payum\Core\Request\Capture;
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
use Payum\Core\Security\GenericTokenFactoryInterface;
use Payum\Core\Security\TokenInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Webmozart\Assert\Assert;
final class CaptureAction implements ActionInterface, ApiAwareInterface, GenericTokenFactoryAwareInterface, GatewayAwareInterface
{
use GatewayAwareTrait;
/** @var OpenPayUBridgeInterface */
private $openPayUBridge;
/** @var GenericTokenFactoryInterface */
private $tokenFactory;
public function __construct(OpenPayUBridgeInterface $openPayUBridge)
{
$this->openPayUBridge = $openPayUBridge;
}
/**
* @throws UnsupportedApiException if the given Api is not supported.
*/
public function setApi($api): void
{
if (false === is_array($api)) {
throw new UnsupportedApiException('Not supported. Expected to be set as array.');
}
$this->openPayUBridge->setAuthorizationData(
$api['environment'],
$api['signature_key'],
$api['pos_id'],
$api['oauth_client_id'],
$api['oauth_client_secret']
);
}
public function execute($request): void
{
RequestNotSupportedException::assertSupports($this, $request);
$model = $request->getModel();
/** @var OrderInterface $orderData */
$order = $request->getFirstModel()->getOrder();
/** @var TokenInterface $token */
$token = $request->getToken();
$payUdata = $this->prepareOrder($token, $order);
if (null !== $model['orderId']) {
/** @var mixed $response */
$response = $this->openPayUBridge->retrieve((string) $model['orderId'])->getResponse();
Assert::keyExists($response->orders, 0);
if (OpenPayUBridgeInterface::SUCCESS_API_STATUS === $response->status->statusCode) {
$model['statusPayU'] = $response->orders[0]->status;
$request->setModel($model);
}
if (OpenPayUBridgeInterface::NEW_API_STATUS !== $response->orders[0]->status) {
return;
}
}
$result = $this->openPayUBridge->create($payUdata);
if (null !== $result) {
/** @var mixed $response */
$response = $result->getResponse();
if ($response && OpenPayUBridgeInterface::SUCCESS_API_STATUS === $response->status->statusCode) {
$model['orderId'] = $response->orderId;
$request->setModel($model);
throw new HttpRedirect($response->redirectUri);
}
}
throw PayUException::newInstance($response->status);
}
public function setGenericTokenFactory(GenericTokenFactoryInterface $genericTokenFactory = null): void
{
$this->tokenFactory = $genericTokenFactory;
}
public function supports($request): bool
{
return
$request instanceof Capture
&& $request->getModel() instanceof ArrayObject;
}
private function prepareOrder(TokenInterface $token, OrderInterface $order): array
{
$notifyToken = $this->tokenFactory->createNotifyToken($token->getGatewayName(), $token->getDetails());
$payUdata = [];
$payUdata['continueUrl'] = $token->getTargetUrl();
$payUdata['notifyUrl'] = $notifyToken->getTargetUrl();
$payUdata['customerIp'] = $order->getCustomerIp();
$payUdata['merchantPosId'] = OpenPayU_Configuration::getMerchantPosId();
$payUdata['description'] = $order->getNumber();
$payUdata['currencyCode'] = $order->getCurrencyCode();
$payUdata['totalAmount'] = $order->getTotal();
/** @var CustomerInterface $customer */
$customer = $order->getCustomer();
Assert::isInstanceOf(
$customer,
CustomerInterface::class,
sprintf(
'Make sure the first model is the %s instance.',
CustomerInterface::class
)
);
$buyer = [
'email' => (string) $customer->getEmail(),
'firstName' => (string) $customer->getFirstName(),
'lastName' => (string) $customer->getLastName(),
'language' => $this->getFallbackLocaleCode($order->getLocaleCode()),
];
$payUdata['buyer'] = $buyer;
$payUdata['products'] = $this->getOrderItems($order);
return $payUdata;
}
private function getOrderItems(OrderInterface $order): array
{
$itemsData = [];
if ($items = $order->getItems()) {
/** @var OrderItemInterface $item */
foreach ($items as $key => $item) {
$itemsData[$key] = [
'name' => $item->getProductName(),
'unitPrice' => $item->getUnitPrice(),
'quantity' => $item->getQuantity(),
];
}
}
return $itemsData;
}
private function getFallbackLocaleCode(string $localeCode): string
{
return explode('_', $localeCode)[0];
}
}