1 | <?php |
||
2 | namespace DigitalOrigin\Pmt\Controller\Payment; |
||
3 | |||
4 | use Magento\Framework\App\Action\Action; |
||
5 | use Magento\Framework\View\Element\BlockInterface; |
||
6 | use Magento\Framework\App\ResourceConnection; |
||
7 | use Magento\Framework\DB\Ddl\Table; |
||
8 | |||
9 | class Iframe extends Action |
||
10 | { |
||
11 | /** Concurrency tablename */ |
||
12 | const LOGS_TABLE = 'pmt_logs'; |
||
13 | |||
14 | /** |
||
15 | * @var \Magento\Framework\View\Result\PageFactory |
||
16 | */ |
||
17 | protected $_pageFactory; |
||
18 | |||
19 | /** @var ResourceConnection $dbObject */ |
||
20 | protected $dbObject; |
||
21 | |||
22 | public function __construct( |
||
23 | \Magento\Framework\App\Action\Context $context, |
||
24 | \Magento\Framework\View\Result\PageFactory $pageFactory, |
||
25 | ResourceConnection $dbObject, |
||
26 | \DigitalOrigin\Pmt\Helper\Config $pmtconfig |
||
27 | ) { |
||
28 | $this->_pageFactory = $pageFactory; |
||
29 | $this->dbObject = $dbObject; |
||
30 | $this->config = $pmtconfig->getConfig(); |
||
31 | return parent::__construct($context); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|\Magento\Framework\View\Result\Page |
||
36 | */ |
||
37 | public function execute() |
||
38 | { |
||
39 | try { |
||
40 | $resultPage = $this->_pageFactory->create(); |
||
41 | $orderId = $this->getRequest()->getParam('orderId'); |
||
42 | if ($orderId == '') { |
||
43 | throw new \Exception('Empty orderId'); |
||
44 | } |
||
45 | |||
46 | if ($this->config['public_key'] == '' || $this->config['secret_key'] == '') { |
||
47 | throw new \Exception('Public and Secret Key not found'); |
||
48 | } |
||
49 | |||
50 | $orderClient = new \PagaMasTarde\OrdersApiClient\Client( |
||
51 | $this->config['public_key'], |
||
52 | $this->config['secret_key'] |
||
53 | ); |
||
54 | |||
55 | $order = $orderClient->getOrder($orderId); |
||
56 | |||
57 | /** @var \DigitalOrigin\Pmt\Block\Payment\Iframe $block */ |
||
58 | $block = $resultPage->getLayout()->getBlock('paylater_payment_iframe'); |
||
59 | $block |
||
60 | ->setEmail($order->getUser()->getEmail()) |
||
61 | ->setOrderUrl($order->getActionUrls()->getForm()) |
||
62 | ->setCheckoutUrl($order->getConfiguration()->getUrls()->getCancel()) |
||
63 | ; |
||
64 | |||
65 | return $resultPage; |
||
66 | } catch (\Exception $exception) { |
||
67 | $this->insertLog($exception); |
||
68 | die($exception->getMessage()); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | private function checkDbLogTable() |
||
73 | { |
||
74 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
75 | $dbConnection = $this->dbObject->getConnection(); |
||
76 | $tableName = $this->dbObject->getTableName(self::LOGS_TABLE); |
||
77 | if (!$dbConnection->isTableExists($tableName)) { |
||
78 | $table = $dbConnection |
||
79 | ->newTable($tableName) |
||
80 | ->addColumn('id', Table::TYPE_SMALLINT, null, array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true)) |
||
81 | ->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false)) |
||
82 | ->addColumn('createdAt', Table::TYPE_TIMESTAMP, null, array('nullable'=>false, 'default'=>TIMESTAMP_INIT)); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
83 | return $dbConnection->createTable($table); |
||
84 | } |
||
85 | |||
86 | return; |
||
87 | } |
||
88 | |||
89 | private function insertLog($exceptionMessage) |
||
90 | { |
||
91 | if ($exceptionMessage instanceof \Exception) { |
||
92 | $this->checkDbLogTable(); |
||
93 | $logObject = new \stdClass(); |
||
94 | $logObject->message = $exceptionMessage->getMessage(); |
||
95 | $logObject->code = $exceptionMessage->getCode(); |
||
96 | $logObject->line = $exceptionMessage->getLine(); |
||
97 | $logObject->file = $exceptionMessage->getFile(); |
||
98 | $logObject->trace = $exceptionMessage->getTraceAsString(); |
||
99 | |||
100 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
101 | $dbConnection = $this->dbObject->getConnection(); |
||
102 | $tableName = $this->dbObject->getTableName(self::LOGS_TABLE); |
||
103 | $dbConnection->insert($tableName, array('log' => json_encode($logObject))); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 |