Skip to content

Commit 23694ed

Browse files
Merge pull request #100 from AbdullahAldakheel/master
version 5.1.0
2 parents c5f89ad + 4f427dd commit 23694ed

18 files changed

+591
-8
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
9+
## 5.1.0 - 2025-01-18
10+
### Added
11+
- Input field for customizing the store name in Apple Pay.
12+
- Samsung Pay integration as a new payment method.
13+
14+
15+
## 1.0.0 - unknown
16+
17+
- First release 🎉.

Controller/Payment/Initiate.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function execute()
8484
// check session
8585
if (!$this->checkoutSession->getLastRealOrderId()) {
8686
$this->logger->warning('Moyasar payment accessed without active order.');
87-
return $resultJson->setData(['message' => 'Invalid request.']);
87+
return $resultJson->setData(['message' => 'Invalid request.'])->setHttpResponseCode(422);
8888
}
8989

9090
$this->token = $this->request->getPostValue('token') ?? null;
@@ -155,6 +155,10 @@ public function execute()
155155
$responseData = array_merge($responseData, $this->applepayResponseData($response));
156156
}
157157

158+
if ($this->method == 'samsungpay') {
159+
$responseData = array_merge($responseData, $this->samsungpayResponseData($response));
160+
}
161+
158162
$this->moyasarHelper->processInitiateOrder($this->order, $paymentId, $this->method);
159163

160164
return $resultJson->setData($responseData);
@@ -227,6 +231,16 @@ private function stcpayPayload()
227231
return $basePayload;
228232
}
229233

234+
private function samsungpayPayload()
235+
{
236+
$basePayload = $this->basePayload();
237+
$basePayload['source'] = [
238+
'type' => 'samsungpay',
239+
'token' => $this->token,
240+
];
241+
return $basePayload;
242+
}
243+
230244
private function creditcardResponseData($response)
231245
{
232246
return [
@@ -243,6 +257,13 @@ private function applepayResponseData($response)
243257
];
244258
}
245259

260+
private function samsungpayResponseData($response)
261+
{
262+
return [
263+
'redirect_url' => $this->urlBuilder->getUrl('moyasar/payment/validate')
264+
];
265+
}
266+
246267

