Test Setup Failed
Push — master ( 9fa851...19bcef )
by
unknown
15:59 queued 13:07
created

Paystack::getSecretKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Iamolayemi\Paystack;
4
5
use Iamolayemi\Paystack\Endpoints\Balance;
6
use Iamolayemi\Paystack\Endpoints\Bank;
7
use Iamolayemi\Paystack\Endpoints\Country;
8
use Iamolayemi\Paystack\Endpoints\Customer;
9
use Iamolayemi\Paystack\Endpoints\DedicatedAccount;
10
use Iamolayemi\Paystack\Endpoints\Invoice;
11
use Iamolayemi\Paystack\Endpoints\Page;
12
use Iamolayemi\Paystack\Endpoints\Plan;
13
use Iamolayemi\Paystack\Endpoints\Product;
14
use Iamolayemi\Paystack\Endpoints\Refund;
15
use Iamolayemi\Paystack\Endpoints\Resolve;
16
use Iamolayemi\Paystack\Endpoints\Settlement;
17
use Iamolayemi\Paystack\Endpoints\Split;
18
use Iamolayemi\Paystack\Endpoints\SubAccount;
19
use Iamolayemi\Paystack\Endpoints\Subscription;
20
use Iamolayemi\Paystack\Endpoints\Transaction;
21
use Iamolayemi\Paystack\Endpoints\Transfer;
22
use Iamolayemi\Paystack\Endpoints\TransferRecipient;
23
use Iamolayemi\Paystack\Exceptions\PaystackValidationException;
24
use Illuminate\Http\Client\PendingRequest;
25
use Illuminate\Support\Facades\Http;
26
27
class Paystack
28
{
29
    protected string $secretKey;
30
31
    private ?PendingRequest $connection = null;
32
33
    /**
34
     * Paystack constructor.
35
     *
36
     * @throws PaystackValidationException
37
     */
38
    public function __construct(string $secretKey)
39
    {
40
        $this->setSecretKey($secretKey);
41
        $this->connect();
42
    }
43
44
    /**
45
     * Set the secret key with validation.
46
     *
47
     * @throws PaystackValidationException
48
     */
49
    private function setSecretKey(string $secretKey): void
50
    {
51
        if (empty($secretKey)) {
52
            throw new PaystackValidationException('Paystack secret key is required');
53
        }
54
55
        if (! str_starts_with($secretKey, 'sk_')) {
56
            throw new PaystackValidationException('Invalid Paystack secret key format. Must start with "sk_"');
57
        }
58
59
        $this->secretKey = $secretKey;
60
    }
61
62
    /**
63
     * Establish connection to Paystack API.
64
     */
65
    private function connect(): void
66
    {
67
        $this->connection = Http::withToken($this->secretKey)
68
            ->withHeaders([
69
                'User-Agent' => 'Laravel-Paystack/2.0',
70
                'Accept' => 'application/json',
71
                'Content-Type' => 'application/json',
72
            ])
73
            ->timeout(30)
74
            ->retry(3, 1000);
75
    }
76
77
    public function getConnection(): ?PendingRequest
78
    {
79
        return $this->connection;
80
    }
81
82
    /**
83
     * Get the secret key (for internal use only).
84
     */
85
    public function getSecretKey(): string
86
    {
87
        return $this->secretKey;
88
    }
89
90
    /**
91
     * Generates a unique reference
92
     */
93
    public function generateReference(?string $transactionPrefix = null): string
94
    {
95
        if ($transactionPrefix) {
96
            return $transactionPrefix.'_'.uniqid((string) time());
97
        }
98
99
        return 'PK_'.uniqid((string) time());
100
    }
101
102
    /**
103
     * Create a new Balance instance.
104
     */
105
    public function balance(): Balance
106
    {
107
        return new Balance($this);
108
    }
109
110
    /**
111
     * Create a new Bank instance.
112
     */
113
    public function bank(): Bank
114
    {
115
        return new Bank($this);
116
    }
117
118
    /**
119
     * Create a new Country instance.
120
     */
121
    public function country(): Country
122
    {
123
        return new Country($this);
124
    }
125
126
    /**
127
     * Create a new customer instance.
128
     */
129
    public function customer(): Customer
130
    {
131
        return new Customer($this);
132
    }
133
134
    /**
135
     * Create a new dedicated account instance.
136
     */
137
    public function dedicatedAccount(): DedicatedAccount
138
    {
139
        return new DedicatedAccount($this);
140
    }
141
142
    /**
143
     * Create a new invoice instance.
144
     */
145
    public function invoice(): Invoice
146
    {
147
        return new Invoice($this);
148
    }
149
150
    /**
151
     * Create a new page instance.
152
     */
153
    public function page(): Page
154
    {
155
        return new Page($this);
156
    }
157
158
    /**
159
     * Create a new plan instance.
160
     */
161
    public function plan(): Plan
162
    {
163
        return new Plan($this);
164
    }
165
166
    /**
167
     * Create a new Product instance.
168
     */
169
    public function product(): Product
170
    {
171
        return new Product($this);
172
    }
173
174
    /**
175
     * Create a new TransferRecipient instance.
176
     */
177
    public function recipient(): TransferRecipient
178
    {
179
        return new TransferRecipient($this);
180
    }
181
182
    /**
183
     * Create a new Refund instance.
184
     */
185
    public function refund(): Refund
186
    {
187
        return new Refund($this);
188
    }
189
190
    /**
191
     * Create a new Resolve instance.
192
     */
193
    public function resolve(): Resolve
194
    {
195
        return new Resolve($this);
196
    }
197
198
    /**
199
     * Create a new Settlement instance.
200
     */
201
    public function settlement(): Settlement
202
    {
203
        return new Settlement($this);
204
    }
205
206
    /**
207
     * Create a new split transaction instance.
208
     */
209
    public function split(): Split
210
    {
211
        return new Split($this);
212
    }
213
214
    /**
215
     * Create a new sub account instance.
216
     */
217
    public function subAccount(): SubAccount
218
    {
219
        return new SubAccount($this);
220
    }
221
222
    /**
223
     * Create a new subscription instance.
224
     */
225
    public function subscription(): Subscription
226
    {
227
        return new Subscription($this);
228
    }
229
230
    /**
231
     * Create a new transaction instance.
232
     */
233
    public function transaction(): Transaction
234
    {
235
        return new Transaction($this);
236
    }
237
238
    /**
239
     * Create a new transfer instance.
240
     */
241
    public function transfer(): Transfer
242
    {
243
        return new Transfer($this);
244
    }
245
}
246