Skip to content

Commit c51d678

Browse files
committed
Implement Payment Request API with CRUD operations and update README
1 parent b8ee920 commit c51d678

7 files changed

Lines changed: 548 additions & 0 deletions

File tree

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,66 @@ array(21) { ... }
6060
...
6161
```
6262

63+
### Using Payment Requests
64+
65+
```php
66+
<?php
67+
68+
require_once "vendor/autoload.php";
69+
70+
use StarfolkSoftware\Paystack\Client as PaystackClient;
71+
72+
$paystack = new PaystackClient([
73+
'secretKey' => '*******',
74+
]);
75+
76+
// Create a payment request
77+
$response = $paystack->paymentRequests->create([
78+
'description' => 'a test invoice',
79+
'line_items' => [
80+
['name' => 'item 1', 'amount' => 20000],
81+
['name' => 'item 2', 'amount' => 20000]
82+
],
83+
'tax' => [
84+
['name' => 'VAT', 'amount' => 2000]
85+
],
86+
'customer' => 'CUS_xwaj0txjryg393b',
87+
'due_date' => '2025-07-08'
88+
]);
89+
90+
// List payment requests
91+
$paymentRequests = $paystack->paymentRequests->all(['page' => 1]);
92+
93+
// Fetch a specific payment request
94+
$paymentRequest = $paystack->paymentRequests->fetch('PRQ_1weqqsn2wwzgft8');
95+
96+
// Verify a payment request
97+
$verification = $paystack->paymentRequests->verify('PRQ_1weqqsn2wwzgft8');
98+
99+
// Send notification for a payment request
100+
$notification = $paystack->paymentRequests->sendNotification('PRQ_1weqqsn2wwzgft8');
101+
102+
// Get payment request totals
103+
$totals = $paystack->paymentRequests->totals();
104+
105+
// Finalize a draft payment request
106+
$finalized = $paystack->paymentRequests->finalize('PRQ_1weqqsn2wwzgft8', ['send_notification' => true]);
107+
108+
// Update a payment request
109+
$updated = $paystack->paymentRequests->update('PRQ_1weqqsn2wwzgft8', [
110+
'description' => 'Updated test invoice',
111+
'due_date' => '2025-07-15'
112+
]);
113+
114+
// Archive a payment request
115+
$archived = $paystack->paymentRequests->archive('PRQ_1weqqsn2wwzgft8');
116+
```
117+
63118
## Available endpoints
64119

65120
- [x] Customer
66121
- [x] Invoice
122+
- [x] Payment Request
67123
- [x] Plan
68124
- [x] Subscription
69125
- [x] Transaction

src/API/PaymentRequest.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace StarfolkSoftware\Paystack\API;
4+
5+
use StarfolkSoftware\Paystack\Abstracts\ApiAbstract;
6+
use StarfolkSoftware\Paystack\HttpClient\Message\ResponseMediator;
7+
use StarfolkSoftware\Paystack\Options\PaymentRequest as PaymentRequestOptions;
8+
9+
class PaymentRequest extends ApiAbstract
10+
{
11+
/**
12+
* Create a payment request
13+
*
14+
* @param array $params
15+
* @return array
16+
*/
17+
public function create(array $params): array
18+
{
19+
$options = new PaymentRequestOptions\CreateOptions($params);
20+
21+
$response = $this->httpClient->post('/paymentrequest', body: json_encode($options->all()));
22+
23+
return ResponseMediator::getContent($response);
24+
}
25+
26+
/**
27+
* List payment requests
28+
*
29+
* @param array $params
30+
* @return array
31+
*/
32+
public function all(array $params = []): array
33+
{
34+
$options = new PaymentRequestOptions\ReadAllOptions($params);
35+
36+
$response = $this->httpClient->get('/paymentrequest', [
37+
'query' => $options->all()
38+
]);
39+
40+
return ResponseMediator::getContent($response);
41+
}
42+
43+
/**
44+
* Fetch a payment request
45+
*
46+
* @param string $idOrCode
47+
* @return array
48+
*/
49+
public function fetch(string $idOrCode): array
50+
{
51+
$response = $this->httpClient->get("/paymentrequest/{$idOrCode}");
52+
53+
return ResponseMediator::getContent($response);
54+
}
55+
56+
/**
57+
* Verify a payment request
58+
*
59+
* @param string $code
60+
* @return array
61+
*/
62+
public function verify(string $code): array
63+
{
64+
$response = $this->httpClient->get("/paymentrequest/verify/{$code}");
65+
66+
return ResponseMediator::getContent($response);
67+
}
68+
69+
/**
70+
* Send notification for a payment request
71+
*
72+
* @param string $code
73+
* @return array
74+
*/
75+
public function sendNotification(string $code): array
76+
{
77+
$response = $this->httpClient->post("/paymentrequest/notify/{$code}");
78+
79+
return ResponseMediator::getContent($response);
80+
}
81+
82+
/**
83+
* Get payment request totals
84+
*
85+
* @return array
86+
*/
87+
public function totals(): array
88+
{
89+
$response = $this->httpClient->get("/paymentrequest/totals");
90+
91+
return ResponseMediator::getContent($response);
92+
}
93+
94+
/**
95+
* Finalize a draft payment request
96+
*
97+
* @param string $code
98+
* @param array $params
99+
* @return array
100+
*/
101+
public function finalize(string $code, array $params = []): array
102+
{
103+
$response = $this->httpClient->post("/paymentrequest/finalize/{$code}", body: json_encode($params));
104+
105+
return ResponseMediator::getContent($response);
106+
}
107+
108+
/**
109+
* Update a payment request
110+
*
111+
* @param string $idOrCode
112+
* @param array $params
113+
* @return array
114+
*/
115+
public function update(string $idOrCode, array $params): array
116+
{
117+
$options = new PaymentRequestOptions\UpdateOptions($params);
118+
119+
$response = $this->httpClient->put("/paymentrequest/{$idOrCode}", body: json_encode($options->all()));
120+
121+
return ResponseMediator::getContent($response);
122+
}
123+
124+
/**
125+
* Archive a payment request
126+
*
127+
* @param string $code
128+
* @return array
129+
*/
130+
public function archive(string $code): array
131+
{
132+
$response = $this->httpClient->post("/paymentrequest/archive/{$code}");
133+
134+
return ResponseMediator::getContent($response);
135+
}
136+
}

src/Client.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class Client
1818
{
1919
/** @var ClientBuilder $clientBuilder */
2020
private ClientBuilder $clientBuilder;
21+
22+
/** @var string $apiVersion */
23+
private string $apiVersion;
2124

2225
/**
2326
* Intantiate the client class
@@ -111,6 +114,16 @@ protected function transactions(): API\Transaction
111114
return new API\Transaction($this);
112115
}
113116

117+
/**
118+
* PaymentRequest API
119+
*
120+
* @return API\PaymentRequest
121+
*/
122+
protected function paymentRequests(): API\PaymentRequest
123+
{
124+
return new API\PaymentRequest($this);
125+
}
126+
114127
/**
115128
* Read data from inaccessible (protected or private)
116129
* or non-existing properties.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace StarfolkSoftware\Paystack\Options\PaymentRequest;
4+
5+
use StarfolkSoftware\Paystack\Abstracts\OptionsAbstract;
6+
use Symfony\Component\OptionsResolver\OptionsResolver;
7+
8+
class CreateOptions extends OptionsAbstract
9+
{
10+
/**
11+
* Set defaults, allowed types and values of the options.
12+
*
13+
* @param OptionsResolver $resolver
14+
*
15+
* @return void
16+
*/
17+
public function configureOptions(OptionsResolver $resolver): void
18+
{
19+
$resolver->define('customer')
20+
->allowedTypes('string')
21+
->info('Customer id or code');
22+
23+
$resolver->define('amount')
24+
->allowedTypes('int')
25+
->info('Payment request amount. It should be used when line items and tax values aren\'t specified.');
26+
27+
$resolver->define('due_date')
28+
->allowedTypes('string')
29+
->info('ISO 8601 representation of request due date');
30+
31+
$resolver->define('description')
32+
->allowedTypes('string')
33+
->info('A short description of the payment request');
34+
35+
$resolver->define('line_items')
36+
->allowedTypes('array')
37+
->info('Array of line items in the format [{"name":"item 1", "amount":2000, "quantity": 1}]');
38+
39+
$resolver->define('tax')
40+
->allowedTypes('array')
41+
->info('Array of taxes to be charged in the format [{"name":"VAT", "amount":2000}]');
42+
43+
$resolver->define('currency')
44+
->allowedTypes('string')
45+
->info('Specify the currency of the payment request. Defaults to NGN.');
46+
47+
$resolver->define('send_notification')
48+
->allowedTypes('bool')
49+
->info('Indicates whether Paystack sends an email notification to customer. Defaults to true');
50+
51+
$resolver->define('draft')
52+
->allowedTypes('bool')
53+
->info('Indicate if request should be saved as draft. Defaults to false and overrides send_notification');
54+
55+
$resolver->define('has_invoice')
56+
->allowedTypes('bool')
57+
->info('Set to true to create a draft payment request (adds an auto incrementing payment request number if none is provided) even if there are no line_items or tax passed');
58+
59+
$resolver->define('invoice_number')
60+
->allowedTypes('int')
61+
->info('Numeric value of the payment request. Payment Requests will start from 1 and auto increment from there.');
62+
63+
$resolver->define('split_code')
64+
->allowedTypes('string')
65+
->info('The split code of the transaction split. e.g. SPL_98WF13Eb3w');
66+
}
67+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace StarfolkSoftware\Paystack\Options\PaymentRequest;
4+
5+
use StarfolkSoftware\Paystack\Abstracts\OptionsAbstract;
6+
use Symfony\Component\OptionsResolver\OptionsResolver;
7+
8+
class ReadAllOptions extends OptionsAbstract
9+
{
10+
/**
11+
* Set defaults, allowed types and values of the options.
12+
*
13+
* @param OptionsResolver $resolver
14+
*
15+
* @return void
16+
*/
17+
public function configureOptions(OptionsResolver $resolver): void
18+
{
19+
$resolver->define('perPage')
20+
->allowedTypes('int')
21+
->info('Specify how many records you want to retrieve per page. If not specified we use a default value of 50.');
22+
23+
$resolver->define('page')
24+
->allowedTypes('int')
25+
->info('Specify exactly what page you want to retrieve. If not specified we use a default value of 1.');
26+
27+
$resolver->define('customer')
28+
->allowedTypes('string')
29+
->info('Filter by customer ID');
30+
31+
$resolver->define('status')
32+
->allowedTypes('string')
33+
->info('Filter by payment request status');
34+
35+
$resolver->define('currency')
36+
->allowedTypes('string')
37+
->info('Filter by currency');
38+
39+
$resolver->define('include_archive')
40+
->allowedTypes('string')
41+
->info('Show archived payment requests');
42+
43+
$resolver->define('from')
44+
->allowedTypes('string')
45+
->info('A timestamp from which to start listing payment requests e.g. 2016-09-24T00:00:05.000Z, 2016-09-21');
46+
47+
$resolver->define('to')
48+
->allowedTypes('string')
49+
->info('A timestamp at which to stop listing payment requests e.g. 2016-09-24T00:00:05.000Z, 2016-09-21');
50+
}
51+
}

0 commit comments

Comments
 (0)