diff --git a/src/ConfigurationAwareTrait.php b/src/ConfigurationAwareTrait.php
new file mode 100644
index 0000000..b1f133f
--- /dev/null
+++ b/src/ConfigurationAwareTrait.php
@@ -0,0 +1,29 @@
+getTestMode()) {
+ $this->getBraintree()->config->environment('sandbox');
+ } else {
+ $this->getBraintree()->config->environment('production');
+ }
+
+ // Set the keys
+ $this->getBraintree()->config->merchantId($this->getMerchantId());
+ $this->getBraintree()->config->publicKey($this->getPublicKey());
+ $this->getBraintree()->config->privateKey($this->getPrivateKey());
+ }
+}
\ No newline at end of file
diff --git a/src/Gateway.php b/src/Gateway.php
index a52ae7d..1fdc18c 100644
--- a/src/Gateway.php
+++ b/src/Gateway.php
@@ -13,6 +13,8 @@
*/
class Gateway extends AbstractGateway
{
+ use ConfigurationAwareTrait;
+
/**
* @var BraintreeGateway
*/
@@ -278,13 +280,51 @@ public function cancelSubscription($subscriptionId)
}
/**
- * @return \Omnipay\Common\Message\PlansRequest
+ * @param string $subscriptionId
+ *
+ * @return \Omnipay\Common\Message\AbstractRequest
+ */
+ public function findSubscription($subscriptionId)
+ {
+ return $this->createRequest('\Omnipay\Braintree\Message\FindSubscriptionRequest', array('id' => $subscriptionId));
+ }
+
+ /**
+ * @param array $parameters
+ *
+ * @return Message\UpdateSubscriptionRequest
+ */
+ public function updateSubscription(array $parameters = [])
+ {
+ return $this->createRequest('\Omnipay\Braintree\Message\UpdateSubscriptionRequest', $parameters);
+ }
+
+ /**
+ * @return Message\PlanRequest
*/
public function plans()
{
return $this->createRequest('\Omnipay\Braintree\Message\PlanRequest', []);
}
+ /**
+ * @return Message\DiscountRequest
+ */
+ public function discounts()
+ {
+ return $this->createRequest('\Omnipay\Braintree\Message\DiscountRequest', []);
+ }
+
+ /**
+ * @param array $parameters
+ *
+ * @return Message\SearchRequest
+ */
+ public function searchTransactions(array $parameters = [])
+ {
+ return $this->createRequest('\Omnipay\Braintree\Message\SearchRequest', $parameters);
+ }
+
/**
* @param array $parameters
*
@@ -294,6 +334,8 @@ public function plans()
*/
public function parseNotification(array $parameters = [])
{
+ $this->configure();
+
return WebhookNotification::parse(
$parameters['bt_signature'],
$parameters['bt_payload']
@@ -309,4 +351,9 @@ public function fetchTransaction(array $parameters = [])
{
return $this->createRequest('\Omnipay\Braintree\Message\FindRequest', $parameters);
}
+
+ public function getBraintree(): BraintreeGateway
+ {
+ return $this->braintree;
+ }
}
diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php
index 7e946a1..dda2f80 100644
--- a/src/Message/AbstractRequest.php
+++ b/src/Message/AbstractRequest.php
@@ -3,6 +3,7 @@
namespace Omnipay\Braintree\Message;
use Braintree\Gateway;
+use Omnipay\Braintree\ConfigurationAwareTrait;
use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Http\ClientInterface;
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
@@ -13,6 +14,8 @@
*/
abstract class AbstractRequest extends BaseAbstractRequest
{
+ use ConfigurationAwareTrait;
+
/**
* @var Gateway
*/
@@ -44,21 +47,6 @@ public function send()
return parent::send();
}
- public function configure()
- {
- // When in testMode, use the sandbox environment
- if ($this->getTestMode()) {
- $this->braintree->config->environment('sandbox');
- } else {
- $this->braintree->config->environment('production');
- }
-
- // Set the keys
- $this->braintree->config->merchantId($this->getMerchantId());
- $this->braintree->config->publicKey($this->getPublicKey());
- $this->braintree->config->privateKey($this->getPrivateKey());
- }
-
public function getMerchantId()
{
return $this->getParameter('merchantId');
@@ -462,4 +450,9 @@ protected function createResponse($data)
{
return $this->response = new Response($this, $data);
}
+
+ public function getBraintree(): Gateway
+ {
+ return $this->braintree;
+ }
}
diff --git a/src/Message/DiscountRequest.php b/src/Message/DiscountRequest.php
new file mode 100644
index 0000000..08a25fb
--- /dev/null
+++ b/src/Message/DiscountRequest.php
@@ -0,0 +1,24 @@
+braintree->discount()->all();
+ return $this->response = new DiscountResponse($this, $response);
+ }
+}
\ No newline at end of file
diff --git a/src/Message/DiscountResponse.php b/src/Message/DiscountResponse.php
new file mode 100644
index 0000000..27a1d53
--- /dev/null
+++ b/src/Message/DiscountResponse.php
@@ -0,0 +1,23 @@
+data)) {
+ return $this->data;
+ }
+
+ return [];
+ }
+}
diff --git a/src/Message/FindCustomerRequest.php b/src/Message/FindCustomerRequest.php
index c0b6e8d..121daa5 100644
--- a/src/Message/FindCustomerRequest.php
+++ b/src/Message/FindCustomerRequest.php
@@ -10,7 +10,7 @@ class FindCustomerRequest extends AbstractRequest
{
public function getData()
{
- return $this->getCustomerData();
+ return null;
}
/**
diff --git a/src/Message/FindSubscriptionRequest.php b/src/Message/FindSubscriptionRequest.php
new file mode 100644
index 0000000..12206f0
--- /dev/null
+++ b/src/Message/FindSubscriptionRequest.php
@@ -0,0 +1,39 @@
+subscriptionId;
+ }
+
+ /**
+ * Send the request with specified data
+ *
+ * @param mixed $data
+ *
+ * @return SubscriptionResponse|ResponseInterface
+ */
+ public function sendData($subscriptionId)
+ {
+ $response = $this->braintree->subscription()->find($subscriptionId);
+
+ return $this->response = new SubscriptionResponse($this, $response);
+ }
+
+ public function setId($subscriptionId)
+ {
+ $this->subscriptionId = $subscriptionId;
+ }
+}
diff --git a/src/Message/SearchRequest.php b/src/Message/SearchRequest.php
new file mode 100644
index 0000000..df0eeb7
--- /dev/null
+++ b/src/Message/SearchRequest.php
@@ -0,0 +1,44 @@
+validate('searchQuery');
+
+ return $this->getSearchQuery();
+ }
+
+ /**
+ * Send the request with specified data.
+ *
+ * @param mixed $data The data to send
+ *
+ * @return ResponseInterface
+ */
+ public function sendData($data)
+ {
+ $response = $this->braintree->transaction()->search($data);
+
+ return $this->response = new TransactionsResponse($this, $response);
+ }
+
+ public function setSearchQuery($value)
+ {
+ return $this->setParameter('searchQuery', $value);
+ }
+
+ public function getSearchQuery()
+ {
+ return $this->getParameter('searchQuery');
+ }
+}
\ No newline at end of file
diff --git a/src/Message/TransactionsResponse.php b/src/Message/TransactionsResponse.php
new file mode 100644
index 0000000..e8f9455
--- /dev/null
+++ b/src/Message/TransactionsResponse.php
@@ -0,0 +1,23 @@
+data)) {
+ return $this->data;
+ }
+
+ return [];
+ }
+}
diff --git a/src/Message/UpdateSubscriptionRequest.php b/src/Message/UpdateSubscriptionRequest.php
new file mode 100644
index 0000000..dfa5099
--- /dev/null
+++ b/src/Message/UpdateSubscriptionRequest.php
@@ -0,0 +1,52 @@
+ $this->getSubscriptionData(),
+ 'subscriptionId' => $this->subscriptionId,
+ ];
+ }
+
+ /**
+ * Send the request with specified data.
+ *
+ * @param mixed $data The data to send
+ *
+ * @return ResponseInterface
+ */
+ public function sendData($data)
+ {
+ $response = $this->braintree->subscription()->update($data['subscriptionId'], $data['subscriptionData']);
+
+ return $this->response = new SubscriptionResponse($this, $response);
+ }
+
+ public function setId($subscriptionId)
+ {
+ $this->subscriptionId = $subscriptionId;
+ }
+
+ public function setSubscriptionData($value)
+ {
+ return $this->setParameter('subscriptionData', $value);
+ }
+
+ public function getSubscriptionData()
+ {
+ return $this->getParameter('subscriptionData');
+ }
+}
diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php
index 6c616c2..79d6375 100644
--- a/tests/GatewayTest.php
+++ b/tests/GatewayTest.php
@@ -147,33 +147,53 @@ public function testCancelSubscription()
$this->assertInstanceOf('Omnipay\Braintree\Message\CancelSubscriptionRequest', $request);
}
+ public function testFindSubscription()
+ {
+ $request = $this->gateway->findSubscription('1');
+ $this->assertInstanceOf('Omnipay\Braintree\Message\FindSubscriptionRequest', $request);
+ }
+
+ public function testUpdateSubscription()
+ {
+ $request = $this->gateway->updateSubscription();
+ $this->assertInstanceOf('Omnipay\Braintree\Message\UpdateSubscriptionRequest', $request);
+ }
+
+ public function testPlans()
+ {
+ $request = $this->gateway->plans();
+ $this->assertInstanceOf('Omnipay\Braintree\Message\PlanRequest', $request);
+ }
+
+ public function testDiscounts()
+ {
+ $request = $this->gateway->discounts();
+ $this->assertInstanceOf('Omnipay\Braintree\Message\DiscountRequest', $request);
+ }
+
+ public function testSearchTransactions()
+ {
+ $request = $this->gateway->searchTransactions();
+ $this->assertInstanceOf('Omnipay\Braintree\Message\SearchRequest', $request);
+ }
+
public function testParseNotification()
{
- if(Version::MAJOR >= 3) {
- $xml = '';
- $payload = base64_encode($xml);
- $signature = Digest::hexDigestSha1(Configuration::privateKey(), $payload);
- $gatewayMock = $this->buildGatewayMock($payload);
- $gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest(), $gatewayMock);
- $params = array(
- 'bt_signature' => $payload.'|'.$signature,
- 'bt_payload' => $payload
- );
- $request = $gateway->parseNotification($params);
- $this->assertInstanceOf('\Braintree\WebhookNotification', $request);
+ if (Version::MAJOR >= 3) {
+ $xml = '';
} else {
$xml = '';
- $payload = base64_encode($xml);
- $signature = Digest::hexDigestSha1(Configuration::privateKey(), $payload);
- $gatewayMock = $this->buildGatewayMock($payload);
- $gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest(), $gatewayMock);
- $params = array(
- 'bt_signature' => $payload.'|'.$signature,
- 'bt_payload' => $payload
- );
- $request = $gateway->parseNotification($params);
- $this->assertInstanceOf('\Braintree\WebhookNotification', $request);
}
+ $payload = base64_encode($xml);
+ $signature = Digest::hexDigestSha1(Configuration::privateKey(), $payload);
+ $gatewayMock = $this->buildGatewayMock($payload);
+ $gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest(), $gatewayMock);
+ $params = array(
+ 'bt_signature' => $payload.'|'.$signature,
+ 'bt_payload' => $payload
+ );
+ $request = $gateway->parseNotification($params);
+ $this->assertInstanceOf('\Braintree\WebhookNotification', $request);
}
/**
diff --git a/tests/Message/DiscountRequestTest.php b/tests/Message/DiscountRequestTest.php
new file mode 100644
index 0000000..e832029
--- /dev/null
+++ b/tests/Message/DiscountRequestTest.php
@@ -0,0 +1,60 @@
+buildMockGateway();
+ $this->request = new DiscountRequest($this->getHttpClient(), $this->getHttpRequest(), $gateway);
+ $this->request->initialize([]);
+ }
+
+ public function testGetData()
+ {
+ $data = $this->request->getData();
+ $this->assertNull($data);
+ }
+
+ public function testSendData()
+ {
+ $data = [];
+ $response = $this->request->sendData($data);
+
+ $this->assertInstanceOf('Omnipay\BrainTree\Message\DiscountResponse', $response);
+ }
+
+ protected function buildMockGateway()
+ {
+ $gateway = $this->getMockBuilder('\Braintree\Gateway')
+ ->disableOriginalConstructor()
+ ->setMethods([
+ 'discount'
+ ])
+ ->getMock();
+
+ $discount = $this->getMockBuilder('\Braintree\DiscountGateway')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $gateway->expects($this->any())
+ ->method('discount')
+ ->will($this->returnValue($discount));
+
+ return $gateway;
+ }
+}
\ No newline at end of file
diff --git a/tests/Message/DiscountResponseTest.php b/tests/Message/DiscountResponseTest.php
new file mode 100644
index 0000000..9cd89d3
--- /dev/null
+++ b/tests/Message/DiscountResponseTest.php
@@ -0,0 +1,41 @@
+request = new DiscountRequest(
+ $this->getHttpClient(), $this->getHttpRequest(), Configuration::gateway()
+ );
+ }
+
+ public function testGetDiscountsData()
+ {
+ $data = null;
+
+ $response = new DiscountResponse($this->request, $data);
+ $this->assertTrue(is_array($response->getDiscountsData()));
+ $this->assertTrue(count($response->getDiscountsData()) === 0);
+
+ $data = "discountData";
+
+ $response = new DiscountResponse($this->request, $data);
+ $this->assertEquals('discountData', $response->getDiscountsData());
+ }
+}
\ No newline at end of file
diff --git a/tests/Message/FindSubscriptionRequestTest.php b/tests/Message/FindSubscriptionRequestTest.php
new file mode 100644
index 0000000..7ef70ba
--- /dev/null
+++ b/tests/Message/FindSubscriptionRequestTest.php
@@ -0,0 +1,55 @@
+buildMockGateway();
+ $this->request = new FindSubscriptionRequest($this->getHttpClient(), $this->getHttpRequest(), $gateway);
+ $this->request->initialize(['id' => 1]);
+ }
+
+ public function testGetData()
+ {
+ $data = $this->request->getData();
+ $this->assertEquals(1, $data);
+ }
+
+ public function testSendData()
+ {
+ $data = 1;
+ $response = $this->request->sendData($data);
+
+ $this->assertInstanceOf('Omnipay\Braintree\Message\SubscriptionResponse', $response);
+ }
+
+ protected function buildMockGateway()
+ {
+ $gateway = $this->getMockBuilder('\Braintree\Gateway')
+ ->disableOriginalConstructor()
+ ->setMethods(array(
+ 'subscription'
+ ))
+ ->getMock();
+
+ $subscription = $this->getMockBuilder('\Braintree\SubscriptionGateway')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $gateway->expects($this->any())
+ ->method('subscription')
+ ->will($this->returnValue($subscription));
+
+ return $gateway;
+ }
+}
diff --git a/tests/Message/SearchRequestTest.php b/tests/Message/SearchRequestTest.php
new file mode 100644
index 0000000..2e04fec
--- /dev/null
+++ b/tests/Message/SearchRequestTest.php
@@ -0,0 +1,69 @@
+buildMockGateway();
+ $this->request = new SearchRequest($this->getHttpClient(), $this->getHttpRequest(), $gateway);
+ $this->request->initialize([
+ 'searchQuery' => [
+ TransactionSearch::customerId()->is(12345)
+ ]
+ ]);
+ }
+
+ public function testGetData()
+ {
+ $data = $this->request->getData();
+ $this->assertEquals(
+ [
+ TransactionSearch::customerId()->is(12345)
+ ],
+ $data
+ );
+ }
+
+ public function testSendData()
+ {
+ $response = $this->request->sendData($this->request->getData());
+
+ $this->assertInstanceOf('Omnipay\BrainTree\Message\TransactionsResponse', $response);
+ }
+
+ protected function buildMockGateway()
+ {
+ $gateway = $this->getMockBuilder('\Braintree\Gateway')
+ ->disableOriginalConstructor()
+ ->setMethods(array(
+ 'transaction'
+ ))
+ ->getMock();
+
+ $discount = $this->getMockBuilder('\Braintree\TransactionGateway')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $gateway->expects($this->any())
+ ->method('transaction')
+ ->will($this->returnValue($discount));
+
+ return $gateway;
+ }
+}
\ No newline at end of file
diff --git a/tests/Message/TransactionsResponseTest.php b/tests/Message/TransactionsResponseTest.php
new file mode 100644
index 0000000..f2da396
--- /dev/null
+++ b/tests/Message/TransactionsResponseTest.php
@@ -0,0 +1,41 @@
+request = new SearchRequest(
+ $this->getHttpClient(), $this->getHttpRequest(), Configuration::gateway()
+ );
+ }
+
+ public function testGetDiscountsData()
+ {
+ $data = null;
+
+ $response = new TransactionsResponse($this->request, $data);
+ $this->assertTrue(is_array($response->getTransactionsData()));
+ $this->assertTrue(count($response->getTransactionsData()) === 0);
+
+ $data = "transactionData";
+
+ $response = new TransactionsResponse($this->request, $data);
+ $this->assertEquals('transactionData', $response->getTransactionsData());
+ }
+}
\ No newline at end of file
diff --git a/tests/Message/UpdateSubscriptionRequestTest.php b/tests/Message/UpdateSubscriptionRequestTest.php
new file mode 100644
index 0000000..ffc2b70
--- /dev/null
+++ b/tests/Message/UpdateSubscriptionRequestTest.php
@@ -0,0 +1,44 @@
+request = new UpdateSubscriptionRequest($this->getHttpClient(), $this->getHttpRequest(), Configuration::gateway());
+ $this->request->initialize(
+ [
+ 'id' => '4815162342',
+ 'subscriptionData' => [
+ 'numberOfBillingCycles' => 3,
+ 'paymentMethodToken' => 'fake-token-123',
+ ],
+ ]
+ );
+ }
+
+ public function testGetData()
+ {
+ $data = $this->request->getData();
+
+ $this->assertSame('4815162342', $data['subscriptionId']);
+ $this->assertSame(
+ [
+ 'numberOfBillingCycles' => 3,
+ 'paymentMethodToken' => 'fake-token-123',
+ ],
+ $data['subscriptionData']
+ );
+ }
+}