-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PISHPS-367: reduced ajax calls on order details page (#894)
* PISHPS-367: reduced ajax calls on order details page * PISHPS-367: fixed import issue
- Loading branch information
1 parent
030266f
commit 2ff7b44
Showing
10 changed files
with
248 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Controller\Api\Controller; | ||
|
||
use Kiener\MolliePayments\Controller\Api\Order\CancelLineController; | ||
use Kiener\MolliePayments\Controller\Api\Order\OrderControllerBase; | ||
use Kiener\MolliePayments\Controller\Api\Order\ShippingControllerBase; | ||
use Kiener\MolliePayments\Controller\Api\PluginConfig\ConfigControllerBase; | ||
use Shopware\Core\Framework\Context; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class OrderController extends AbstractController | ||
{ | ||
/** | ||
* @var RequestBagFactory | ||
*/ | ||
private $requestBagFactory; | ||
|
||
/** | ||
* @var ConfigControllerBase | ||
*/ | ||
private $baseController; | ||
|
||
/** | ||
* @var ShippingControllerBase | ||
*/ | ||
private $shippingController; | ||
|
||
/** | ||
* @var OrderControllerBase | ||
*/ | ||
private $orderController; | ||
|
||
/** | ||
* @var CancelLineController | ||
*/ | ||
private $cancelLineController; | ||
|
||
/** | ||
* @var EntityRepository | ||
*/ | ||
private $orderRepository; | ||
|
||
public function __construct( | ||
RequestBagFactory $requestBagFactory, | ||
ConfigControllerBase $baseController, | ||
ShippingControllerBase $shippingController, | ||
OrderControllerBase $orderController, | ||
CancelLineController $cancelLineController, | ||
EntityRepository $orderRepository | ||
) { | ||
$this->baseController = $baseController; | ||
$this->shippingController = $shippingController; | ||
$this->requestBagFactory = $requestBagFactory; | ||
$this->orderController = $orderController; | ||
$this->cancelLineController = $cancelLineController; | ||
$this->orderRepository = $orderRepository; | ||
} | ||
|
||
/** | ||
* Requires orderId, salesChannelId and mollieOrderId to be set in the request. | ||
*/ | ||
public function getOrderDetails(Request $request, Context $context): JsonResponse | ||
{ | ||
$orderId = $request->get('orderId'); | ||
$scId = $this->getSalesChannelId($orderId, $context); | ||
$mollieOrderId = $this->getMollieOrderId($orderId, $context); | ||
|
||
$request->request->set('mollieOrderId', $mollieOrderId); | ||
$request->request->set('salesChannelId', $scId); | ||
|
||
$config = $this->baseController->getRefundManagerConfig($request, $context); | ||
$shipping = $this->shippingController->total($this->requestBagFactory->createForShipping($request), $context); | ||
$payment = $this->orderController->paymentUrl($request, $context); | ||
$cancelStatus = $this->cancelLineController->statusAction($request, $context); | ||
|
||
$result = [ | ||
'config' => $config->getContent(), | ||
'shipping' => $shipping->getContent(), | ||
'payment' => $payment->getContent(), | ||
'cancelStatus' => $cancelStatus->getContent(), | ||
]; | ||
|
||
foreach ($result as &$item) { | ||
if (is_string($item)) { | ||
$item = json_decode($item); | ||
} | ||
} | ||
|
||
return new JsonResponse(array_merge(['success' => true,], $result)); | ||
} | ||
|
||
private function getSalesChannelId(string $orderId, Context $context): string | ||
{ | ||
$orders = $this->orderRepository->search(new Criteria([$orderId]), $context); | ||
|
||
if ($orders->count() === 0) { | ||
throw new \RuntimeException('Order not found'); | ||
} | ||
|
||
$order = $orders->first(); | ||
|
||
return $order->getSalesChannelId(); | ||
} | ||
|
||
private function getMollieOrderId(string $orderId, Context $context): string | ||
{ | ||
$orders = $this->orderRepository->search(new Criteria([$orderId]), $context); | ||
|
||
if ($orders->count() === 0) { | ||
throw new \RuntimeException('Order not found'); | ||
} | ||
|
||
$order = $orders->first(); | ||
|
||
$customFields = $order->getCustomFieldsValue('mollie_payments'); | ||
|
||
return $customFields['order_id']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Controller\Api\Controller; | ||
|
||
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class RequestBagFactory | ||
{ | ||
public function createForShipping(Request $request): RequestDataBag | ||
{ | ||
$result = new RequestDataBag(); | ||
$result->set('orderId', $request->get('orderId')); | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...p/administration/src/core/service/api/mollie-payments-refund-bundle-repository.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// eslint-disable-next-line no-undef | ||
const ApiService = Shopware.Classes.ApiService; | ||
|
||
export default class MolliePaymentsRefundBundleRepositoryService extends ApiService { | ||
static response = null; | ||
static orderId = null; | ||
static headers = null | ||
static client = null; | ||
|
||
static setOrderId(orderId) { | ||
if (orderId !== null) { | ||
MolliePaymentsRefundBundleRepositoryService.orderId = orderId; | ||
} | ||
} | ||
|
||
static setHeaders(headers) { | ||
if (headers !== null) { | ||
MolliePaymentsRefundBundleRepositoryService.headers = headers; | ||
} | ||
} | ||
|
||
static setClient(client) { | ||
if (client !== null) { | ||
MolliePaymentsRefundBundleRepositoryService.client = client; | ||
} | ||
} | ||
|
||
static fetch() { | ||
if (!MolliePaymentsRefundBundleRepositoryService.client) { | ||
throw new Error('Client not set. Please set the client using setClient() method.'); | ||
} | ||
|
||
if (!MolliePaymentsRefundBundleRepositoryService.orderId) { | ||
throw new Error('orderId not set. Please set the orderId using setOrderId() method.'); | ||
} | ||
|
||
if (MolliePaymentsRefundBundleRepositoryService.response !== null) { | ||
return MolliePaymentsRefundBundleRepositoryService.response; | ||
} | ||
|
||
MolliePaymentsRefundBundleRepositoryService.response = MolliePaymentsRefundBundleRepositoryService.client.post( | ||
'_action/mollie/refund-manager/bundled', | ||
{ | ||
orderId: MolliePaymentsRefundBundleRepositoryService.orderId, | ||
}, | ||
{ | ||
headers: MolliePaymentsRefundBundleRepositoryService.headers, | ||
} | ||
); | ||
|
||
return MolliePaymentsRefundBundleRepositoryService.response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters