Skip to content

Commit 7f9ccaa

Browse files
committed
Add Test Module Mocking AuthorizenetAcceptjs
1 parent 21b3caf commit 7f9ccaa

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestModuleAuthorizenetAcceptjs\Gateway\Http;
9+
10+
use Magento\Framework\Math\Random;
11+
use Magento\Framework\Stdlib\ArrayManager;
12+
use Magento\Payment\Gateway\Http\ClientInterface;
13+
use Magento\Payment\Gateway\Http\TransferInterface;
14+
15+
/**
16+
* A client for mocking communicate with the Authorize.net API
17+
*/
18+
class MockClient implements ClientInterface
19+
{
20+
/**
21+
* @var Random
22+
*/
23+
private $random;
24+
25+
/**
26+
* @var ArrayManager
27+
*/
28+
private $arrayManager;
29+
30+
/**
31+
* @param Random $random
32+
* @param ArrayManager $arrayManager
33+
*/
34+
public function __construct(
35+
Random $random,
36+
ArrayManager $arrayManager
37+
) {
38+
$this->random = $random;
39+
$this->arrayManager = $arrayManager;
40+
}
41+
42+
/**
43+
* Places request to gateway. Returns result as ENV array
44+
*
45+
* @param TransferInterface $transferObject
46+
* @return array
47+
*/
48+
public function placeRequest(TransferInterface $transferObject): array
49+
{
50+
$request = $transferObject->getBody();
51+
$nonce = $this->arrayManager->get('transactionRequest/payment/opaqueData/dataValue', $request);
52+
$descriptor = $this->arrayManager->get('transactionRequest/payment/opaqueData/dataDescriptor', $request);
53+
54+
$approve = true;
55+
if ($nonce !== 'fake-nonce' || $descriptor !== 'COMMON.ACCEPT.INAPP.PAYMENT') {
56+
$approve = false;
57+
}
58+
59+
return $this->createResponse($approve);
60+
}
61+
62+
/**
63+
* Create mock response body
64+
*
65+
* @param bool $approve
66+
* @return array
67+
* @throws \Magento\Framework\Exception\LocalizedException
68+
*/
69+
private function createResponse(bool $approve): array
70+
{
71+
return [
72+
'transactionResponse' => [
73+
'responseCode' => $approve ? '1' : '2',
74+
'authCode' => strtoupper($this->random->getRandomString(6)),
75+
'avsResultCode' => 'Y',
76+
'cvvResultCode' => 'P',
77+
'cavvResultCode' => '2',
78+
'transId' => random_int(10000000000, 99999999999),
79+
'refTransId' => '',
80+
'transHash' => '',
81+
'testRequest' => '0',
82+
'accountNumber' => 'XXXX1111',
83+
'accountType' => 'Visa',
84+
'messages' => $approve ? $this->getApprovalMessage() : $this->getDeclineMessage(),
85+
'userFields' => [
86+
[
87+
'name' => 'transactionType',
88+
'value' => 'authOnlyTransaction',
89+
],
90+
],
91+
'transHashSha2' => 'fake-hash',
92+
'SupplementalDataQualificationIndicator' => '0',
93+
],
94+
'messages' => [
95+
'resultCode' => 'Ok',
96+
'message' => [
97+
[
98+
'code' => 'I00001',
99+
'text' => 'Successful.',
100+
],
101+
],
102+
],
103+
];
104+
}
105+
106+
/**
107+
* Provide approval message for response
108+
*
109+
* @return array
110+
*/
111+
private function getApprovalMessage(): array
112+
{
113+
return [
114+
[
115+
'code' => '1',
116+
'description' => 'This transaction has been approved.',
117+
],
118+
];
119+
}
120+
121+
/**
122+
* Provide decline message for response
123+
*
124+
* @return array
125+
*/
126+
private function getDeclineMessage(): array
127+
{
128+
return [
129+
[
130+
'code' => '2',
131+
'description' => 'This transaction has been declined.',
132+
],
133+
];
134+
}
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestModuleAuthorizenetAcceptjs\Gateway\Validator;
9+
10+
use Magento\Payment\Gateway\Validator\AbstractValidator;
11+
use Magento\Payment\Gateway\Validator\ResultInterface;
12+
13+
/**
14+
* Force validation of the transaction hash
15+
*/
16+
class TransactionHashValidator extends AbstractValidator
17+
{
18+
/**
19+
* Skip validation of transaction hash in mock response
20+
*
21+
* @param array $validationSubject
22+
* @return ResultInterface
23+
*/
24+
public function validate(array $validationSubject): ResultInterface
25+
{
26+
return $this->createResult(true);
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\AuthorizenetAcceptjs\Gateway\Http\Client" type="Magento\TestModuleAuthorizenetAcceptjs\Gateway\Http\MockClient" />
10+
<preference for="Magento\AuthorizenetAcceptjs\Gateway\Validator\TransactionHashValidator" type="Magento\TestModuleAuthorizenetAcceptjs\Gateway\Validator\TransactionHashValidator" />
11+
</config>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_TestModuleAuthorizenetAcceptjs" />
10+
</config>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
use Magento\Framework\Component\ComponentRegistrar;
7+
$registrar = new ComponentRegistrar();
8+
if ($registrar->getPath(ComponentRegistrar::MODULE, 'Magento_TestModuleAuthorizenetAcceptjs') === null) {
9+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_TestModuleAuthorizenetAcceptjs', __DIR__);
10+
}

0 commit comments

Comments
 (0)