Skip to content

Commit 22a67ba

Browse files
authored
Merge pull request #31 from chen-gloria/add-refund-standalone-function-support
Add refund_standalone function with tests
2 parents 6d08b3e + c0777b3 commit 22a67ba

4 files changed

Lines changed: 75 additions & 2 deletions

File tree

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PHP API Library for Fat Zebra
22
==============================
33

4-
Release 1.2.3 for API version 1.0
4+
Release 1.2.4 for API version 1.0
55

66
A PHP library for the [Fat Zebra](https://www.fatzebra.com.au) Online Payment Gateway (for Australian Merchants)
77
Now supports recurring billing (subscriptions, plans, customers)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fatzebra/fatzebra-php",
3-
"version": "1.2.3",
3+
"version": "1.2.4",
44
"repositories": [
55
{
66
"type": "vcs",

src/Gateway.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,36 @@ public function refund($transaction_id, $amount, $reference, $extra = null)
363363
return $this->do_request("POST", "/refunds", $payload);
364364
}
365365

366+
/**
367+
* Performs a standalone refund against the FatZebra gateway without an original transaction
368+
* @param float $amount the amount to be refunded
369+
* @param string $reference the refund reference
370+
* @param string $card_token the card token to refund to
371+
* @param array<string,string> $extra an assoc. array of extra params to merge into the request (e.g. metadata, fraud etc)
372+
* @return \StdObject
373+
*/
374+
public function refund_standalone($amount, $reference, $card_token, $extra = null)
375+
{
376+
if (is_null($amount) || strlen($amount) === 0) throw new \InvalidArgumentException("Amount is required");
377+
if (intval($amount) < 1) throw new \InvalidArgumentException("Amount is invalid - must be a positive value");
378+
if (is_null($reference) || strlen($reference) === 0) throw new \InvalidArgumentException("Reference is required");
379+
if (is_null($card_token) || strlen($card_token) === 0) throw new \InvalidArgumentException("Card token is required");
380+
381+
$int_amount = self::floatToInt($amount);
382+
383+
$payload = array(
384+
"amount" => $int_amount,
385+
"reference" => $reference,
386+
"card_token" => $card_token
387+
);
388+
389+
if (is_array($extra)) {
390+
$payload = array_merge_recursive($payload, $extra);
391+
}
392+
393+
return $this->do_request("POST", "/refunds", $payload);
394+
}
395+
366396
/**
367397
* Retrieves a purchase from the FatZebra gateway
368398
* @param string $reference the purchase ID

test/GatewayTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,47 @@ public function test_create_customer()
331331
$this->assertTrue($result->successful);
332332
$this->assertNotNull($result->response->id);
333333
}
334+
335+
/**
336+
* Test a standalone refund with a valid card token (mocked)
337+
*/
338+
public function test_refund_standalone()
339+
{
340+
$stubbed_response = '{"successful":true,"response":{"id":"refund123","successful":true,"message":"Refund processed"},"errors":[],"test":true}';
341+
$stub = $this->createMock(FatZebra\Gateway::class);
342+
$stub->method('refund_standalone')->willReturn(json_decode($stubbed_response));
343+
$result = $stub->refund_standalone(50.00, "UNITTEST" . rand(), "TOK123");
344+
345+
$this->assertTrue($result->successful);
346+
$this->assertTrue($result->response->successful);
347+
$this->assertEquals($result->response->message, "Refund processed");
348+
}
349+
350+
/**
351+
* Test standalone refund with an invalid card token
352+
*/
353+
public function test_refund_standalone_invalid_token()
354+
{
355+
$gw = new FatZebra\Gateway("TEST", "TEST", true, GW_URL);
356+
$gw->timeout = 30;
357+
358+
$result = $gw->refund_standalone(50.00, "UNITTEST" . rand(), "INVALID_TOKEN");
359+
360+
$this->assertFalse($result->successful);
361+
}
362+
363+
/**
364+
* Test standalone refund with extra parameters (mocked)
365+
*/
366+
public function test_refund_standalone_with_extra()
367+
{
368+
$stubbed_response = '{"successful":true,"response":{"id":"refund456","successful":true,"message":"Refund processed"},"errors":[],"test":true}';
369+
$stub = $this->createMock(FatZebra\Gateway::class);
370+
$stub->method('refund_standalone')->willReturn(json_decode($stubbed_response));
371+
$extra = array("metadata" => "test_metadata");
372+
$result = $stub->refund_standalone(50.00, "UNITTEST" . rand(), "TOK123", $extra);
373+
374+
$this->assertTrue($result->successful);
375+
$this->assertTrue($result->response->successful);
376+
}
334377
}

0 commit comments

Comments
 (0)