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

Fix missed method PUT and DELETE #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
176 changes: 56 additions & 120 deletions src/Tco/Source/Api/Rest/V6/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,144 +2,80 @@

namespace Tco\Source\Api\Rest\V6;

use Tco\Interfaces\Auth;
use Tco\Source\TcoConfig;
use Tco\Exceptions\TcoException;

class Subscription {

private $core;
private $acceptedSearchSubscriptionParams = array(
'SubscriptionReference',
'CustomerEmail',
'DeliveredCode',
'AvangateCustomerReference',
'ExternalCustomerReference',
'Aggregate',
'SubscriptionEnabled',
'RecurringEnabled',
'ProductCodes',
'CountryCodes',
'PurchasedAfter',
'PurchasedBefore',
'ExpireAfter',
'ExpireBefore',
'RenewedAfter',
'RenewedBefore',
'NotificationAfter',
'NotificationBefore',
'ModifiedAfter',
'ModifiedBefore',
'NextBillingDateAfter',
'NextBillingDateBefore',
'LifetimeSubscription',
'ModifiedAfter',
'MerchantCode'
);
class ApiCore {

public function __construct( $apiCore ) {
$this->core = $apiCore;
}
/**
* @var TcoConfig
*/
public $tcoConfig;

public function validateSubscriptionSearchParams( $searchParams ) {
$notAccepted = array();
foreach ( $searchParams as $k => $v ) {
if ( ! array_key_exists( $k, array_flip( $this->acceptedSearchSubscriptionParams ) ) ) {
$notAccepted[] = $k;
}
}
/**
* @var Auth
*/
public $auth;

return $notAccepted;
/**
* ApiCore constructor.
*
* @param TcoConfig $tcoConfig
* @param Auth $auth
*/
public function __construct( $tcoConfig, $auth ) {
$this->tcoConfig = $tcoConfig;
$this->auth = $auth;
}

/**
* @returns array with keys (LineItemReference) && values (array of SubscriptionReferences)
* or empty array
* @param $endpoint
* @param $params
* @param string $method
* @param string $apiLocation
*
* @return mixed
* @throws \TcoException
*/
public function getSubscriptionsByOrderRefNo( $orderRefNo ) {
$order = new Order( $this->core );
$orderData = null;
try {
$orderData = $order->getOrder( array( 'RefNo' => $orderRefNo ) );
} catch ( TcoException $exception ) {
throw new TcoException( sprintf( 'Exception getting subscriptions by order RefNo %s',
$exception->getMessage() ) );
}
$items = $orderData["Items"];
$subscriptionsRefNoArray = array();
public function call( $endpoint, $params, $method = 'POST', $apiLocation = 'restApi' ) {

foreach ( $items as $k => $productData ) {
if ( isset( $productData['ProductDetails']['Subscriptions'][0] ) ) {
$productSubscriptions = $productData['ProductDetails']['Subscriptions'];
$lineItemReference = $items[ $k ]['LineItemReference'];
foreach ( $productSubscriptions as $k => $subscription ) {
$subscriptionsRefNoArray[ $lineItemReference ][] = $subscription['SubscriptionReference'];
}
try {
$url = $this->tcoConfig->getApiEndpoints()[ $apiLocation ] . $endpoint;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $this->auth->getHeaders() );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HEADER, false );

if ( ! $this->tcoConfig->getCurlVerifySsl() ) {
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false ); //by default value is 2
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); //by default value is 1
}
}

return $subscriptionsRefNoArray;

}

public function searchSubscriptions( $searchParams ) {
$rejectedParams = $this->validateSubscriptionSearchParams( $searchParams );
if ( count( $rejectedParams ) == 0 ) {
try {
$search = '/subscriptions/';
//If subscriptionReferenceId then we only search by it
if ( isset( $searchParams['SubscriptionReference'] ) ) {
$search .= $searchParams['SubscriptionReference'] . '/';
unset( $searchParams );
}

//Else do search for multiple orders using the mix of parameters.
if ( ! empty( $searchParams ) ) {
$search .= '?' . http_build_query( $searchParams );
}

return $this->core->call( $search, [], 'GET' );
} catch ( TcoException $exception ) {
throw new TcoException( sprintf( 'Error when trying to search for subscription: %s',
$exception->getMessage() ) );
if ( $method === 'POST' ) {
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $params, JSON_UNESCAPED_UNICODE ) );
}
} else {
throw new TcoException( sprintf( 'Exception! Some subscription search parameters are not accepted: %s',
implode( ', ', $rejectedParams ) ) );
}
}

public function updateSubscriptions( $updatedSubscriptionParams ) {
if ( isset( $updatedSubscriptionParams['SubscriptionReference'] ) ) {
$endPoint = '/subscriptions/' . $updatedSubscriptionParams['SubscriptionReference'] . '/';
try {
return $this->core->call( $endPoint, $updatedSubscriptionParams, 'PUT' );
} catch ( TcoException $exception ) {
throw new TcoException( sprintf( 'Exception when updating subscription! Message: %s',
$exception->getMessage() ) );
if ( $method === 'PUT' ) {
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $params, JSON_UNESCAPED_UNICODE ) );
}
}
}

public function enableSubscriptions( $updatedSubscriptionParams ) {
if ( isset( $updatedSubscriptionParams['SubscriptionReference'] ) ) {
$endPoint = '/subscriptions/' . $updatedSubscriptionParams['SubscriptionReference'] . '/';
try {
return $this->core->call( $endPoint, [], 'POST' );
} catch ( TcoException $exception ) {
throw new TcoException( sprintf( 'Exception when Enabling subscription! Message: %s',
$exception->getMessage() ) );
if ( $method === 'DELETE' ) {
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
}
}

public function disableSubscriptions( $updatedSubscriptionParams ) {
if ( isset( $updatedSubscriptionParams['SubscriptionReference'] ) ) {
$endPoint = '/subscriptions/' . $updatedSubscriptionParams['SubscriptionReference'] . '/';
try {
return $this->core->call( $endPoint, [], 'DELETE' );
} catch ( TcoException $exception ) {
throw new TcoException( sprintf( 'Exception when Enabling subscription! Message: %s',
$exception->getMessage() ) );
$response = curl_exec( $ch );
curl_close( $ch );
if ( $response === false ) {
throw new TcoException( sprintf('Curl response :%s', curl_error( $ch )));
}

return json_decode( $response, true );
} catch ( Exception $e ) {
throw new TcoException( sprintf('Exception ApiCore response: %s', $e->getMessage()
) );
}
}
}