Skip to content

Commit 72fdf92

Browse files
authored
Merge pull request #7 from WeGetFinancing/GET-2124
GET-2124 | feat(ppe): Implemented test call
2 parents c531bff + fdd6138 commit 72fdf92

File tree

6 files changed

+134
-1
lines changed

6 files changed

+134
-1
lines changed

.env.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
TEST_USERNAME=
22
TEST_PASSWORD=
33
TEST_MERCHANT_ID=
4+
MERCHANT_TOKEN_EMPTY=use demo default a token
5+
MERCHANT_TOKEN_SUCCESS=use demo master token
6+
MERCHANT_TOKEN_ERROR=fakeToken

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ env:
1515
TEST_WEGETFINANCING_URL: ${{ secrets.TEST_WEGETFINANCING_URL }}
1616
TEST_WEGETFINANCING_URL_V3: ${{ secrets.TEST_WEGETFINANCING_URL_V3 }}
1717
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
18+
MERCHANT_TOKEN_EMPTY: ${{ secrets.MERCHANT_TOKEN_EMPTY }}
19+
MERCHANT_TOKEN_SUCCESS: ${{ secrets.MERCHANT_TOKEN_SUCCESS }}
20+
MERCHANT_TOKEN_ERROR: ${{ secrets.MERCHANT_TOKEN_ERROR }}
1821

