Skip to content

Commit

Permalink
add providers selection handling
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad-konieczny committed Nov 21, 2023
1 parent 2566ea5 commit 3c89855
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 10 deletions.
30 changes: 30 additions & 0 deletions Model/PaytrailAdditionalDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Paytrail\PaymentServiceGraphQl\Model;

use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderInterface;

/**
* Get payment additional data for Payflow pro payment
*/
class PaytrailAdditionalDataProvider implements AdditionalDataProviderInterface
{
private const PATH_ADDITIONAL_DATA = 'paytrail';

/**
* Returns additional data
*
* @param array $args
*
* @return array
*/
public function getData(array $args): array
{
return $args[self::PATH_ADDITIONAL_DATA] ?? [];
}
}
107 changes: 107 additions & 0 deletions Model/Resolver/PaymentDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Paytrail\PaymentServiceGraphQl\Model\Resolver;

use Magento\Checkout\Model\Session;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Payment\Gateway\Command\CommandManagerPoolInterface;
use Magento\Sales\Model\Order;
use Paytrail\PaymentService\Exceptions\CheckoutException;
use Paytrail\PaymentService\Gateway\Config\Config;
use Paytrail\PaymentService\Model\ProviderForm;
use Paytrail\PaymentService\Model\Ui\DataProvider\PaymentProvidersData;
use Paytrail\SDK\Response\PaymentResponse;
use Psr\Log\LoggerInterface;

class PaymentDetails implements ResolverInterface
{
public function __construct(
private readonly Session $checkoutSession,
private readonly LoggerInterface $logger,
private readonly CommandManagerPoolInterface $commandManagerPool,
private readonly PaymentProvidersData $paymentProvidersData,
private readonly ProviderForm $providerForm
) {
}

/**
* @param Field $field
* @param $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
*
* @return string[]
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): array
{
$result = [
'payment_url' => '',
'error' => '',
'payment_form_html' => ''
];

try {
$order = $this->checkoutSession->getLastRealOrder();
if ($order->getPayment()->getMethod() == Config::CODE) {
$paytrailPayment = $this->getPaytrailPayment($order);
$result['payment_url'] = $paytrailPayment->getHref();
$result['payment_form'] = $this->getForm($paytrailPayment, $order);
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
$result['error'] = $e->getMessage();
}

return $result;
}

/**
* @param Order $order
*
* @return PaymentResponse
* @throws CheckoutException
*/
private function getPaytrailPayment(Order $order): PaymentResponse
{
$commandExecutor = $this->commandManagerPool->get('paytrail');
$response = $commandExecutor->executeByCode(
'payment',
null,
[
'order' => $order,
'payment_method' => $this->paymentProvidersData->getIdWithoutIncrement(
$order->getPayment()->getAdditionalInformation()['provider'] ?? ''
)
]
);

if ($response['error']) {
$this->errorMsg = ($response['error']);
$this->processService->processError($response['error']);
}

return $response["data"];
}

/**
* @param PaymentResponse $response
* @param Order $order
*
* @return array|null
*/
private function getForm(PaymentResponse $response, Order $order): ?array
{
$paymentMethodId = $this->paymentProvidersData->getIdWithoutIncrement(
$order->getPayment()->getAdditionalInformation()['provider'] ?? ''
);

if (!$paymentMethodId) {
return null;
}

return $this->providerForm->getFormParams($response, $paymentMethodId);
}
}
46 changes: 46 additions & 0 deletions Model/Resolver/PaytrailConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Paytrail\PaymentServiceGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Paytrail\PaymentService\Model\ConfigProvider;

