Skip to content

Commit

Permalink
Update pin gateway to add support for purchasing with card tokens fro…
Browse files Browse the repository at this point in the history
…m Pin.js. Closes #128
  • Loading branch information
justinjones authored and amacneil committed Sep 20, 2013
1 parent 040938d commit 4f5e822
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Omnipay/Pin/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ public function setSecretKey($value)

public function getData()
{
$this->validate('amount');
$this->validate('amount', 'card');

$data = array();
$data['amount'] = $this->getAmountInteger();
$data['currency'] = strtolower($this->getCurrency());
$data['description'] = $this->getDescription();
$data['ip_address'] = $this->getClientIp();
$data['email'] = $this->getCard()->getEmail();

if ($this->getCard()) {
if ($this->getToken()) {
$data['card_token'] = $this->getToken();
} else {
$this->getCard()->validate();

$data['card']['number'] = $this->getCard()->getNumber();
Expand All @@ -55,7 +58,6 @@ public function getData()
$data['card']['address_postcode'] = $this->getCard()->getPostcode();
$data['card']['address_state'] = $this->getCard()->getState();
$data['card']['address_country'] = $this->getCard()->getCountry();
$data['email'] = $this->getCard()->getEmail();
}

return $data;
Expand Down
60 changes: 60 additions & 0 deletions tests/Omnipay/Pin/Message/PurchaseRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Omnipay\Pin\Message;

use Omnipay\TestCase;

class PurchaseRequestTest extends TestCase
{
public function setUp()
{
$this->request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->initialize(
array(
'amount' => '10.00',
'currency' => 'AUD',
'card' => $this->getValidCard(),
)
);
}

public function testDataWithToken()
{
$this->request->setToken('abc');
$data = $this->request->getData();

$this->assertSame('abc', $data['card_token']);
}

public function testDataWithCard()
{
$card = $this->getValidCard();
$this->request->setCard($card);
$data = $this->request->getData();

$this->assertSame($card['number'], $data['card']['number']);
}

public function testSendSuccess()
{
$this->setMockHttpResponse('PurchaseSuccess.txt');

$response = $this->request->send();

$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertEquals('ch_fXIxWf0gj1yFHJcV1W-d-w', $response->getTransactionReference());
$this->assertSame('Success!', $response->getMessage());
}

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

$this->assertFalse($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getTransactionReference());
$this->assertSame('The current resource was deemed invalid.', $response->getMessage());
}
}

0 comments on commit 4f5e822

Please sign in to comment.