Skip to content

Commit b253d95

Browse files
Merge pull request #22 from utopia-php/feat-disputes
Feat: list disputes
2 parents 65608ef + 34a1da7 commit b253d95

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed

src/Pay/Adapter.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ abstract public function updateFuturePayment(string $id, ?string $customerId = n
250250
*/
251251
abstract public function getMandate(string $id): array;
252252

253+
/**
254+
* List disputes
255+
*
256+
* @param int|null $limit
257+
* @param string|null $paymentIntentId
258+
* @param string|null $chargeId
259+
* @param int|null $createdAfter
260+
* @return array
261+
*/
262+
abstract public function listDisputes(?int $limit = null, ?string $paymentIntentId = null, ?string $chargeId = null, ?int $createdAfter = null): array;
263+
253264
/**
254265
* Call
255266
* Make a request
@@ -265,8 +276,9 @@ protected function call(string $method, string $url, array $params = [], array $
265276
{
266277
$responseHeaders = [];
267278
$ch = \curl_init();
279+
$query = null;
268280

269-
switch ($headers['content-type']) {
281+
switch ($headers['content-type'] ?? null) {
270282
case 'application/json':
271283
$query = json_encode($params);
272284
break;
@@ -304,7 +316,7 @@ protected function call(string $method, string $url, array $params = [], array $
304316

305317
return $len;
306318
});
307-
if ($method != self::METHOD_GET && $method != self::METHOD_DELETE) {
319+
if (! empty($query)) {
308320
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
309321
}
310322

src/Pay/Adapter/Stripe.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,41 @@ public function getMandate(string $id): array
332332
return $this->execute(self::METHOD_GET, $path);
333333
}
334334

335+
/**
336+
* List disputes
337+
*
338+
* @param int|null $limit
339+
* @param string|null $paymentIntentId
340+
* @param string|null $chargeId
341+
* @param int|null $createdAfter
342+
* @return array
343+
*/
344+
public function listDisputes(?int $limit = null, ?string $paymentIntentId = null, ?string $chargeId = null, ?int $createdAfter = null): array
345+
{
346+
$path = '/disputes';
347+
$requestBody = [];
348+
349+
if ($limit !== null) {
350+
$requestBody['limit'] = $limit;
351+
}
352+
353+
if ($paymentIntentId !== null) {
354+
$requestBody['payment_intent'] = $paymentIntentId;
355+
}
356+
if ($chargeId !== null) {
357+
$requestBody['charge'] = $chargeId;
358+
}
359+
if ($createdAfter !== null) {
360+
$requestBody['created'] = [
361+
'gte' => $createdAfter,
362+
];
363+
}
364+
365+
$result = $this->execute(self::METHOD_GET, $path, $requestBody);
366+
367+
return $result['data'];
368+
}
369+
335370
/**
336371
* Execute
337372
*
@@ -343,7 +378,12 @@ public function getMandate(string $id): array
343378
*/
344379
private function execute(string $method, string $path, array $requestBody = [], array $headers = []): array
345380
{
346-
$headers = array_merge(['content-type' => 'application/x-www-form-urlencoded', 'Authorization' => 'Bearer '.$this->secretKey], $headers);
381+
$defaultHeaders = ['Authorization' => 'Bearer '.$this->secretKey];
382+
383+
if ($method !== self::METHOD_GET) {
384+
$defaultHeaders['content-type'] = 'application/x-www-form-urlencoded';
385+
}
386+
$headers = array_merge($defaultHeaders, $headers);
347387

348388
return $this->call($method, $this->baseUrl.$path, $requestBody, $headers);
349389
}

src/Pay/Pay.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,18 @@ public function getMandate(string $id): array
312312
{
313313
return $this->adapter->getMandate($id);
314314
}
315+
316+
/**
317+
* List disputes
318+
*
319+
* @param int|null $limit
320+
* @param string|null $paymentIntentId
321+
* @param string|null $chargeId
322+
* @param int|null $createdAfter
323+
* @return array
324+
*/
325+
public function listDisputes(?int $limit = null, ?string $paymentIntentId = null, ?string $chargeId = null, ?int $createdAfter = null): array
326+
{
327+
return $this->adapter->listDisputes($limit, $paymentIntentId, $chargeId, $createdAfter);
328+
}
315329
}

tests/Pay/Adapter/StripeTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,55 @@ public function testDeleteCustomer(array $data): void
355355
$this->assertTrue($res['deleted']);
356356
}
357357

358+
/**
359+
* Test list disputes
360+
*
361+
* @param array $data
362+
* @return void
363+
*/
364+
public function testListDisputes(): void
365+
{
366+
$customer = $this->stripe->createCustomer('Test customer', '[email protected]', ['city' => 'Kathmandu', 'country' => 'NP', 'line1' => 'Gaurighat', 'line2' => 'Pambu Marga', 'postal_code' => '44600', 'state' => 'Bagmati']);
367+
$this->assertNotEmpty($customer['id']);
368+
$customerId = $customer['id'];
369+
370+
$pm = $this->stripe->createPaymentMethod($customerId, 'card', [
371+
'number' => 4000000000000259,
372+
'exp_month' => 8,
373+
'exp_year' => 2030,
374+
'cvc' => 123,
375+
]);
376+
$this->assertNotEmpty($pm['id']);
377+
$this->assertNotEmpty($pm['card']);
378+
379+
$card = $pm['card'];
380+
$this->assertEquals('visa', $card['brand']);
381+
$this->assertEquals('US', $card['country']);
382+
$this->assertEquals(2030, $card['exp_year']);
383+
$this->assertEquals(8, $card['exp_month']);
384+
$this->assertEquals('0259', $card['last4']);
385+
386+
$paymentMethodId = $pm['id'];
387+
388+
$purchase = $this->stripe->purchase(5000, $customerId, $paymentMethodId);
389+
390+
$this->assertNotEmpty($purchase['id']);
391+
$this->assertEquals(5000, $purchase['amount_received']);
392+
$this->assertEquals('payment_intent', $purchase['object']);
393+
$this->assertEquals('succeeded', $purchase['status']);
394+
395+
// list disputes
396+
$paymentIntentId = $purchase['id'];
397+
398+
$disputes = $this->stripe->listDisputes(1);
399+
$this->assertIsArray($disputes);
400+
$this->assertEquals(1, count($disputes));
401+
402+
$disputes = $this->stripe->listDisputes(paymentIntentId: $paymentIntentId);
403+
$this->assertEquals(1, count($disputes));
404+
$this->assertEquals($paymentIntentId, $disputes[0]['payment_intent']);
405+
}
406+
358407
public function testErrorHandling(): void
359408
{
360409
try {

0 commit comments

Comments
 (0)