Skip to content

Commit 8fbedfa

Browse files
Merge pull request #8 from Deyjandi:develop
update main branch
2 parents beb903f + b1bef14 commit 8fbedfa

File tree

8 files changed

+61
-40
lines changed

8 files changed

+61
-40
lines changed

phpstan.neon.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ parameters:
1111
checkOctaneCompatibility: true
1212
checkModelProperties: true
1313
checkMissingIterableValueType: false
14-

src/Enums/VivaWalletEnv.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ enum VivaWalletEnv: string
1010
case Demo = 'demo';
1111
case Live = 'live';
1212

13-
private const CFG_KEY_USERNAME = 'viva-wallet.merchant_id';
14-
private const CFG_KEY_PASSWORD = 'viva-wallet.api_key';
15-
16-
private const CFG_KEY_CLIENT_ID = 'viva-wallet.client_id';
17-
private const CFG_KEY_CLIENT_SECRET = 'viva-wallet.client_secret';
18-
19-
public function requestWebhookKey(): array
13+
public function requestWebhookKey(string $merchantId, string $apiKey): array
2014
{
2115
return [
2216
'method' => 'GET',
@@ -25,12 +19,12 @@ public function requestWebhookKey(): array
2519
self::Live => 'https://www.vivapayments.com/api/messages/config/token',
2620
},
2721
'options' => [
28-
'auth' => ClientAuth::basic(config(self::CFG_KEY_USERNAME), config(self::CFG_KEY_PASSWORD)),
22+
'auth' => ClientAuth::basic($merchantId, $apiKey),
2923
],
3024
];
3125
}
3226

