diff --git a/Model/AdditionalDataProvider.php b/Model/AdditionalDataProvider.php index 5d96bf4..7c6054b 100644 --- a/Model/AdditionalDataProvider.php +++ b/Model/AdditionalDataProvider.php @@ -21,6 +21,7 @@ namespace Buckaroo\Magento2Graphql\Model; +use Buckaroo\Magento2\Gateway\Http\TransactionBuilder\AbstractTransactionBuilder; use Buckaroo\Magento2Graphql\Plugin\AdditionalDataProviderPool; use Buckaroo\Magento2Graphql\Model\Payment\Method\ConfigFactory; use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderInterface; @@ -59,4 +60,28 @@ public function getData(array $args): array return $args; } + + /** + * @param AdditionalDataProvider $subject + * @param array $result + * @param array $args + * @return array + * @suppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterGetData(AdditionalDataProvider $subject, array $result, array $args): array + { + if (isset($args['buckaroo_additional']['return_url'])) { + $result[AbstractTransactionBuilder::ADDITIONAL_RETURN_URL] = $args['buckaroo_additional']['return_url']; + } + if (isset($args['buckaroo_additional']['cancel_url'])) { + $result[AbstractTransactionBuilder::ADDITIONAL_CANCEL_URL] = $args['buckaroo_additional']['cancel_url']; + } + if (isset($args['buckaroo_additional']['error_url'])) { + $result[AbstractTransactionBuilder::ADDITIONAL_ERROR_URL] = $args['buckaroo_additional']['error_url']; + } + if (isset($args['buckaroo_additional']['reject_url'])) { + $result[AbstractTransactionBuilder::ADDITIONAL_REJECT_URL] = $args['buckaroo_additional']['reject_url']; + } + return $result; + } } diff --git a/Model/Payment/Method/AbstractMethodPlugin.php b/Model/Payment/Method/AbstractMethodPlugin.php new file mode 100644 index 0000000..36b3c1a --- /dev/null +++ b/Model/Payment/Method/AbstractMethodPlugin.php @@ -0,0 +1,54 @@ +getAdditionalData(); + + if (isset($additionalSkip['buckaroo_return_url'])) { + $subject->getInfoInstance()->setAdditionalInformation( + 'buckaroo_return_url', + $additionalSkip['buckaroo_return_url'] + ); + } + if (isset($additionalSkip['buckaroo_cancel_url'])) { + $subject->getInfoInstance()->setAdditionalInformation( + 'buckaroo_cancel_url', + $additionalSkip['buckaroo_cancel_url'] + ); + } + if (isset($additionalSkip['buckaroo_error_url'])) { + $subject->getInfoInstance()->setAdditionalInformation( + 'buckaroo_error_url', + $additionalSkip['buckaroo_error_url'] + ); + } + if (isset($additionalSkip['buckaroo_reject_url'])) { + $subject->getInfoInstance()->setAdditionalInformation( + 'buckaroo_reject_url', + $additionalSkip['buckaroo_reject_url'] + ); + } + return $result; + } +} diff --git a/Model/Payment/Method/Config/ReturnUrl.php b/Model/Payment/Method/Config/ReturnUrl.php new file mode 100644 index 0000000..5402748 --- /dev/null +++ b/Model/Payment/Method/Config/ReturnUrl.php @@ -0,0 +1,27 @@ +returnUrl = $scopeConfig->getValue( + 'payment/buckaroo/return_url', + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); + + // Or you can set a default URL if not configured + if (!$this->returnUrl) { + $this->returnUrl = "https://default-return-url.com"; + } + } + + public function getReturnUrl() + { + return $this->returnUrl; + } +} \ No newline at end of file diff --git a/Plugin/FixSession.php b/Plugin/FixSession.php index e2c0cc1..315a922 100644 --- a/Plugin/FixSession.php +++ b/Plugin/FixSession.php @@ -63,4 +63,4 @@ public function beforeSetPublicCookie( } return [$name, $value, $metadata]; } -} +} \ No newline at end of file diff --git a/Plugin/OrderTransactionBuilderPlugin.php b/Plugin/OrderTransactionBuilderPlugin.php new file mode 100644 index 0000000..d5f42dd --- /dev/null +++ b/Plugin/OrderTransactionBuilderPlugin.php @@ -0,0 +1,185 @@ +getReturnUrl($subject); + return $returnUrl . '?' . http_build_query([ + 'orderId' => $subject->getOrder()->getIncrementId(), + ]); + } + + /** + * When a specific returnURl is set on the payment, use that one. + * Otherwise, use the default return url. + * + * @param OrderTransactionBuilder $subject + * @return string + */ + protected function getReturnUrl(OrderTransactionBuilder $subject): string + { + $payment = $subject->getOrder()->getPayment(); + $additionalInfo = $payment->getAdditionalInformation(); + if (isset($additionalInfo[AbstractTransactionBuilder::ADDITIONAL_RETURN_URL])) { + return $additionalInfo[AbstractTransactionBuilder::ADDITIONAL_RETURN_URL]; + } + + return $this->getDefaultUrl(); + + } + /** + * Overwrite the return url to include the order_id and maskedId, + * There is an issue with `setReturnUrl` mutation not working, and we don't want to do extra mutations anyway. + * + * @param OrderTransactionBuilder $subject + * @param string $result + * @return string + * @throws Exception + * @suppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterGetCancelUrl( + OrderTransactionBuilder $subject, + string $result + ): string { + $cancelUrl = $this->getCancelUrl($subject); + return $cancelUrl . '?' . http_build_query([ + 'orderId' => $subject->getOrder()->getIncrementId(), + ]); + } + + /** + * When a specific returnURl is set on the payment, use that one. + * Otherwise, use the default return url. + * + * @param OrderTransactionBuilder $subject + * @return string + */ + protected function getCancelUrl(OrderTransactionBuilder $subject): string + { + $payment = $subject->getOrder()->getPayment(); + $additionalInfo = $payment->getAdditionalInformation(); + if (isset($additionalInfo[AbstractTransactionBuilder::ADDITIONAL_CANCEL_URL])) { + return $additionalInfo[AbstractTransactionBuilder::ADDITIONAL_CANCEL_URL]; + } + + return $this->getDefaultUrl(); + } + + /** + * Overwrite the return url to include the order_id and maskedId, + * There is an issue with `setReturnUrl` mutation not working, and we don't want to do extra mutations anyway. + * + * @param OrderTransactionBuilder $subject + * @param string $result + * @return string + * @throws Exception + * @suppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterGetErrorUrl( + OrderTransactionBuilder $subject, + string $result + ): string { + $errorUrl = $this->getErrorUrl($subject); + return $errorUrl . '?' . http_build_query([ + 'orderId' => $subject->getOrder()->getIncrementId(), + ]); + } + + /** + * When a specific returnURl is set on the payment, use that one. + * Otherwise, use the default return url. + * + * @param OrderTransactionBuilder $subject + * @return string + */ + protected function getErrorUrl(OrderTransactionBuilder $subject): string + { + $payment = $subject->getOrder()->getPayment(); + $additionalInfo = $payment->getAdditionalInformation(); + if (isset($additionalInfo[AbstractTransactionBuilder::ADDITIONAL_ERROR_URL])) { + return $additionalInfo[AbstractTransactionBuilder::ADDITIONAL_ERROR_URL]; + } + + return $this->getDefaultUrl(); + } + + /** + * Overwrite the return url to include the order_id and maskedId, + * There is an issue with `setReturnUrl` mutation not working, and we don't want to do extra mutations anyway. + * + * @param OrderTransactionBuilder $subject + * @param string $result + * @return string + * @throws Exception + * @suppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterGetRejectUrl( + OrderTransactionBuilder $subject, + string $result + ): string { + $rejectUrl = $this->getRejectUrl($subject); + return $rejectUrl . '?' . http_build_query([ + 'orderId' => $subject->getOrder()->getIncrementId(), + ]); + } + + /** + * When a specific returnURl is set on the payment, use that one. + * Otherwise, use the default return url. + * + * @param OrderTransactionBuilder $subject + * @return string + */ + protected function getRejectUrl(OrderTransactionBuilder $subject): string + { + $payment = $subject->getOrder()->getPayment(); + $additionalInfo = $payment->getAdditionalInformation(); + if (isset($additionalInfo[AbstractTransactionBuilder::ADDITIONAL_REJECT_URL])) { + return $additionalInfo[AbstractTransactionBuilder::ADDITIONAL_REJECT_URL]; + } + + return $this->getDefaultUrl(); + } + + protected function getDefaultUrl(){ + $returnUrl = $this->scopeConfig->getValue('web/secure/frontend_base_link_url'); + try { + /** @var Account $accountConfig */ + $accountConfig = $this->configProviderFactory->get('account'); + $returnUrl .= $accountConfig->getSuccessRedirect(); + } catch (Exception $e) { + $returnUrl .= self::DEFAULT_REDIRECT_PATH; + } + return $returnUrl; + } +} diff --git a/README.md b/README.md index 4c91162..51ba822 100644 --- a/README.md +++ b/README.md @@ -19,15 +19,15 @@ This plugin is needed for our [Hÿva React checkout extension](https://github.co ### Requirements -To use the plugin you must use: +To use the plugin you must use: - Magento Open Source version 2.4.x - Buckaroo Magento 2 Payment module 1.43.0 or higher. ## Installation - - Install the module composer by running the following command: `composer require buckaroo/magento2graphql` - - Enable the module by running the following command: `php bin/magento module:enable Buckaroo_Magento2Graphql` - - Apply database updates by running the following command: `php bin/magento setup:upgrade` - - Flush the cache by running the following command: `php bin/magento cache:flush` +- Install the module composer by running the following command: `composer require buckaroo/magento2graphql` +- Enable the module by running the following command: `php bin/magento module:enable Buckaroo_Magento2Graphql` +- Apply database updates by running the following command: `php bin/magento setup:upgrade` +- Flush the cache by running the following command: `php bin/magento cache:flush` ## Usage @@ -36,21 +36,21 @@ To use the plugin you must use: - Get the available payments methods with additional data for gateways: ```graphql query { - cart(cart_id: "{ CART_ID }") { - available_payment_methods { - code - title - buckaroo_additional { - key - values { - name - code - img - } - value - } + cart(cart_id: "{ CART_ID }") { + available_payment_methods { + code + title + buckaroo_additional { + key + values { + name + code + img } + value + } } + } } ``` @@ -111,8 +111,8 @@ In order to get the payment status after the user is redirected back we will use ```graphql mutation buckarooPaymentTransactionStatus(input: { transaction_id: "E397CF4C24E64AA299F45246F9906F45" }) { - payment_status, - status_code +payment_status, +status_code } ``` @@ -201,18 +201,19 @@ mutation buckarooPaymentTransactionStatus(input: { transaction_id: "E397CF4C24E6 - **Contact:** [support@buckaroo.nl](mailto:support@buckaroo.nl) or [+31 (0)30 711 50 50](tel:+310307115050) ### Contribute -We really appreciate it when developers contribute to improve the Buckaroo plugins. +We really appreciate it when ers contribute to improve the Buckaroo plugins. If you want to contribute as well, then please follow our [Contribution Guidelines](CONTRIBUTING.md). > ### Community is the :green_heart: of open source > Developing beautiful products is not possible without the input of a community. We thank everyone who actively contributes to this. -> +> > [](https://github.com/florinel-chis) [](https://github.com/peterkoppenaal) [](https://github.com/serpentscode) [](https://github.com/paales) [](https://github.com/raoulguillermo) ### Versioning +