Skip to content

Commit

Permalink
Merge pull request #17 from andrewtweber/master
Browse files Browse the repository at this point in the history
Void transactions
  • Loading branch information
greydnls committed Mar 16, 2015
2 parents ec01803 + f58131d commit b3ed102
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ public function refund(array $parameters = array())
return $this->createRequest('\Omnipay\Stripe\Message\RefundRequest', $parameters);
}

/**
* @param array $parameters
* @return \Omnipay\Stripe\Message\VoidRequest
*/
public function void(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\VoidRequest', $parameters);
}

/**
* @param array $parameters
* @return \Omnipay\Stripe\Message\FetchTransactionRequest
Expand Down
50 changes: 50 additions & 0 deletions src/Message/VoidRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Stripe Void Request
*/

namespace Omnipay\Stripe\Message;

/**
* Stripe Void Request
*
* Stripe does not support voiding, per se, but
* we treat it as a full refund.
*
* See RefundRequest for additional information
*
* Example -- note this example assumes that the purchase has been successful
* and that the transaction ID returned from the purchase is held in $sale_id.
* See PurchaseRequest for the first part of this example transaction:
*
* <code>
* // Do a void transaction on the gateway
* $transaction = $gateway->void(array(
* 'transactionReference' => $sale_id,
* ));
* $response = $transaction->send();
* if ($response->isSuccessful()) {
* echo "Void transaction was successful!\n";
* $void_id = $response->getTransactionReference();
* echo "Transaction reference = " . $void_id . "\n";
* }
* </code>
*
* @see RefundRequest
* @see Omnipay\Stripe\Gateway
* @link https://stripe.com/docs/api#create_refund
*/
class VoidRequest extends AbstractRequest
{
public function getData()
{
$this->validate('transactionReference');

return null;
}

public function getEndpoint()
{
return $this->endpoint.'/charges/'.$this->getTransactionReference().'/refund';
}
}
7 changes: 7 additions & 0 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public function testRefund()
$this->assertSame('10.00', $request->getAmount());
}

public function testVoid()
{
$request = $this->gateway->void();

$this->assertInstanceOf('Omnipay\Stripe\Message\VoidRequest', $request);
}

public function testFetchTransaction()
{
$request = $this->gateway->fetchTransaction(array());
Expand Down
43 changes: 43 additions & 0 deletions tests/Message/VoidRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Omnipay\Stripe\Message;

use Omnipay\Tests\TestCase;

class VoidRequestTest extends TestCase
{
public function setUp()
{
$this->request = new VoidRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->setTransactionReference('ch_12RgN9L7XhO9mI');
}

public function testEndpoint()
{
$this->assertSame('https://api.stripe.com/v1/charges/ch_12RgN9L7XhO9mI/refund', $this->request->getEndpoint());
}

public function testSendSuccess()
{
$this->setMockHttpResponse('VoidSuccess.txt');
$response = $this->request->send();

$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertSame('ch_12RgN9L7XhO9mI', $response->getTransactionReference());
$this->assertNull($response->getCardReference());
$this->assertNull($response->getMessage());
}

public function testSendError()
{
$this->setMockHttpResponse('VoidFailure.txt');
$response = $this->request->send();

$this->assertFalse($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getTransactionReference());
$this->assertNull($response->getCardReference());
$this->assertSame('Charge ch_12RgN9L7XhO9mI has already been refunded.', $response->getMessage());
}
}
3 changes: 3 additions & 0 deletions tests/Mock/VoidFailure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HTTP/1.1 200 OK

{"error":{"message":"Charge ch_12RgN9L7XhO9mI has already been refunded."}}
3 changes: 3 additions & 0 deletions tests/Mock/VoidSuccess.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HTTP/1.1 200 OK

{"id":"ch_12RgN9L7XhO9mI","object": "charge"}

0 comments on commit b3ed102

Please sign in to comment.