1922
jobs:
2023

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"minimum-stability": "stable",
55
"prefer-stable": true,
66
"license": "LGPL-3.0-only",
7-
"version": "2.3.0",
7+
"version": "2.4.0",
88
"authors": [
99
{
1010
"name": "Riccardo De Leo",

src/Client.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace WeGetFinancing\SDK;
66

7+
use GuzzleHttp\Exception\GuzzleException;
78
use WeGetFinancing\SDK\Command\RequestNewLoanCommand;
89
use WeGetFinancing\SDK\Command\UpdateShippingStatusCommand;
910
use WeGetFinancing\SDK\Entity\AuthEntity;
1011
use WeGetFinancing\SDK\Entity\Request\LoanRequestEntity;
1112
use WeGetFinancing\SDK\Entity\Request\UpdateShippingStatusRequestEntity;
1213
use WeGetFinancing\SDK\Entity\Response\ResponseEntity;
1314
use WeGetFinancing\SDK\Exception\EntityValidationException;
15+
use WeGetFinancing\SDK\Service\PpeClient;
1416

1517
class Client
1618
{
@@ -54,4 +56,17 @@ public function updateStatus(UpdateShippingStatusRequestEntity $requestEntity):
5456
$command = UpdateShippingStatusCommand::make($this->authEntity);
5557
return $command->execute($requestEntity);
5658
}
59+
60+
/**
61+
* @SuppressWarnings(PHPMD.StaticAccess)
62+
*
63+
* @param string $merchantToken
64+
* @throws GuzzleException
65+
* @return array<string, string>
66+
*/
67+
public function testPpe(string $merchantToken): array
68+
{
69+
$command = PpeClient::make($merchantToken, $this->authEntity->isProd());
70+
return $command->testPpe();
71+
}
5772
}

src/Service/PpeClient.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WeGetFinancing\SDK\Service;
6+
7+
use Exception;
8+
use GuzzleHttp\Client;
9+
use GuzzleHttp\ClientInterface;
10+
use GuzzleHttp\Exception\GuzzleException;
11+
12+
class PpeClient
13+
{
14+
public const MERCHANT_TOKEN_REPLACE = '%MERCHANT_TOKEN%';
15+
public const URL_TEST_PPE_PROD = 'https://partner.wegetfinancing.com/integration/%MERCHANT_TOKEN%/ppe';
16+
public const URL_TEST_PPE_SANDBOX = 'https://partner.sandbox.wegetfinancing.com/integration/%MERCHANT_TOKEN%/ppe';
17+
public const TEST_ERROR_RESPONSE = 'error';
18+
public const TEST_EMPTY_RESPONSE = 'empty';
19+
public const TEST_SUCCESS_RESPONSE = 'success';
20+
public const EMPTY_LENDERS_MESSAGE =
21+
"The merchant account you've selected isn't properly configured for PPE use yet. " .
22+
"Please reach out to our support team to get your account set up correctly.";
23+
24+
public function __construct(
25+
protected string $merchantToken,
26+
protected bool $isProd,
27+
protected ClientInterface $client
28+
) {
29+
}
30+
31+
/**
32+
* @SuppressWarnings(PHPMD.StaticAccess)
33+
*
34+
* @param string $merchantToken
35+
* @param bool $isProd
36+
* @return PpeClient
37+
*/
38+
public static function make(string $merchantToken, bool $isProd): PpeClient
39+
{
40+
return new PpeClient(
41+
$merchantToken,
42+
$isProd,
43+
new Client()
44+
);
45+
}
46+
47+
/**
48+
* @throws GuzzleException
49+
* @throws Exception
50+
* @return array<string, string>
51+
*/
52+
public function testPpe(): array
53+
{
54+
$response = $this->client->request('GET', $this->getTestPpePath());
55+
$content = $response->getBody()->getContents();
56+
$data = json_decode($content, true);
57+
$error = json_last_error();
58+
if (JSON_ERROR_NONE !== $error) {
59+
throw new Exception(
60+
self::class . "::testPpe() json error: " . json_last_error_msg(),
61+
$error
62+
);
63+
}
64+
65+
if (true === array_key_exists(self::TEST_ERROR_RESPONSE, $data)) {
66+
return [
67+
'status' => self::TEST_ERROR_RESPONSE,
68+
'message' => $data[self::TEST_ERROR_RESPONSE],
69+
];
70+
}
71+
72+
if (true === array_key_exists(0, $data) && true === array_key_exists('max_amount', $data[0])) {
73+
return [
74+
'status' => self::TEST_SUCCESS_RESPONSE,
75+
];
76+
}
77+
78+
return [
79+
'status' => self::TEST_EMPTY_RESPONSE,
80+
'message' => self::EMPTY_LENDERS_MESSAGE,
81+
];
82+
}
83+
84+
protected function getTestPpePath(): string
85+
{
86+
return str_replace(
87+
self::MERCHANT_TOKEN_REPLACE,
88+
$this->merchantToken,
89+
(true === $this->isProd) ? self::URL_TEST_PPE_PROD : self::URL_TEST_PPE_SANDBOX
90+
);
91+
}
92+
}

tests/Integration/ClientTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use WeGetFinancing\SDK\Entity\Request\UpdateShippingStatusRequestEntity;
1212
use Functional\Entity\Request\LoanRequestEntityTest;
1313
use PHPUnit\Framework\TestCase;
14+
use WeGetFinancing\SDK\Service\PpeClient;
1415

1516
/**
1617
* @SuppressWarnings(PHPMD.StaticAccess)
@@ -105,4 +106,23 @@ public function testSuccessfullyUpdateShippingStatus(): void
105106

106107
$this->assertEquals(204, $response->getCode());
107108
}
109+
110+
public function testEmptyPpe(): void
111+
{
112+
$response = $this->sut->testPpe((string)getenv('MERCHANT_TOKEN_EMPTY'));
113+
$this->assertEquals(PpeClient::TEST_EMPTY_RESPONSE, $response['status']);
114+
$this->assertEquals(PpeClient::EMPTY_LENDERS_MESSAGE, $response['message']);
115+
}
116+
117+
public function testErrorPpe(): void
118+
{
119+
$response = $this->sut->testPpe((string)getenv('MERCHANT_TOKEN_ERROR'));
120+
$this->assertEquals(PpeClient::TEST_ERROR_RESPONSE, $response['status']);
121+
}
122+
123+
public function testSuccessPpe(): void
124+
{
125+
$response = $this->sut->testPpe((string)getenv('MERCHANT_TOKEN_SUCCESS'));
126+
$this->assertEquals(PpeClient::TEST_SUCCESS_RESPONSE, $response['status']);
127+
}
108128
}

0 commit comments

Comments
 (0)