Skip to content

Commit 74ede46

Browse files
committed
[change] (UMCS-181) Refactor applepay adapter: Add test for merchant id key file.
1 parent e42a42b commit 74ede46

File tree

2 files changed

+105
-28
lines changed

2 files changed

+105
-28
lines changed

src/Services/EnvironmentService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class EnvironmentService
4141
public const ENV_VAR_TEST_PRIVATE_KEY_NON_3DS = 'UNZER_PAPI_TEST_PRIVATE_KEY_NON_3DS';
4242
public const ENV_VAR_TEST_PUBLIC_KEY_NON_3DS = 'UNZER_PAPI_TEST_PUBLIC_KEY_NON_3DS';
4343

44-
public const ENV_VAR_TEST_APPLE_MERCHANT_CERTIFICATE = 'UNZER_APPLE_MERCHANT_CERTIFICATE_PATH';
44+
public const ENV_VAR_TEST_APPLE_MERCHANT_ID_FOLDER = 'UNZER_APPLE_MERCHANT_ID_PATH';
4545
public const ENV_VAR_TEST_APPLE_CA_CERTIFICATE = 'UNZER_APPLE_CA_CERTIFICATE_PATH';
4646

4747
private const ENV_VAR_NAME_TIMEOUT = 'UNZER_PAPI_TIMEOUT';
@@ -142,13 +142,13 @@ public static function getTestPublicKey($non3ds = false): string
142142
}
143143

144144
/**
145-
* Returns the apple merchant certificate path set via environment variable.
145+
* Returns the path to apple merchant ID folder set via environment variable.
146146
*
147147
* @return string
148148
*/
149-
public static function getAppleMerchantCertificatePath(): string
149+
public static function getAppleMerchantIdPath(): string
150150
{
151-
return stripslashes($_SERVER[self::ENV_VAR_TEST_APPLE_MERCHANT_CERTIFICATE] ?? '');
151+
return stripslashes($_SERVER[self::ENV_VAR_TEST_APPLE_MERCHANT_ID_FOLDER] ?? '');
152152
}
153153

154154
/**

test/integration/ApplepayAdapterTest.php

Lines changed: 101 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,33 @@
3434

3535
class ApplepayAdapterTest extends BaseIntegrationTest
3636
{
37+
/** @var string $appleCaCertificatePath Path to ca Certificate file. */
38+
protected $appleCaCertificatePath;
39+
/** @var string $merchantValidationUrl merchant validation url for testing. */
3740
private $merchantValidationUrl;
38-
private $merchantValidationCertificatePath;
39-
private $appleCaCertificatePath;
40-
41-
public function domainShouldBeValidatedCorrectlyDP()
42-
{
43-
return [
44-
'invalid: example.domain.com' => ['https://example.domain.com', false],
45-
'valid: https://apple-pay-gateway.apple.com/some/path' => ['https://apple-pay-gateway.apple.com/some/path', true],
46-
'valid: https://cn-apple-pay-gateway.apple.com' => ['https://cn-apple-pay-gateway.apple.com', true],
47-
'invalid: apple-pay-gateway-nc-pod1.apple.com' => ['apple-pay-gateway-nc-pod1.apple.com', false],
48-
'invalid: (empty)' => ['', false],
49-
];
50-
}
41+
/** @var string $applepayCombinedCertPath Path to combined certificate file. */
42+
private $applepayCombinedCertPath;
43+
/** @var string $applepayCertPath Path to merchant ID certificate file. */
44+
private $applepayCertPath;
45+
/** @var string $applepayKeyPath Path to merchant ID key file. */
46+
private $applepayKeyPath;
5147

5248
/**
5349
* @inheritDoc
5450
*/
5551
protected function setUp(): void
5652
{
5753
$this->merchantValidationUrl = 'https://apple-pay-gateway-cert.apple.com/paymentservices/startSession';
58-
$this->merchantValidationCertificatePath = EnvironmentService::getAppleMerchantCertificatePath();
54+
55+
$appleMerchantIdPath = EnvironmentService::getAppleMerchantIdPath();
56+
$this->applepayCertPath = $appleMerchantIdPath . 'merchant_id.pem';
57+
$this->applepayKeyPath = $appleMerchantIdPath . 'merchant_id.key';
58+
$this->applepayCombinedCertPath = $appleMerchantIdPath . 'apple-pay-cert.pem';
5959
$this->appleCaCertificatePath = EnvironmentService::getAppleCaCertificatePath();
6060
}
6161

