Skip to content

Commit

Permalink
Merge pull request #10 from ruudk/fetch-token
Browse files Browse the repository at this point in the history
Added FetchTokenRequest
  • Loading branch information
Adrian Macneil committed Jan 27, 2015
2 parents 1087670 + 3802ef5 commit 23a5f77
Show file tree
Hide file tree
Showing 8 changed files with 196 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 @@ -106,4 +106,13 @@ public function deleteCard(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\DeleteCardRequest', $parameters);
}

/**
* @param array $parameters
* @return \Omnipay\Stripe\Message\FetchTokenRequest
*/
public function fetchToken(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\FetchTokenRequest', $parameters);
}
}
28 changes: 28 additions & 0 deletions src/Message/FetchTokenRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Omnipay\Stripe\Message;

/**
* Stripe Fetch Token Request
*/
class FetchTokenRequest extends AbstractRequest
{
public function getData()
{
$this->validate('token');

$data = array();

return $data;
}

public function getEndpoint()
{
return $this->endpoint.'/tokens/'.$this->getToken();
}

public function getHttpMethod()
{
return 'GET';
}
}
5 changes: 5 additions & 0 deletions src/Message/FetchTransactionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function getEndpoint()
{
return $this->endpoint.'/charges/'.$this->getTransactionReference();
}

public function getHttpMethod()
{
return 'GET';
}
}
42 changes: 42 additions & 0 deletions src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,71 @@
*/
class Response extends AbstractResponse
{
/**
* @return bool
*/
public function isSuccessful()
{
return !isset($this->data['error']);
}

/**
* @return string|null
*/
public function getTransactionReference()
{
if (isset($this->data['object']) && 'charge' === $this->data['object']) {
return $this->data['id'];
}

return null;
}

/**
* @return string|null
*/
public function getCardReference()
{
if (isset($this->data['object']) && 'customer' === $this->data['object']) {
return $this->data['id'];
}

return null;
}

/**
* @return string|null
*/
public function getToken()
{
if (isset($this->data['object']) && 'token' === $this->data['object']) {
return $this->data['id'];
}

return null;
}

/**
* @return array|null
*/
public function getCard()
{
if (isset($this->data['card'])) {
return $this->data['card'];
}

return null;
}

/**
* @return string|null
*/
public function getMessage()
{
if (!$this->isSuccessful()) {
return $this->data['error']['message'];
}

return null;
}
}
7 changes: 7 additions & 0 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public function testFetchTransaction()
$this->assertInstanceOf('Omnipay\Stripe\Message\FetchTransactionRequest', $request);
}

public function testFetchToken()
{
$request = $this->gateway->fetchToken(array());

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

public function testCreateCard()
{
$request = $this->gateway->createCard(array('description' => 'foo'));
Expand Down
48 changes: 48 additions & 0 deletions tests/Message/FetchTokenRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Omnipay\Stripe\Message;

use Omnipay\Tests\TestCase;

class FetchTokenRequestTest extends TestCase
{
/**
* @var FetchTokenRequest
*/
private $request;

public function setUp()
{
$this->request = new FetchTokenRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->setToken('tok_15Kuns2eZvKYlo2CDt9wRdzS');
}

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

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

$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertSame('tok_15Kuns2eZvKYlo2CDt9wRdzS', $response->getToken());
$this->assertInternalType('array', $response->getCard());
$this->assertNull($response->getMessage());
}

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

$this->assertFalse($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getToken());
$this->assertNull($response->getCard());
$this->assertSame('No such token: tok_15Kuns2eZvKYlo2CDt9wRdzS', $response->getMessage());
}
}
17 changes: 17 additions & 0 deletions tests/Mock/FetchTokenFailure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 404 Not Found
Server: nginx
Date: Wed, 24 Jul 2013 13:40:31 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 132
Connection: keep-alive
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 300
Cache-Control: no-cache, no-store

{
"error": {
"type": "invalid_request_error",
"message": "No such token: tok_15Kuns2eZvKYlo2CDt9wRdzS",
"param": "id"
}
}
40 changes: 40 additions & 0 deletions tests/Mock/FetchTokenSuccess.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 24 Jul 2013 07:14:02 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 1092
Connection: keep-alive
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 300
Cache-Control: no-cache, no-store

{
"id": "tok_15Kuns2eZvKYlo2CDt9wRdzS",
"livemode": false,
"created": 1421255976,
"used": false,
"object": "token",
"type": "card",
"card": {
"id": "card_15Kuns2eZvKYlo2CugO37SA3",
"object": "card",
"last4": "4242",
"brand": "Visa",
"funding": "credit",
"exp_month": 8,
"exp_year": 2016,
"fingerprint": "Xt5EWLLDS7FJjR1c",
"country": "US",
"name": null,
"address_line1": null,
"address_line2": null,
"address_city": null,
"address_state": null,
"address_zip": null,
"address_country": null,
"cvc_check": null,
"address_line1_check": null,
"address_zip_check": null,
"dynamic_last4": null
}
}

0 comments on commit 23a5f77

Please sign in to comment.