diff --git a/src/Gateway.php b/src/Gateway.php index 8893e969..9554fc9a 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -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 diff --git a/src/Message/VoidRequest.php b/src/Message/VoidRequest.php new file mode 100644 index 00000000..c0af154f --- /dev/null +++ b/src/Message/VoidRequest.php @@ -0,0 +1,50 @@ + + * // 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"; + * } + * + * + * @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'; + } +} diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index 222d2761..83fa2aa0 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -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()); diff --git a/tests/Message/VoidRequestTest.php b/tests/Message/VoidRequestTest.php new file mode 100644 index 00000000..318e170f --- /dev/null +++ b/tests/Message/VoidRequestTest.php @@ -0,0 +1,43 @@ +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()); + } +} diff --git a/tests/Mock/VoidFailure.txt b/tests/Mock/VoidFailure.txt new file mode 100644 index 00000000..c8c1a818 --- /dev/null +++ b/tests/Mock/VoidFailure.txt @@ -0,0 +1,3 @@ +HTTP/1.1 200 OK + +{"error":{"message":"Charge ch_12RgN9L7XhO9mI has already been refunded."}} diff --git a/tests/Mock/VoidSuccess.txt b/tests/Mock/VoidSuccess.txt new file mode 100644 index 00000000..9ce07134 --- /dev/null +++ b/tests/Mock/VoidSuccess.txt @@ -0,0 +1,3 @@ +HTTP/1.1 200 OK + +{"id":"ch_12RgN9L7XhO9mI","object": "charge"}