6262
/**
63-
* test merchant validation request.
63+
* Test merchant validation request.
6464
*
6565
* @test
6666
*
@@ -70,7 +70,7 @@ public function verifyMerchantValidationRequest(): void
7070
{
7171
$applepaySession = $this->createApplepaySession();
7272
$appleAdapter = new ApplepayAdapter();
73-
$appleAdapter->init($this->merchantValidationCertificatePath, null, $this->appleCaCertificatePath);
73+
$appleAdapter->init($this->applepayCertPath, $this->applepayKeyPath, $this->appleCaCertificatePath);
7474

7575
$validationResponse = $appleAdapter->validateApplePayMerchant(
7676
$this->merchantValidationUrl,
@@ -81,17 +81,25 @@ public function verifyMerchantValidationRequest(): void
8181
}
8282

8383
/**
84-
* test merchant validation request without ca certificate.
84+
* @return ApplepaySession
85+
*/
86+
private function createApplepaySession(): ApplepaySession
87+
{
88+
return new ApplepaySession('merchantIdentifier', 'displayName', 'domainName');
89+
}
90+
91+
/**
92+
* Test merchant validation request without ca certificate.
8593
*
8694
* @test
8795
*
8896
* @throws ApplepayMerchantValidationException
8997
*/
90-
public function merchantValidationWorksWithoutCaCert(): void
98+
public function merchantValidationWorksWithApplepayCertOnly(): void
9199
{
92100
$applepaySession = $this->createApplepaySession();
93101
$appleAdapter = new ApplepayAdapter();
94-
$appleAdapter->init($this->merchantValidationCertificatePath);
102+
$appleAdapter->init($this->applepayCombinedCertPath);
95103

96104
$validationResponse = $appleAdapter->validateApplePayMerchant(
97105
$this->merchantValidationUrl,
@@ -101,6 +109,68 @@ public function merchantValidationWorksWithoutCaCert(): void
101109
$this->assertNotNull($validationResponse);
102110
}
103111

112+
/**
113+
* Test merchant validation request without ca certificate.
114+
*
115+
* @test
116+
*
117+
* @throws ApplepayMerchantValidationException
118+
*/
119+
public function merchantValidationWorksWithCertAndKey(): void
120+
{
121+
$applepaySession = $this->createApplepaySession();
122+
$appleAdapter = new ApplepayAdapter();
123+
$appleAdapter->init($this->applepayCertPath, $this->applepayKeyPath);
124+
125+
$validationResponse = $appleAdapter->validateApplePayMerchant(
126+
$this->merchantValidationUrl,
127+
$applepaySession
128+
);
129+
130+
$this->assertNotNull($validationResponse);
131+
}
132+
133+
/**
134+
* Test merchant validation request without key and only the merchant id certificate should throw exception.
135+
*
136+
* @test
137+
*
138+
* @throws ApplepayMerchantValidationException
139+
*/
140+
public function missingKeyShouldThrowException(): void
141+
{
142+
$applepaySession = $this->createApplepaySession();
143+
$appleAdapter = new ApplepayAdapter();
144+
$appleAdapter->init($this->applepayCertPath);
145+
146+
$this->expectException(ApplepayMerchantValidationException::class);
147+
$appleAdapter->validateApplePayMerchant(
148+
$this->merchantValidationUrl,
149+
$applepaySession
150+
);
151+
}
152+
153+
/**
154+
* Test merchant validation request without init() call should throw exception.
155+
*
156+
* @test
157+
*
158+
* @throws ApplepayMerchantValidationException
159+
*/
160+
public function missingInitCallThrowsException(): void
161+
{
162+
$applepaySession = $this->createApplepaySession();
163+
$appleAdapter = new ApplepayAdapter();
164+
165+
$this->expectException(ApplepayMerchantValidationException::class);
166+
$this->expectExceptionMessage('No curl adapter initiated yet. Make sure to cal init() function before.');
167+
168+
$appleAdapter->validateApplePayMerchant(
169+
$this->merchantValidationUrl,
170+
$applepaySession
171+
);
172+
}
173+
104174
/**
105175
* Merchant validation call should throw Exception if domain of Validation url is invalid.
106176
*
@@ -111,7 +181,7 @@ public function merchantValidationThrowsErrorForInvalidDomain(): void
111181
{
112182
$applepaySession = $this->createApplepaySession();
113183
$appleAdapter = new ApplepayAdapter();
114-
$appleAdapter->init($this->merchantValidationCertificatePath);
184+
$appleAdapter->init($this->applepayCombinedCertPath);
115185

116186
$this->expectException(ApplepayMerchantValidationException::class);
117187
$this->expectExceptionMessage('Invalid URL used for merchantValidation request.');
@@ -140,11 +210,18 @@ public function domainShouldBeValidatedCorrectly($validationUrl, $expectedResult
140210
$this->assertEquals($expectedResult, $domainValidation);
141211
}
142212

143-
/**
144-
* @return ApplepaySession
213+
/** Provides different urls to test domain validation.
214+
*
215+
* @return array[]
145216
*/
146-
private function createApplepaySession(): ApplepaySession
217+
public function domainShouldBeValidatedCorrectlyDP(): array
147218
{
148-
return new ApplepaySession('merchantIdentifier', 'displayName', 'domainName');
219+
return [
220+
'invalid: example.domain.com' => ['https://example.domain.com', false],
221+
'valid: https://apple-pay-gateway.apple.com/some/path' => ['https://apple-pay-gateway.apple.com/some/path', true],
222+
'valid: https://cn-apple-pay-gateway.apple.com' => ['https://cn-apple-pay-gateway.apple.com', true],
223+
'invalid: apple-pay-gateway-nc-pod1.apple.com' => ['apple-pay-gateway-nc-pod1.apple.com', false],
224+
'invalid: (empty)' => ['', false],
225+
];
149226
}
150227
}

0 commit comments

Comments
 (0)