aboutsummaryrefslogtreecommitdiffstats
path: root/public/admin/model/extension/payment/cardinity.php
blob: 81ee76ae31c871ea06599a39153ad3c86262dbef (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
<?php
use Cardinity\Client;
use Cardinity\Method\Payment;
use Cardinity\Method\Refund;

class ModelExtensionPaymentCardinity extends Model {
	public function getOrder($order_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cardinity_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");

		return $query->row;
	}

	public function createClient($credentials) {
		return Client::create(array(
			'consumerKey'    => $credentials['key'],
			'consumerSecret' => $credentials['secret'],
		));
	}

	public function verifyCredentials($client) {
		$method = new Payment\GetAll(10);

		try {
			$client->call($method);

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

			return false;
		}
	}

	public function getPayment($client, $payment_id) {
		$method = new Payment\Get($payment_id);

		try {
			$payment = $client->call($method);

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

			return false;
		}
	}

	public function getRefunds($client, $payment_id) {
		$method = new Refund\GetAll($payment_id);

		try {
			$refunds = $client->call($method);

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

			return false;
		}
	}

	public function refundPayment($client, $payment_id, $amount, $description) {
		$method = new Refund\Create($payment_id, $amount, $description);

		try {
			$refund = $client->call($method);

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

			return false;
		}
	}

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

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

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