Skip to content

Commit 1ead928

Browse files
Merge pull request #103 from AbdullahAldakheel/master
feat(creditcard): Update submit button text
2 parents 5c918d9 + 549512e commit 1ead928

File tree

13 files changed

+198
-9
lines changed

13 files changed

+198
-9
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 5.1.3 - 2025-03-06
9+
10+
## Changed
11+
- Add Pay and currency to the credit cart submits button
12+
13+
### Fixed
14+
- Apply discount if moyasar coupon is used
15+
816
## 5.1.2 - 2025-01-26
917
### Changed
1018
- Force ```isPlaceOrderActionAllowed``` to true to avoid billing address validation

Controller/Confirm/Stcpay.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Sales\Model\Order;
1212
use Moyasar\Magento2\Helper\Http\Exceptions\ConnectionException;
1313
use Moyasar\Magento2\Helper\Http\Exceptions\HttpException;
14+
use Moyasar\Magento2\Helper\MoyasarCoupon;
1415
use Moyasar\Magento2\Helper\MoyasarHelper;
1516
use Moyasar\Magento2\Helper\PaymentHelper;
1617
use Psr\Log\LoggerInterface;
@@ -27,6 +28,8 @@ class Stcpay implements ActionInterface
2728
protected $messageManager;
2829
protected $logger;
2930

31+
protected $moyasarCoupon;
32+
3033
/**
3134
* @var string
3235
* STC Pay Tokens
@@ -41,7 +44,8 @@ public function __construct(
4144
MoyasarHelper $helper,
4245
UrlInterface $urlBuilder,
4346
ManagerInterface $messageManager,
44-
LoggerInterface $logger
47+
LoggerInterface $logger,
48+
MoyasarCoupon $moyasarCoupon
4549
)
4650
{
4751
$this->context = $context;
@@ -50,6 +54,7 @@ public function __construct(
5054
$this->urlBuilder = $urlBuilder;
5155
$this->messageManager = $messageManager;
5256
$this->logger = $logger;
57+
$this->moyasarCoupon = $moyasarCoupon;
5358
}
5459

5560
public function execute()
@@ -83,6 +88,8 @@ public function execute()
8388
return $this->redirectToCart();
8489
}
8590

91+
$this->moyasarCoupon->tryApplyCouponToOrder($order, $payment);
92+
8693
$errors = $this->moyasarHelper->checkPaymentForErrors($order, $payment);
8794
if (count($errors) > 0) {
8895
$this->processUnMatchingInfoFail($payment, $order, $errors);

Controller/Order/Webhook.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Moyasar\Magento2\Helper\Http\Exceptions\ConnectionException;
1717
use Moyasar\Magento2\Helper\Http\Exceptions\HttpException;
1818
use Moyasar\Magento2\Helper\Http\QuickHttp;
19+
use Moyasar\Magento2\Helper\MoyasarCoupon;
1920
use Moyasar\Magento2\Helper\MoyasarHelper;
2021
use Psr\Log\LoggerInterface;
2122

@@ -38,17 +39,21 @@ class Webhook implements HttpPostActionInterface, CsrfAwareActionInterface
3839
/** @var LoggerInterface */
3940
private $logger;
4041

42+
protected $moyasarCoupon;
43+
4144
public function __construct(
4245
Context $context,
4346
MoyasarHelper $helper,
4447
OrderRepository $orderRepo,
45-
LoggerInterface $logger
48+
LoggerInterface $logger,
49+
MoyasarCoupon $moyasarCoupon
4650
) {
4751
$this->context = $context;
4852
$this->moyasarHelper = $helper;
4953
$this->url = $context->getUrl();
5054
$this->orderRepo = $orderRepo;
5155
$this->logger = $logger;
56+
$this->moyasarCoupon = $moyasarCoupon;
5257
}
5358

5459
public function execute()
@@ -81,6 +86,7 @@ public function execute()
8186
->get($this->moyasarHelper->apiBaseUrl("/v1/payments/$paymentId"))
8287
->json();
8388

