Skip to content

Commit

Permalink
added fetch transaction request by id
Browse files Browse the repository at this point in the history
  • Loading branch information
Ednei Oliveira committed Aug 27, 2015
1 parent d4610d7 commit df2e328
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,15 @@ public function calculateInstallments(array $parameters = array())
{
return $this->createRequest('\Omnipay\Pagarme\Message\InstallmentsRequest', $parameters);
}

/**
* Pagarme Fetch Transaction by Id.
*
* @param array $parameters
* @return \Omnipay\Pagarme\Message\FetchTransactionRequest
*/
public function fetchTransaction(array $parameters = array())
{
return $this->createRequest('\Omnipay\Pagarme\Message\FetchTransactionRequest', $parameters);
}
}
8 changes: 8 additions & 0 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ function ($event) {
return $this->response = new Response($this, $httpResponse->json());
}

/**
* Get Query Options.
*
* Must be over-ridden in sub classes that make GET requests
* with query parameters.
*
* @return array The query Options
*/
protected function getOptions()
{
return array();
Expand Down
62 changes: 62 additions & 0 deletions src/Message/FetchTransactionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Pagarme Fetch Transaction Request
*/

namespace Omnipay\Pagarme\Message;

/**
* Pagarme Fetch Transaction Request
*
* Example -- note this example assumes that the purchase has been successful
* and that the transaction ID returned from the purchase is held in $sale_id.
* See PurchaseRequest for the first part of this example transaction:
*
* <code>
* // Fetch the transaction so that details can be found for refund, etc.
* $transaction = $gateway->fetchTransaction();
* $transaction->setTransactionReference($sale_id);
* $response = $transaction->send();
* $data = $response->getData();
* echo "Gateway fetchTransaction response data == " . print_r($data, true) . "\n";
* </code>
*
* @see PurchaseRequest
* @see Omnipay\Pagarme\Gateway
* @link https://docs.pagar.me/api/#retornando-uma-transao
*/
class FetchTransactionRequest extends AbstractRequest
{
public function getData()
{
$data = array();

return $data;
}

public function getQuery()
{
$this->validate('transactionReference');

$data = array();
$data['api_key'] = $this->getApiKey();

return $data;
}

protected function getOptions() {
$options['query'] = $this->getQuery();

return $options;
}

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

public function getHttpMethod()
{
return 'GET';
}
}
7 changes: 7 additions & 0 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ public function testCalculateInstallments()

$this->assertInstanceOf('Omnipay\Pagarme\Message\InstallmentsRequest', $request);
}

public function testFetchTransaction()
{
$request = $this->gateway->fetchTransaction();

$this->assertInstanceOf('Omnipay\Pagarme\Message\FetchTransactionRequest', $request);
}
}
75 changes: 75 additions & 0 deletions tests/Message/FetchTransactionRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
namespace Omnipay\Pagarme\Message;

use Omnipay\Tests\TestCase;

class FetchTransactionRequestTest extends TestCase
{
public function setUp()
{
$this->request = new FetchTransactionRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->initialize(
array(
'transactionReference' => 123456,
)
);
}

public function testGetData()
{
$data = $this->request->getData();

$this->assertSame(array(), $data);
}

public function testGetQuery()
{
$data = $this->request->getQuery();

$this->assertArrayHasKey('api_key', $data);
}

/**
* @expectedException \Omnipay\Common\Exception\InvalidRequestException
* @expectedExceptionMessage The transactionReference parameter is required
*/
public function testAmountRequired()
{
$this->request->setTransactionReference(null);
$this->request->getQuery();
}

public function testGetHttpMethod()
{
$this->assertSame('GET', $this->request->getHttpMethod());
}

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

$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertSame('transaction', $transactionArray['object'] );
$this->assertSame('paid', $transactionArray['status'] );
$this->assertSame(1537, $transactionArray['amount']);
$this->assertNull($response->getMessage());
}

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

$this->assertFalse($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertSame('Transaction não encontrado', $response->getMessage());
}

public function testEndpoint()
{
$this->assertSame('https://api.pagar.me/1/transactions/123456', $this->request->getEndpoint());
}
}
7 changes: 7 additions & 0 deletions tests/Message/InstallmentsRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public function testGetHttpMethod()
$this->assertSame('GET', $this->request->getHttpMethod());
}

public function testGetQuery()
{
$data = $this->request->getQuery();

$this->assertArrayHasKey('api_key', $data);
}

