Skip to content

Commit

Permalink
Add Merchant business event endpoint
Browse files Browse the repository at this point in the history
Add v1/me/business event endpoint
  • Loading branch information
Francois-Gomis authored Jan 24, 2025
2 parents 4d795bc + aa609bb commit 3cb562d
Show file tree
Hide file tree
Showing 7 changed files with 760 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Endpoints/Merchants.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@

namespace Alma\API\Endpoints;

use Alma\API\Entities\DTO\MerchantBusinessEvent\CartInitiatedBusinessEvent;
use Alma\API\Entities\DTO\MerchantBusinessEvent\OrderConfirmedBusinessEvent;
use Alma\API\Entities\FeePlan;
use Alma\API\Entities\Merchant;
use Alma\API\Exceptions\RequestException;
use Alma\API\RequestError;

class Merchants extends Base
Expand Down Expand Up @@ -80,4 +83,61 @@ public function feePlans($kind = FeePlan::KIND_GENERAL, $installmentsCounts = "a
return new FeePlan($val);
}, $res->json);
}

/**
* Prepare and send a business event for a cart initiated
*
* @param CartInitiatedBusinessEvent $cartEventData
* @return void
* @throws RequestException
*/
public function sendCartInitiatedBusinessEvent(CartInitiatedBusinessEvent $cartEventData)
{
$cartEventDataPayload = [
'event_type' => $cartEventData->getEventType(),
'cart_id' => $cartEventData->getCartId()
];
$this->sendBusinessEvent($cartEventDataPayload);
}

/**
* Prepare and send a business event for Order confirmed
*
* @param OrderConfirmedBusinessEvent $orderConfirmedBusinessEvent
* @return void
* @throws RequestException
*/
public function sendOrderConfirmedBusinessEvent(OrderConfirmedBusinessEvent $orderConfirmedBusinessEvent)
{
$cartEventDataPayload = [
'event_type' => $orderConfirmedBusinessEvent->getEventType(),
'is_alma_p1x' => $orderConfirmedBusinessEvent->isAlmaP1X(),
'is_alma_bnpl' => $orderConfirmedBusinessEvent->isAlmaBNPL(),
'was_bnpl_eligible' => $orderConfirmedBusinessEvent->wasBNPLEligible(),
'order_id' => $orderConfirmedBusinessEvent->getOrderId(),
'cart_id' => $orderConfirmedBusinessEvent->getCartId(),
'alma_payment_id' => $orderConfirmedBusinessEvent->getAlmaPaymentId()
];
$this->sendBusinessEvent($cartEventDataPayload);
}

/**
* Send merchant_business_event and return 204 no content
*
* @param array $eventData
* @return void
* @throws RequestException
*/
private function sendBusinessEvent($eventData)
{
try {
$res = $this->request(self::ME_PATH . "/business-events")->setRequestBody($eventData)->post();
} catch (RequestError $e) {
throw new RequestException($e->getErrorMessage(), null);
}
if ($res->isError()) {
throw new RequestException($res->errorMessage, null, $res);
}
}

}
22 changes: 22 additions & 0 deletions src/Entities/DTO/MerchantBusinessEvent/AbstractBusinessEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Alma\API\Entities\DTO\MerchantBusinessEvent;

abstract class AbstractBusinessEvent
{
/**
* @var string
*/
protected $eventType;

/**
* Get Event Type for merchant business event
*
* @return string
*/
public function getEventType()
{
return $this->eventType;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Alma\API\Entities\DTO\MerchantBusinessEvent;

use Alma\API\Exceptions\ParametersException;

class CartInitiatedBusinessEvent extends AbstractBusinessEvent
{
/**
* @var string
*/
private $cartId;

/**
* @param string $cartId
* @throws ParametersException
*/
public function __construct($cartId)
{
$this->eventType = 'cart_initiated';
if(empty($cartId) || !is_string($cartId)) {
throw new ParametersException('CartId must be a string');
}
$this->cartId = $cartId;
}

/**
* Get Cart Id
*
* @return string
*/
public function getCartId()
{
return $this->cartId;
}
}
151 changes: 151 additions & 0 deletions src/Entities/DTO/MerchantBusinessEvent/OrderConfirmedBusinessEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace Alma\API\Entities\DTO\MerchantBusinessEvent;


use Alma\API\Exceptions\ParametersException;

class OrderConfirmedBusinessEvent extends AbstractBusinessEvent
{

/**
* @var bool
*/
private $almaP1XStatus;
/**
* @var bool
*/
private $almaBNPLStatus;
/**
* @var bool
*/
private $wasBNPLEligible;
/**
* @var string
*/
private $orderId;
/**
* @var string
*/
private $cartId;
/**
* @var string | null
*/
private $almaPaymentId;


/**
* For non alma payment, almaPaymentId should be null
* For Alma payment, almaPaymentId should be a string
*
* @param bool $isAlmaP1X
* @param bool $isAlmaBNPL
* @param bool $wasBNPLEligible
* @param string $orderId
* @param string $cartId
* @param string | null $almaPaymentId
* @throws ParametersException
*/
public function __construct($isAlmaP1X, $isAlmaBNPL, $wasBNPLEligible, $orderId, $cartId, $almaPaymentId = null)
{
$this->eventType = 'order_confirmed';
$this->almaP1XStatus = $isAlmaP1X;
$this->almaBNPLStatus = $isAlmaBNPL;
$this->wasBNPLEligible = $wasBNPLEligible;
$this->orderId = $orderId;
$this->cartId = $cartId;
$this->almaPaymentId = $almaPaymentId;
$this->validateData();
}

/**
* @return bool
*/
public function isAlmaP1X()
{
return $this->almaP1XStatus;
}

/**
* @return bool
*/
public function isAlmaBNPL()
{
return $this->almaBNPLStatus;
}

/**
* Was eligible at the time of payment
*
* @return bool
*/
public function wasBNPLEligible()
{
return $this->wasBNPLEligible;
}

/**
* @return string
*/
public function getOrderId()
{
return $this->orderId;
}

/**
* @return string
*/
public function getCartId()
{
return $this->cartId;
}

/**
* @return string | null
*/
public function getAlmaPaymentId()
{
return $this->almaPaymentId;
}

/**
* Check if it is an Alma payment
*
* @return bool
*/
public function isAlmaPayment()
{
return $this->almaP1XStatus || $this->almaBNPLStatus;
}

/**
* @return void
* @throws ParametersException
*/
protected function validateData()
{
if(
!is_bool($this->almaP1XStatus) ||
!is_bool($this->almaBNPLStatus) ||
!is_bool($this->wasBNPLEligible) ||
!is_string($this->orderId) ||
!is_string($this->cartId) ||
// Alma payment id should be absent for non Alma payments
(!$this->isAlmaPayment() && !is_null($this->almaPaymentId))
)
{
throw new ParametersException('Invalid data type in OrderConfirmedBusinessEvent constructor');
}

//Alma payment id for Alma payment, Must be a string
if(
$this->isAlmaPayment() &&
!is_string($this->almaPaymentId)
)
{
throw new ParametersException('Alma payment id is mandatory for Alma payment');
}
}


}
Loading

0 comments on commit 3cb562d

Please sign in to comment.