Skip to content

Commit

Permalink
Merge pull request #24 from BitBagCommerce/damonsson-patch-1
Browse files Browse the repository at this point in the history
fix capture action
  • Loading branch information
damonsson authored Oct 9, 2019
2 parents 9759d28 + 7a73fda commit 1429f89
Showing 1 changed file with 39 additions and 43 deletions.
82 changes: 39 additions & 43 deletions src/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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
Expand Down Expand Up @@ -67,41 +68,35 @@ public function setApi($api): void
public function execute($request): void
{
RequestNotSupportedException::assertSupports($this, $request);

$model = $request->getModel();
ArrayObject::ensureArrayObject($model);

/** @var OrderInterface $order */
/** @var OrderInterface $orderData */
$order = $request->getFirstModel()->getOrder();

$model['customer'] = $order->getCustomer();
$model['locale'] = $this->getFallbackLocaleCode($order->getLocaleCode());
/** @var TokenInterface $token */
$token = $request->getToken();
$payUdata = $this->prepareOrder($token, $order);

$result = $this->openPayUBridge->create($payUdata);

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;
}
}

/** @var TokenInterface $token */
$token = $request->getToken();
$order = $this->prepareOrder($token, $model);

$result = $this->openPayUBridge->create($order);
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);
Expand All @@ -123,21 +118,20 @@ public function supports($request): bool
&& $request->getModel() instanceof ArrayObject;
}

private function prepareOrder(TokenInterface $token, ArrayObject $model): array
private function prepareOrder(TokenInterface $token, OrderInterface $order): array
{
$notifyToken = $this->tokenFactory->createNotifyToken($token->getGatewayName(), $token->getDetails());

$order = [];
$order['continueUrl'] = $token->getTargetUrl();
$order['notifyUrl'] = $notifyToken->getTargetUrl();
$order['customerIp'] = $model['customerIp'];
$order['merchantPosId'] = OpenPayU_Configuration::getMerchantPosId();
$order['description'] = $model['description'];
$order['currencyCode'] = $model['currencyCode'];
$order['totalAmount'] = $model['totalAmount'];
$order['extOrderId'] = $model['extOrderId'];
$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 = $model['customer'];
$customer = $order->getCustomer();

Assert::isInstanceOf(
$customer,
Expand All @@ -149,31 +143,33 @@ private function prepareOrder(TokenInterface $token, ArrayObject $model): array
);

$buyer = [
'email' => (string) $customer->getEmail(),
'firstName' => (string) $customer->getFirstName(),
'lastName' => (string) $customer->getLastName(),
'language' => $model['locale'],
'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);

$order['buyer'] = $buyer;
$order['products'] = $this->resolveProducts($model);

return $order;
return $payUdata;
}

private function resolveProducts(ArrayObject $model): array
private function getOrderItems(OrderInterface $order): array
{
if (!array_key_exists('products', $model) || 0 === count($model['products'])) {
return [
[
'name' => $model['description'],
'unitPrice' => $model['totalAmount'],
'quantity' => 1,
],
];
$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 $model['products'];
return $itemsData;
}

private function getFallbackLocaleCode(string $localeCode): string
Expand Down

0 comments on commit 1429f89

Please sign in to comment.