Skip to content

Commit 86b34dd

Browse files
author
iampersistent
committed
add payment methods
1 parent 71eeca0 commit 86b34dd

10 files changed

+313
-5
lines changed

src/Gateway.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,33 @@ public function find(array $parameters = array())
149149
return $this->createRequest('\Omnipay\Braintree\Message\FindRequest', $parameters);
150150
}
151151

152+
/**
153+
* @param array $parameters
154+
* @return Message\CreatePaymentMethodRequest
155+
*/
156+
public function createPaymentMethod(array $parameters = array())
157+
{
158+
return $this->createRequest('\Omnipay\Braintree\Message\CreatePaymentMethodRequest', $parameters);
159+
}
160+
161+
/**
162+
* @param array $parameters
163+
* @return Message\DeletePaymentMethodRequest
164+
*/
165+
public function deletePaymentMethod(array $parameters = array())
166+
{
167+
return $this->createRequest('\Omnipay\Braintree\Message\DeletePaymentMethodRequest', $parameters);
168+
}
169+
170+
/**
171+
* @param array $parameters
172+
* @return Message\UpdatePaymentMethodRequest
173+
*/
174+
public function updatePaymentMethod(array $parameters = array())
175+
{
176+
return $this->createRequest('\Omnipay\Braintree\Message\UpdatePaymentMethodRequest', $parameters);
177+
}
178+
152179
/**
153180
* @param array $parameters
154181
* @return Message\PurchaseRequest

src/Message/AbstractRequest.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,36 @@ public function setFailOnDuplicatePaymentMethod($value)
309309
return $this->setParameter('failOnDuplicatePaymentMethod', (bool) $value);
310310
}
311311

312+
public function getMakeDefault()
313+
{
314+
return $this->getParameter('makeDefault');
315+
}
316+
317+
public function setMakeDefault($value)
318+
{
319+
return $this->setParameter('makeDefault', (bool) $value);
320+
}
321+
322+
public function getVerifyCard()
323+
{
324+
return $this->getParameter('verifyCard');
325+
}
326+
327+
public function setVerifyCard($value)
328+
{
329+
return $this->setParameter('verifyCard', (bool) $value);
330+
}
331+
332+
public function getVerificationMerchantAccountId()
333+
{
334+
return $this->getParameter('verificationMerchantAccountId');
335+
}
336+
337+
public function setVerificationMerchantAccountId($value)
338+
{
339+
return $this->setParameter('verificationMerchantAccountId', $value);
340+
}
341+
312342
/**
313343
* @return array
314344
*/
@@ -353,11 +383,14 @@ public function getOptionData()
353383
{
354384
$data = array(
355385
'addBillingAddressToPaymentMethod' => $this->getAddBillingAddressToPaymentMethod(),
356-
'failOnDuplicatePaymentMethod' => $this->getFailOnDuplicatePaymentMethod(),
357-
'holdInEscrow' => $this->getHoldInEscrow(),
358-
'storeInVault' => $this->getStoreInVault(),
359-
'storeInVaultOnSuccess' => $this->getStoreInVaultOnSuccess(),
360-
'storeShippingAddressInVault' => $this->getStoreShippingAddressInVault(),
386+
'failOnDuplicatePaymentMethod' => $this->getFailOnDuplicatePaymentMethod(),
387+
'holdInEscrow' => $this->getHoldInEscrow(),
388+
'makeDefault' => $this->getMakeDefault(),
389+
'storeInVault' => $this->getStoreInVault(),
390+
'storeInVaultOnSuccess' => $this->getStoreInVaultOnSuccess(),
391+
'storeShippingAddressInVault' => $this->getStoreShippingAddressInVault(),
392+
'verifyCard' => $this->getVerifyCard(),
393+
'verificationMerchantAccountId' => $this->getVerificationMerchantAccountId(),
361394
);
362395

363396
// Remove null values
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Omnipay\Braintree\Message;
4+
5+
use Omnipay\Common\Message\ResponseInterface;
6+
7+
/**
8+
* Create PaymentMethod Request
9+
*
10+
* @method Response send()
11+
*/
12+
class CreatePaymentMethodRequest extends AbstractRequest
13+
{
14+
public function getData()
15+
{
16+
$data = array(
17+
'customerId' => $this->getCustomerId(),
18+
'paymentMethodNonce' => $this->getToken(),
19+
);
20+
$data += $this->getOptionData();
21+
22+
return $data;
23+
}
24+
25+
/**
26+
* Send the request with specified data
27+
*
28+
* @param mixed $data The data to send
29+
* @return ResponseInterface
30+
*/
31+
public function sendData($data)
32+
{
33+
$response = $this->braintree->paymentMethod()->create($data);
34+
35+
return $this->createResponse($response);
36+
}
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Omnipay\Braintree\Message;
4+
5+
use Omnipay\Common\Message\ResponseInterface;
6+
7+
/**
8+
* Delete PaymentMethod Request
9+
*
10+
* @method Response send()
11+
*/
12+
class DeletePaymentMethodRequest extends AbstractRequest
13+
{
14+
public function getData()
15+
{
16+
return $this->getToken();
17+
}
18+
19+
/**
20+
* Send the request with specified data
21+
*
22+
* @param mixed $data The data to send
23+
* @return ResponseInterface
24+
*/
25+
public function sendData($data)
26+
{
27+
$response = $this->braintree->paymentMethod()->delete($data);
28+
29+
return $this->createResponse($response);
30+
}
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Omnipay\Braintree\Message;
4+
5+
use Omnipay\Common\Message\ResponseInterface;
6+
7+
/**
8+
* Update PaymentMethod Request
9+
*
10+
* @method Response send()
11+
*/
12+
class UpdatePaymentMethodRequest extends AbstractRequest
13+
{
14+
public function getData()
15+
{
16+
$parameters = array();
17+
$parameters += $this->getOptionData();
18+
19+
$data['token'] = $this->getToken();
20+
if (!empty($parameters)) {
21+
$data['parameters'] = $parameters;
22+
}
23+
24+
return $data;
25+
}
26+
27+
/**
28+
* Send the request with specified data
29+
*
30+
* @param mixed $data The data to send
31+
* @return ResponseInterface
32+
*/
33+
public function sendData($data)
34+
{
35+
$response = $this->braintree->paymentMethod()->update($data['token'], $data['parameters']);
36+
37+
return $this->createResponse($response);
38+
}
39+
}

tests/GatewayTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ public function testUpdateCustomer()
5656
$this->assertInstanceOf('Omnipay\Braintree\Message\UpdateCustomerRequest', $request);
5757
}
5858

59+
public function testCreatePaymentMethod()
60+
{
61+
$request = $this->gateway->createPaymentMethod();
62+
$this->assertInstanceOf('Omnipay\Braintree\Message\CreatePaymentMethodRequest', $request);
63+
}
64+
65+
public function testDeletePaymentMethod()
66+
{
67+
$request = $this->gateway->deletePaymentMethod();
68+
$this->assertInstanceOf('Omnipay\Braintree\Message\DeletePaymentMethodRequest', $request);
69+
}
70+
71+
public function testUpdatePaymentMethod()
72+
{
73+
$request = $this->gateway->updatePaymentMethod();
74+
$this->assertInstanceOf('Omnipay\Braintree\Message\UpdatePaymentMethodRequest', $request);
75+
}
76+
5977
public function testPurchase()
6078
{
6179
$request = $this->gateway->purchase(array('amount' => '10.00'));

tests/Message/AbstractRequestTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,26 @@ public function testOptionData()
131131
{
132132
$options = [
133133
'addBillingAddressToPaymentMethod' => false,
134+
'makeDefault' => true,
134135
'failOnDuplicatePaymentMethod' => true,
135136
'holdInEscrow' => false,
136137
'storeInVault' => true,
137138
'storeInVaultOnSuccess' => false,
138139
'storeShippingAddressInVault' => true,
140+
'verifyCard' => false,
141+
'verificationMerchantAccountId' => true,
139142
];
140143
$this->request->initialize($options);
141144
$data = $this->request->getOptionData();
142145

143146
$this->assertSame($options['addBillingAddressToPaymentMethod'], $data['options']['addBillingAddressToPaymentMethod']);
147+
$this->assertSame($options['makeDefault'], $data['options']['makeDefault']);
144148
$this->assertSame($options['failOnDuplicatePaymentMethod'], $data['options']['failOnDuplicatePaymentMethod']);
145149
$this->assertSame($options['holdInEscrow'], $data['options']['holdInEscrow']);
146150
$this->assertSame($options['storeInVault'], $data['options']['storeInVault']);
147151
$this->assertSame($options['storeInVaultOnSuccess'], $data['options']['storeInVaultOnSuccess']);
148152
$this->assertSame($options['storeShippingAddressInVault'], $data['options']['storeShippingAddressInVault']);
153+
$this->assertSame($options['verifyCard'], $data['options']['verifyCard']);
154+
$this->assertSame($options['verificationMerchantAccountId'], $data['options']['verificationMerchantAccountId']);
149155
}
150156
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Omnipay\Braintree\Message;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class CreatePaymentMethodRequestTest extends TestCase
8+
{
9+
/**
10+
* @var CreatePaymentMethodRequest
11+
*/
12+
private $request;
13+
14+
public function setUp()
15+
{
16+
$this->request = new CreatePaymentMethodRequest($this->getHttpClient(), $this->getHttpRequest(), \Braintree_Configuration::gateway());
17+
$this->request->initialize(
18+
array(
19+
'customerId' => '4815162342',
20+
'token' => 'abc123',
21+
'verifyCard' => true,
22+
'verificationMerchantAccountId' => '123581321',
23+
)
24+
);
25+
}
26+
27+
public function testGetData()
28+
{
29+
$expectedData = array(
30+
'customerId' => '4815162342',
31+
'paymentMethodNonce' => 'abc123',
32+
'options' => array(
33+
'verifyCard' => true,
34+
'verificationMerchantAccountId' => '123581321',
35+
)
36+
);
37+
$this->assertSame($expectedData, $this->request->getData());
38+
}
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Omnipay\Braintree\Message;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class DeletePaymentMethodRequestTest extends TestCase
8+
{
9+
/**
10+
* @var DeletePaymentMethodRequest
11+
*/
12+
private $request;
13+
14+
public function setUp()
15+
{
16+
$this->request = new DeletePaymentMethodRequest($this->getHttpClient(), $this->getHttpRequest(), \Braintree_Configuration::gateway());
17+
$this->request->initialize(
18+
array(
19+
'token' => 'abcd1234',
20+
)
21+
);
22+
}
23+
24+
public function testGetData()
25+
{
26+
$this->assertSame('abcd1234', $this->request->getData());
27+
}
28+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Omnipay\Braintree\Message;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class UpdatePaymentMethodRequestTest extends TestCase
8+
{
9+
/**
10+
* @var UpdatePaymentMethodRequest
11+
*/
12+
private $request;
13+
14+
public function setUp()
15+
{
16+
$this->request = new UpdatePaymentMethodRequest($this->getHttpClient(), $this->getHttpRequest(), \Braintree_Configuration::gateway());
17+
}
18+
19+
public function testGetData()
20+
{
21+
$this->request->initialize(
22+
array(
23+
'token' => 'abcd1234',
24+
'makeDefault' => true,
25+
)
26+
);
27+
$expected = array(
28+
'token' => 'abcd1234',
29+
'parameters' => array(
30+
'options' => array(
31+
'makeDefault' => true,
32+
),
33+
),
34+
);
35+
$this->assertSame($expected, $this->request->getData());
36+
}
37+
38+
public function testGetDataNoParameters()
39+
{
40+
$this->request->initialize(
41+
array(
42+
'token' => 'abcd1234',
43+
)
44+
);
45+
$expected = array(
46+
'token' => 'abcd1234',
47+
);
48+
$this->assertSame($expected, $this->request->getData());
49+
}
50+
}

0 commit comments

Comments
 (0)