89+
$this->moyasarCoupon->tryApplyCouponToOrder($order, $payment);
8490
$errors = $this->moyasarHelper->checkPaymentForErrors($order, $payment);
8591
if (count($errors) > 0) {
8692
$this->processUnMatchingInfoFail($payment, $order, $errors);

Controller/Payment/Validate.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Moyasar\Magento2\Helper\Http\Exceptions\HttpException;
1414
use Moyasar\Magento2\Helper\MoyasarHelper;
1515
use Moyasar\Magento2\Helper\PaymentHelper;
16+
use Moyasar\Magento2\Helper\MoyasarCoupon;
1617
use Psr\Log\LoggerInterface;
1718

1819
class Validate implements ActionInterface
@@ -27,14 +28,16 @@ class Validate implements ActionInterface
2728
protected $http;
2829
protected $messageManager;
2930
protected $logger;
31+
protected $moyasarCoupon;
3032

3133
public function __construct(
3234
Context $context,
3335
Session $checkoutSession,
3436
MoyasarHelper $helper,
3537
UrlInterface $urlBuilder,
3638
ManagerInterface $messageManager,
37-
LoggerInterface $logger
39+
LoggerInterface $logger,
40+
MoyasarCoupon $moyasarCoupon
3841
)
3942
{
4043
$this->context = $context;
@@ -43,6 +46,7 @@ public function __construct(
4346
$this->urlBuilder = $urlBuilder;
4447
$this->messageManager = $messageManager;
4548
$this->logger = $logger;
49+
$this->moyasarCoupon = $moyasarCoupon;
4650
}
4751

4852
public function execute()
@@ -72,6 +76,10 @@ public function execute()
7276
return $this->redirectToCart();
7377
}
7478

79+
$this->moyasarCoupon->tryApplyCouponToOrder($order, $payment);
80+
81+
82+
7583
$errors = $this->moyasarHelper->checkPaymentForErrors($order, $payment);
7684
if (count($errors) > 0) {
7785
$this->processUnMatchingInfoFail($payment, $order, $errors);

Helper/MoyasarCoupon.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Moyasar\Magento2\Helper;
4+
5+
use Magento\Framework\App\Helper\AbstractHelper;
6+
use Magento\Framework\App\Helper\Context;
7+
use Magento\Sales\Model\Order;
8+
use Magento\SalesRule\Model\RuleFactory;
9+
use Magento\SalesRule\Model\CouponFactory;
10+
use Magento\SalesRule\Model\Rule;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Moyasar\Magento2\Helper\CurrencyHelper;
13+
use Psr\Log\LoggerInterface;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Moyasar\Magento2\Helper\MoyasarLogs;
16+
17+
18+
class MoyasarCoupon extends AbstractHelper
19+
{
20+
/**
21+
* @var CurrencyHelper
22+
*/
23+
protected $currencyHelper;
24+
25+
/**
26+
* @var LoggerInterface
27+
*/
28+
protected $logger;
29+
30+
/**
31+
* Constructor
32+
*
33+
* @param Context $context
34+
* @param CurrencyHelper $currencyHelper
35+
*/
36+
public function __construct(
37+
Context $context,
38+
CurrencyHelper $currencyHelper
39+
)
40+
{
41+
parent::__construct($context);
42+
$this->currencyHelper = $currencyHelper;
43+
$this->logger = new MoyasarLogs();;
44+
}
45+
46+
/**
47+
* Try applying a coupon to an existing order, based on Payment metadata.
48+
*
49+
* **Note**: This will NOT re-collect or re-price an already placed order.
50+
* It simply stores the coupon code & notes on the order.
51+
*
52+
* @param $order
53+
* @param $payment
54+
*/
55+
public function tryApplyCouponToOrder($order, $payment)
56+
{
57+
// We expect these custom keys from your payment metadata
58+
if (!isset($payment['metadata']['#coupon_id'])) {
59+
return; // No coupon metadata to apply
60+
}
61+
// Extract discount info from Payment metadata:
62+
$capturedAmount = $payment['amount'] ?? 0;
63+
$totalPrice = $payment['metadata']['#coupon_original_amount'] ?? 0;
64+
$currency = $payment['currency'] ?? $order->getOrderCurrencyCode();
65+
$discountedAmount = $this->currencyHelper->amountToMajor($totalPrice - $capturedAmount, $currency);
66+
if ($order->getSubtotal() !== $order->getGrandTotal()) {
67+
$taxPercent = $order->getTaxAmount() / $order->getSubtotal();
68+
$discountedAmount = $discountedAmount / (1 + $taxPercent);
69+
}
70+
71+
72+
$order->setDiscountAmount(-$discountedAmount);
73+
$order->setDiscountDescription('Moyasar Discount: ' . $payment['metadata']['#coupon_id']);
74+
$order->setBaseDiscountAmount(-$discountedAmount);
75+
76+
// Recalculate grand total
77+
$newGrandTotal = $this->currencyHelper->amountToMajor($capturedAmount, $currency);
78+
$order->setGrandTotal($newGrandTotal);
79+
$order->setBaseGrandTotal($newGrandTotal);
80+
$order->setTotalPaid($newGrandTotal);
81+
$order->save();
82+
$this->logger->info("[Moyasar][Coupon] Successfully applied coupon code '{$payment['metadata']['#coupon_id']}' to order #{$order->getIncrementId()} (no recalculation).");
83+
}
84+
}

Helper/MoyasarHelper.php

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

2222
class MoyasarHelper extends AbstractHelper
2323
{
24-
const VERSION = '5.1.2';
24+
const VERSION = '5.1.3';
2525

2626
const XML_PATH_CREDIT_CARD_IS_ACTIVE = 'payment/moyasar_payments/active';
2727
const XML_PATH_APPLE_PAY_IS_ACTIVE = 'payment/moyasar_payments_apple_pay/active';

Helper/MoyasarLogs.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ public function info($message, $context = [])
3232
$this->log('info', $message, $context);
3333
}
3434

35+
public function error($message, $context = [])
36+
{
37+
$this->log('error', $message, $context);
38+
}
39+
3540
}

Schedule/CheckPending.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Sales\Api\OrderRepositoryInterface;
1111
use Magento\Sales\Model\Order;
1212
use Magento\Sales\Model\ResourceModel\Order\Collection;
13+
use Moyasar\Magento2\Helper\MoyasarCoupon;
1314
use Moyasar\Magento2\Helper\MoyasarHelper;
1415
use Moyasar\Magento2\Model\Payment\MoyasarPayments;
1516
use Moyasar\Magento2\Model\Payment\MoyasarPaymentsApplePay;
@@ -24,18 +25,22 @@ class CheckPending
2425
private $cachePool;
2526
private $logger;
2627

28+
protected $moyasarCoupon;
29+
2730
public function __construct(
2831
Collection $orderCollection,
2932
OrderRepositoryInterface $orderRepo,
3033
MoyasarHelper $moyasarHelper,
3134
Pool $cachePool,
32-
LoggerInterface $logger
35+
LoggerInterface $logger,
36+
MoyasarCoupon $moyasarCoupon
3337
) {
3438
$this->orderCollection = $orderCollection;
3539
$this->orderRepo = $orderRepo;
3640
$this->moyasarHelper = $moyasarHelper;
3741
$this->cachePool = $cachePool;
3842
$this->logger = $logger;
43+
$this->moyasarCoupon = $moyasarCoupon;
3944
}
4045

4146
public function cron(): void
@@ -123,6 +128,8 @@ private function processPayment($order)
123128
}
124129

