Skip to content

Commit

Permalink
Add data types to support ARB
Browse files Browse the repository at this point in the history
ARB data types and request support.
  • Loading branch information
heddn authored and mglaman committed Aug 3, 2018
1 parent f7d62f8 commit 5e80027
Show file tree
Hide file tree
Showing 17 changed files with 584 additions and 6 deletions.
30 changes: 30 additions & 0 deletions src/ARBCancelSubscriptionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace CommerceGuys\AuthNet;

use GuzzleHttp\Client;
use CommerceGuys\AuthNet\DataTypes\Paging;
use CommerceGuys\AuthNet\DataTypes\Sorting;
use CommerceGuys\AuthNet\Request\RequestInterface;

/**
* Use this method to cancel subscriptions using Automated Recurring Billing.
*/
class ARBCancelSubscriptionRequest extends ARBSubscriptionRequest
{
protected $subscriptionId;

public function __construct(
Configuration $configuration,
Client $client,
$subscriptionId
) {
parent::__construct($configuration, $client);
$this->subscriptionId = $subscriptionId;
}

protected function attachData(RequestInterface $request)
{
$request->addData('subscriptionId', $this->subscriptionId);
}
}
39 changes: 39 additions & 0 deletions src/ARBCreateSubscriptionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace CommerceGuys\AuthNet;

use GuzzleHttp\Client;
use CommerceGuys\AuthNet\DataTypes\Subscription;
use CommerceGuys\AuthNet\Request\RequestInterface;

/**
* Use this method to create subscriptions using Automated Recurring Billing.
*/
class ARBCreateSubscriptionRequest extends ARBSubscriptionRequest
{
protected $subscription;

public function __construct(
Configuration $configuration,
Client $client,
Subscription $subscription = null
) {
parent::__construct($configuration, $client);
$this->subscription = $subscription;
}

/**
* @param \CommerceGuys\AuthNet\DataTypes\Subscription $subscription
* @return $this
*/
public function setSubscription(Subscription $subscription)
{
$this->subscription = $subscription;
return $this;
}

protected function attachData(RequestInterface $request)
{
$request->addDataType($this->subscription);
}
}
60 changes: 60 additions & 0 deletions src/ARBGetSubscriptionListRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace CommerceGuys\AuthNet;

use GuzzleHttp\Client;
use CommerceGuys\AuthNet\DataTypes\Paging;
use CommerceGuys\AuthNet\DataTypes\Sorting;
use CommerceGuys\AuthNet\Request\RequestInterface;

/**
* Use this method to get subscriptions using Automated Recurring Billing.
*/
class ARBGetSubscriptionListRequest extends ARBSubscriptionRequest
{
protected $searchType;

protected $sorting;

protected $paging;

public function __construct(
Configuration $configuration,
Client $client,
$searchType
) {
parent::__construct($configuration, $client);
$this->searchType = $searchType;
}

/**
* @param \CommerceGuys\AuthNet\DataTypes\Sorting $sorting
* @return $this
*/
public function setSorting(Sorting $sorting)
{
$this->sorting = $sorting;
return $this;
}

/**
* @param \CommerceGuys\AuthNet\DataTypes\Paging $paging
* @return $this
*/
public function setPaging(Paging $paging)
{
$this->paging = $paging;
return $this;
}

protected function attachData(RequestInterface $request)
{
$request->addData('searchType', $this->searchType);
if ($this->sorting) {
$request->addDataType($this->sorting);
}
if ($this->paging) {
$request->addDataType($this->paging);
}
}
}
34 changes: 34 additions & 0 deletions src/ARBGetSubscriptionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace CommerceGuys\AuthNet;

use GuzzleHttp\Client;
use CommerceGuys\AuthNet\DataTypes\Paging;
use CommerceGuys\AuthNet\DataTypes\Sorting;
use CommerceGuys\AuthNet\Request\RequestInterface;