33-
public function requestToken(): array
27+
public function requestToken(string $clientId, string $clientSecret): array
3428
{
3529
return [
3630
'method' => 'POST',
@@ -39,7 +33,7 @@ public function requestToken(): array
3933
self::Live => 'https://accounts.vivapayments.com/connect/token',
4034
},
4135
'options' => [
42-
'auth' => ClientAuth::basic(config(self::CFG_KEY_CLIENT_ID), config(self::CFG_KEY_CLIENT_SECRET)),
36+
'auth' => ClientAuth::basic($clientId, $clientSecret),
4337
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
4438
'form_params' => ['grant_type' => 'client_credentials'],
4539
],

src/VivaWallet.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class VivaWallet
66
{
7+
private array $config;
8+
79
public function __construct()
810
{
911
$this->config = config('viva-wallet');

src/VivaWalletCustomer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function toJson(): string
7878
public function setEmail(?string $email): static
7979
{
8080
if ($email && strlen($email) > 50) {
81-
throw new InvalidArgumentException('Customer\'s email value must be less than 50 characters.', static::EX_CODE);
81+
throw new InvalidArgumentException('Customer\'s email value must be less than 50 characters.', self::EX_CODE);
8282
}
8383

8484
$this->email = $email;
@@ -89,7 +89,7 @@ public function setEmail(?string $email): static
8989
public function setFullName(?string $fullName): static
9090
{
9191
if ($fullName && strlen($fullName) > 50) {
92-
throw new InvalidArgumentException('Customer\'s email value must be less than 50 characters.', static::EX_CODE);
92+
throw new InvalidArgumentException('Customer\'s email value must be less than 50 characters.', self::EX_CODE);
9393
}
9494

9595
$this->fullName = $fullName;
@@ -100,7 +100,7 @@ public function setFullName(?string $fullName): static
100100
public function setPhone(?string $phone): static
101101
{
102102
if ($phone && strlen($phone) > 30) {
103-
throw new InvalidArgumentException('Customer\'s phone value must be less than 30 characters.', static::EX_CODE);
103+
throw new InvalidArgumentException('Customer\'s phone value must be less than 30 characters.', self::EX_CODE);
104104
}
105105

106106
$this->phone = $phone;
@@ -111,7 +111,7 @@ public function setPhone(?string $phone): static
111111
public function setCountryCode(?string $countryCode): static
112112
{
113113
if ($countryCode && strlen($countryCode) !== 2) {
114-
throw new InvalidArgumentException('Customer\'s country code value is invalid.', static::EX_CODE);
114+
throw new InvalidArgumentException('Customer\'s country code value is invalid.', self::EX_CODE);
115115
}
116116

117117
$this->countryCode = $countryCode;

src/VivaWalletPayment.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ public function setAllowRecurring(bool $allowRecurring): static
204204

205205
public function setMaxInstallments(int $maxInstallments): static
206206
{
207-
$this->maxInstallments = $maxInstallments;
208-
209207
if ($maxInstallments < 0 || $maxInstallments > 36) {
210208
throw new InvalidArgumentException('maxInstallments value must be between 0 and 36');
211209
}
212210

211+
$this->maxInstallments = $maxInstallments;
212+
213213
return $this;
214214
}
215215

@@ -278,10 +278,11 @@ public function setMerchantTrns(?string $merchantTrns): static
278278
public function setTags(?array $tags): static
279279
{
280280
if ($tags) {
281-
collect($tags)->each(
282-
fn (mixed $tag) =>
283-
is_string($tag) or throw new InvalidArgumentException('tags must be an array of strings.')
284-
);
281+
collect($tags)->each(function (mixed $tag) {
282+
if (! is_string($tag)) {
283+
throw new InvalidArgumentException('tags must be an array of strings.');
284+
}
285+
});
285286
}
286287

287288
$this->tags = $tags;
@@ -322,12 +323,10 @@ public function __construct(int $amount, ?VivaWalletCustomer $customer = null, ?
322323

323324
public function setConfig(array $config): static
324325
{
325-
$this->config = $config;
326-
327-
$configPayment = $this->config['payment'];
326+
$configPayment = $config['payment'];
328327

329328
$this
330-
->setEnv($this->config['env'])
329+
->setEnv($config['env'])
331330
->setPreauth($configPayment['preauth'])
332331
->setPaymentTimeOut($configPayment['timeout'])
333332
->setDisableCash($configPayment['disable_cash'])

src/VivaWalletToken.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Deyjandi\VivaWallet\Contracts\AuthToken;
77
use Deyjandi\VivaWallet\Traits\HasClient;
88
use Deyjandi\VivaWallet\Traits\HasEnv;
9+
use Illuminate\Support\Facades\Cache;
910

1011
class VivaWalletToken implements AuthToken
1112
{
@@ -14,8 +15,6 @@ class VivaWalletToken implements AuthToken
1415

1516
private const CACHE_KEY = 'viva_wallet_token';
1617

17-
private string $method;
18-
1918
private string $clientId;
2019

2120
private string $clientSecret;
@@ -90,22 +89,27 @@ public function isExpired(): bool
9089

9190
private function requestToken(): static
9291
{
93-
$response = $this->request(...$this->env->requestToken());
92+
$response = $this->request(
93+
...$this->env->requestToken(
94+
$this->clientId,
95+
$this->clientSecret
96+
)
97+
);
9498

9599
$this->accessToken = $response['access_token'];
96100
$this->expiresIn = $response['expires_in'];
97101
$this->tokenType = $response['token_type'];
98102
$this->scope = $response['scope'];
99103
$this->issuedAt = CarbonImmutable::now();
100104

101-
cache([static::CACHE_KEY => $this], $this->issuedAt->addSeconds($this->expiresIn - 30));
105+
Cache::put(self::CACHE_KEY, $this, $this->issuedAt->addSeconds($this->expiresIn - 30));
102106

103107
return $this;
104108
}
105109

106110
public static function getInstance(): static
107111
{
108-
return cache(static::CACHE_KEY) ?? (new static())->requestToken();
112+
return Cache::get(self::CACHE_KEY) ?? (new self())->requestToken();
109113
}
110114

111115
public function refresh(): static

src/VivaWalletTransaction.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ class VivaWalletTransaction
1414

1515
public function __construct(array $config)
1616
{
17-
$this->config = $config;
18-
1917
$this->setEnv($config['env']);
2018
}
2119

@@ -24,12 +22,12 @@ public function __construct(array $config)
2422
*
2523
* @see https://developer.vivawallet.com/apis-for-payments/payment-api/#tag/Transactions/paths/~1checkout~1v2~1transactions~1{transactionId}/get
2624
*/
27-
public function retrieve(string $transaction_id): array
25+
public function retrieve(string $transactionId): array
2826
{
29-
if (! Uuid::isValid($transaction_id)) {
27+
if (! Uuid::isValid($transactionId)) {
3028
throw new InvalidArgumentException('Transaction id is invalid.');
3129
}
3230

33-
return $this->request(...$this->env->retrieveTransaction($transaction_id));
31+
return $this->request(...$this->env->retrieveTransaction($transactionId));
3432
}
3533
}

src/VivaWalletWebhook.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,50 @@ class VivaWalletWebhook
1010
use HasClient;
1111
use HasEnv;
1212

13-
private string $api_key;
13+
private string $merchantId;
14+
15+
private string $apiKey;
16+
17+
private ?string $webhookKey;
1418

1519
public function __construct(array $config)
1620
{
1721
$this
1822
->setEnv($config['env'])
23+
->setMerchantId($config['merchant_id'])
24+
->setApiKey($config['api_key'])
1925
->setWebhookKey($config['webhook_key']);
2026
}
2127

22-
public function setWebhookKey(?string $webhook_key): static
28+
public function setMerchantId(?string $merchantId): static
29+
{
30+
$this->merchantId = $merchantId;
31+
32+
return $this;
33+
}
34+
35+
public function setApiKey(string $apiKey): static
36+
{
37+
$this->apiKey = $apiKey;
38+
39+
return $this;
40+
}
41+
42+
public function setWebhookKey(?string $webhookKey): static
2343
{
24-
$this->webhook_key = $webhook_key;
44+
$this->webhookKey = $webhookKey;
2545

2646
return $this;
2747
}
2848

2949
public function requestKey(): string
3050
{
31-
return $this->request(...$this->env->requestWebhookKey())['Key'];
51+
return $this->request(
52+
...$this->env->requestWebhookKey(
53+
$this->merchantId,
54+
$this->apiKey
55+
)
56+
)['Key'];
3257
}
3358

3459
public function verifyEndpointResponse(): array
@@ -38,6 +63,6 @@ public function verifyEndpointResponse(): array
3863
throw new \Exception('Webhook verification key not set.');
3964
}
4065

41-
return ['Key' => $this->webhook_key];
66+
return ['Key' => $this->webhookKey];
4267
}
4368
}

0 commit comments

Comments
 (0)