Skip to content

Commit

Permalink
Merge pull request #48 from WellingGuzman/dev
Browse files Browse the repository at this point in the history
Support: FetchChargeRequest
  • Loading branch information
delatbabel committed Apr 26, 2016
2 parents 54ca88b + 861d21f commit 0ea7a64
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ public function void(array $parameters = array())
return $this->createRequest('\Omnipay\Stripe\Message\VoidRequest', $parameters);
}

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

/**
* @param array $parameters
*
Expand Down
2 changes: 1 addition & 1 deletion src/Message/CreateCustomerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function getData()
} elseif ($this->getCard()) {
$data['card'] = $this->getCardData();
$data['email'] = $this->getCard()->getEmail();
} elseif ($this->getMetadata()) {
} elseif ($this->getEmail()) {
$data['email'] = $this->getEmail();
}

Expand Down
50 changes: 50 additions & 0 deletions src/Message/FetchChargeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* Stripe Fetch Charge Request.
*/
namespace Omnipay\Stripe\Message;

/**
* Stripe Fetch Charge Request.
*
* @link https://stripe.com/docs/api#retrieve_charge
*/
class FetchChargeRequest extends AbstractRequest
{
/**
* Get the charge reference.
*
* @return string
*/
public function getChargeReference()
{
return $this->getParameter('chargeReference');
}

/**
* Set the charge reference.
*
* @param string
* @return FetchChargeRequest provides a fluent interface.
*/
public function setChargeReference($value)
{
return $this->setParameter('chargeReference', $value);
}

public function getData()
{
$this->validate('chargeReference');
}

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

public function getHttpMethod()
{
return 'GET';
}
}
14 changes: 14 additions & 0 deletions src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ public function isSuccessful()
return !isset($this->data['error']);
}

/**
* Get the charge reference from the response of FetchChargeRequest.
*
* @return array|null
*/
public function getChargeReference()
{
if (isset($this->data['object']) && $this->data['object'] == 'charge') {
return $this->data['id'];
}

return null;
}

/**
* Get the transaction reference.
*
Expand Down
43 changes: 43 additions & 0 deletions tests/Message/FetchChargeRequestTest.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 FetchChargeRequestTest extends TestCase
{
public function setUp()
{
$this->request = new FetchChargeRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->setChargeReference('ch_180ZdUCryC0oikg4v4lc4F59D');
}

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

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

$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertSame('ch_180ZdUCryC0oikg4v4lc4F59D', $response->getChargeReference());
$this->assertInternalType('array', $response->getSource());
$this->assertNull($response->getMessage());
}

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

$this->assertFalse($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getChargeReference());
$this->assertNull($response->getSource());
$this->assertSame('No such charge: ch_180ZdUCryC0oikg4v4lc4F59D', $response->getMessage());
}
}
16 changes: 16 additions & 0 deletions tests/Mock/FetchChargeFailure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
HTTP/1.1 404 Not Found
Server: nginx
Date: Fri, 15 Apr 2016 21:42:18 GMT
Content-Type: application/json
Content-Length: 138
Connection: keep-alive
Access-Control-Allow-Credentials: true
Cache-Control: no-cache, no-store

{
"error": {
"type": "invalid_request_error",
"message": "No such charge: ch_180ZdUCryC0oikg4v4lc4F59D",
"param": "id"
}
}
70 changes: 70 additions & 0 deletions tests/Mock/FetchChargeSuccess.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 15 Apr 2016 21:37:26 GMT
Content-Type: application/json
Content-Length: 1521
Connection: keep-alive
Cache-Control: no-cache, no-store

{
"id": "ch_180ZdUCryC0oikg4v4lc4F59D",
"object": "charge",
"created": 1460437056,
"livemode": false,
"paid": true,
"status": "succeeded",
"amount": 1000,
"currency": "usd",
"refunded": false,
"source": {
"id": "card_990JozzC88C4rAAg4vg5yLG3j3",
"object": "card",
"last4": "1234",
"brand": "Visa",
"funding": "credit",
"exp_month": 1,
"exp_year": 2020,
"fingerprint": "32Q1po9Ujn5DpPgL",
"country": "US",
"name": "",
"address_line1": "",
"address_line2": "",
"address_city": "",
"address_state": "",
"address_zip": "",
"address_country": "",
"cvc_check": null,
"address_line1_check": null,
"address_zip_check": null,
"tokenization_method": null,
"dynamic_last4": null,
"metadata": {},
"customer": "cus_8GoWuzFake3R8C"
},
"source_transfer": null,
"captured": true,
"balance_transaction": "txn_180ZdUCry4Lot2g4vHVZH6y4A",
"failure_message": null,
"failure_code": null,
"amount_refunded": 0,
"customer": "cus_8GoWuzFake3R8C",
"invoice": "in_180Z1234yC4r2g4vCYO4qcIY",
"order": null,
"description": null,
"dispute": null,
"metadata": {},
"statement_descriptor": "STATEMENT",
"fraud_details": {},
"receipt_email": null,
"receipt_number": null,
"shipping": null,
"destination": null,
"application_fee": null,
"refunds": {
"object": "list",
"total_count": 0,
"has_more": false,
"url": "/v1/charges/ch_180ZdUCryC0oikg4v4lc4F59D/refunds",
"data": []
}
}

0 comments on commit 0ea7a64

Please sign in to comment.