Skip to content

Commit 25696ec

Browse files
Merge pull request #28 from buckaroo-it/develop
Develop
2 parents d93574d + 24fdc2f commit 25696ec

File tree

7 files changed

+491
-2
lines changed

7 files changed

+491
-2
lines changed

Helper/GraphqlDetector.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
/**
3+
* NOTICE OF LICENSE
4+
*
5+
* This source file is subject to the MIT License
6+
* It is available through the world-wide-web at this URL:
7+
* https://tldrlegal.com/license/mit-license
8+
* If you are unable to obtain it through the world-wide-web, please send an email
9+
* to [email protected] so we can send you a copy immediately.
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this module to newer
14+
* versions in the future. If you wish to customize this module for your
15+
* needs please contact [email protected] for more information.
16+
*
17+
* @copyright Copyright (c) Buckaroo B.V.
18+
* @license https://tldrlegal.com/license/mit-license
19+
*/
20+
21+
namespace Buckaroo\Magento2Graphql\Helper;
22+
23+
use Magento\Framework\App\RequestInterface;
24+
use Magento\Framework\Registry;
25+
use Buckaroo\Magento2Graphql\Resolver\Cart\SetReturnUrl;
26+
27+
class GraphqlDetector
28+
{
29+
/**
30+
* @var RequestInterface
31+
*/
32+
protected $request;
33+
34+
/**
35+
* @var Registry
36+
*/
37+
protected $registry;
38+
39+
/**
40+
* @param RequestInterface $request
41+
* @param Registry $registry
42+
*/
43+
public function __construct(
44+
RequestInterface $request,
45+
Registry $registry
46+
) {
47+
$this->request = $request;
48+
$this->registry = $registry;
49+
}
50+
51+
/**
52+
* Check if the current request is a GraphQL request
53+
*
54+
* @param mixed $transactionBuilder
55+
* @return bool
56+
*/
57+
public function isGraphqlRequest($transactionBuilder = null)
58+
{
59+
// Method 1: Check if we're in a GraphQL context via request path
60+
if ($this->isGraphqlEndpoint()) {
61+
return true;
62+
}
63+
64+
// Method 2: Check for GraphQL-specific markers in payment data
65+
if ($this->hasGraphqlPaymentMarkers($transactionBuilder)) {
66+
return true;
67+
}
68+
69+
// Method 3: Check registry for GraphQL context
70+
if ($this->hasGraphqlRegistryMarkers()) {
71+
return true;
72+
}
73+
74+
return false;
75+
}
76+
77+
/**
78+
* Check if current request is to GraphQL endpoint
79+
*
80+
* @return bool
81+
*/
82+
protected function isGraphqlEndpoint()
83+
{
84+
$requestUri = $this->request->getRequestUri();
85+
$pathInfo = $this->request->getPathInfo();
86+
87+
// Check for GraphQL endpoints
88+
return (
89+
strpos($pathInfo, '/graphql') !== false ||
90+
strpos($requestUri, '/graphql') !== false ||
91+
$this->request->getHeader('Content-Type') === 'application/json' &&
92+
$this->request->getModuleName() === 'graphql'
93+
);
94+
}
95+
96+
/**
97+
* Check for GraphQL-specific markers in payment data
98+
*
99+
* @param mixed $transactionBuilder
100+
* @return bool
101+
*/
102+
protected function hasGraphqlPaymentMarkers($transactionBuilder = null)
103+
{
104+
if (!$transactionBuilder) {
105+
return false;
106+
}
107+
108+
try {
109+
// Check if transaction builder has order with GraphQL return URL
110+
if (method_exists($transactionBuilder, 'getOrder')) {
111+
$order = $transactionBuilder->getOrder();
112+
if ($order && $order->getPayment()) {
113+
$additionalInfo = $order->getPayment()->getAdditionalInformation();
114+
115+
// Check for the GraphQL return URL marker set by SetReturnUrl resolver
116+
if (isset($additionalInfo[SetReturnUrl::ADDITIONAL_RETURN_URL])) {
117+
return true;
118+
}
119+
}
120+
}
121+
} catch (\Exception $e) {
122+
// If any error occurs, assume it's not GraphQL
123+
return false;
124+
}
125+
126+
return false;
127+
}
128+
129+
/**
130+
* Check registry for GraphQL context markers
131+
*
132+
* @return bool
133+
*/
134+
protected function hasGraphqlRegistryMarkers()
135+
{
136+
// Check if GraphQL context is set in registry
137+
return $this->registry->registry('buckaroo_graphql_context') === true;
138+
}
139+
140+
/**
141+
* Mark current context as GraphQL in registry
142+
*
143+
* @return void
144+
*/
145+
public function markAsGraphqlContext()
146+
{
147+
if (!$this->registry->registry('buckaroo_graphql_context')) {
148+
$this->registry->register('buckaroo_graphql_context', true);
149+
}
150+
}
151+
152+
/**
153+
* Clear GraphQL context marker from registry
154+
*
155+
* @return void
156+
*/
157+
public function clearGraphqlContext()
158+
{
159+
if ($this->registry->registry('buckaroo_graphql_context')) {
160+
$this->registry->unregister('buckaroo_graphql_context');
161+
}
162+
}
163+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* NOTICE OF LICENSE
4+
*
5+
* This source file is subject to the MIT License
6+
* It is available through the world-wide-web at this URL:
7+
* https://tldrlegal.com/license/mit-license
8+
* If you are unable to obtain it through the world-wide-web, please send an email
9+
* to [email protected] so we can send you a copy immediately.
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this module to newer
14+
* versions in the future. If you wish to customize this module for your
15+
* needs please contact [email protected] for more information.
16+
*
17+
* @copyright Copyright (c) Buckaroo B.V.
18+
* @license https://tldrlegal.com/license/mit-license
19+
*/
20+
21+
namespace Buckaroo\Magento2Graphql\Model\ConfigProvider;
22+
23+
use Magento\Framework\App\Config\ScopeConfigInterface;
24+
use Magento\Store\Model\ScopeInterface;
25+
26+
class Configuration
27+
{
28+
/**
29+
* XPATHs to configuration values for buckaroo_magento2_graphql
30+
*/
31+
const XPATH_GRAPHQL_OVERRIDE_ENABLED = 'buckaroo_magento2_graphql/configuration/override_enabled';
32+
const XPATH_GRAPHQL_STATIC_PUSH_URL = 'buckaroo_magento2_graphql/configuration/static_push_url';
33+
34+
/**
35+
* @var ScopeConfigInterface
36+
*/
37+
protected $scopeConfig;
38+
39+
/**
40+
* @param ScopeConfigInterface $scopeConfig
41+
*/
42+
public function __construct(
43+
ScopeConfigInterface $scopeConfig
44+
) {
45+
$this->scopeConfig = $scopeConfig;
46+
}
47+
48+
/**
49+
* Check if GraphQL push URL override is enabled
50+
*
51+
* @param null|int|string $store
52+
* @return bool
53+
*/
54+
public function isOverrideEnabled($store = null)
55+
{
56+
return (bool) $this->scopeConfig->getValue(
57+
self::XPATH_GRAPHQL_OVERRIDE_ENABLED,
58+
ScopeInterface::SCOPE_STORE,
59+
$store
60+
);
61+
}
62+
63+
/**
64+
* Get static push URL for GraphQL requests
65+
*
66+
* @param null|int|string $store
67+
* @return string|null
68+
*/
69+
public function getStaticPushUrl($store = null)
70+
{
71+
if (!$this->isOverrideEnabled($store)) {
72+
return null;
73+
}
74+
75+
$url = $this->scopeConfig->getValue(
76+
self::XPATH_GRAPHQL_STATIC_PUSH_URL,
77+
ScopeInterface::SCOPE_STORE,
78+
$store
79+
);
80+
81+
return $url ? trim($url) : null;
82+
}
83+
84+
/**
85+
* Check if static push URL should be used for GraphQL requests
86+
*
87+
* @param null|int|string $store
88+
* @return bool
89+
*/
90+
public function useStaticPushUrl($store = null)
91+
{
92+
return $this->isOverrideEnabled($store) && !empty($this->getStaticPushUrl($store));
93+
}
94+
95+
/**
96+
* Check if dynamic push URL should be used for GraphQL requests
97+
*
98+
* @param null|int|string $store
99+
* @return bool
100+
*/
101+
public function useDynamicPushUrl($store = null)
102+
{
103+
return !$this->isOverrideEnabled($store);
104+
}
105+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* NOTICE OF LICENSE
4+
*
5+
* This source file is subject to the MIT License
6+
* It is available through the world-wide-web at this URL:
7+
* https://tldrlegal.com/license/mit-license
8+
* If you are unable to obtain it through the world-wide-web, please send an email
9+
* to [email protected] so we can send you a copy immediately.
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this module to newer
14+
* versions in the future. If you wish to customize this module for your
15+
* needs please contact [email protected] for more information.
16+
*
17+
* @copyright Copyright (c) Buckaroo B.V.
18+
* @license https://tldrlegal.com/license/mit-license
19+
*/
20+
21+
namespace Buckaroo\Magento2Graphql\Plugin\TransactionBuilder;
22+
23+
use Buckaroo\Magento2\Gateway\Http\TransactionBuilder\Order;
24+
use Buckaroo\Magento2Graphql\Model\ConfigProvider\Configuration;
25+
use Buckaroo\Magento2Graphql\Helper\GraphqlDetector;
26+
use Magento\Framework\UrlInterface;
27+
use Buckaroo\Magento2\Logging\Log;
28+
29+
class PushUrlModifier
30+
{
31+
/**
32+
* @var Configuration
33+
*/
34+
protected $graphqlConfig;
35+
36+
/**
37+
* @var GraphqlDetector
38+
*/
39+
protected $graphqlDetector;
40+
41+
/**
42+
* @var UrlInterface
43+
*/
44+
protected $urlBuilder;
45+
46+
/**
47+
* @var Log
48+
*/
49+
protected $logger;
50+
51+
/**
52+
* @param Configuration $graphqlConfig
53+
* @param GraphqlDetector $graphqlDetector
54+
* @param UrlInterface $urlBuilder
55+
* @param Log $logger
56+
*/
57+
public function __construct(
58+
Configuration $graphqlConfig,
59+
GraphqlDetector $graphqlDetector,
60+
UrlInterface $urlBuilder,
61+
Log $logger
62+
) {
63+
$this->graphqlConfig = $graphqlConfig;
64+
$this->graphqlDetector = $graphqlDetector;
65+
$this->urlBuilder = $urlBuilder;
66+
$this->logger = $logger;
67+
}
68+
69+
/**
70+
* Modify push URL for GraphQL requests only
71+
*
72+
* @param Order $subject
73+
* @param array $result
74+
* @return array
75+
*/
76+
public function afterGetBody(Order $subject, array $result)
77+
{
78+
try {
79+
// Only modify if this is a GraphQL request
80+
if (!$this->graphqlDetector->isGraphqlRequest($subject)) {
81+
return $result;
82+
}
83+
84+
// Only modify if GraphQL override is enabled
85+
if (!$this->graphqlConfig->isOverrideEnabled()) {
86+
return $result;
87+
}
88+
89+
$this->logger->addDebug(__METHOD__ . '|GraphQL push URL override active');
90+
91+
// With override enabled, we use static push URL
92+
if ($this->graphqlConfig->useStaticPushUrl()) {
93+
$result = $this->applyStaticPushUrl($result);
94+
} else {
95+
$this->logger->addDebug(__METHOD__ . '|Override enabled but no static URL configured, using dynamic');
96+
}
97+
98+
} catch (\Exception $e) {
99+
$this->logger->addError(__METHOD__ . '|Error: ' . $e->getMessage());
100+
// Return original result if any error occurs
101+
}
102+
103+
return $result;
104+
}
105+
106+
/**
107+
* Apply static push URL to transaction body
108+
*
109+
* @param array $body
110+
* @return array
111+
*/
112+
protected function applyStaticPushUrl(array $body)
113+
{
114+
$staticUrl = $this->graphqlConfig->getStaticPushUrl();
115+
116+
if ($staticUrl) {
117+
$body['PushURL'] = $staticUrl;
118+
$this->logger->addDebug(__METHOD__ . '|Applied static push URL: ' . $staticUrl);
119+
} else {
120+
$this->logger->addDebug(__METHOD__ . '|Static push URL not configured, keeping dynamic');
121+
}
122+
123+
return $body;
124+
}
125+
126+
127+
}

0 commit comments

Comments
 (0)