/**
* Use this method to get a subscription using Automated Recurring Billing.
*/
class ARBGetSubscriptionRequest extends ARBSubscriptionRequest
{
protected $subscriptionId;
protected $includeTransactions;

public function __construct(
Configuration $configuration,
Client $client,
$subscriptionId,
$includeTransactions = false
) {
parent::__construct($configuration, $client);
$this->subscriptionId = $subscriptionId;
$this->includeTransactions = filter_var($includeTransactions, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
}

protected function attachData(RequestInterface $request)
{
$request->addData('subscriptionId', $this->subscriptionId);
$request->addData('includeTransactions', $this->includeTransactions);
}
}
30 changes: 30 additions & 0 deletions src/ARBGetSubscriptionStatusRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace CommerceGuys\AuthNet;

use GuzzleHttp\Client;
use CommerceGuys\AuthNet\DataTypes\Paging;
use CommerceGuys\AuthNet\DataTypes\Sorting;
use CommerceGuys\AuthNet\Request\RequestInterface;

/**
* Use this method to get a subscription using Automated Recurring Billing.
*/
class ARBGetSubscriptionStatusRequest extends ARBSubscriptionRequest
{
protected $subscriptionId;

public function __construct(
Configuration $configuration,
Client $client,
$subscriptionId
) {
parent::__construct($configuration, $client);
$this->subscriptionId = $subscriptionId;
}

protected function attachData(RequestInterface $request)
{
$request->addData('subscriptionId', $this->subscriptionId);
}
}
22 changes: 22 additions & 0 deletions src/ARBSubscriptionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace CommerceGuys\AuthNet;

use GuzzleHttp\Client;
use CommerceGuys\AuthNet\DataTypes\Paging;
use CommerceGuys\AuthNet\DataTypes\Sorting;
use CommerceGuys\AuthNet\Request\RequestInterface;

/**
* Use this method to create subscriptions using Automated Recurring Billing.
*
* @link https://developer.authorize.net/api/reference/#recurring-billing
*/
abstract class ARBSubscriptionRequest extends BaseApiRequest
{

public function getType()
{
return (new \ReflectionClass($this))->getShortName();
}
}
29 changes: 29 additions & 0 deletions src/ARBUpdateSubscriptionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace CommerceGuys\AuthNet;

use GuzzleHttp\Client;
use CommerceGuys\AuthNet\DataTypes\Subscription;
use CommerceGuys\AuthNet\Request\RequestInterface;

/**
* Use this method to update subscriptions using Automated Recurring Billing.
*/
class ARBUpdateSubscriptionRequest extends ARBCreateSubscriptionRequest
{
protected $subscriptionId;

public function __construct(
Configuration $configuration,
Client $client,
$subscriptionId
) {
parent::__construct($configuration, $client);
$this->subscriptionId = $subscription;
}

protected function attachData(RequestInterface $request)
{
$request->addData('subscriptionId', $this->subscriptionId);
}
}
17 changes: 17 additions & 0 deletions src/DataTypes/CustomerPaymentProfileBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace CommerceGuys\AuthNet\DataTypes;

class CustomerPaymentProfileBase extends BaseDataType
{

protected $propertyMap = [
'customerType',
'billTo',
];

public function addBillTo(BillTo $billTo)
{
$this->addDataType($billTo);
}
}
13 changes: 13 additions & 0 deletions src/DataTypes/CustomerProfileId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace CommerceGuys\AuthNet\DataTypes;

class CustomerProfileId extends BaseDataType
{

protected $propertyMap = [
'customerProfileId',
'customerPaymentProfileId',
'customerAddressId',
];
}
44 changes: 44 additions & 0 deletions src/DataTypes/Interval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace CommerceGuys\AuthNet\DataTypes;

class Interval extends BaseDataType
{
protected $propertyMap = [
'length',
'unit',
];

/**
* Allows child classes to validate incoming values.
*
* @param array $values
*/
protected function validate(array $values)
{
if (!isset($values['length'])) {
throw new \InvalidArgumentException('Interval must have a length.');
}
if (!isset($values['unit'])) {
throw new \InvalidArgumentException('Interval must have a unit.');
}
if (!array_intersect(['days', 'months'], (array) $values['unit'])) {
throw new \InvalidArgumentException('Interval unit must be days or months.');
}
switch ($values['unit']) {
case 'days':
if ($values['length'] < 7 || $values['length'] > 365) {
$message = 'Interval length for days must be between 7 and 365, inclusive.';
throw new \InvalidArgumentException($message);
}
break;

case 'months':
if ($values['length'] < 1 || $values['length'] > 12) {
$message = 'Interval length for months must be between 1 and 12, inclusive.';
throw new \InvalidArgumentException($message);
}
break;
}
}
}
12 changes: 12 additions & 0 deletions src/DataTypes/Paging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace CommerceGuys\AuthNet\DataTypes;

class Paging extends BaseDataType
{

protected $propertyMap = [
'limit',
'offset',
];
}
7 changes: 1 addition & 6 deletions src/DataTypes/PaymentProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CommerceGuys\AuthNet\DataTypes;

class PaymentProfile extends BaseDataType
class PaymentProfile extends CustomerPaymentProfileBase
{

protected $propertyMap = [
Expand All @@ -18,11 +18,6 @@ public function addPayment(PaymentMethodInterface $paymentMethod)
$this->properties['payment'][$paymentMethod->getType()] = $paymentMethod->toArray();
}

public function addBillTo(BillTo $billTo)
{
$this->addDataType($billTo);
}

public function addCustomerPaymentProfileId($id)
{
$this->properties['customerPaymentProfileId'] = $id;
Expand Down
18 changes: 18 additions & 0 deletions src/DataTypes/PaymentSchedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace CommerceGuys\AuthNet\DataTypes;

class PaymentSchedule extends BaseDataType
{
protected $propertyMap = [
'interval',
'startDate',
'totalOccurrences',
'trialOccurrences',
];

public function addInterval(Interval $interval)
{
$this->properties['interval'] = $interval->toArray();
}
}
Loading

0 comments on commit 5e80027

Please sign in to comment.