class PaytrailConfig implements ResolverInterface
{

/**
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
private readonly MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
private readonly CartRepositoryInterface $cartRepository,
private readonly \Paytrail\PaymentService\Model\Ui\ConfigProvider $configProvider
) {
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (empty($args['cart_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}

$cartId = $this->maskedQuoteIdToQuoteId->execute($args['cart_id']);
$config = $this->configProvider->getConfig();

$methodGroups = $config['payment']['paytrail']['method_groups'];


return [
'groups' => $methodGroups
];
}
}
37 changes: 37 additions & 0 deletions Observer/PaytrailSetAdditionalData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Paytrail\PaymentServiceGraphQl\Observer;

use Magento\Framework\Event\Observer;
use Magento\Payment\Observer\AbstractDataAssignObserver;
use Magento\Quote\Api\Data\PaymentInterface;

/**
* Set additional data for payflow link payment
*/
class PaytrailSetAdditionalData extends AbstractDataAssignObserver
{
/**
* @inheritdoc
*/
public function execute(Observer $observer): void
{
$dataObject = $this->readDataArgument($observer);

$additionalData = $dataObject->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
if (isset($additionalData['additional_information']) and isset($additionalData['additional_information']['provider'])) {
$additionalData = $additionalData['additional_information'];
}
if (!is_array($additionalData)) {
return;
}

$paymentModel = $this->readPaymentModelArgument($observer);
$paymentModel->setAdditionalInformation($additionalData);
}
}
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
# paytrail-for-adobe-commerce-graphql
Paytrail for Adobe Commerce - GraphQL Module
The README.md file content is generated automatically, see [Magento module README.md](https://github.com/magento/devdocs/wiki/Magento-module-README.md) for more information.

# Paytrail_PaymentServiceGraphQl module



## Installation details

For information about a module installation in Magento 2, see [Enable or disable modules](https://devdocs.magento.com/guides/v2.4/install-gde/install/cli/install-cli-subcommands-enable.html).

## Extensibility

Extension developers can interact with the Paytrail_PaymentServiceGraphQl module. For more information about the Magento extension mechanism, see [Magento plug-ins](https://devdocs.magento.com/guides/v2.4/extension-dev-guide/plugins.html).

[The Magento dependency injection mechanism](https://devdocs.magento.com/guides/v2.4/extension-dev-guide/depend-inj.html) enables you to override the functionality of the Paytrail_PaymentServiceGraphQl module.

### Layouts

The module introduces layout handles in the `view/adminhtml/layout` directory.

For more information about a layout in Magento 2, see the [Layout documentation](https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/layouts/layout-overview.html).

### UI components

You can extend product and category updates using the UI components located in the `view/adminhtml/ui_component` directory.

For information about a UI component in Magento 2, see [Overview of UI components](https://devdocs.magento.com/guides/v2.4/ui_comp_guide/bk-ui_comps.html).

## Additional information

For information about significant changes in patch releases, see [Release information](https://devdocs.magento.com/guides/v2.4/release-notes/bk-release-notes.html).
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"paytrail/paytrail-for-adobe-commerce": "^1.4"
"paytrail/paytrail-for-adobe-commerce": "*"
},
"version": "1.0.0",
"type": "magento2-module",
Expand Down
12 changes: 12 additions & 0 deletions etc/graphql/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<type name="Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderPool">
<arguments>
<argument name="dataProviders" xsi:type="array">
<item name="paytrail" xsi:type="object">Paytrail\PaymentServiceGraphQl\Model\PaytrailAdditionalDataProvider</item>
</argument>
</arguments>
</type>
</config>
12 changes: 12 additions & 0 deletions etc/graphql/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="payment_method_assign_data_paytrail">
<observer name="paytrail_data_assigner" instance="Paytrail\PaymentServiceGraphQl\Observer\PaytrailSetAdditionalData"/>
</event>
</config>
69 changes: 62 additions & 7 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,80 @@ input RestoreQuoteInput {
cart_id: String!
}

input PaytrailInput {
provider: String! @doc(description:"Paytrail provider ID")
}

input PaymentMethodInput @doc(description:"allows to add specific paytrail method selection to the request") {
paytrail: PaytrailInput @doc(description:"Required input for Express Checkout and Payments Standard payments.")
}


type Mutation {
restoreQuote(input: RestoreQuoteInput): String @doc(description:"Restore Paytrail cancelled payment quote by masked Cart ID") @resolver(class: "Paytrail\\PaymentServiceGraphQl\\Model\\Resolver\\RestoreQuote")
}


type Query {
paytrailConfigData(
cart_id: String!
): PaytrailConfig
@resolver (class: "Paytrail\\PaymentServiceGraphQl\\Model\\Resolver\\PaytrailConfig")
@doc(description:"Returns Paytrail configuration")
@cache(cacheable: false)

paytrailCart(
order_id: String!
cart_id: String!
): Cart
@resolver (class: "Paytrail\\PaymentServiceGraphQl\\Model\\Resolver\\PaytrailCart")
@doc(description:"Returns information about shopping cart")
@cache(cacheable: false)
}

type PaytrailPaymentUrl {
type PaytrailPaymentDetails {
payment_url: String
payment_form: PaytrailProviderForm
error: String
}

type Order {
paytrail_payment_url: PaytrailPaymentUrl! @resolver(class: "Paytrail\\PaymentServiceGraphQl\\Model\\Resolver\\PaymentUrl")
type PaytrailProviderForm {
form: String
method: String
action: String
inputs: [PaytrailProviderFormInput]
}

type CustomerOrder {
paytrail_payment_url: PaytrailPaymentUrl! @resolver(class: "Paytrail\\PaymentServiceGraphQl\\Model\\Resolver\\PaymentUrl")
type PaytrailProviderFormInput {
name: String
value: String
type: String
}

type PaytrailConfig {
groups: [PaytrailConfigGroup]
}

type Query {
paytrailCart(order_id: String!, cart_id: String!): Cart @resolver (class: "Paytrail\\PaymentServiceGraphQl\\Model\\Resolver\\PaytrailCart") @doc(description:"Returns information about shopping cart") @cache(cacheable: false)
type PaytrailConfigGroup {
id: String,
name: String,
icon: String,
providers: [PaytrailConfigMethod]
}

type PaytrailConfigMethod {
checkoutId: String,
id: String,
name: String,
group: String,
icon: String,
svg: String
}

type Order {
paytrail_payment_details: PaytrailPaymentDetails! @resolver(class: "Paytrail\\PaymentServiceGraphQl\\Model\\Resolver\\PaymentDetails")
}

type CustomerOrder {
paytrail_payment_details: PaytrailPaymentDetails! @resolver(class: "Paytrail\\PaymentServiceGraphQl\\Model\\Resolver\\PaymentDetails")
}

0 comments on commit 3c89855

Please sign in to comment.