-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f1f005
commit e30dccf
Showing
10 changed files
with
340 additions
and
0 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,6 @@ | ||
.idea/ | ||
/vendor/ | ||
release/ | ||
archive/ | ||
composer.lock | ||
.DS_Store |
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,12 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) | ||
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). | ||
|
||
## 0.1.0 - 2020-04-22 | ||
|
||
### Added | ||
|
||
- Initial release |
File renamed without changes.
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ingenico\ConnectGraphQl\Model\Resolver; | ||
|
||
use Ingenico\Connect\Api\SessionManagerInterface; | ||
use Ingenico\Connect\Sdk\DataObject; | ||
use Magento\Customer\Model\Session; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
use function __; | ||
use function json_decode; | ||
|
||
class IngenicoClientSession implements ResolverInterface | ||
{ | ||
/** | ||
* @var SessionManagerInterface | ||
*/ | ||
private $sessionManager; | ||
|
||
/** | ||
* @var Session | ||
*/ | ||
private $customerSession; | ||
|
||
public function __construct( | ||
SessionManagerInterface $sessionManager, | ||
Session $customerSession | ||
) { | ||
$this->sessionManager = $sessionManager; | ||
$this->customerSession = $customerSession; | ||
} | ||
|
||
/** | ||
* @param Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @return Value|mixed | ||
* @throws LocalizedException | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
$session = $this->customerSession->isLoggedIn() ? | ||
$this->sessionManager->createCustomerSession((int) $this->customerSession->getCustomerId()) : | ||
$this->sessionManager->createAnonymousSession(); | ||
|
||
if (!$session instanceof DataObject) { | ||
throw new LocalizedException(__('Invalid session type')); | ||
} | ||
|
||
return json_decode($session->toJson(), true); | ||
} | ||
} |
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 @@ | ||
# Ingenico Connect GraphQL Module for Magento 2 | ||
|
||
This module adds GraphQl support to the [Ingenico Connect Module for Magento 2](https://github.com/Ingenico-ePayments/connect-extension-magento2). | ||
|
||
**Please note that this module is currently in `0.x`-release and should | ||
therefor be considered 'unstable'. This does not mean that the module will | ||
not work as expected but that it's public API is not definitive yet.** | ||
|
||
## Usage | ||
|
||
This module adds an option to GraphQl to generate a [consumer session](https://epayments-api.developer-ingenico.com/s2sapi/v1/en_US/java/sessions/create.html) | ||
that you can use with the [Ingenico mobile and browser SDK's](https://epayments.developer-ingenico.com/documentation/sdk/mobile/). | ||
|
||
## Example | ||
|
||
The following GraphQl query will create a consumer session with Ingenico: | ||
|
||
```graphql | ||
{ | ||
ingenicoClientSession { | ||
assetUrl | ||
clientApiUrl | ||
clientSessionId | ||
customerId | ||
invalidTokens | ||
region | ||
} | ||
} | ||
``` | ||
|
||
Example response: | ||
|
||
```json | ||
{ | ||
"data": { | ||
"ingenicoClientSession": { | ||
"assetUrl": "https://assets.pay1.preprod.secured-by-ingenico.com/", | ||
"clientApiUrl": "https://ams1.preprod.api-ingenico.com/client", | ||
"clientSessionId": "ccf8ee1015944ab09e053411e683b43f", | ||
"customerId": "11492-214bf4b4d0db4321a5e006e0ec6f080b", | ||
"invalidTokens": null, | ||
"region": "EU" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Registered Customers in Magento | ||
|
||
If you're making a request for a registered customer in Magento (a customer | ||
that is currently logged in), please make sure that you've | ||
[generated a customer token](https://devdocs.magento.com/guides/v2.3/graphql/get-customer-authorization-token.html) | ||
and included it in the HTTP Headers. |
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,152 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ingenico\ConnectGraphQl\Test\Unit\Model\Resolver; | ||
|
||
use Ingenico\Connect\Api\SessionManagerInterface; | ||
use Ingenico\Connect\Model\Ingenico\Session\Session as IngenicoSession; | ||
use Ingenico\ConnectGraphQl\Model\Resolver\IngenicoClientSession; | ||
use Magento\Customer\Model\Session; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class IngenicoClientSessionTest extends TestCase | ||
{ | ||
/** | ||
* @var IngenicoClientSession|MockObject | ||
*/ | ||
private $subject; | ||
|
||
/** | ||
* @var SessionManagerInterface|MockObject | ||
*/ | ||
private $mockedSessionManager; | ||
|
||
/** | ||
* @var Session|MockObject | ||
*/ | ||
private $mockedCustomerSession; | ||
|
||
protected function setUp() | ||
{ | ||
$this->mockSessionManager(); | ||
$this->mockedCustomerSession = $this | ||
->getMockBuilder(Session::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->subject = $this->getObjectManager()->getObject( | ||
IngenicoClientSession::class, | ||
[ | ||
'sessionManager' => $this->mockedSessionManager, | ||
'customerSession' => $this->mockedCustomerSession, | ||
] | ||
); | ||
} | ||
|
||
public function testAnonymousCustomerWillGetAnonymousSession() | ||
{ | ||
// Setup: | ||
$this->mockedCustomerSession | ||
->method('isLoggedIn') | ||
->willReturn(false); | ||
|
||
// Set expectations: | ||
$this->mockedSessionManager | ||
->expects($this->once()) | ||
->method('createAnonymousSession'); | ||
$this->mockedSessionManager | ||
->expects($this->never()) | ||
->method('createCustomerSession'); | ||
|
||
// Exercise: | ||
$this->subject->resolve( | ||
$this->mockField(), | ||
$this->mockContext(), | ||
$this->mockResolveInfo() | ||
); | ||
} | ||
|
||
public function testRegisteredCustomerWillGetCustomerSession() | ||
{ | ||
// Setup: | ||
$this->mockedCustomerSession | ||
->method('isLoggedIn') | ||
->willReturn(true); | ||
|
||
// Set expectations: | ||
$this->mockedSessionManager | ||
->expects($this->never()) | ||
->method('createAnonymousSession'); | ||
$this->mockedSessionManager | ||
->expects($this->once()) | ||
->method('createCustomerSession'); | ||
|
||
// Exercise: | ||
$this->subject->resolve( | ||
$this->mockField(), | ||
$this->mockContext(), | ||
$this->mockResolveInfo() | ||
); | ||
} | ||
|
||
private function getObjectManager(): ObjectManager | ||
{ | ||
return new ObjectManager($this); | ||
} | ||
|
||
/** | ||
* @return MockObject|Field | ||
*/ | ||
private function mockField() | ||
{ | ||
return $this | ||
->getMockBuilder(Field::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
} | ||
|
||
/** | ||
* @return MockObject|ContextInterface | ||
*/ | ||
private function mockContext() | ||
{ | ||
return $this | ||
->getMockBuilder(ContextInterface::class) | ||
->getMock(); | ||
} | ||
|
||
/** | ||
* @return MockObject|ResolveInfo | ||
*/ | ||
private function mockResolveInfo() | ||
{ | ||
return $this | ||
->getMockBuilder(ResolveInfo::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
} | ||
|
||
private function mockSessionManager() | ||
{ | ||
$this->mockedSessionManager = $this | ||
->getMockBuilder(SessionManagerInterface::class) | ||
->getMock(); | ||
/** @var IngenicoSession|MockObject $mockedSession */ | ||
$mockedSession = $this | ||
->getMockBuilder(IngenicoSession::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$mockedSession->method('toJson')->willReturn('{}'); | ||
$this->mockedSessionManager | ||
->method('createAnonymousSession') | ||
->willReturn($mockedSession); | ||
$this->mockedSessionManager | ||
->method('createCustomerSession') | ||
->willReturn($mockedSession); | ||
} | ||
} |
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,20 @@ | ||
{ | ||
"name": "ingenico/connect-extension-magento2-graphql", | ||
"description": "GraphQL support for the Ingenico Connect Magento 2 Module", | ||
"type": "magento2-module", | ||
"license": "MIT", | ||
"require": { | ||
"ext-json": "*", | ||
"ingenico-epayments/connect-extension-magento2": "^2.2", | ||
"webonyx/graphql-php": "^0.13.8" | ||
}, | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"Ingenico\\ConnectGraphQl\\": "" | ||
} | ||
}, | ||
"version": "0.1.0" | ||
} |
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,8 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Ingenico_ConnectGraphQl" setup_version="0.1.0"> | ||
<sequence> | ||
<module name="Ingenico_Connect"/> | ||
</sequence> | ||
</module> | ||
</config> |
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,12 @@ | ||
type Query { | ||
ingenicoClientSession: ingenicoClientSession @resolver(class: "Ingenico\\ConnectGraphQl\\Model\\Resolver\\ingenicoClientSession") @doc(description: "Create a client session with Ingenico") @cache(cacheable: false) | ||
} | ||
|
||
type ingenicoClientSession @doc(description: "Ingenico Client Session") { | ||
assetUrl: String @doc(description: "The datacenter-specific base url for assets. This value needs to be passed to the Client SDK to make sure that the client software connects to the right datacenter.") | ||
clientApiUrl: String @doc(description: "The datacenter-specific base url for client requests. This value needs to be passed to the Client SDK to make sure that the client software connects to the right datacenter.") | ||
clientSessionId: String @doc(description: "The identifier of the session that has been created.") | ||
customerId: String @doc(description: "The session is build up around the customer in the form of the customerId. All of the Client APIs use this customerId in the URI to identify the customer.") | ||
invalidTokens: [String] @doc(description: "Tokens that are submitted in the request are validated. In case any of the tokens can't be used anymore they are returned in this array. You should most likely remove those tokens from your system.") | ||
region: String | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Magento\Framework\Component\ComponentRegistrar; | ||
|
||
ComponentRegistrar::register( | ||
ComponentRegistrar::MODULE, | ||
'Ingenico_ConnectGraphQl', | ||
__DIR__ | ||
); |