From c3714e8974f03b91813faf2aa87b9463019ef186 Mon Sep 17 00:00:00 2001 From: Matej Grula Date: Wed, 14 Sep 2022 14:44:24 +0200 Subject: [PATCH 1/2] Added new requests with tests: updateSubscription, findSubscription, searchTransactions and discounts Some small fixes --- src/Gateway.php | 40 ++++++++++- src/Message/DiscountRequest.php | 24 +++++++ src/Message/DiscountResponse.php | 23 +++++++ src/Message/FindCustomerRequest.php | 2 +- src/Message/FindSubscriptionRequest.php | 39 +++++++++++ src/Message/SearchRequest.php | 44 ++++++++++++ src/Message/TransactionsResponse.php | 23 +++++++ src/Message/UpdateSubscriptionRequest.php | 52 ++++++++++++++ tests/GatewayTest.php | 30 ++++++++ tests/Message/DiscountRequestTest.php | 60 ++++++++++++++++ tests/Message/DiscountResponseTest.php | 41 +++++++++++ tests/Message/FindSubscriptionRequestTest.php | 55 +++++++++++++++ tests/Message/SearchRequestTest.php | 69 +++++++++++++++++++ tests/Message/TransactionsResponseTest.php | 41 +++++++++++ .../Message/UpdateSubscriptionRequestTest.php | 44 ++++++++++++ 15 files changed, 585 insertions(+), 2 deletions(-) create mode 100644 src/Message/DiscountRequest.php create mode 100644 src/Message/DiscountResponse.php create mode 100644 src/Message/FindSubscriptionRequest.php create mode 100644 src/Message/SearchRequest.php create mode 100644 src/Message/TransactionsResponse.php create mode 100644 src/Message/UpdateSubscriptionRequest.php create mode 100644 tests/Message/DiscountRequestTest.php create mode 100644 tests/Message/DiscountResponseTest.php create mode 100644 tests/Message/FindSubscriptionRequestTest.php create mode 100644 tests/Message/SearchRequestTest.php create mode 100644 tests/Message/TransactionsResponseTest.php create mode 100644 tests/Message/UpdateSubscriptionRequestTest.php diff --git a/src/Gateway.php b/src/Gateway.php index a52ae7d..54b1285 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -278,13 +278,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 * 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..6263bdc 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -147,6 +147,36 @@ 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) { 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'] + ); + } +} From d17e0ca677149a81e4a5b1f6b32b21b674624406 Mon Sep 17 00:00:00 2001 From: Matej Grula Date: Fri, 16 Sep 2022 09:26:53 +0200 Subject: [PATCH 2/2] Moved Braintree configure to a new trait and calling it before Webhook notification parsing. Fixed and refactored test --- src/ConfigurationAwareTrait.php | 29 ++++++++++++++++++++++++++++ src/Gateway.php | 9 +++++++++ src/Message/AbstractRequest.php | 23 ++++++++-------------- tests/GatewayTest.php | 34 ++++++++++++--------------------- 4 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 src/ConfigurationAwareTrait.php 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 54b1285..1fdc18c 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -13,6 +13,8 @@ */ class Gateway extends AbstractGateway { + use ConfigurationAwareTrait; + /** * @var BraintreeGateway */ @@ -332,6 +334,8 @@ public function searchTransactions(array $parameters = []) */ public function parseNotification(array $parameters = []) { + $this->configure(); + return WebhookNotification::parse( $parameters['bt_signature'], $parameters['bt_payload'] @@ -347,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/tests/GatewayTest.php b/tests/GatewayTest.php index 6263bdc..79d6375 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -179,31 +179,21 @@ public function testSearchTransactions() 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); } /**