Skip to content

Commit b3ed102

Browse files
committed
Merge pull request #17 from andrewtweber/master
Void transactions
2 parents ec01803 + f58131d commit b3ed102

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

src/Gateway.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ public function refund(array $parameters = array())
141141
return $this->createRequest('\Omnipay\Stripe\Message\RefundRequest', $parameters);
142142
}
143143

144+
/**
145+
* @param array $parameters
146+
* @return \Omnipay\Stripe\Message\VoidRequest
147+
*/
148+
public function void(array $parameters = array())
149+
{
150+
return $this->createRequest('\Omnipay\Stripe\Message\VoidRequest', $parameters);
151+
}
152+
144153
/**
145154
* @param array $parameters
146155
* @return \Omnipay\Stripe\Message\FetchTransactionRequest

src/Message/VoidRequest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Stripe Void Request
4+
*/
5+
6+
namespace Omnipay\Stripe\Message;
7+
8+
/**
9+
* Stripe Void Request
10+
*
11+
* Stripe does not support voiding, per se, but
12+
* we treat it as a full refund.
13+
*
14+
* See RefundRequest for additional information
15+
*
16+
* Example -- note this example assumes that the purchase has been successful
17+
* and that the transaction ID returned from the purchase is held in $sale_id.
18+
* See PurchaseRequest for the first part of this example transaction:
19+
*
20+
* <code>
21+
* // Do a void transaction on the gateway
22+
* $transaction = $gateway->void(array(
23+
* 'transactionReference' => $sale_id,
24+
* ));
25+
* $response = $transaction->send();
26+
* if ($response->isSuccessful()) {
27+
* echo "Void transaction was successful!\n";
28+
* $void_id = $response->getTransactionReference();
29+
* echo "Transaction reference = " . $void_id . "\n";
30+
* }
31+
* </code>
32+
*
33+
* @see RefundRequest
34+
* @see Omnipay\Stripe\Gateway
35+
* @link https://stripe.com/docs/api#create_refund
36+
*/
37+
class VoidRequest extends AbstractRequest
38+
{
39+
public function getData()
40+
{
41+
$this->validate('transactionReference');
42+
43+
return null;
44+
}
45+
46+
public function getEndpoint()
47+
{
48+
return $this->endpoint.'/charges/'.$this->getTransactionReference().'/refund';
49+
}
50+
}

tests/GatewayTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public function testRefund()
4545
$this->assertSame('10.00', $request->getAmount());
4646
}
4747

48+
public function testVoid()
49+
{
50+
$request = $this->gateway->void();
51+
52+
$this->assertInstanceOf('Omnipay\Stripe\Message\VoidRequest', $request);
53+
}
54+
4855
public function testFetchTransaction()
4956
{
5057
$request = $this->gateway->fetchTransaction(array());

tests/Message/VoidRequestTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Omnipay\Stripe\Message;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class VoidRequestTest extends TestCase
8+
{
9+
public function setUp()
10+
{
11+
$this->request = new VoidRequest($this->getHttpClient(), $this->getHttpRequest());
12+
$this->request->setTransactionReference('ch_12RgN9L7XhO9mI');
13+
}
14+
15+
public function testEndpoint()
16+
{
17+
$this->assertSame('https://api.stripe.com/v1/charges/ch_12RgN9L7XhO9mI/refund', $this->request->getEndpoint());
18+
}
19+
20+
public function testSendSuccess()
21+
{
22+
$this->setMockHttpResponse('VoidSuccess.txt');
23+
$response = $this->request->send();
24+
25+
$this->assertTrue($response->isSuccessful());
26+
$this->assertFalse($response->isRedirect());
27+
$this->assertSame('ch_12RgN9L7XhO9mI', $response->getTransactionReference());
28+
$this->assertNull($response->getCardReference());
29+
$this->assertNull($response->getMessage());
30+
}
31+
32+
public function testSendError()
33+
{
34+
$this->setMockHttpResponse('VoidFailure.txt');
35+
$response = $this->request->send();
36+
37+
$this->assertFalse($response->isSuccessful());
38+
$this->assertFalse($response->isRedirect());
39+
$this->assertNull($response->getTransactionReference());
40+
$this->assertNull($response->getCardReference());
41+
$this->assertSame('Charge ch_12RgN9L7XhO9mI has already been refunded.', $response->getMessage());
42+
}
43+
}

tests/Mock/VoidFailure.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
HTTP/1.1 200 OK
2+
3+
{"error":{"message":"Charge ch_12RgN9L7XhO9mI has already been refunded."}}

tests/Mock/VoidSuccess.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
HTTP/1.1 200 OK
2+
3+
{"id":"ch_12RgN9L7XhO9mI","object": "charge"}

0 commit comments

Comments
 (0)