1: | <?php
2: | namespace Opencart\Catalog\Controller\Checkout;
3: | /**
4: | * Class PaymentMethod
5: | *
6: | * @package Opencart\Catalog\Controller\Checkout
7: | */
8: | class PaymentMethod extends \Opencart\System\Engine\Controller {
9: | /**
10: | * @return string
11: | */
12: | public function index(): string {
13: | $this->load->language('checkout/payment_method');
14: |
15: | if (isset($this->session->data['payment_method'])) {
16: | $data['payment_method'] = $this->session->data['payment_method']['name'];
17: | $data['code'] = $this->session->data['payment_method']['code'];
18: | } else {
19: | $data['payment_method'] = '';
20: | $data['code'] = '';
21: | }
22: |
23: | if (isset($this->session->data['comment'])) {
24: | $data['comment'] = $this->session->data['comment'];
25: | } else {
26: | $data['comment'] = '';
27: | }
28: |
29: | $this->load->model('catalog/information');
30: |
31: | $information_info = $this->model_catalog_information->getInformation($this->config->get('config_checkout_id'));
32: |
33: | if ($information_info) {
34: | $data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information.info', 'language=' . $this->config->get('config_language') . '&information_id=' . $this->config->get('config_checkout_id')), $information_info['title']);
35: | } else {
36: | $data['text_agree'] = '';
37: | }
38: |
39: | $data['language'] = $this->config->get('config_language');
40: |
41: | return $this->load->view('checkout/payment_method', $data);
42: | }
43: |
44: | /**
45: | * Get Methods
46: | *
47: | * @return void
48: | */
49: | public function getMethods(): void {
50: | $this->load->language('checkout/payment_method');
51: |
52: | $json = [];
53: |
54: | // Validate cart has products and has stock.
55: | if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
56: | $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
57: | }
58: |
59: | // Validate minimum quantity requirements.
60: | $products = $this->cart->getProducts();
61: |
62: | foreach ($products as $product) {
63: | if (!$product['minimum']) {
64: | $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
65: |
66: | break;
67: | }
68: | }
69: |
70: | if (!$json) {
71: | // Validate if customer session data is set
72: | if (!isset($this->session->data['customer'])) {
73: | $json['error'] = $this->language->get('error_customer');
74: | }
75: |
76: | if ($this->config->get('config_checkout_payment_address') && !isset($this->session->data['payment_address'])) {
77: | $json['error'] = $this->language->get('error_payment_address');
78: | }
79: |
80: | // Validate shipping
81: | if ($this->cart->hasShipping()) {
82: | // Validate shipping address
83: | if (!isset($this->session->data['shipping_address']['address_id'])) {
84: | $json['error'] = $this->language->get('error_shipping_address');
85: | }
86: |
87: | // Validate shipping method
88: | if (!isset($this->session->data['shipping_method'])) {
89: | $json['error'] = $this->language->get('error_shipping_method');
90: | }
91: | }
92: | }
93: |
94: | if (!$json) {
95: | $payment_address = [];
96: |
97: | if ($this->config->get('config_checkout_payment_address') && isset($this->session->data['payment_address'])) {
98: | $payment_address = $this->session->data['payment_address'];
99: | } elseif ($this->config->get('config_checkout_shipping_address') && isset($this->session->data['shipping_address']['address_id'])) {
100: | $payment_address = $this->session->data['shipping_address'];
101: | }
102: |
103: | // Payment methods
104: | $this->load->model('checkout/payment_method');
105: |
106: | $payment_methods = $this->model_checkout_payment_method->getMethods($payment_address);
107: |
108: | if ($payment_methods) {
109: | $json['payment_methods'] = $this->session->data['payment_methods'] = $payment_methods;
110: | } else {
111: | $json['error'] = sprintf($this->language->get('error_no_payment'), $this->url->link('information/contact', 'language=' . $this->config->get('config_language')));
112: | }
113: | }
114: |
115: | $this->response->addHeader('Content-Type: application/json');
116: | $this->response->setOutput(json_encode($json));
117: | }
118: |
119: | /**
120: | * Save
121: | *
122: | * @return void
123: | */
124: | public function save(): void {
125: | $this->load->language('checkout/payment_method');
126: |
127: | $json = [];
128: |
129: | // Validate cart has products and has stock.
130: | if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
131: | $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
132: | }
133: |
134: | // Validate minimum quantity requirements.
135: | $products = $this->cart->getProducts();
136: |
137: | foreach ($products as $product) {
138: | if (!$product['minimum']) {
139: | $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
140: |
141: | break;
142: | }
143: | }
144: |
145: | if (!$json) {
146: | // Validate has payment address if required
147: | if ($this->config->get('config_checkout_payment_address') && !isset($this->session->data['payment_address'])) {
148: | $json['error'] = $this->language->get('error_payment_address');
149: | }
150: |
151: | // Validate shipping
152: | if ($this->cart->hasShipping()) {
153: | // Validate shipping address
154: | if (!isset($this->session->data['shipping_address']['address_id'])) {
155: | $json['error'] = $this->language->get('error_shipping_address');
156: | }
157: |
158: | // Validate shipping method
159: | if (!isset($this->session->data['shipping_method'])) {
160: | $json['error'] = $this->language->get('error_shipping_method');
161: | }
162: | }
163: |
164: | // Validate payment methods
165: | if (isset($this->request->post['payment_method']) && isset($this->session->data['payment_methods'])) {
166: | $payment = explode('.', $this->request->post['payment_method']);
167: |
168: | if (!isset($payment[0]) || !isset($payment[1]) || !isset($this->session->data['payment_methods'][$payment[0]]['option'][$payment[1]])) {
169: | $json['error'] = $this->language->get('error_payment_method');
170: | }
171: | } else {
172: | $json['error'] = $this->language->get('error_payment_method');
173: | }
174: | }
175: |
176: | if (!$json) {
177: | $this->session->data['payment_method'] = $this->session->data['payment_methods'][$payment[0]]['option'][$payment[1]];
178: |
179: | $json['success'] = $this->language->get('text_success');
180: | }
181: |
182: | $this->response->addHeader('Content-Type: application/json');
183: | $this->response->setOutput(json_encode($json));
184: | }
185: |
186: | /**
187: | * Comment
188: | *
189: | * @return void
190: | */
191: | public function comment(): void {
192: | $this->load->language('checkout/payment_method');
193: |
194: | $json = [];
195: |
196: | if (isset($this->session->data['order_id'])) {
197: | $order_id = (int)$this->session->data['order_id'];
198: | } else {
199: | $order_id = 0;
200: | }
201: |
202: | $order_info = $this->model_checkout_order->getOrder($order_id);
203: |
204: | if (!$order_info) {
205: | $json['error'] = $this->language->get('error_order');
206: | }
207: |
208: | if (!$json) {
209: | $this->session->data['comment'] = $this->request->post['comment'];
210: |
211: | $this->load->model('checkout/order');
212: |
213: | $this->model_checkout_order->editComment($order_id, $this->request->post['comment']);
214: |
215: | $json['success'] = $this->language->get('text_comment');
216: | }
217: |
218: | $this->response->addHeader('Content-Type: application/json');
219: | $this->response->setOutput(json_encode($json));
220: | }
221: |
222: | /**
223: | * Agree
224: | *
225: | * @return void
226: | */
227: | public function agree(): void {
228: | $this->load->language('checkout/payment_method');
229: |
230: | $json = [];
231: |
232: | if (isset($this->request->post['agree'])) {
233: | $this->session->data['agree'] = $this->request->post['agree'];
234: | } else {
235: | unset($this->session->data['agree']);
236: | }
237: |
238: | $this->response->addHeader('Content-Type: application/json');
239: | $this->response->setOutput(json_encode($json));
240: | }
241: | }
242: |

OpenCart API API documentation generated by ApiGen dev-master