Skip to content

Commit 9315b8b

Browse files
committed
Use Braintree gateway instance instead of static methods
1 parent 65d39f3 commit 9315b8b

10 files changed

+69
-24
lines changed

src/Gateway.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,44 @@
33
namespace Omnipay\Braintree;
44

55
use Omnipay\Common\AbstractGateway;
6-
6+
use Braintree_Gateway;
7+
use Braintree_Configuration;
8+
use Guzzle\Http\ClientInterface;
9+
use Symfony\Component\HttpFoundation\Request as HttpRequest;
710
/**
811
* Braintree Gateway
912
*/
1013
class Gateway extends AbstractGateway
1114
{
15+
/**
16+
* @var \Braintree_Gateway
17+
*/
18+
protected $braintree;
19+
20+
/**
21+
* Create a new gateway instance
22+
*
23+
* @param ClientInterface $httpClient A Guzzle client to make API calls with
24+
* @param HttpRequest $httpRequest A Symfony HTTP request object
25+
* @param Braintree_Gateway $braintree The Braintree gateway
26+
*/
27+
public function __construct(ClientInterface $httpClient = null, HttpRequest $httpRequest = null, Braintree_Gateway $braintree = null)
28+
{
29+
$this->braintree = $braintree ?: Braintree_Configuration::gateway();
30+
31+
parent::__construct($httpClient, $httpRequest);
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
protected function createRequest($class, array $parameters)
38+
{
39+
$obj = new $class($this->httpClient, $this->httpRequest, $this->braintree);
40+
41+
return $obj->initialize(array_replace($this->getParameters(), $parameters));
42+
}
43+
1244
public function getName()
1345
{
1446
return 'Braintree';

src/Message/AbstractRequest.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Omnipay\Braintree\Message;
44

5-
use Braintree_Configuration;
5+
use Braintree_Gateway;
6+
use Guzzle\Http\ClientInterface;
7+
use Symfony\Component\HttpFoundation\Request as HttpRequest;
68
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
79

810
/**
@@ -11,6 +13,25 @@
1113
*/
1214
abstract class AbstractRequest extends BaseAbstractRequest
1315
{
16+
/**
17+
* @var \Braintree_Gateway
18+
*/
19+
protected $braintree;
20+
21+
/**
22+
* Create a new Request
23+
*
24+
* @param ClientInterface $httpClient A Guzzle client to make API calls with
25+
* @param HttpRequest $httpRequest A Symfony HTTP request object
26+
* @param Braintree_Gateway $braintree The Braintree Gateway
27+
*/
28+
public function __construct(ClientInterface $httpClient, HttpRequest $httpRequest, Braintree_Gateway $braintree)
29+
{
30+
$this->braintree = $braintree;
31+
32+
parent::__construct($httpClient, $httpRequest);
33+
}
34+
1435
/**
1536
* Set the correct configuration sending
1637
*
@@ -25,21 +46,19 @@ public function send()
2546

2647
public function configure()
2748
{
28-
// Reset to the initial state
29-
Braintree_Configuration::reset();
30-
3149
// When in testMode, use the sandbox environment
3250
if ($this->getTestMode()) {
33-
Braintree_Configuration::environment('sandbox');
51+
$this->braintree->config->environment('sandbox');
3452
} else {
35-
Braintree_Configuration::environment('production');
53+
$this->braintree->config->environment('production');
3654
}
3755

3856
// Set the keys
39-
Braintree_Configuration::merchantId($this->getMerchantId());
40-
Braintree_Configuration::publicKey($this->getPublicKey());
41-
Braintree_Configuration::privateKey($this->getPrivateKey());
57+
$this->braintree->config->merchantId($this->getMerchantId());
58+
$this->braintree->config->publicKey($this->getPublicKey());
59+
$this->braintree->config->privateKey($this->getPrivateKey());
4260
}
61+
4362
public function getMerchantId()
4463
{
4564
return $this->getParameter('merchantId');

src/Message/AuthorizeRequest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Omnipay\Braintree\Message;
33

4-
use Braintree_Transaction;
54
use Omnipay\Common\Message\ResponseInterface;
65

76
/**
@@ -60,7 +59,7 @@ public function getData()
6059
*/
6160
public function sendData($data)
6261
{
63-
$response = Braintree_Transaction::sale($data);
62+
$response = $this->braintree->transaction()->sale($data);
6463

6564
return $this->createResponse($response);
6665
}

src/Message/CaptureRequest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Omnipay\Braintree\Message;
33

4-
use Braintree_Transaction;
54
use Omnipay\Common\Message\ResponseInterface;
65

76
/**
@@ -29,7 +28,7 @@ public function getData()
2928
*/
3029
public function sendData($data)
3130
{
32-
$response = Braintree_Transaction::submitForSettlement($data['transactionReference'], $data['amount']);
31+
$response = $this->braintree->transaction()->submitForSettlement($data['transactionReference'], $data['amount']);
3332

3433
return $this->createResponse($response);
3534
}

src/Message/ClientTokenRequest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Omnipay\Braintree\Message;
33

4-
use Braintree_ClientToken;
54
use Omnipay\Common\Message\ResponseInterface;
65

76
/**
@@ -25,7 +24,7 @@ public function getData()
2524
*/
2625
public function sendData($data)
2726
{
28-
$token = Braintree_ClientToken::generate($data);
27+
$token = $this->braintree->clientToken()->generate($data);
2928

3029
return new ClientTokenResponse($this, $token);
3130
}

src/Message/FindRequest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Omnipay\Braintree\Message;
33

4-
use Braintree_Transaction;
54
use Omnipay\Common\Message\ResponseInterface;
65

76
/**
@@ -28,7 +27,7 @@ public function getData()
2827
*/
2928
public function sendData($data)
3029
{
31-
$response = Braintree_Transaction::find($data['transactionReference']);
30+
$response = $this->braintree->transaction()->find($data['transactionReference']);
3231

3332
return $this->createResponse($response);
3433
}

src/Message/RefundRequest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Omnipay\Braintree\Message;
33

4-
use Braintree_Transaction;
54
use Omnipay\Common\Message\ResponseInterface;
65

76
/**
@@ -29,7 +28,7 @@ public function getData()
2928
*/
3029
public function sendData($data)
3130
{
32-
$response = Braintree_Transaction::void($data['transactionReference'], $data['amount']);
31+
$response = $this->braintree->transaction()->refund($data['transactionReference'], $data['amount']);
3332

3433
return $this->createResponse($response);
3534
}

src/Message/VoidRequest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Omnipay\Braintree\Message;
33

4-
use Braintree_Transaction;
54
use Omnipay\Common\Message\ResponseInterface;
65

76
/**
@@ -28,7 +27,7 @@ public function getData()
2827
*/
2928
public function sendData($data)
3029
{
31-
$response = Braintree_Transaction::void($data['transactionReference']);
30+
$response = $this->braintree->transaction()->void($data['transactionReference']);
3231

3332
return $this->createResponse($response);
3433
}

tests/Message/AuthorizeRequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function setUp()
1515
{
1616
parent::setUp();
1717

18-
$this->request = new AuthorizeRequest($this->getHttpClient(), $this->getHttpRequest());
18+
$this->request = new AuthorizeRequest($this->getHttpClient(), $this->getHttpRequest(), \Braintree_Configuration::gateway());
1919
$this->request->initialize(
2020
array(
2121
'amount' => '10.00',

tests/Message/PurchaseRequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function setUp()
1515
{
1616
parent::setUp();
1717

18-
$this->request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest());
18+
$this->request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest(), \Braintree_Configuration::gateway());
1919
$this->request->initialize(
2020
array(
2121
'amount' => '10.00',

0 commit comments

Comments
 (0)