Skip to content

Commit 0ea7a64

Browse files
committed
Merge pull request #48 from WellingGuzman/dev
Support: FetchChargeRequest
2 parents 54ca88b + 861d21f commit 0ea7a64

File tree

7 files changed

+204
-1
lines changed

7 files changed

+204
-1
lines changed

src/Gateway.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,16 @@ public function void(array $parameters = array())
247247
return $this->createRequest('\Omnipay\Stripe\Message\VoidRequest', $parameters);
248248
}
249249

250+
/**
251+
* @param array $parameters
252+
*
253+
* @return \Omnipay\Stripe\Message\FetchChargeRequest
254+
*/
255+
public function fetchCharge(array $parameters = array())
256+
{
257+
return $this->createRequest('\Omnipay\Stripe\Message\FetchChargeRequest', $parameters);
258+
}
259+
250260
/**
251261
* @param array $parameters
252262
*

src/Message/CreateCustomerRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function getData()
114114
} elseif ($this->getCard()) {
115115
$data['card'] = $this->getCardData();
116116
$data['email'] = $this->getCard()->getEmail();
117-
} elseif ($this->getMetadata()) {
117+
} elseif ($this->getEmail()) {
118118
$data['email'] = $this->getEmail();
119119
}
120120

src/Message/FetchChargeRequest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* Stripe Fetch Charge Request.
5+
*/
6+
namespace Omnipay\Stripe\Message;
7+
8+
/**
9+
* Stripe Fetch Charge Request.
10+
*
11+
* @link https://stripe.com/docs/api#retrieve_charge
12+
*/
13+
class FetchChargeRequest extends AbstractRequest
14+
{
15+
/**
16+
* Get the charge reference.
17+
*
18+
* @return string
19+
*/
20+
public function getChargeReference()
21+
{
22+
return $this->getParameter('chargeReference');
23+
}
24+
25+
/**
26+
* Set the charge reference.
27+
*
28+
* @param string
29+
* @return FetchChargeRequest provides a fluent interface.
30+
*/
31+
public function setChargeReference($value)
32+
{
33+
return $this->setParameter('chargeReference', $value);
34+
}
35+
36+
public function getData()
37+
{
38+
$this->validate('chargeReference');
39+
}
40+
41+
public function getEndpoint()
42+
{
43+
return $this->endpoint.'/charges/'.$this->getChargeReference();
44+
}
45+
46+
public function getHttpMethod()
47+
{
48+
return 'GET';
49+
}
50+
}

src/Message/Response.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ public function isSuccessful()
2626
return !isset($this->data['error']);
2727
}
2828