125130
$payment = $paidPayments[0];
131+
132+
$this->moyasarCoupon->tryApplyCouponToOrder($order, $payment);
126133
$errors = $this->moyasarHelper->checkPaymentForErrors($order, $payment);
127134
if (count($errors) > 0) {
128135
$this->logger->info("Order had errors " . $order->getIncrementId() . ' trying canceling...');

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "moyasar-fintech/magento2",
33
"description": "Magento 2 payment module that integrate with https:\\\\moyasar.com Gateway",
44
"type": "magento2-module",
5-
"version": "5.1.2",
5+
"version": "5.1.3",
66
"authors": [
77
{
88
"email": "[email protected]",

etc/di.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
1+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
23
<type name="Magento\Framework\Console\CommandListInterface">
34
<arguments>
45
<argument name="commands" xsi:type="array">
5-
<item name="moyasar_magento2_process_pending" xsi:type="object">Moyasar\Magento2\Console\Command\CheckPending</item>
6+
<item name="moyasar_magento2_process_pending" xsi:type="object">
7+
Moyasar\Magento2\Console\Command\CheckPending
8+
</item>
69
</argument>
710
</arguments>
811
</type>
12+
<type name="Moyasar\Magento2\Helper\MoyasarHelper">
13+
<arguments>
14+
<argument name="moyasarCoupon" xsi:type="object">Moyasar\Magento2\Helper\MoyasarCoupon</argument>
15+
</arguments>
16+
</type>
917
</config>

0 commit comments

Comments
 (0)