Skip to content

Commit fc62eab

Browse files
authored
initial commit: Interfaces and apis to support email opt-in (#679)
* initial commit: Interfaces and apis to support email opt-in * Tweaking interfaces, adding implementation for EmailSubscription endpoint * Removing PriceAdjustment logic for a later commit * Initial commit: Declarative apis for discount rule creation * fixing di.xml settings * confirmed working check subscription endpoint * Confirmed working all APIs * Fixing nits from review * adding back authentication
1 parent c409620 commit fc62eab

11 files changed

+630
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
namespace Meta\Sales\Api;
21+
22+
interface DiscountDeleteApiInterface
23+
{
24+
/**
25+
* Delete expired/used coupon codes
26+
*
27+
* @param string $externalBusinessId
28+
* @param string[] $couponIds
29+
* @return string[]
30+
*/
31+
public function deleteDiscount(string $externalBusinessId, array $couponIds): array;
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
namespace Meta\Sales\Api;
21+
22+
use Magento\SalesRule\Api\Data\RuleInterface;
23+
24+
interface DiscountRuleApiInterface
25+
{
26+
/**
27+
* Create a cart rule
28+
*
29+
* @param string $externalBusinessId
30+
* @param RuleInterface $rule
31+
* @return string
32+
*/
33+
public function createRule(string $externalBusinessId, RuleInterface $rule): string;
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
namespace Meta\Sales\Api;
21+
22+
use Magento\SalesRule\Api\Data\CouponInterface;
23+
24+
interface NewsletterSubscriptionDiscountApiInterface
25+
{
26+
/**
27+
* Add an email to the newsletter subscription list
28+
*
29+
* If valid, returns an opt-in coupon.
30+
*
31+
* @param string $externalBusinessId
32+
* @param string $email
33+
* @param int $ruleId
34+
* @return CouponInterface
35+
*/
36+
public function subscribeForCoupon(string $externalBusinessId, string $email, int $ruleId): CouponInterface;
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
namespace Meta\Sales\Api;
21+
22+
interface NewsletterSubscriptionDiscountStatusApiInterface
23+
{
24+
/**
25+
* Add an email to the newsletter subscription list
26+
*
27+
* / Split existence check into a separate API
28+
*
29+
* @param string $externalBusinessId
30+
* @param string $email
31+
* @return bool
32+
*/
33+
public function checkSubscriptionStatus(string $externalBusinessId, string $email): bool;
34+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Sales\Model\Api;
22+
23+
use Magento\Framework\App\ResourceConnection;
24+
use Magento\Framework\Exception\LocalizedException;
25+
use Meta\BusinessExtension\Model\Api\CustomApiKey\Authenticator;
26+
27+
class DiscountDeleteApi implements \Meta\Sales\Api\DiscountDeleteApiInterface
28+
{
29+
/**
30+
* @var ResourceConnection
31+
*/
32+
private ResourceConnection $resourceConnection;
33+
34+
/**
35+
* @var Authenticator
36+
*/
37+
private Authenticator $authenticator;
38+
39+
/**
40+
* DiscountDeleteApi constructor.
41+
*
42+
* @param ResourceConnection $resourceConnection
43+
* @param Authenticator $authenticator
44+
*/
45+
public function __construct(ResourceConnection $resourceConnection, Authenticator $authenticator)
46+
{
47+
$this->resourceConnection = $resourceConnection;
48+
$this->authenticator = $authenticator;
49+
}
50+
51+
/**
52+
* Delete expired/used coupon codes
53+
*
54+
* @param string $externalBusinessId
55+
* @param string[] $couponCodes
56+
* @return string[]
57+
* @throws LocalizedException
58+
*/
59+
public function deleteDiscount(string $externalBusinessId, array $couponCodes): array
60+
{
61+
$this->authenticator->authenticateRequest();
62+
63+
try {
64+
$connection = $this->resourceConnection->getConnection();
65+
$couponTable = $this->resourceConnection->getTableName('salesrule_coupon');
66+
67+
$deletedCouponCodes = [];
68+
69+
if (!empty($couponCodes)) {
70+
// Code is indexed. OTHER FIELDS ARE NOT. Use of resourceconnection should
71+
// be restricted to ONLY indexed columns
72+
$predicate = $connection->quoteInto('code IN (?)', $couponCodes);
73+
$deletedCouponCodes = $connection->fetchCol(
74+
$connection->select()->from($couponTable, 'code')->where($predicate)
75+
);
76+
$connection->delete($couponTable, $predicate);
77+
}
78+
79+
return $deletedCouponCodes;
80+
} catch (\Exception $e) {
81+
throw new LocalizedException(__('Failed to delete expired/used coupon codes: %1', $e->getMessage()));
82+
}
83+
}
84+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Copyright (c) Meta Platforms, Inc. and affiliates.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
namespace Meta\Sales\Model\Api;
21+
22+
use Magento\SalesRule\Api\RuleRepositoryInterface;
23+
use Magento\SalesRule\Api\Data\RuleInterface;
24+
use Magento\Framework\Exception\LocalizedException;
25+
use Meta\BusinessExtension\Helper\FBEHelper;
26+
use Meta\Sales\Api\DiscountRuleApiInterface;
27+
use Meta\Sales\Helper\OrderHelper;
28+
use Meta\BusinessExtension\Model\Api\CustomApiKey\Authenticator;
29+
30+
class DiscountRuleApi implements DiscountRuleApiInterface
31+
{
32+
/**
33+
* @var RuleRepositoryInterface
34+
*/
35+
private $ruleRepository;
36+
37+
/**
38+
* @var FBEHelper
39+
*/
40+
private FBEHelper $fbeHelper;
41+
42+
/**
43+
* @var OrderHelper
44+
*/
45+
private OrderHelper $orderHelper;
46+
47+
/**
48+
* @var Authenticator
49+
*/
50+
private Authenticator $authenticator;
51+
52+
/**
53+
* DiscountRuleApi constructor.
54+
*
55+
* @param RuleRepositoryInterface $ruleRepository
56+
* @param FBEHelper $fbeHelper
57+
* @param OrderHelper $orderHelper
58+
* @param Authenticator $authenticator
59+
*/
60+
public function __construct(
61+
RuleRepositoryInterface $ruleRepository,
62+
FBEHelper $fbeHelper,
63+
OrderHelper $orderHelper,
64+
Authenticator $authenticator
65+
) {
66+
$this->ruleRepository = $ruleRepository;
67+
$this->fbeHelper = $fbeHelper;
68+
$this->orderHelper = $orderHelper;
69+
$this->authenticator = $authenticator;
70+
}
71+
72+
/**
73+
* Create a cart rule
74+
*
75+
* @param string $externalBusinessId
76+
* @param RuleInterface $rule
77+
* @return int
78+
* @throws LocalizedException
79+
*/
80+
public function createRule(string $externalBusinessId, RuleInterface $rule): string
81+
{
82+
$this->authenticator->authenticateRequest();
83+
$storeId = $this->orderHelper->getStoreIdByExternalBusinessId($externalBusinessId);
84+
try {
85+
$savedRule = $this->ruleRepository->save($rule);
86+
return (string)$savedRule->getRuleId();
87+
} catch (\Exception $e) {
88+
$this->fbeHelper->logExceptionImmediatelyToMeta(
89+
$e,
90+
[
91+
'store_id' => $storeId,
92+
'event' => 'create_discount_rule',
93+
'event_type' => 'rule_creation_exception',
94+
'extra_data' => [
95+
'external_business_id' => $externalBusinessId,
96+
]
97+
]
98+
);
99+
throw new LocalizedException(__('Failed to create cart rule: %1', $e->getMessage()));
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)