29+
/**
30+
* Get the charge reference from the response of FetchChargeRequest.
31+
*
32+
* @return array|null
33+
*/
34+
public function getChargeReference()
35+
{
36+
if (isset($this->data['object']) && $this->data['object'] == 'charge') {
37+
return $this->data['id'];
38+
}
39+
40+
return null;
41+
}
42+
2943
/**
3044
* Get the transaction reference.
3145
*
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 FetchChargeRequestTest extends TestCase
8+
{
9+
public function setUp()
10+
{
11+
$this->request = new FetchChargeRequest($this->getHttpClient(), $this->getHttpRequest());
12+
$this->request->setChargeReference('ch_180ZdUCryC0oikg4v4lc4F59D');
13+
}
14+
15+
public function testEndpoint()
16+
{
17+
$this->assertSame('https://api.stripe.com/v1/charges/ch_180ZdUCryC0oikg4v4lc4F59D', $this->request->getEndpoint());
18+
}
19+
20+
public function testSendSuccess()
21+
{
22+
$this->setMockHttpResponse('FetchChargeSuccess.txt');
23+
$response = $this->request->send();
24+
25+
$this->assertTrue($response->isSuccessful());
26+
$this->assertFalse($response->isRedirect());
27+
$this->assertSame('ch_180ZdUCryC0oikg4v4lc4F59D', $response->getChargeReference());
28+
$this->assertInternalType('array', $response->getSource());
29+
$this->assertNull($response->getMessage());
30+
}
31+
32+
public function testSendFailure()
33+
{
34+
$this->setMockHttpResponse('FetchChargeFailure.txt');
35+
$response = $this->request->send();
36+
37+
$this->assertFalse($response->isSuccessful());
38+
$this->assertFalse($response->isRedirect());
39+
$this->assertNull($response->getChargeReference());
40+
$this->assertNull($response->getSource());
41+
$this->assertSame('No such charge: ch_180ZdUCryC0oikg4v4lc4F59D', $response->getMessage());
42+
}
43+
}

tests/Mock/FetchChargeFailure.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
HTTP/1.1 404 Not Found
2+
Server: nginx
3+
Date: Fri, 15 Apr 2016 21:42:18 GMT
4+
Content-Type: application/json
5+
Content-Length: 138
6+
Connection: keep-alive
7+
Access-Control-Allow-Credentials: true
8+
Cache-Control: no-cache, no-store
9+
10+
{
11+
"error": {
12+
"type": "invalid_request_error",
13+
"message": "No such charge: ch_180ZdUCryC0oikg4v4lc4F59D",
14+
"param": "id"
15+
}
16+
}

tests/Mock/FetchChargeSuccess.txt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
HTTP/1.1 200 OK
2+
Server: nginx
3+
Date: Fri, 15 Apr 2016 21:37:26 GMT
4+
Content-Type: application/json
5+
Content-Length: 1521
6+
Connection: keep-alive
7+
Cache-Control: no-cache, no-store
8+
9+
{
10+
"id": "ch_180ZdUCryC0oikg4v4lc4F59D",
11+
"object": "charge",
12+
"created": 1460437056,
13+
"livemode": false,
14+
"paid": true,
15+
"status": "succeeded",
16+
"amount": 1000,
17+
"currency": "usd",
18+
"refunded": false,
19+
"source": {
20+
"id": "card_990JozzC88C4rAAg4vg5yLG3j3",
21+
"object": "card",
22+
"last4": "1234",
23+
"brand": "Visa",
24+
"funding": "credit",
25+
"exp_month": 1,
26+
"exp_year": 2020,
27+
"fingerprint": "32Q1po9Ujn5DpPgL",
28+
"country": "US",
29+
"name": "",
30+
"address_line1": "",
31+
"address_line2": "",
32+
"address_city": "",
33+
"address_state": "",
34+
"address_zip": "",
35+
"address_country": "",
36+
"cvc_check": null,
37+
"address_line1_check": null,
38+
"address_zip_check": null,
39+
"tokenization_method": null,
40+
"dynamic_last4": null,
41+
"metadata": {},
42+
"customer": "cus_8GoWuzFake3R8C"
43+
},
44+
"source_transfer": null,
45+
"captured": true,
46+
"balance_transaction": "txn_180ZdUCry4Lot2g4vHVZH6y4A",
47+
"failure_message": null,
48+
"failure_code": null,
49+
"amount_refunded": 0,
50+
"customer": "cus_8GoWuzFake3R8C",
51+
"invoice": "in_180Z1234yC4r2g4vCYO4qcIY",
52+
"order": null,
53+
"description": null,
54+
"dispute": null,
55+
"metadata": {},
56+
"statement_descriptor": "STATEMENT",
57+
"fraud_details": {},
58+
"receipt_email": null,
59+
"receipt_number": null,
60+
"shipping": null,
61+
"destination": null,
62+
"application_fee": null,
63+
"refunds": {
64+
"object": "list",
65+
"total_count": 0,
66+
"has_more": false,
67+
"url": "/v1/charges/ch_180ZdUCryC0oikg4v4lc4F59D/refunds",
68+
"data": []
69+
}
70+
}

0 commit comments

Comments
 (0)