public function testSendSuccess()
{
$this->setMockHttpResponse('CalculateInstallmentsSuccess.txt');
Expand Down
30 changes: 30 additions & 0 deletions tests/Mock/FetchTransactionFailure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
HTTP/1.1 404 Not Found
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With, X-HTTP-Method-Override
Access-Control-Allow-Methods: GET,PUT,POST,DELETE
Access-Control-Allow-Origin: *
Charset: utf-8
Content-Type: application/json
Date: Thu, 27 Aug 2015 19:05:45 GMT
Server: nginx
X-Powered-By: Express
X-RateLimit-Limit: 30
X-RateLimit-Remaining: 29
X-RateLimit-Reset: 1440702347
X-Response-Time: 38ms
Content-Length: 186
Connection: keep-alive
X-Iinfo: 9-5852669-5835142 PNNN RT(1440702344579 28) q(0 0 0 -1) r(1 1) U11
X-CDN: Incapsula

{
"errors": [
{
"type": "not_found",
"parameter_name": null,
"message": "Transaction não encontrado"
}
],
"url": "/transactions/957784?api_key=ak_test_R2j5LA1IqqxBZWLfy9yU03dSJbqY6G",
"method": "get"
}
98 changes: 98 additions & 0 deletions tests/Mock/FetchTransactionSuccess.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With, X-HTTP-Method-Override
Access-Control-Allow-Methods: GET,PUT,POST,DELETE
Access-Control-Allow-Origin: *
Charset: utf-8
Content-Type: application/json
Date: Thu, 27 Aug 2015 19:03:48 GMT
ETag: "1613867490"
Server: nginx
X-Powered-By: Express
X-RateLimit-Limit: 30
X-RateLimit-Remaining: 29
X-RateLimit-Reset: 1440702230
X-Response-Time: 68ms
Content-Length: 1559
Connection: keep-alive
Set-Cookie: nlbi_166741=ne8mAdHtCF0RMpwFW7H6TQAAAAALnHhodUiZzDcri4qShiSs; path=/; Domain=.pagar.me
Set-Cookie: incap_ses_297_166741=nwxBX703rVp/+/VfJygfBBNf31UAAAAA8ji4YXrBE6Be/MOKcMuYxQ==; path=/; Domain=.pagar.me
X-Iinfo: 9-5828000-5828009 NNNN CT(144 143 0) RT(1440702227197 52) q(0 0 3 1) r(5 5)
X-CDN: Incapsula

{
"object": "transaction",
"status": "paid",
"refuse_reason": null,
"status_reason": "acquirer",
"acquirer_response_code": "00",
"acquirer_name": "development",
"authorization_code": "70272",
"soft_descriptor": null,
"tid": "1440094339345",
"nsu": "1440094339345",
"date_created": "2015-08-20T18:12:18.000Z",
"date_updated": "2015-08-20T18:12:19.000Z",
"amount": 1537,
"installments": 1,
"id": 257784,
"cost": 73.055,
"card_holder_name": "Leon F. Castillo",
"card_last_digits": "1278",
"card_first_digits": "492798",
"card_brand": "visa",
"postback_url": null,
"payment_method": "credit_card",
"antifraud_score": null,
"boleto_url": null,
"boleto_barcode": null,
"boleto_expiration_date": null,
"referer": "api_key",
"ip": "201.82.63.71",
"subscription_id": null,
"phone": {
"object": "phone",
"ddi": "55",
"ddd": "15",
"number": "190590746",
"id": 15788
},
"address": {
"object": "address",
"street": "Onowu Loop",
"complementary": "",
"street_number": "476",
"neighborhood": "Mevwaw Road",
"city": "Rio de Janeiro",
"state": "RJ",
"zipcode": "20511170",
"country": "Brasil",
"id": 16035
},
"customer": {
"object": "customer",
"document_number": "27611355805",
"document_type": "cpf",
"name": "Leon F. Castillo",
"email": "nachurroj@example.com",
"born_at": null,
"gender": null,
"date_created": "2015-08-20T18:12:18.000Z",
"id": 22970
},
"card": {
"object": "card",
"id": "card_cidkizjc2002tzs6eqh2ns2bk",
"date_created": "2015-08-20T18:12:18.000Z",
"date_updated": "2015-08-20T18:12:19.000Z",
"brand": "visa",
"holder_name": "Leon F. Castillo",
"first_digits": "492798",
"last_digits": "1278",
"country": "US",
"fingerprint": "7Era0vQDhYFm",
"valid": true
},
"metadata": {},
"antifraud_metadata": {}
}

0 comments on commit df2e328

Please sign in to comment.