Skip to content

Commit e7df0eb

Browse files
committed
add: magento extension code base
0 parents  commit e7df0eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3035
-0
lines changed

Fave/PaymentGateway/Block/Info.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Fave\PaymentGateway\Block;
7+
8+
use Magento\Framework\Phrase;
9+
use Magento\Payment\Block\ConfigurableInfo;
10+
use Fave\PaymentGateway\Gateway\Response\FraudHandler;
11+
12+
class Info extends ConfigurableInfo
13+
{
14+
/**
15+
* Returns label
16+
*
17+
* @param string $field
18+
* @return Phrase
19+
*/
20+
protected function getLabel($field)
21+
{
22+
return __($field);
23+
}
24+
25+
/**
26+
* Returns value view
27+
*
28+
* @param string $field
29+
* @param string $value
30+
* @return string | Phrase
31+
*/
32+
protected function getValueView($field, $value)
33+
{
34+
switch ($field) {
35+
case FraudHandler::FRAUD_MSG_LIST:
36+
return implode('; ', $value);
37+
}
38+
return parent::getValueView($field, $value);
39+
}
40+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
namespace Fave\PaymentGateway\Block;
3+
4+
class Thankyou extends \Magento\Sales\Block\Order\Totals
5+
{
6+
protected $checkoutSession;
7+
protected $customerSession;
8+
protected $_orderFactory;
9+
protected $_storeManager;
10+
protected $_messageManager;
11+
private $order;
12+
13+
public function __construct(
14+
\Magento\Checkout\Model\Session $checkoutSession,
15+
\Magento\Customer\Model\Session $customerSession,
16+
\Magento\Sales\Model\OrderFactory $orderFactory,
17+
\Magento\Framework\View\Element\Template\Context $context,
18+
\Magento\Framework\Registry $registry,
19+
\Magento\Store\Model\StoreManagerInterface $storeManager,
20+
\Magento\Framework\Message\ManagerInterface $messageManager,
21+
\Magento\Sales\Api\Data\OrderInterface $order,
22+
array $data = []
23+
) {
24+
parent::__construct($context, $registry, $data);
25+
$this->_checkoutSession = $checkoutSession;
26+
$this->customerSession = $customerSession;
27+
$this->_orderFactory = $orderFactory;
28+
$this->_storeManager = $storeManager;
29+
$this->_messageManager = $messageManager;
30+
$this->order = $order;
31+
}
32+
33+
public function getRealOrderId()
34+
{
35+
$lastorderId = $this->_checkoutSession->getLastOrderId();
36+
return $lastorderId;
37+
}
38+
39+
public function getOrder()
40+
{
41+
if ($this->_checkoutSession->getLastRealOrderId()) {
42+
$order = $this->order->loadByIncrementId($this->_checkoutSession->getLastRealOrderId());
43+
return $order;
44+
}
45+
return false;
46+
}
47+
48+
public function getCustomerId()
49+
{
50+
return $this->customerSession->getCustomer()->getId();
51+
}
52+
53+
public function getShippingInfo()
54+
{
55+
$order = $this->getOrder();
56+
57+
if($order) {
58+
$address = $order->getShippingAddress();
59+
60+
return $address;
61+
}
62+
return false;
63+
}
64+
65+
public function getStatus()
66+
{
67+
$order = $this->getOrder();
68+
//$additional_info = $order->getPayment()->getAdditionalInformation();
69+
//$status_code = isset($additional_info) ? $additional_info['status_code'] : null;
70+
71+
$route_params = $this->getRequest()->getParams();
72+
$status = $route_params['status'];
73+
74+
if (!empty($status)) {
75+
if ($status == "rejected") {
76+
$this->_messageManager->addError('Payment was declined.');
77+
}
78+
}
79+
return $status;
80+
}
81+
82+
public function getFormattedPrice()
83+
{
84+
$order = $this->getOrder();
85+
86+
if($order) {
87+
$currency_code = $this->_storeManager->getStore()->getCurrentCurrencyCode();
88+
$formatted_price = $currency_code . ' ' . number_format((float)$order->getGrandTotal(), 2, '.', '');
89+
return $formatted_price;
90+
}
91+
return false;
92+
}
93+
94+
public function getContinueUrl()
95+
{
96+
return $this->_storeManager->getStore()->getBaseUrl();
97+
}
98+
}
99+
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
namespace Fave\PaymentGateway\Controller\Callback;
3+
4+
use Magento\Framework\App\CsrfAwareActionInterface;
5+
use Magento\Framework\App\RequestInterface;
6+
use Magento\Framework\App\Request\InvalidRequestException;
7+
use Magento\Sales\Model\Service\InvoiceService;
8+
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
9+
use Magento\Framework\Controller\ResultInterface;
10+
use Magento\Framework\Controller\Result\JsonFactory;
11+
use Magento\Payment\Gateway\ConfigInterface;
12+
use Magento\Sales\Model\Order\Invoice;
13+
use Magento\Sales\Api\InvoiceRepositoryInterface;
14+
use Magento\Framework\DB\Transaction;
15+
use Magento\Checkout\Model\Session;
16+
17+
class Index extends \Magento\Framework\App\Action\Action implements CsrfAwareActionInterface
18+
{
19+
protected $_pageFactory;
20+
protected $_curl;
21+
protected $_storeManager;
22+
protected $orderRepository;
23+
protected $transactionBuilder;
24+
protected $invoiceService;
25+
protected $invoiceSender;
26+
private $resultJsonFactory;
27+
private $config;
28+
private $order;
29+
private $checkoutSession;
30+
31+
public function __construct(
32+
\Magento\Framework\App\Action\Context $context,
33+
\Magento\Framework\View\Result\PageFactory $pageFactory,
34+
\Magento\Framework\HTTP\Client\Curl $curl,
35+
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
36+
\Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface $transactionBuilder,
37+
InvoiceService $invoiceService,
38+
InvoiceRepositoryInterface $invoiceRepository,
39+
Transaction $transaction,
40+
InvoiceSender $invoiceSender,
41+
JsonFactory $resultJsonFactory,
42+
ConfigInterface $config,
43+
\Magento\Sales\Api\Data\OrderInterface $order,
44+
Session $checkoutSession,
45+
\Magento\Store\Model\StoreManagerInterface $storeManager)
46+
{
47+
$this->_pageFactory = $pageFactory;
48+
$this->_curl = $curl;
49+
$this->_storeManager = $storeManager;
50+
$this->orderRepository = $orderRepository;
51+
$this->transactionBuilder = $transactionBuilder;
52+
$this->invoiceService = $invoiceService;
53+
$this->invoiceRepository = $invoiceRepository;
54+
$this->transaction = $transaction;
55+
$this->invoiceSender = $invoiceSender;
56+
$this->resultJsonFactory = $resultJsonFactory;
57+
$this->config = $config;
58+
$this->order = $order;
59+
$this->checkoutSession = $checkoutSession;
60+
return parent::__construct($context);
61+
}
62+
63+
public function execute()
64+
{
65+
$post = $this->getRequest()->getContent();
66+
$array = json_decode($post, true);
67+
$omni_reference = $array['omni_reference'];
68+
$statusCode = $array['status_code'];
69+
70+
$route_params = $this->getRequest()->getParams();
71+
$orderId = $route_params['order_id'];
72+
73+
$order = $this->order->loadByIncrementId($orderId);
74+
75+
$payment = $order->getPayment();
76+
$trxId = $payment->getLastTransId();
77+
$additional_info = $payment->getAdditionalInformation();
78+
79+
if (array_key_exists('status_code', $additional_info)) {
80+
$comments = 'This transaction has been acknowledged.';
81+
}
82+
else {
83+
$comments = $this-> addTransactionToOrder($orderId, $order, $statusCode);
84+
}
85+
86+
$resultJson = $this->resultJsonFactory->create();
87+
return $resultJson->setData([
88+
'order_id' => $orderId,
89+
'status_code' => $statusCode,
90+
'message' => $comments
91+
]);
92+
93+
exit;
94+
}
95+
96+
public function addTransactionToOrder($orderId, $order, $statusCode) {
97+
try {
98+
99+
100+
// Prepare payment object
101+
$payment = $order->getPayment();
102+
$trxId = $payment->getLastTransId();
103+
$uuid = $trxId . '-' . $orderId;
104+
105+
$payment->setParentTransactionId($trxId);
106+
$payment->setLastTransId($uuid);
107+
$payment->setTransactionId($uuid);
108+
$payment->setAdditionalInformation('status_code', $statusCode);
109+
$payment->canRefund();
110+
111+
112+
$trxId = $payment->getLastTransId();
113+
$orderCurrencyCode = $this->_storeManager->getStore()->getCurrentCurrency()->getCode();
114+
115+
$formatedPrice = $orderCurrencyCode . ' ' . number_format((float)$order->getGrandTotal(), 2, '.', '');
116+
117+
// Prepare transaction
118+
$transaction = $this->transactionBuilder->setPayment($payment)
119+
->setOrder($order)
120+
->setTransactionId($uuid)
121+
->setFailSafe(true)
122+
->build(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_CAPTURE);
123+
124+
if ($statusCode == "4") {
125+
$this->cancelOrder($order);
126+
$comments = __('Payment of %1 was declined.', $formatedPrice);
127+
}
128+
elseif ($statusCode == "2") {
129+
$comments = __('Payment of %1 has been completed successfully.', $formatedPrice);
130+
$this-> createInvoice($order, $formatedPrice);
131+
}
132+
else {
133+
$comments = __('There was a problem processing the payment of %1. Please contact Fave for more information', $formatedPrice);
134+
}
135+
136+
$payment->addTransactionCommentsToOrder($transaction, $comments);
137+
$payment->save();
138+
139+
return $comments;
140+
141+
} catch (Exception $e) {
142+
$this->messageManager->addExceptionMessage($e, $e->getMessage());
143+
}
144+
}
145+
146+
private function cancelOrder($order) {
147+
if ($order->canCancel()) {
148+
try {
149+
$order->cancel();
150+
151+
// remove status history set in _setState
152+
$order->getStatusHistoryCollection(true);
153+
$order->save();
154+
} catch (Exception $e) {
155+
$this->messageManager->addExceptionMessage($e, $e->getMessage());
156+
}
157+
}
158+
}
159+
160+
public function createInvoice($order, $formatedPrice) {
161+
// Prepare the invoice
162+
$invoice = $this->invoiceService->prepareInvoice($order);
163+
$invoice->setRequestedCaptureCase(Invoice::CAPTURE_ONLINE);
164+
$invoice->setState(Invoice::STATE_PAID);
165+
$invoice->setTransactionId($order->getPayment()->getLastTransId());
166+
$invoice->setBaseGrandTotal((float)$order->getGrandTotal());
167+
$invoice->register();
168+
$invoice->getOrder()->setIsInProcess(true);
169+
$invoice->pay();
170+
171+
172+
// Create the transaction
173+
$transactionSave = $this->transaction
174+
->addObject($invoice)
175+
->addObject($order);
176+
$transactionSave->save();
177+
178+
// Update the order
179+
$order->setTotalPaid($order->getTotalPaid());
180+
$order->setBaseTotalPaid($order->getBaseTotalPaid());
181+
$order->save();
182+
183+
// Save the invoice
184+
$this->invoiceRepository->save($invoice);
185+
}
186+
187+
public function createCsrfValidationException(RequestInterface $request): ? InvalidRequestException
188+
{
189+
return null;
190+
}
191+
192+
public function validateForCsrf(RequestInterface $request): ?bool
193+
{
194+
return true;
195+
}
196+
}
197+

0 commit comments

Comments
 (0)