1: | <?php
2: | namespace Opencart\Catalog\Controller\Api\Sale;
3: | /**
4: | * Class Payment Method
5: | *
6: | * @package Opencart\Catalog\Controller\Api\Sale
7: | */
8: | class PaymentMethod extends \Opencart\System\Engine\Controller {
9: | /**
10: | * @return void
11: | */
12: | public function index(): void {
13: | $this->load->language('api/sale/payment_method');
14: |
15: | $json = [];
16: |
17: | if (!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) {
18: | $json['error'] = $this->language->get('error_product');
19: | }
20: |
21: | if ($this->config->get('config_checkout_payment_address') && !isset($this->session->data['payment_address'])) {
22: | $json['error'] = $this->language->get('error_payment_address');
23: | }
24: |
25: | if (!$json) {
26: | $payment_address = [];
27: |
28: | if (isset($this->session->data['payment_address'])) {
29: | $payment_address = $this->session->data['payment_address'];
30: | } elseif ($this->config->get('config_checkout_shipping_address') && isset($this->session->data['shipping_address'])) {
31: | $payment_address = $this->session->data['shipping_address'];
32: | }
33: |
34: | $this->load->model('checkout/payment_method');
35: |
36: | $payment_methods = $this->model_checkout_payment_method->getMethods($payment_address);
37: |
38: | if ($payment_methods) {
39: | $json['payment_methods'] = $this->session->data['payment_methods'] = $payment_methods;
40: | } else {
41: | $json['error'] = $this->language->get('error_no_payment');
42: | }
43: | }
44: |
45: | $this->response->addHeader('Content-Type: application/json');
46: | $this->response->setOutput(json_encode($json));
47: | }
48: |
49: | /**
50: | * Save
51: | *
52: | * @return void
53: | */
54: | public function save(): void {
55: | $this->load->language('api/sale/payment_method');
56: |
57: | $json = [];
58: |
59: | // Payment Address
60: | if ($this->config->get('config_checkout_payment_address') && !isset($this->session->data['payment_address'])) {
61: | $json['error'] = $this->language->get('error_payment_address');
62: | }
63: |
64: | // Payment Method
65: | if (isset($this->request->post['payment_method']) && isset($this->session->data['payment_methods'])) {
66: | $payment = explode('.', $this->request->post['payment_method']);
67: |
68: | if (!isset($payment[0]) || !isset($payment[1]) || !isset($this->session->data['payment_methods'][$payment[0]]['option'][$payment[1]])) {
69: | $json['error'] = $this->language->get('error_payment_method');
70: | }
71: | } else {
72: | $json['error'] = $this->language->get('error_payment_method');
73: | }
74: |
75: | if (!$json) {
76: | $this->session->data['payment_method'] = $this->session->data['payment_methods'][$payment[0]]['option'][$payment[1]];
77: |
78: | $json['success'] = $this->language->get('text_success');
79: | }
80: |
81: | $this->response->addHeader('Content-Type: application/json');
82: | $this->response->setOutput(json_encode($json));
83: | }
84: | }
85: |

OpenCart API API documentation generated by ApiGen dev-master