aboutsummaryrefslogtreecommitdiffstats
path: root/public/admin/model/extension/payment/klarna_checkout.php
blob: d9e9cbc97356b0d25ec82a51aee071c432fda405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
use Klarna\Rest\Transport\Connector as KCConnector;
use Klarna\Rest\Transport\ConnectorInterface as KCConnectorInterface;
use Klarna\Rest\OrderManagement\Order as KCOrder;

class ModelExtensionPaymentKlarnaCheckout extends Model {
	public function connector($merchant_id, $secret, $url) {
		try {
			$connector = KCConnector::create(
				$merchant_id,
				$secret,
				$url
			);

			return $connector;
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function omRetrieve(KCConnector $connector, $order_id) {
		try {
			$order = new KCOrder($connector, $order_id);

			return $order->fetch();
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function omCancel(KCConnector $connector, $order_id) {
		try {
			$order = new KCOrder($connector, $order_id);

			return $order->cancel();
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function omCapture(KCConnector $connector, $order_id, $data) {
		try {
			$order = new KCOrder($connector, $order_id);

			return $order->createCapture($data);
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function omRefund(KCConnector $connector, $order_id, $data) {
		try {
			$order = new KCOrder($connector, $order_id);

			return $order->refund($data);
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function omExtendAuthorizationTime(KCConnector $connector, $order_id) {
		try {
			$order = new KCOrder($connector, $order_id);

			return $order->extendAuthorizationTime();
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function omUpdateMerchantReference(KCConnector $connector, $order_id, $data) {
		try {
			$order = new KCOrder($connector, $order_id);

			return $order->updateMerchantReferences($data);
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function omUpdateAddress(KCConnector $connector, $order_id, $data) {
		try {
			$order = new KCOrder($connector, $order_id);

			return $order->updateCustomerDetails($data);
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function omReleaseAuthorization(KCConnector $connector, $order_id) {
		try {
			$order = new KCOrder($connector, $order_id);

			return $order->releaseRemainingAuthorization();
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function omShippingInfo(KCConnector $connector, $order_id, $capture_id, $data) {
		try {
			$order = new KCOrder($connector, $order_id);

			$capture = $order->fetchCapture($capture_id);
			return $capture->addShippingInfo($data);
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function omCustomerDetails(KCConnector $connector, $order_id, $capture_id, $data) {
		try {
			$order = new KCOrder($connector, $order_id);

			$capture = $order->fetchCapture($capture_id);
			return $capture->updateCustomerDetails($data);
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function omTriggerSendOut(KCConnector $connector, $order_id, $capture_id) {
		try {
			$order = new KCOrder($connector, $order_id);

			$capture = $order->fetchCapture($capture_id);
			return $capture->triggerSendout();
		} catch (\Exception $e) {
			$this->log($e->getMessage());

			return false;
		}
	}

	public function getConnector($accounts, $currency) {
		$klarna_account = false;
		$connector = false;

		if ($accounts && $currency) {
			foreach ($accounts as $account) {
				if ($account['currency'] == $currency) {
					if ($account['environment'] == 'test') {
						if ($account['api'] == 'NA') {
							$base_url = KCConnectorInterface::NA_TEST_BASE_URL;
						} elseif ($account['api'] == 'EU')  {
							$base_url = KCConnectorInterface::EU_TEST_BASE_URL;
						}
					} elseif ($account['environment'] == 'live') {
						if ($account['api'] == 'NA') {
							$base_url = KCConnectorInterface::NA_BASE_URL;
						} elseif ($account['api'] == 'EU')  {
							$base_url = KCConnectorInterface::EU_BASE_URL;
						}
					}

					$klarna_account = $account;
					$connector = $this->connector(
						$account['merchant_id'],
						$account['secret'],
						$base_url
					);

					break;
				}
			}
		}

		return array($klarna_account, $connector);
	}

	public function getOrder($order_id) {
		return $this->db->query("SELECT * FROM `" . DB_PREFIX . "klarna_checkout_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1")->row;
	}

	public function checkForPaymentTaxes() {
		$query = $this->db->query("SELECT COUNT(*) AS `total` FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "tax_rule tr ON (`tr`.`tax_class_id` = `p`.`tax_class_id`) WHERE `tr`.`based` = 'payment'");

		return $query->row['total'];
	}

	public function install() {
		$this->db->query("
			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "klarna_checkout_order` (
			  `klarna_checkout_order_id` INT(11) NOT NULL AUTO_INCREMENT,
			  `order_id` INT(11) NOT NULL,
			  `order_ref` VARCHAR(255) NOT NULL,
			  `data` text NOT NULL,
			  PRIMARY KEY (`klarna_checkout_order_id`)
			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
	}

	public function uninstall() {
		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "klarna_checkout_order`;");
	}

	public function log($data) {
		if ($this->config->get('klarna_checkout_debug')) {
			$backtrace = debug_backtrace();
			$log = new Log('klarna_checkout.log');
			$log->write('(' . $backtrace[1]['class'] . '::' . $backtrace[1]['function'] . ') - ' . print_r($data, true));
		}
	}
}