Skip to content

Commit fa2d068

Browse files
author
iampersistent
committed
add Customer support
1 parent 6e12fb9 commit fa2d068

12 files changed

+345
-5
lines changed

src/Gateway.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@ public function clientToken(array $parameters = array())
113113
return $this->createRequest('\Omnipay\Braintree\Message\ClientTokenRequest', $parameters);
114114
}
115115

116+
/**
117+
* @param array $parameters
118+
* @return Message\CreateCustomerRequest
119+
*/
120+
public function createCustomer(array $parameters = array())
121+
{
122+
return $this->createRequest('\Omnipay\Braintree\Message\CreateCustomerRequest', $parameters);
123+
}
124+
125+
/**
126+
* @param array $parameters
127+
* @return Message\DeleteCustomerRequest
128+
*/
129+
public function deleteCustomer(array $parameters = array())
130+
{
131+
return $this->createRequest('\Omnipay\Braintree\Message\DeleteCustomerRequest', $parameters);
132+
}
133+
134+
/**
135+
* @param array $parameters
136+
* @return Message\UpdateCustomerRequest
137+
*/
138+
public function updateCustomer(array $parameters = array())
139+
{
140+
return $this->createRequest('\Omnipay\Braintree\Message\UpdateCustomerRequest', $parameters);
141+
}
142+
116143
/**
117144
* @param array $parameters
118145
* @return Message\PurchaseRequest

src/Message/AbstractRequest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ public function setCustomFields($value)
119119
return $this->setParameter('customFields', $value);
120120
}
121121

122+
public function getCustomerData()
123+
{
124+
return $this->getParameter('customerData');
125+
}
126+
127+
public function setCustomerData($value)
128+
{
129+
return $this->setParameter('customerData', $value);
130+
}
131+
122132
public function getCustomerId()
123133
{
124134
return $this->getParameter('customerId');
@@ -249,6 +259,16 @@ public function setPurchaseOrderNumber($value)
249259
return $this->setParameter('purchaseOrderNumber', $value);
250260
}
251261

262+
public function getOptions()
263+
{
264+
return $this->getParameter('options');
265+
}
266+
267+
public function setOptions($value)
268+
{
269+
return $this->setParameter('options', $value);
270+
}
271+
252272
public function getTaxAmount()
253273
{
254274
return $this->getParameter('taxAmount');

src/Message/ClientTokenRequest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ class ClientTokenRequest extends AbstractRequest
1212
{
1313
public function getData()
1414
{
15-
// TODO; customer data?
16-
return [];
15+
$data = [];
16+
if ($customerId = $this->getCustomerId()) {
17+
$data['customerId'] = $customerId;
18+
}
19+
if ($options = $this->getOptions()) {
20+
$data['options'] = $options;
21+
}
22+
23+
return $data;
1724
}
1825

1926
/**

src/Message/CreateCustomerRequest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace Omnipay\Braintree\Message;
3+
4+
use Omnipay\Common\Message\ResponseInterface;
5+
6+
/**
7+
* Authorize Request
8+
*
9+
* @method CreateCustomerRequest send()
10+
*/
11+
class CreateCustomerRequest extends AbstractRequest
12+
{
13+
public function getData()
14+
{
15+
return $this->getCustomerData();
16+
}
17+
18+
/**
19+
* Send the request with specified data
20+
*
21+
* @param mixed $data The data to send
22+
* @return ResponseInterface
23+
*/
24+
public function sendData($data)
25+
{
26+
$response = $this->braintree->customer()->create($data);
27+
28+
return $this->response = new CustomerResponse($this, $response);
29+
}
30+
}

src/Message/CustomerResponse.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Omnipay\Braintree\Message;
4+
5+
use Omnipay\Common\Message\RequestInterface;
6+
7+
/**
8+
* Response
9+
*/
10+
class CustomerResponse extends Response
11+
{
12+
public function getCustomerData()
13+
{
14+
return $this->data->customer;
15+
}
16+
}

src/Message/DeleteCustomerRequest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace Omnipay\Braintree\Message;
3+
4+
use Omnipay\Common\Message\ResponseInterface;
5+
6+
/**
7+
* Authorize Request
8+
*
9+
* @method DeleteCustomerRequest send()
10+
*/
11+
class DeleteCustomerRequest extends AbstractRequest
12+
{
13+
public function getData()
14+
{
15+
return $this->getCustomerId();
16+
}
17+
18+
/**
19+
* Send the request with specified data
20+
*
21+
* @param mixed $data The data to send
22+
* @return ResponseInterface
23+
*/
24+
public function sendData($data)
25+
{
26+
$response = $this->braintree->customer()->delete($data);
27+
28+
return $this->createResponse($response);
29+
}
30+
}

