Skip to content

Commit 4f5e822

Browse files
justinjonesamacneil
authored andcommitted
Update pin gateway to add support for purchasing with card tokens from Pin.js. Closes #128
1 parent 040938d commit 4f5e822

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/Omnipay/Pin/Message/PurchaseRequest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ public function setSecretKey($value)
3333

3434
public function getData()
3535
{
36-
$this->validate('amount');
36+
$this->validate('amount', 'card');
3737

3838
$data = array();
3939
$data['amount'] = $this->getAmountInteger();
4040
$data['currency'] = strtolower($this->getCurrency());
4141
$data['description'] = $this->getDescription();
4242
$data['ip_address'] = $this->getClientIp();
43+
$data['email'] = $this->getCard()->getEmail();
4344

44-
if ($this->getCard()) {
45+
if ($this->getToken()) {
46+
$data['card_token'] = $this->getToken();
47+
} else {
4548
$this->getCard()->validate();
4649

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

6163
return $data;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Omnipay\Pin\Message;
4+
5+
use Omnipay\TestCase;
6+
7+
class PurchaseRequestTest extends TestCase
8+
{
9+
public function setUp()
10+
{
11+
$this->request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest());
12+
$this->request->initialize(
13+
array(
14+
'amount' => '10.00',
15+
'currency' => 'AUD',
16+
'card' => $this->getValidCard(),
17+
)
18+
);
19+
}
20+
21+
public function testDataWithToken()
22+
{
23+
$this->request->setToken('abc');
24+
$data = $this->request->getData();
25+
26+
$this->assertSame('abc', $data['card_token']);
27+
}
28+
29+
public function testDataWithCard()
30+
{
31+
$card = $this->getValidCard();
32+
$this->request->setCard($card);
33+
$data = $this->request->getData();
34+
35+
$this->assertSame($card['number'], $data['card']['number']);
36+
}
37+
38+
public function testSendSuccess()
39+
{
40+
$this->setMockHttpResponse('PurchaseSuccess.txt');
41+
42+
$response = $this->request->send();
43+
44+
$this->assertTrue($response->isSuccessful());
45+
$this->assertFalse($response->isRedirect());
46+
$this->assertEquals('ch_fXIxWf0gj1yFHJcV1W-d-w', $response->getTransactionReference());
47+
$this->assertSame('Success!', $response->getMessage());
48+
}
49+
50+
public function testSendError()
51+
{
52+
$this->setMockHttpResponse('PurchaseFailure.txt');
53+
$response = $this->request->send();
54+
55+
$this->assertFalse($response->isSuccessful());
56+
$this->assertFalse($response->isRedirect());
57+
$this->assertNull($response->getTransactionReference());
58+
$this->assertSame('The current resource was deemed invalid.', $response->getMessage());
59+
}
60+
}

0 commit comments

Comments
 (0)