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

Add v1/me/business event endpoint #158

Merged
Show file tree
Hide file tree
Changes from 3 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
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->getIsAlmaP1X(),
'is_alma_bnpl' => $orderConfirmedBusinessEvent->getIsAlmaBNPL(),
'was_bnpl_eligible' => $orderConfirmedBusinessEvent->getWasBNPLEligible(),
'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);
}
}

}
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(!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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace Alma\API\Entities\DTO\MerchantBusinessEvent;


use Alma\API\Exceptions\ParametersException;

class OrderConfirmedBusinessEvent extends AbstractBusinessEvent
{

/**
* @var bool
*/
private $isAlmaP1X;
/**
* @var bool
*/
private $isAlmaBNPL;
/**
* @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
* @throws ParametersException
*/
public function __construct($isAlmaP1X, $isAlmaBNPL, $wasBNPLEligible, $orderId, $cartId, $almaPaymentId = null)
{
$this->eventType = 'order_confirmed';
$this->isAlmaP1X = $isAlmaP1X;
$this->isAlmaBNPL = $isAlmaBNPL;
$this->wasBNPLEligible = $wasBNPLEligible;
$this->orderId = $orderId;
$this->cartId = $cartId;
$this->almaPaymentId = $almaPaymentId;
$this->validateData();
}

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

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

/**
* @return bool
*/
public function getWasBNPLEligible()
{
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->isAlmaP1X || $this->isAlmaBNPL;
}

/**
* @return void
* @throws ParametersException
*/
protected function validateData()
{
if(
!is_bool($this->isAlmaP1X) ||
!is_bool($this->isAlmaBNPL) ||
!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