Skip to content

Commit 1b3e113

Browse files
committed
feat: resolve IPN secret key by merchant when currency missing
1 parent ce98ea6 commit 1b3e113

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/Http/Middleware/VerifySimplePaySignature.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,33 @@ public function handle(Request $request, Closure $next): Response
2626
$body = $request->getContent();
2727
$data = json_decode($body, true);
2828

29-
if (! is_array($data) || ! isset($data['currency'])) {
29+
if (! is_array($data)) {
3030
abort(401, 'Invalid SimplePay request body');
3131
}
3232

33-
$currency = Currency::from($data['currency']);
34-
$secretKey = $this->merchantResolver->getSecretKey($currency);
33+
$secretKey = $this->resolveSecretKey($data);
34+
35+
if (! $secretKey) {
36+
abort(401, 'Cannot resolve SimplePay merchant for request');
37+
}
3538

3639
if (! Signature::verify($secretKey, $body, $signature)) {
3740
abort(401, 'Invalid SimplePay signature');
3841
}
3942

4043
return $next($request);
4144
}
45+
46+
private function resolveSecretKey(array $data): ?string
47+
{
48+
if (isset($data['currency'])) {
49+
return $this->merchantResolver->getSecretKey(Currency::from($data['currency']));
50+
}
51+
52+
if (isset($data['merchant'])) {
53+
return $this->merchantResolver->getSecretKeyByMerchant($data['merchant']);
54+
}
55+
56+
return null;
57+
}
4258
}

src/Support/MerchantResolver.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,17 @@ public function getSecretKey(Currency $currency): string
3131
{
3232
return $this->resolve($currency)['secret_key'];
3333
}
34+
35+
public function getSecretKeyByMerchant(string $merchant): string
36+
{
37+
$merchants = config('simplepay.merchants');
38+
39+
foreach ($merchants as $config) {
40+
if (isset($config['merchant'], $config['secret_key']) && $config['merchant'] === $merchant) {
41+
return $config['secret_key'];
42+
}
43+
}
44+
45+
throw new InvalidArgumentException("SimplePay merchant not configured: {$merchant}");
46+
}
3447
}

0 commit comments

Comments
 (0)