Skip to content

Commit 9606ef2

Browse files
Merge pull request #162 from alma/release/v2.5.0
Release v2.5.0
2 parents e2df0d8 + f1927b9 commit 9606ef2

12 files changed

Lines changed: 798 additions & 3 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ repos:
3535
stages: [commit]
3636

3737
- repo: https://github.com/returntocorp/semgrep
38-
rev: v1.92.0
38+
rev: v1.103.0
3939
hooks:
4040
- id: semgrep
4141
args:

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# CHANGELOG
22

3+
## v2.5.0 - 2025-01-30
4+
5+
### Changes
6+
7+
### 🚀 New Features
8+
9+
- Deprecation of v2/orders status endpoint (#161)
10+
- Not allow empty string for order confirmed business event construct (#160)
11+
- Add v1/me/business event endpoint (#158)
12+
13+
#### Contributors
14+
15+
@Francois-Gomis, @alma-renovate-bot[bot], @webaaz, [alma-renovate-bot[bot]](https://github.com/apps/alma-renovate-bot) and [github-actions[bot]](https://github.com/apps/github-actions)
16+
317
## v2.4.0 - 2024-12-09
418

519
### Changes
@@ -205,6 +219,7 @@
205219

206220

207221

222+
208223
```
209224
* Add fields and docs to the Payment entity
210225
* Add a Refund entity and extract refunds data within the Payment entity constructor

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "alma/alma-php-client",
33
"description": "PHP API client for the Alma payments API",
4-
"version": "2.4.0",
4+
"version": "2.5.0",
55
"type": "library",
66
"require": {
77
"php": "^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1 || ~8.2 || ~8.3",

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
class Client
3232
{
33-
const VERSION = '2.4.0';
33+
const VERSION = '2.5.0';
3434

3535
const LIVE_MODE = 'live';
3636
const TEST_MODE = 'test';

src/Endpoints/Merchants.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525

2626
namespace Alma\API\Endpoints;
2727

28+
use Alma\API\Entities\DTO\MerchantBusinessEvent\CartInitiatedBusinessEvent;
29+
use Alma\API\Entities\DTO\MerchantBusinessEvent\OrderConfirmedBusinessEvent;
2830
use Alma\API\Entities\FeePlan;
2931
use Alma\API\Entities\Merchant;
32+
use Alma\API\Exceptions\RequestException;
3033
use Alma\API\RequestError;
3134

3235
class Merchants extends Base
@@ -80,4 +83,61 @@ public function feePlans($kind = FeePlan::KIND_GENERAL, $installmentsCounts = "a
8083
return new FeePlan($val);
8184
}, $res->json);
8285
}
86+
87+
/**
88+
* Prepare and send a business event for a cart initiated
89+
*
90+
* @param CartInitiatedBusinessEvent $cartEventData
91+
* @return void
92+
* @throws RequestException
93+
*/
94+
public function sendCartInitiatedBusinessEvent(CartInitiatedBusinessEvent $cartEventData)
95+
{
96+
$cartEventDataPayload = [
97+
'event_type' => $cartEventData->getEventType(),
98+
'cart_id' => $cartEventData->getCartId()
99+
];
100+
$this->sendBusinessEvent($cartEventDataPayload);
101+
}
102+
103+
/**
104+
* Prepare and send a business event for Order confirmed
105+
*
106+
* @param OrderConfirmedBusinessEvent $orderConfirmedBusinessEvent
107+
* @return void
108+
* @throws RequestException
109+
*/
110+
public function sendOrderConfirmedBusinessEvent(OrderConfirmedBusinessEvent $orderConfirmedBusinessEvent)
111+
{
112+
$cartEventDataPayload = [
113+
'event_type' => $orderConfirmedBusinessEvent->getEventType(),
114+
'is_alma_p1x' => $orderConfirmedBusinessEvent->isAlmaP1X(),
115+
'is_alma_bnpl' => $orderConfirmedBusinessEvent->isAlmaBNPL(),
116+
'was_bnpl_eligible' => $orderConfirmedBusinessEvent->wasBNPLEligible(),
117+
'order_id' => $orderConfirmedBusinessEvent->getOrderId(),
118+
'cart_id' => $orderConfirmedBusinessEvent->getCartId(),
119+
'alma_payment_id' => $orderConfirmedBusinessEvent->getAlmaPaymentId()
120+
];
121+
$this->sendBusinessEvent($cartEventDataPayload);
122+
}
123+
124+
/**
125+
* Send merchant_business_event and return 204 no content
126+
*
127+
* @param array $eventData
128+
* @return void
129+
* @throws RequestException
130+
*/
131+
private function sendBusinessEvent($eventData)
132+
{
133+
try {
134+
$res = $this->request(self::ME_PATH . "/business-events")->setRequestBody($eventData)->post();
135+
} catch (RequestError $e) {
136+
throw new RequestException($e->getErrorMessage(), null);
137+
}
138+
if ($res->isError()) {
139+
throw new RequestException($res->errorMessage, null, $res);
140+
}
141+
}
142+
83143
}

src/Endpoints/Orders.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function fetch($orderId)
133133
}
134134

135135
/**
136+
* @deprecated since version 2.5.0 - Use addOrderStatusByMerchantOrderReference() in Payments endpoint
136137
* @param string $orderExternalId
137138
* @param array $orderData
138139
* @return void
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Alma\API\Entities\DTO\MerchantBusinessEvent;
4+
5+
abstract class AbstractBusinessEvent
6+
{
7+
/**
8+
* @var string
9+
*/
10+
protected $eventType;
11+
12+
/**
13+
* Get Event Type for merchant business event
14+
*
15+
* @return string
16+
*/
17+
public function getEventType()
18+
{
19+
return $this->eventType;
20+
}
21+
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Alma\API\Entities\DTO\MerchantBusinessEvent;
4+
5+
use Alma\API\Exceptions\ParametersException;
6+
7+
class CartInitiatedBusinessEvent extends AbstractBusinessEvent
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private $cartId;
13+
14+
/**
15+
* @param string $cartId
16+
* @throws ParametersException
17+
*/
18+
public function __construct($cartId)
19+
{
20+
$this->eventType = 'cart_initiated';
21+
if(empty($cartId) || !is_string($cartId)) {
22+
throw new ParametersException('CartId must be a string');
23+
}
24+
$this->cartId = $cartId;
25+
}
26+
27+
/**
28+
* Get Cart Id
29+
*
30+
* @return string
31+
*/
32+
public function getCartId()
33+
{
34+
return $this->cartId;
35+
}
36+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
namespace Alma\API\Entities\DTO\MerchantBusinessEvent;
4+
5+
6+
use Alma\API\Exceptions\ParametersException;
7+
8+
class OrderConfirmedBusinessEvent extends AbstractBusinessEvent
9+
{
10+
11+
/**
12+
* @var bool
13+
*/
14+
private $almaP1XStatus;
15+
/**
16+
* @var bool
17+
*/
18+
private $almaBNPLStatus;
19+
/**
20+
* @var bool
21+
*/
22+
private $wasBNPLEligible;
23+
/**
24+
* @var string
25+
*/
26+
private $orderId;
27+
/**
28+
* @var string
29+
*/
30+
private $cartId;
31+
/**
32+
* @var string | null
33+
*/
34+
private $almaPaymentId;
35+
36+
37+
/**
38+
* For non alma payment, almaPaymentId should be null
39+
* For Alma payment, almaPaymentId should be a string
40+
*
41+
* @param bool $isAlmaP1X
42+
* @param bool $isAlmaBNPL
43+
* @param bool $wasBNPLEligible
44+
* @param string $orderId
45+
* @param string $cartId
46+
* @param string | null $almaPaymentId
47+
* @throws ParametersException
48+
*/
49+
public function __construct($isAlmaP1X, $isAlmaBNPL, $wasBNPLEligible, $orderId, $cartId, $almaPaymentId = null)
50+
{
51+
$this->eventType = 'order_confirmed';
52+
$this->almaP1XStatus = $isAlmaP1X;
53+
$this->almaBNPLStatus = $isAlmaBNPL;
54+
$this->wasBNPLEligible = $wasBNPLEligible;
55+
$this->orderId = $orderId;
56+
$this->cartId = $cartId;
57+
$this->almaPaymentId = $almaPaymentId;
58+
$this->validateData();
59+
}
60+
61+
/**
62+
* @return bool
63+
*/
64+
public function isAlmaP1X()
65+
{
66+
return $this->almaP1XStatus;
67+
}
68+
69+
/**
70+
* @return bool
71+
*/
72+
public function isAlmaBNPL()
73+
{
74+
return $this->almaBNPLStatus;
75+
}
76+
77+
/**
78+
* Was eligible at the time of payment
79+
*
80+
* @return bool
81+
*/
82+
public function wasBNPLEligible()
83+
{
84+
return $this->wasBNPLEligible;
85+
}
86+
87+
/**
88+
* @return string
89+
*/
90+
public function getOrderId()
91+
{
92+
return $this->orderId;
93+
}
94+
95+
/**
96+
* @return string
97+
*/
98+
public function getCartId()
99+
{
100+
return $this->cartId;
101+
}
102+
103+
/**
104+
* @return string | null
105+
*/
106+
public function getAlmaPaymentId()
107+
{
108+
return $this->almaPaymentId;
109+
}
110+
111+
/**
112+
* Check if it is an Alma payment
113+
*
114+
* @return bool
115+
*/
116+
public function isAlmaPayment()
117+
{
118+
return $this->almaP1XStatus || $this->almaBNPLStatus;
119+
}
120+
121+
/**
122+
* @return void
123+
* @throws ParametersException
124+
*/
125+
protected function validateData()
126+
{
127+
if(
128+
!is_bool($this->almaP1XStatus) ||
129+
!is_bool($this->almaBNPLStatus) ||
130+
!is_bool($this->wasBNPLEligible) ||
131+
(!is_string($this->orderId) || empty($this->orderId)) ||
132+
(!is_string($this->cartId) || empty($this->cartId)) ||
133+
// Alma payment id should be absent for non Alma payments
134+
(!$this->isAlmaPayment() && !is_null($this->almaPaymentId))
135+
)
136+
{
137+
throw new ParametersException('Invalid data type in OrderConfirmedBusinessEvent constructor');
138+
}
139+
140+
//Alma payment id for Alma payment, Must be a string
141+
if(
142+
$this->isAlmaPayment() &&
143+
(!is_string($this->almaPaymentId) || empty($this->almaPaymentId))
144+
)
145+
{
146+
throw new ParametersException('Alma payment id is mandatory for Alma payment');
147+
}
148+
}
149+
150+
151+
}

0 commit comments

Comments
 (0)