Skip to content

Commit 581d878

Browse files
committed
feat: add support for refunds
1 parent de6a89f commit 581d878

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

src/Paystack/Refund.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Leaf\Billing\Paystack;
4+
5+
/**
6+
* PayStack Refund
7+
* ----
8+
* API wrapper for PayStack Refund API
9+
*/
10+
class Refund
11+
{
12+
public Transaction $transaction;
13+
public $integration;
14+
public $deducted_amount;
15+
public $channel;
16+
public $currency;
17+
public $amount;
18+
public $fully_deducted;
19+
public $refunded_by;
20+
public $expected_at;
21+
public $merchant_note;
22+
public $customer_note;
23+
public $id;
24+
public $status;
25+
public $createdAt;
26+
public $updatedAt;
27+
28+
public function __construct(array $refund)
29+
{
30+
$this->transaction = $refund['transaction'];
31+
$this->integration = $refund['integration'];
32+
$this->deducted_amount = $refund['deducted_amount'];
33+
$this->channel = $refund['channel'];
34+
$this->currency = $refund['currency'];
35+
$this->amount = $refund['amount'];
36+
$this->fully_deducted = $refund['fully_deducted'];
37+
$this->refunded_by = $refund['refunded_by'];
38+
$this->expected_at = $refund['expected_at'];
39+
$this->merchant_note = $refund['merchant_note'];
40+
$this->customer_note = $refund['customer_note'];
41+
$this->id = $refund['id'];
42+
$this->status = $refund['status'];
43+
$this->createdAt = $refund['createdAt'];
44+
$this->updatedAt = $refund['updatedAt'];
45+
}
46+
47+
/**
48+
* Refund initiated?
49+
* @return bool
50+
*/
51+
public function isPending(): bool
52+
{
53+
return $this->status === 'pending';
54+
}
55+
56+
/**
57+
* Refund received by bank?
58+
* @return bool
59+
*/
60+
public function isProcessing(): bool
61+
{
62+
return $this->status === 'processing';
63+
}
64+
65+
/**
66+
* Refund successful?
67+
* @return bool
68+
*/
69+
public function isSuccessful(): bool
70+
{
71+
return $this->status === 'success';
72+
}
73+
74+
/**
75+
* Refund failed?
76+
* @return bool
77+
*/
78+
public function isFailed(): bool
79+
{
80+
return $this->status === 'failed';
81+
}
82+
83+
public function toArray()
84+
{
85+
return [
86+
'id' => $this->id,
87+
'transaction' => $this->transaction->toArray(),
88+
'integration' => $this->integration,
89+
'deducted_amount' => $this->deducted_amount,
90+
'channel' => $this->channel,
91+
'currency' => $this->currency,
92+
'amount' => $this->amount,
93+
'fully_deducted' => $this->fully_deducted,
94+
'refunded_by' => $this->refunded_by,
95+
'expected_at' => $this->expected_at,
96+
'merchant_note' => $this->merchant_note,
97+
'customer_note' => $this->customer_note,
98+
'status' => $this->status,
99+
'createdAt' => $this->createdAt,
100+
'updatedAt' => $this->updatedAt,
101+
];
102+
}
103+
}

src/Paystack/Refunds.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Leaf\Billing\Paystack;
4+
5+
/**
6+
* PayStack Refunds
7+
* ----
8+
* API wrapper for PayStack Refunds API
9+
*/
10+
class Refunds
11+
{
12+
protected $client;
13+
14+
public function __construct($client)
15+
{
16+
$this->client = $client;
17+
}
18+
19+
/**
20+
* Create a new paystack refund
21+
*/
22+
public function create(array $params): Refund
23+
{
24+
$response = $this->client->post('/refund', [
25+
'body' => json_encode($params)
26+
]);
27+
28+
$refund = json_decode($response->getBody(), true);
29+
$refund = $refund['data'];
30+
31+
$refund['transaction'] = (is_numeric($refund['transaction']))
32+
? (new Transactions($this->client))->get($refund['transaction'])
33+
: new Transaction($refund['transaction']);
34+
35+
return new Refund($refund);
36+
}
37+
38+
/**
39+
* Get all paystack refunds
40+
*/
41+
public function all(): array
42+
{
43+
$response = $this->client->get('/refund');
44+
45+
$refunds = json_decode($response->getBody(), true);
46+
47+
return array_map(function ($refund) {
48+
return new Refund($refund);
49+
}, $refunds['data']);
50+
}
51+
52+
/**
53+
* Get a paystack refund by id
54+
*/
55+
public function get($id): Refund
56+
{
57+
$response = $this->client->get("/refund/$id");
58+
59+
$refund = json_decode($response->getBody(), true);
60+
61+
return new Refund($refund['data']);
62+
}
63+
64+
public function toArray()
65+
{
66+
return array_map(function ($transaction) {
67+
return $transaction->toArray();
68+
}, $this->all());
69+
}
70+
}

0 commit comments

Comments
 (0)