Skip to content

Commit 8e3a1b1

Browse files
committed
WD-314
1 parent 7ec1935 commit 8e3a1b1

6 files changed

Lines changed: 130 additions & 18 deletions

File tree

Api/ApiInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ interface ApiInterface
88
{
99
/**
1010
* @param string $cartId
11-
* @param string $method The payment method code
11+
* @param string $method
12+
* @param mixed $customerData
1213
* @return string
1314
*/
14-
public function getToken($cartId, $method);
15+
public function getToken($cartId, $method, $customerData);
1516
}

Api/Token.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function __construct(
8484
/**
8585
* @inheritdoc
8686
*/
87-
public function getToken($cartId, $method)
87+
public function getToken($cartId, $method, $customerData)
8888
{
8989
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
9090
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
@@ -115,7 +115,6 @@ public function getToken($cartId, $method)
115115
}
116116
$addData = $cart->getQuote()->getBillingAddress()->getData();
117117
$addInfo = [
118-
'email' => isset($addData['email']) ? $addData['email'] : '',
119118
'firstname' => isset($addData['firstname']) ? $addData['firstname'] : '',
120119
'middlename' => isset($addData['middlename']) ? $addData['middlename'] : '',
121120
'lastname' => isset($addData['lastname']) ? $addData['lastname'] : '',
@@ -127,6 +126,18 @@ public function getToken($cartId, $method)
127126

128127
try {
129128
$decrypted_key = $this->encryptor->decrypt($this->_config['secret_key']);
129+
$email = $this->_checkoutSession->getQuote()->getCustomerEmail();
130+
131+
if (empty($email)){
132+
$email = $customerData;
133+
}
134+
if (empty($email)){
135+
$email = $cart->getQuote()->getShippingAddress()->getEmail();
136+
}
137+
if (empty($email)){
138+
$email = $cart->getQuote()->getBillingAddress()->getEmail();
139+
}
140+
130141
$merchant_id = $this->_config['merchant_id'];
131142
$merchant_data = json_encode($addInfo);
132143
$requestData = [
@@ -140,6 +151,9 @@ public function getToken($cartId, $method)
140151
'response_url' => $this->urlBuilder->getUrl('checkout/onepage/success'),
141152
'currency' => $this->_checkoutSession->getQuote()->getCurrency()->getBaseCurrencyCode()
142153
];
154+
155+
if (!empty($email))
156+
$requestData['sender_email'] = $email;
143157
if (!empty($merchant_data))
144158
$requestData['merchant_data'] = $merchant_data;
145159
$sign = $this->getSignature($requestData, $decrypted_key);

Model/Fondy.php

100644100755
Lines changed: 106 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ protected function getOrder($orderId)
112112
* @param $orderId
113113
* @return float
114114
*/
115-
public function getAmount($orderId)
115+
public function getAmount($order)
116116
{
117-
return $this->getOrder($orderId)->getGrandTotal();
117+
return $order->getGrandTotal();
118118
}
119119

120120
public function getSignature($data, $password, $encoded = true)
@@ -147,14 +147,90 @@ public function getCustomerId($orderId)
147147

148148

149149
/**
150-
* Получить код используемой валюты по orderId заказа
150+
* Получить код используемой валюты по $order
151151
*
152-
* @param $orderId
152+
* @param $order
153153
* @return null|string
154154
*/
155-
public function getCurrencyCode($orderId)
155+
public function getCurrencyCode($order)
156156
{
157-
return $this->getOrder($orderId)->getBaseCurrencyCode();
157+
return $order->getBaseCurrencyCode();
158+
}
159+
160+
/**
161+
* Get Merchant Data string
162+
*
163+
* @param $order
164+
* @return null|string
165+
*/
166+
public function getMerchantDataString($order)
167+
{
168+
$addData = $order->getBillingAddress()->getData();
169+
if(!$addData){
170+
$addData = $order->getShippigAddress()->getData();
171+
}
172+
$skuString = '';
173+
try {
174+
$orderItems = $order->getAllVisibleItems();
175+
$countItems = count($orderItems);
176+
$i = 0;
177+
foreach ($orderItems as $key => $orderItem) {
178+
$sku = $orderItem->getData()['sku'];
179+
if ($countItems > 1) {
180+
$skuString .= ++$i === $countItems ? $sku . '' : $sku . ', ';
181+
} else {
182+
$skuString .= $sku;
183+
}
184+
}
185+
} catch (Exception $e) {
186+
$skuString = "No sku";
187+
$this->_logger->debug("Cant get products sku");
188+
}
189+
190+
$addInfo = [
191+
'email' => isset($addData['email']) ? $addData['email'] : '',
192+
'firstname' => isset($addData['firstname']) ? $addData['firstname'] : '',
193+
'middlename' => isset($addData['middlename']) ? $addData['middlename'] : '',
194+
'lastname' => isset($addData['lastname']) ? $addData['lastname'] : '',
195+
'company' => isset($addData['company']) ? $addData['company'] : '',
196+
'street' => isset($addData['street']) ? $addData['street'] : '',
197+
'city' => isset($addData['city']) ? $addData['city'] : '',
198+
'region' => isset($addData['region']) ? $addData['region'] : '',
199+
'phone' => isset($addData['telephone']) ? $addData['telephone'] : '',
200+
'products_sku' => $skuString
201+
];
202+
try {
203+
$addInfo['shipping_price'] = number_format($order->getShippingAmount(), 2, '.', '');
204+
} catch (Exception $e) {
205+
$this->_logger->debug("Can't get products shipping price");
206+
}
207+
return $addInfo;
208+
}
209+
210+
/**
211+
* Get Reservation Data string
212+
*
213+
* @param $order
214+
* @return null|string
215+
*/
216+
public function getReservDataString($order)
217+
{
218+
$addData = $order->getBillingAddress()->getData();
219+
if(!$addData){
220+
$addData = $order->getShippigAddress()->getData();
221+
}
222+
223+
$addInfo = [
224+
'customer_zip' => isset($addData['postcode']) ? $addData['postcode'] : '',
225+
'customer_name' => $addData['firstname'] . ' ' . $addData['middlename'] . ' ' . $addData['lastname'],
226+
'customer_address' => isset($addData['street']) ? $addData['street'] : '',
227+
'customer_state' => isset($addData['region_id']) ? $addData['region_id'] : '',
228+
'customer_country' => isset($addData['country_id']) ? $addData['country_id'] : '',
229+
'phonemobile' => isset($addData['telephone']) ? $addData['telephone'] : '',
230+
'account' => isset($addData['email']) ? $addData['email'] : ''
231+
];
232+
233+
return $addInfo;
158234
}
159235

160236

@@ -227,18 +303,28 @@ public function getDataIntegrityCode()
227303
*/
228304
public function getPostData($orderId)
229305
{
306+
$order = $this->getOrder($orderId);
307+
$merchant_data = $this->getMerchantDataString($order);
308+
$reservation_data = $this->getReservDataString($order);
309+
$email = $order->getCustomerEmail();
230310
$postData = array(
231311
'order_id' => $orderId . "#" . time(),
232312
'merchant_id' => $this->getConfigData("FONDY_MERCHANT_ID"),
233313
'amount' => round(
234-
number_format($this->getAmount($orderId), 2, '.', '') * 100
314+
number_format($this->getAmount($order), 2, '.', '') * 100
235315
),
236316
'order_desc' => __("Pay order №") . $orderId,
317+
'sender_email' => $email,
237318
'product_id' => 'Fondy',
238319
'server_callback_url' => $this->urlBuilder->getUrl('fondy/url/fondysuccess'),
239320
'response_url' => $this->urlBuilder->getUrl('checkout/onepage/success'),
240-
'currency' => $this->getCurrencyCode($orderId)
321+
'currency' => $this->getCurrencyCode($order)
241322
);
323+
if (!empty($merchant_data)) {
324+
$postData['merchant_data'] = json_encode($merchant_data);
325+
$postData['reservation_data'] = base64_encode(json_encode($reservation_data));
326+
}
327+
242328
if ($this->getConfigData("invoice_before_fraud_review")) {
243329
$postData['preauth'] = "Y";
244330
}
@@ -297,9 +383,11 @@ public function processResponse($responseData)
297383
$this->_logger->debug("processResponse", $debugData);
298384

299385
if ($this->checkFondyResponse($responseData)) {
386+
300387
list($orderId,) = explode('#', $responseData['order_id']);
301388
$order = $this->getOrder($orderId);
302389
$state = $order->getStatus();
390+
303391
if (!empty($state) && $order && ($this->_processOrder($order, $responseData) === true)) {
304392
return 'OK';
305393
} else {
@@ -328,11 +416,19 @@ protected function _processOrder(Order $order, $response)
328416
$this->_logger->debug("_processOrder: amount mismatch, order FAILED");
329417
return false;
330418
}
419+
331420
if ($response["order_status"] == 'approved') {
332421
$this->createTransaction($order, $response);
422+
$order_status = $this->getConfigData("order_status");
423+
if ($order_status == 'pending') {
424+
//Preevent incorrect status
425+
$order_status = 'processing';
426+
}
427+
$order->addStatusHistoryComment("Fondy payment id: " . $response['payment_id']);
428+
$order->addStatusHistoryComment("Fondy order time: " . $response['order_time']);
333429
$order
334-
->setState($this->getConfigData("order_status"))
335-
->setStatus($order->getConfig()->getStateDefaultStatus($this->getConfigData("order_status")))
430+
->setState($order_status)
431+
->setStatus($order->getConfig()->getStateDefaultStatus($order_status))
336432
->save();
337433
$this->_logger->debug("_processOrder: order state changed:" . $this->getConfigData("order_status"));
338434
$this->_logger->debug("_processOrder: order data saved, order OK");

etc/adminhtml/system.xml

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
2121
</field>
2222
<field id="order_status" translate="label" type="select" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
23-
<label>New Order Status</label>
23+
<label>Paid Order Status</label>
2424
<source_model>Magento\Sales\Model\Config\Source\Order\Status</source_model>
2525
</field>
2626
<field id="invoice_before_fraud_review" translate="label" type="select" sortOrder="11" showInDefault="1" showInWebsite="1" showInStore="0">
@@ -49,7 +49,7 @@
4949
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
5050
</field>
5151
<field id="order_status" translate="label" type="select" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
52-
<label>New Order Status</label>
52+
<label>Paid Order Status</label>
5353
<source_model>Magento\Sales\Model\Config\Source\Order\Status</source_model>
5454
</field>
5555
<field id="cctypes" translate="label" type="multiselect" sortOrder="68" showInDefault="1" showInWebsite="1" showInStore="0">

view/frontend/requirejs-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var config = {
22
paths: {
3-
'$checkout': 'https://unpkg.com/ipsp-js-sdk@1.0.15/dist/checkout.min'
3+
'$checkout': 'https://unpkg.com/ipsp-js-sdk@latest/dist/checkout.min'
44
},
55
shim: {
66
'$checkout': {

view/frontend/web/js/view/payment/method-renderer/fondy-direct-method.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ define(
7272
var serviceUrl = urlApi.createUrl('/fondy/get-payment-token', {});
7373
var Payload = {
7474
cartId: quote.getQuoteId(),
75-
method: this.item.method
75+
method: this.item.method,
76+
customerData: $(loginFormSelector + ' input[name=username]').val()
7677
};
7778
storage.post(
7879
serviceUrl, JSON.stringify(Payload)

0 commit comments

Comments
 (0)