Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new requests with tests #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/ConfigurationAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Omnipay\Braintree;

use Braintree\Gateway as BraintreeGateway;

trait ConfigurationAwareTrait
{
abstract public function getBraintree(): BraintreeGateway;
abstract public function getTestMode();
abstract public function getMerchantId();
abstract public function getPublicKey();
abstract public function getPrivateKey();

public function configure()
{
// When in testMode, use the sandbox environment
if ($this->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());
}
}
49 changes: 48 additions & 1 deletion src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
class Gateway extends AbstractGateway
{
use ConfigurationAwareTrait;

/**
* @var BraintreeGateway
*/
Expand Down Expand Up @@ -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
*
Expand All @@ -294,6 +334,8 @@ public function plans()
*/
public function parseNotification(array $parameters = [])
{
$this->configure();

return WebhookNotification::parse(
$parameters['bt_signature'],
$parameters['bt_payload']
Expand All @@ -309,4 +351,9 @@ public function fetchTransaction(array $parameters = [])
{
return $this->createRequest('\Omnipay\Braintree\Message\FindRequest', $parameters);
}

public function getBraintree(): BraintreeGateway
{
return $this->braintree;
}
}
23 changes: 8 additions & 15 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +14,8 @@
*/
abstract class AbstractRequest extends BaseAbstractRequest
{
use ConfigurationAwareTrait;

/**
* @var Gateway
*/
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -462,4 +450,9 @@ protected function createResponse($data)
{
return $this->response = new Response($this, $data);
}

public function getBraintree(): Gateway
{
return $this->braintree;
}
}
24 changes: 24 additions & 0 deletions src/Message/DiscountRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Omnipay\Braintree\Message;

class DiscountRequest extends AbstractRequest
{
/**
* @return null
*/
public function getData()
{
return null;
}

/**
* @param null $data
* @return DiscountResponse
*/
public function sendData($data = null)
{
$response = $this->braintree->discount()->all();
return $this->response = new DiscountResponse($this, $response);
}
}
23 changes: 23 additions & 0 deletions src/Message/DiscountResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* DiscountResponse class.
*/
namespace Omnipay\Braintree\Message;

class DiscountResponse extends Response
{
/**
* Returns array of Braintree\Discount objects with available discounts
* If there aren't any discounts created it will return empty array.
*
* @return array
*/
public function getDiscountsData()
{
if (isset($this->data)) {
return $this->data;
}

return [];
}
}
2 changes: 1 addition & 1 deletion src/Message/FindCustomerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FindCustomerRequest extends AbstractRequest
{
public function getData()
{
return $this->getCustomerData();
return null;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/Message/FindSubscriptionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Omnipay\Braintree\Message;

use Omnipay\Common\Message\ResponseInterface;

/**
* Find Subscription Request
* @method SubscriptionResponse send()
*/
class FindSubscriptionRequest extends AbstractRequest
{
/** @var string */
protected $subscriptionId;

public function getData()
{
return $this->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;
}
}
44 changes: 44 additions & 0 deletions src/Message/SearchRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Omnipay\Braintree\Message;

use Omnipay\Common\Message\ResponseInterface;

/**
* Search Transactions Request.
*
* @method Response send()
*/
class SearchRequest extends AbstractRequest
{
public function getData()
{
$this->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');
}
}
23 changes: 23 additions & 0 deletions src/Message/TransactionsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* TransactionsResponse class.
*/
namespace Omnipay\Braintree\Message;

class TransactionsResponse extends Response
{
/**
* Returns array of Braintree\Transaction objects with found transactions
* If there aren't any transactions found, it will return empty array.
*
* @return array
*/
public function getTransactionsData()
{
if (isset($this->data)) {
return $this->data;
}

return [];
}
}
52 changes: 52 additions & 0 deletions src/Message/UpdateSubscriptionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace Omnipay\Braintree\Message;

use Omnipay\Common\Message\ResponseInterface;

/**
* Authorize Request.
*
* @method CustomerResponse send()
*/
class UpdateSubscriptionRequest extends AbstractRequest
{
/** @var string */
protected $subscriptionId;

public function getData()
{
return [
'subscriptionData' => $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');
}
}
Loading