247268
private function stcpayResponseData($paymentId, $url)
248269
{

Helper/Http/QuickHttp.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
use Moyasar\Magento2\Helper\Http\Exceptions\ConnectionException;
99
use Moyasar\Magento2\Helper\Http\Exceptions\ServerException;
1010
use Moyasar\Magento2\Helper\MoyasarHelper;
11+
use Moyasar\Magento2\Helper\MoyasarLogs;
1112

1213
class QuickHttp
1314
{
1415
private $curl_handler;
1516
private $disposed = false;
1617

18+
// Logger
19+
private $logger;
20+
21+
1722
// Config
1823
private $headers = [];
1924

@@ -25,7 +30,7 @@ public static function make()
2530
public function __construct()
2631
{
2732
global $wp_version;
28-
33+
$this->logger = new MoyasarLogs();
2934
$this->curl_handler = curl_init(null);
3035
$client = 'Moyasar Http; Magento Plugin v' . MoyasarHelper::VERSION;
3136
$this->headers['User-Agent'] = $client;
@@ -80,6 +85,8 @@ public function request($method, $url, $data = [])
8085
$data = json_encode($data);
8186
curl_setopt($this->curl_handler, CURLOPT_POSTFIELDS, $data);
8287
}
88+
// Log the payload with url
89+
$this->logger->info('Request to ' . $url . ' with payload: ' , $data);
8390

8491
if (in_array($method, ['GET', 'HEAD'])) {
8592
$url = $url . $this->encode_url_params($data);
@@ -106,6 +113,9 @@ public function request($method, $url, $data = [])
106113

107114
$this->dispose();
108115

116+
// Log the response
117+
$this->logger->info('Response from ' . $url . ' with payload: ', $response->json());
118+
109119
if ($response->isServerError()) {
110120
throw new ServerException('Server error: server returned status ' . $response->status(), $response);
111121
}

Helper/MoyasarHelper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121

2222
class MoyasarHelper extends AbstractHelper
2323
{
24-
const VERSION = '5.0.6';
24+
const VERSION = '5.1.0';
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';
2828
const XML_PATH_STC_PAY_IS_ACTIVE = 'payment/moyasar_payments_stc_pay/active';
2929

30+
const XML_PATH_SAMSUNG_PAY_IS_ACTIVE = 'payment/moyasar_payments_samsung_pay/active';
31+
32+
3033

3134
protected $orderManagement;
3235
protected $_objectManager;

Helper/MoyasarLogs.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Moyasar\Magento2\Helper;
4+
5+
use Magento\Framework\App\ObjectManager;
6+
use Psr\Log\LoggerInterface;
7+
8+
class MoyasarLogs
9+
{
10+
/**
11+
* Log a message to a custom file.
12+
*
13+
* @param string $level The log level (e.g., 'info', 'error', 'debug').
14+
* @param string $message The message to log.
15+
* @param array $context Additional context for the log message.
16+
*/
17+
public function log($level = 'info', $message = '', $context = [])
18+
{
19+
// Use ObjectManager to get the logger instance
20+
$logger = ObjectManager::getInstance()->get(LoggerInterface::class);
21+
22+
// Define the full file path for the log file
23+
$filePath = BP . '/var/log/' . 'moyasar.log';
24+
25+
26+
// Additionally, log directly to the custom file
27+
error_log($message . '-' . json_encode($context) . PHP_EOL, 3, $filePath);
28+
}
29+
30+
public function info($message, $context = [])
31+
{
32+
$this->log('info', $message, $context);
33+
}
34+
35+
}

Model/Config/PaymentConfigProvider.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,28 @@ public function getConfig()
7171
if ($this->scopeConfig->getValue('payment/moyasar_payments_stc_pay/active')) {
7272
$enabled_method[] = 'stcpay';
7373
}
74+
// Samsung Pay
75+
if ($this->scopeConfig->getValue('payment/moyasar_payments_samsung_pay/active')) {
76+
$enabled_method[] = 'samsungpay';
77+
}
7478

7579
$supported_networks = $this->scopeConfig->getValue('payment/moyasar_payments/schemes');
80+
$samsung_supported_networks = $this->scopeConfig->getValue('payment/moyasar_payments_samsung_pay/schemes');
7681

7782

7883
$config = [
7984
'api_key' => $this->moyasarHelper->publishableApiKey(),
8085
'base_url' => $this->moyasarHelper->apiBaseUrl(),
8186
'country' => $this->scopeConfig->getValue('general/country/default'),
8287
'store_name' => $this->getStoreName(),
88+
'apple_store_name' => $this->getAppleStoreName(),
89+
'samsung' => [
90+
'store_name' => $this->getSamsungStoreName(),
91+
'service_id' => $this->scopeConfig->getValue('payment/moyasar_payments_samsung_pay/service_id'),
92+
'supported_networks' => $samsung_supported_networks ? explode(',', $samsung_supported_networks) : []
93+
],
8394
'domain_name' => $matches[1],
84-
'supported_networks' => explode(',', $supported_networks ? $supported_networks : []),
95+
'supported_networks' => $supported_networks ? explode(',', $supported_networks ) : [],
8596
'methods' => $enabled_method,
8697
'version' => 'Moyasar Http; Magento Plugin v' . MoyasarHelper::VERSION
8798
];
@@ -96,13 +107,33 @@ public function getConfig()
96107
*
97108
* @return string
98109
*/
99-
public function getStoreName()
110+
public function getStoreName($name = null)
100111
{
101-
$store_name = $this->scopeConfig->getValue(self::XML_PATH_STORE_NAME) ?? $this->storeManager->getStore()->getName() ?? 'Store';
112+
$store_name = $name ?? $this->scopeConfig->getValue(self::XML_PATH_STORE_NAME) ?? $this->storeManager->getStore()->getName() ?? 'Store';
102113
// Check is store english (Regex)
103114
if (!preg_match('/\A[\x00-\x7F]+\z/', $store_name)) {
104115
$store_name = 'Store';
105116
}
106117
return $store_name;
107118
}
119+
120+
/**
121+
* Get Apple store name
122+
*
123+
* @return string
124+
*/
125+
public function getAppleStoreName()
126+
{
127+
return $this->getStoreName($this->scopeConfig->getValue('payment/moyasar_payments_apple_pay/apple_store_name'));
128+
}
129+
130+
/**
131+
* Get Samsung store name
132+
*
133+
* @return string
134+
*/
135+
public function getSamsungStoreName()
136+
{
137+
return $this->getStoreName($this->scopeConfig->getValue('payment/moyasar_payments_samsung_pay/samsung_store_name'));
138+
}
108139
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Moyasar\Magento2\Model\Payment;
4+
5+
use Magento\Framework\App\Config\ScopeConfigInterface;
6+
use Magento\Payment\Model\Method\AbstractMethod;
7+
use Moyasar\Magento2\Helper\MoyasarHelper;
8+
9+
class MoyasarPaymentsSamsungPay extends AbstractMethod
10+
{
11+
12+
13+
const CODE = 'moyasar_payments_samsung_pay';
14+
15+
protected $_code = self::CODE;
16+
protected $_canUseInternal = false;
17+
protected $_isGateway = true;
18+
19+
/**
20+
* @var string
21+
* @description Check if the method is active
22+
*/
23+
public function isActive($storeId = null)
24+
{
25+
return $this->_scopeConfig->getValue(MoyasarHelper::XML_PATH_SAMSUNG_PAY_IS_ACTIVE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
26+
}
27+
}

Observer/BeforeOrderPlaceObserver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Magento\Sales\Model\Order;
88
use Moyasar\Magento2\Model\Payment\MoyasarPayments;
99
use Moyasar\Magento2\Model\Payment\MoyasarPaymentsApplePay;
10+
use Moyasar\Magento2\Model\Payment\MoyasarPaymentsSamsungPay;
1011
use Moyasar\Magento2\Model\Payment\MoyasarPaymentsStcPay;
1112

1213
class BeforeOrderPlaceObserver implements ObserverInterface
@@ -21,6 +22,7 @@ public function execute(Observer $observer)
2122
$payment->getMethod() == MoyasarPayments::CODE
2223
|| $payment->getMethod() == MoyasarPaymentsStcPay::CODE
2324
|| $payment->getMethod() == MoyasarPaymentsApplePay::CODE
25+
|| $payment->getMethod() == MoyasarPaymentsSamsungPay::CODE
2426
)) {
2527
$order->setState(Order::STATE_PENDING_PAYMENT);
2628
$order->setCanSendNewEmailFlag(false);

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.0.6",
5+
"version": "5.1.0",
66
"authors": [
77
{
88
"email": "[email protected]",

etc/adminhtml/system.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@
135135
<config_path>payment/moyasar_payments_apple_pay/title</config_path>
136136
<comment>Apple Pay payment title in checkout page.</comment>
137137
</field>
138+
<field id="apple_store_name" translate="label" type="text" sortOrder="3" showInDefault="1"
139+
showInWebsite="1"
140+
showInStore="1">
141+
<label>Apple Store Name</label>
142+
<config_path>payment/moyasar_payments_apple_pay/apple_store_name</config_path>
143+
<comment>This is the name displayed to the customer in the Apple Pay popup during checkout, the
144+
name should be in English. If left empty, the default store name will be used automatically.
145+
</comment>
146+
</field>
138147
</group>
139148
<group id="moyasar_payments_stc_pay" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="5"
140149
translate="label">
@@ -154,6 +163,49 @@
154163
<comment>STC Pay payment title in checkout page.</comment>
155164
</field>
156165
</group>
166+
<group id="moyasar_payments_samsung_pay" showInDefault="1" showInStore="1" showInWebsite="1"
167+
sortOrder="6"
168+
translate="label">
169+
<label>Samsung Pay</label>
170+
<field id="active" showInDefault="1" showInStore="1" showInWebsite="0" sortOrder="2"
171+
translate="label" type="select">
172+
<label>Enabled</label>
173+
<config_path>payment/moyasar_payments_samsung_pay/active</config_path>
174+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
175+
</field>
176+
177+
<field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1"
178+
showInStore="1">
179+
<label>Samsung Pay Title</label>
180+
<config_path>payment/moyasar_payments_samsung_pay/title</config_path>
181+
<comment>Samsung Pay payment title in checkout page.</comment>
182+
</field>
183+
<field id="samsung_store_name" translate="label" type="text" sortOrder="3" showInDefault="1"
184+
showInWebsite="1"
185+
showInStore="1">
186+
<label>Samsung Store Name</label>
187+
<config_path>payment/moyasar_payments_samsung_pay/samsung_store_name</config_path>
188+
<comment>This is the name displayed to the customer in the Samsung Pay popup during checkout,
189+
the name should be in English. If left empty, the default store name will be used
190+
automatically.
191+
</comment>
192+
</field>
193+
<field id="samsung_service_id" translate="label" type="text" sortOrder="4" showInDefault="1"
194+
showInWebsite="1"
195+
showInStore="1">
196+
<label>Service ID</label>
197+
<config_path>payment/moyasar_payments_samsung_pay/service_id</config_path>
198+
<comment>Service id: abcdef1234567890abcdef</comment>
199+
<can_be_empty>0</can_be_empty>
200+
</field>
201+
<field id="samsung_schemes" translate="label" type="multiselect" sortOrder="5" showInDefault="1"
202+
showInWebsite="1" showInStore="0">
203+
<label>Schemes</label>
204+
<config_path>payment/moyasar_payments_samsung_pay/schemes</config_path>
205+
<source_model>Moyasar\Magento2\Model\Config\Schemes</source_model>
206+
<can_be_empty>0</can_be_empty>
207+
</field>
208+
</group>
157209
</group>
158210

159211
</section>

0 commit comments

Comments
 (0)