Skip to content

Commit

Permalink
Merge pull request #86 from alma/develop
Browse files Browse the repository at this point in the history
Release 2.0.3
  • Loading branch information
Benjamin-Freoua-Alma authored Feb 14, 2024
2 parents 336845d + 962cbbc commit 2c73716
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 82 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

v2.0.3
-------
* Added order_id in subscription model
* Added sendCustomerCart endpoint Insurance
* Added cancelSubscription endpoint Insurance

v2.0.2
-------
* Added getSubscription endpoint Insurance
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "alma/alma-php-client",
"description": "PHP API client for the Alma payments API",
"version": "2.0.2",
"version": "2.0.3",
"type": "library",
"require": {
"php": "^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1 || ~8.2",
Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class Client
{
const VERSION = '2.0.2';
const VERSION = '2.0.3';

const LIVE_MODE = 'live';
const TEST_MODE = 'test';
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class Base
public function __construct($client_context)
{
$this->setClientContext($client_context);
$this->logger = $client_context->logger;
}

/**
Expand All @@ -60,5 +59,6 @@ public function request($path)
public function setClientContext($clientContext)
{
$this->clientContext = $clientContext;
$this->logger = $clientContext->logger;
}
}
79 changes: 72 additions & 7 deletions src/Endpoints/Insurance.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function checkParameters($cmsReference, $insuranceContractExternalId, $pr

/**
* @param $subscriptionArray
* @param string $orderId
* @param null $paymentId
* @param string | null $customerSessionId
* @param string | null $cartId
Expand All @@ -110,7 +111,13 @@ public function checkParameters($cmsReference, $insuranceContractExternalId, $pr
* @throws RequestError
* @throws RequestException
*/
public function subscription($subscriptionArray, $paymentId = null, $customerSessionId = null, $cartId = null)
public function subscription(
$subscriptionArray,
$orderId,
$paymentId = null,
$customerSessionId = null,
$cartId = null
)
{

if (!is_array($subscriptionArray)) {
Expand All @@ -122,7 +129,7 @@ public function subscription($subscriptionArray, $paymentId = null, $customerSes
);
}

$subscriptionData = $this->buildSubscriptionData($subscriptionArray, $paymentId);
$subscriptionData = $this->buildSubscriptionData($subscriptionArray, $orderId, $paymentId);
$request = $this->request(self::INSURANCE_PATH . 'subscriptions')
->setRequestBody($subscriptionData);

Expand All @@ -137,15 +144,15 @@ public function subscription($subscriptionArray, $paymentId = null, $customerSes
}

/**
* @throws RequestError
* @return array json_decode in response constructor
* @throws RequestException
* @throws ParametersException
* @return array json_decode in response constructor
* @throws RequestError
*/
public function getSubscription($subscriptionIds)
{
$this->insuranceValidator->checkSubscriptionIds($subscriptionIds);
$response = $this->request(
$response = $this->request(
self::INSURANCE_PATH . 'subscriptions'
)->setQueryParams(
$subscriptionIds
Expand All @@ -158,15 +165,41 @@ public function getSubscription($subscriptionIds)
return $response->json;
}

/**
* @param $cmsReferenceArray
* @param $cartId
* @return void
* @throws RequestError
*/
public function sendCustomerCart($cmsReferenceArray, $cartId)
{
try {
$this->insuranceValidator->checkCmsReference($cmsReferenceArray);
$request = $this->request(self::INSURANCE_PATH . 'customer-cart')
->setRequestBody(
[
'cms_references' => $cmsReferenceArray
]
);

$this->addCustomerSessionToRequest($request, null, $cartId);
$request->post();
} catch (ParametersException $e) {
$this->logger->error('Impossible to send customer cart data', [$e->getMessage()]);
}
}

/**
* @param array $subscriptionArray
* @param string $orderId
* @param string|null $paymentId
* @return array
* @throws ParametersException
*/
protected function buildSubscriptionData($subscriptionArray, $paymentId = null)
protected function buildSubscriptionData($subscriptionArray, $orderId, $paymentId = null)
{
$subscriptionData = ['subscriptions' => []];
$subscriptionData['order_id'] = $orderId;

/**
* @var Subscription $subscription
Expand Down Expand Up @@ -209,7 +242,6 @@ protected function buildSubscriptionData($subscriptionArray, $paymentId = null)
) {
$subscriptionData['payment_id'] = $paymentId;
}

return $subscriptionData;
}

Expand Down Expand Up @@ -273,4 +305,37 @@ public function addCustomerSessionToRequest($request, $customerSessionId = null,
$request->addCartIdToHeader($cartId);
}
}

/**
* @param string $subscriptionId
* @return void
* @throws ParametersException
* @throws RequestError
* @throws RequestException
*/
public function cancelSubscription($subscriptionId)
{
$subscriptionId = trim($subscriptionId);
$this->checkSubscriptionIdFormat($subscriptionId);

$request = $this->request(self::INSURANCE_PATH . 'subscriptions/' . $subscriptionId . '/void');
$response = $request->post();

if ($response->isError()) {
throw new RequestException($response->errorMessage, $request, $response);
}
}

/**
* @param string $subscriptionId
* @return void
* @throws ParametersException
*/
public function checkSubscriptionIdFormat($subscriptionId)
{
if (!is_string($subscriptionId) || empty($subscriptionId)) {
throw new ParametersException('Invalid subscriptions Array');
}

}
}
22 changes: 22 additions & 0 deletions src/Lib/InsuranceValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,26 @@ public function checkSubscriptionIds($subscriptionIds)
));
}
}

/**
* Throw Exception if payload isn't valid
* @throws ParametersException
*/
public function checkCmsReference($cmsReferenceArray)
{
if (!is_array($cmsReferenceArray) || empty($cmsReferenceArray)) {
throw new ParametersException(sprintf(
'Invalid parameters must be an array with strings : %s',
gettype($cmsReferenceArray)
));
}
foreach ($cmsReferenceArray as $cmsReference) {
if (!is_string($cmsReference)) {
throw new ParametersException(sprintf(
'Cms references must be a string : %s',
json_encode($cmsReference)
));
}
}
}
}
Loading

0 comments on commit 2c73716

Please sign in to comment.