Skip to content

Commit 23a5f77

Browse files
author
Adrian Macneil
committed
Merge pull request #10 from ruudk/fetch-token
Added FetchTokenRequest
2 parents 1087670 + 3802ef5 commit 23a5f77

File tree

8 files changed

+196
-0
lines changed

8 files changed

+196
-0
lines changed

src/Gateway.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,13 @@ public function deleteCard(array $parameters = array())
106106
{
107107
return $this->createRequest('\Omnipay\Stripe\Message\DeleteCardRequest', $parameters);
108108
}
109+
110+
/**
111+
* @param array $parameters
112+
* @return \Omnipay\Stripe\Message\FetchTokenRequest
113+
*/
114+
public function fetchToken(array $parameters = array())
115+
{
116+
return $this->createRequest('\Omnipay\Stripe\Message\FetchTokenRequest', $parameters);
117+
}
109118
}

src/Message/FetchTokenRequest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Omnipay\Stripe\Message;
4+
5+
/**
6+
* Stripe Fetch Token Request
7+
*/
8+
class FetchTokenRequest extends AbstractRequest
9+
{
10+
public function getData()
11+
{
12+
$this->validate('token');
13+
14+
$data = array();
15+
16+
return $data;
17+
}
18+
19+
public function getEndpoint()
20+
{
21+
return $this->endpoint.'/tokens/'.$this->getToken();
22+
}
23+
24+
public function getHttpMethod()
25+
{
26+
return 'GET';
27+
}
28+
}

src/Message/FetchTransactionRequest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ public function getEndpoint()
2020
{
2121
return $this->endpoint.'/charges/'.$this->getTransactionReference();
2222
}
23+
24+
public function getHttpMethod()
25+
{
26+
return 'GET';
27+
}
2328
}

src/Message/Response.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,71 @@
99
*/
1010
class Response extends AbstractResponse
1111
{
12+
/**
13+
* @return bool
14+
*/
1215
public function isSuccessful()
1316
{
1417
return !isset($this->data['error']);
1518
}
1619

20+
/**
21+
* @return string|null
22+
*/
1723
public function getTransactionReference()
1824
{
1925
if (isset($this->data['object']) && 'charge' === $this->data['object']) {
2026
return $this->data['id'];
2127
}
28+
29+
return null;
2230
}
2331

32+
/**
33+
* @return string|null
34+
*/
2435
public function getCardReference()
2536
{
2637
if (isset($this->data['object']) && 'customer' === $this->data['object']) {
2738
return $this->data['id'];
2839
}
40+
41+
return null;
42+
}
43+
44+
/**
45+
* @return string|null
46+
*/
47+
public function getToken()
48+
{
49+
if (isset($this->data['object']) && 'token' === $this->data['object']) {
50+
return $this->data['id'];
51+
}
52+
53+
return null;
2954
}
3055

56+
/**
57+
* @return array|null
58+
*/
59+
public function getCard()
60+
{
61+
if (isset($this->data['card'])) {
62+
return $this->data['card'];
63+
}
64+
65+
return null;
66+
}
67+
68+
/**
69+
* @return string|null
70+
*/
3171
public function getMessage()
3272
{
3373
if (!$this->isSuccessful()) {
3474
return $this->data['error']['message'];
3575
}
76+
77+
return null;
3678
}
3779
}

tests/GatewayTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public function testFetchTransaction()
5252
$this->assertInstanceOf('Omnipay\Stripe\Message\FetchTransactionRequest', $request);
5353
}
5454

55+
public function testFetchToken()
56+
{
57+
$request = $this->gateway->fetchToken(array());
58+
59+
$this->assertInstanceOf('Omnipay\Stripe\Message\FetchTokenRequest', $request);
60+
}
61+
5562
public function testCreateCard()
5663
{
5764
$request = $this->gateway->createCard(array('description' => 'foo'));
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Omnipay\Stripe\Message;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class FetchTokenRequestTest extends TestCase
8+
{
9+
/**
10+
* @var FetchTokenRequest
11+
*/
12+
private $request;
13+
14+
public function setUp()
15+
{
16+
$this->request = new FetchTokenRequest($this->getHttpClient(), $this->getHttpRequest());
17+
$this->request->setToken('tok_15Kuns2eZvKYlo2CDt9wRdzS');
18+
}
19+
20+
public function testEndpoint()
21+
{
22+
$this->assertSame('https://api.stripe.com/v1/tokens/tok_15Kuns2eZvKYlo2CDt9wRdzS', $this->request->getEndpoint());
23+
}
24+
25+
public function testSendSuccess()
26+
{
27+
$this->setMockHttpResponse('FetchTokenSuccess.txt');
28+
$response = $this->request->send();
29+
30+
$this->assertTrue($response->isSuccessful());
31+
$this->assertFalse($response->isRedirect());
32+
$this->assertSame('tok_15Kuns2eZvKYlo2CDt9wRdzS', $response->getToken());
33+
$this->assertInternalType('array', $response->getCard());
34+
$this->assertNull($response->getMessage());
35+
}
36+
37+
public function testSendError()
38+
{
39+
$this->setMockHttpResponse('FetchTokenFailure.txt');
40+
$response = $this->request->send();
41+
42+
$this->assertFalse($response->isSuccessful());
43+
$this->assertFalse($response->isRedirect());
44+
$this->assertNull($response->getToken());
45+
$this->assertNull($response->getCard());
46+
$this->assertSame('No such token: tok_15Kuns2eZvKYlo2CDt9wRdzS', $response->getMessage());
47+
}
48+
}

tests/Mock/FetchTokenFailure.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
HTTP/1.1 404 Not Found
2+
Server: nginx
3+
Date: Wed, 24 Jul 2013 13:40:31 GMT
4+
Content-Type: application/json;charset=utf-8
5+
Content-Length: 132
6+
Connection: keep-alive
7+
Access-Control-Allow-Credentials: true
8+
Access-Control-Max-Age: 300
9+
Cache-Control: no-cache, no-store
10+
11+
{
12+
"error": {
13+
"type": "invalid_request_error",
14+
"message": "No such token: tok_15Kuns2eZvKYlo2CDt9wRdzS",
15+
"param": "id"
16+
}
17+
}

tests/Mock/FetchTokenSuccess.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
HTTP/1.1 200 OK
2+
Server: nginx
3+
Date: Wed, 24 Jul 2013 07:14:02 GMT
4+
Content-Type: application/json;charset=utf-8
5+
Content-Length: 1092
6+
Connection: keep-alive
7+
Access-Control-Allow-Credentials: true
8+
Access-Control-Max-Age: 300
9+
Cache-Control: no-cache, no-store
10+
11+
{
12+
"id": "tok_15Kuns2eZvKYlo2CDt9wRdzS",
13+
"livemode": false,
14+
"created": 1421255976,
15+
"used": false,
16+
"object": "token",
17+
"type": "card",
18+
"card": {
19+
"id": "card_15Kuns2eZvKYlo2CugO37SA3",
20+
"object": "card",
21+
"last4": "4242",
22+
"brand": "Visa",
23+
"funding": "credit",
24+
"exp_month": 8,
25+
"exp_year": 2016,
26+
"fingerprint": "Xt5EWLLDS7FJjR1c",
27+
"country": "US",
28+
"name": null,
29+
"address_line1": null,
30+
"address_line2": null,
31+
"address_city": null,
32+
"address_state": null,
33+
"address_zip": null,
34+
"address_country": null,
35+
"cvc_check": null,
36+
"address_line1_check": null,
37+
"address_zip_check": null,
38+
"dynamic_last4": null
39+
}
40+
}

0 commit comments

Comments
 (0)