src/Message/UpdateCustomerRequest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
namespace Omnipay\Braintree\Message;
3+
4+
use Omnipay\Common\Message\ResponseInterface;
5+
6+
/**
7+
* Authorize Request
8+
*
9+
* @method UpdateCustomerRequest send()
10+
*/
11+
class UpdateCustomerRequest extends AbstractRequest
12+
{
13+
public function getData()
14+
{
15+
return array(
16+
'customerData' => $this->getCustomerData(),
17+
'customerId' => $this->getCustomerId(),
18+
);
19+
}
20+
21+
/**
22+
* Send the request with specified data
23+
*
24+
* @param mixed $data The data to send
25+
* @return ResponseInterface
26+
*/
27+
public function sendData($data)
28+
{
29+
$response = $this->braintree->customer()->update($data['customerId'], $data['customerData']);
30+
31+
return $this->response = new CustomerResponse($this, $response);
32+
}
33+
}

tests/GatewayTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ public function testCapture()
3838
$this->assertSame('10.00', $request->getAmount());
3939
}
4040

41+
public function testCreateCustomer()
42+
{
43+
$request = $this->gateway->createCustomer();
44+
$this->assertInstanceOf('Omnipay\Braintree\Message\CreateCustomerRequest', $request);
45+
}
46+
47+
public function testDeleteCustomer()
48+
{
49+
$request = $this->gateway->deleteCustomer();
50+
$this->assertInstanceOf('Omnipay\Braintree\Message\DeleteCustomerRequest', $request);
51+
}
52+
53+
public function testUpdateCustomer()
54+
{
55+
$request = $this->gateway->updateCustomer();
56+
$this->assertInstanceOf('Omnipay\Braintree\Message\UpdateCustomerRequest', $request);
57+
}
58+
4159
public function testPurchase()
4260
{
4361
$request = $this->gateway->purchase(array('amount' => '10.00'));

tests/Message/ClientTokenRequestTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class ClientTokenRequestTest extends TestCase
88
{
99
/**
10-
* @var PurchaseRequest
10+
* @var ClientTokenRequest
1111
*/
1212
private $request;
1313

@@ -16,13 +16,26 @@ public function setUp()
1616
parent::setUp();
1717

1818
$this->request = new ClientTokenRequest($this->getHttpClient(), $this->getHttpRequest(), \Braintree_Configuration::gateway());
19-
$this->request->initialize();
2019
}
2120

2221
public function testGetData()
2322
{
24-
$data = $this->request->getData();
23+
$this->request->initialize();
24+
$this->assertNull($this->request->getCustomerId());
25+
$this->assertEmpty($this->request->getData());
26+
}
2527

28+
public function testGetDataWithCustomer()
29+
{
30+
$data = array(
31+
'customerId' => '4815162342',
32+
'options' => array(
33+
'failOnDuplicatePaymentMethod' => true,
34+
),
35+
);
36+
$this->request->initialize($data);
37+
$this->assertSame('4815162342', $this->request->getCustomerId());
38+
$this->assertSame($data, $this->request->getData());
2639
}
2740

2841
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Omnipay\Braintree\Message;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class CreateCustomerRequestTest extends TestCase
8+
{
9+
/**
10+
* @var CreateCustomerRequest
11+
*/
12+
private $request;
13+
14+
public function setUp()
15+
{
16+
parent::setUp();
17+
18+
$this->request = new CreateCustomerRequest($this->getHttpClient(), $this->getHttpRequest(), \Braintree_Configuration::gateway());
19+
$this->request->initialize(
20+
array(
21+
'customerData' => array(
22+
'firstName' => 'Mike',
23+
'lastName' => 'Jones',
24+
'email' => '[email protected]',
25+
)
26+
)
27+
);
28+
}
29+
30+
public function testGetData()
31+
{
32+
$data = $this->request->getData();
33+
34+
$this->assertSame('Mike', $data['firstName']);
35+
$this->assertSame('Jones', $data['lastName']);
36+
$this->assertSame('[email protected]', $data['email']);
37+
}
38+
39+
public function testRequestData()
40+
{
41+
$this->assertNull($this->request->getCustomerId());
42+
$this->assertSame(
43+
array(
44+
'firstName' => 'Mike',
45+
'lastName' => 'Jones',
46+
'email' => '[email protected]',
47+
),
48+
$this->request->getCustomerData()
49+
);
50+
}
51+
}

0 commit comments

Comments
 (0)