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
|
<?php
class ModelExtensionPaymentPPBraintree extends Model {
public function generateToken($gateway, $data = array()) {
try {
if ($gateway != null) {
$client_token = $gateway->clientToken()->generate($data);
} else {
$client_token = Braintree_ClientToken::generate($data);
}
return $client_token;
} catch (Exception $e) {
$this->log($e->getMessage());
return false;
}
}
public function getTransaction($gateway, $transaction_id) {
try {
if ($gateway != null) {
$transaction = $gateway->transaction()->find($transaction_id);
} else {
$transaction = Braintree_Transaction::find($transaction_id);
}
if ($transaction) {
return $transaction;
} else {
return false;
}
} catch (Exception $e) {
$this->log($e->getMessage());
return false;
}
}
public function getTransactions($gateway, $data = array()) {
try {
if ($gateway != null) {
$transactions = $gateway->transaction()->search($data);
} else {
$transactions = Braintree_Transaction::search($data);
}
if ($transactions) {
return $transactions;
} else {
return false;
}
} catch (Exception $e) {
$this->log($e->getMessage());
return false;
}
}
public function voidTransaction($gateway, $transaction_id) {
try {
if ($gateway != null) {
$transaction = $gateway->transaction()->void($transaction_id);
} else {
$transaction = Braintree_Transaction::void($transaction_id);
}
if ($transaction) {
return $transaction;
} else {
return false;
}
} catch (Exception $e) {
$this->log($e->getMessage());
return false;
}
}
public function settleTransaction($gateway, $transaction_id, $amount) {
try {
if ($gateway != null) {
$transaction = $gateway->transaction()->submitForSettlement($transaction_id, $amount);
} else {
$transaction = Braintree_Transaction::submitForSettlement($transaction_id, $amount);
}
if ($transaction) {
return $transaction;
} else {
return false;
}
} catch (Exception $e) {
$this->log($e->getMessage());
return false;
}
}
public function refundTransaction($gateway, $transaction_id, $amount) {
try {
if ($gateway != null) {
$transaction = $gateway->transaction()->refund($transaction_id, $amount);
} else {
$transaction = Braintree_Transaction::refund($transaction_id, $amount);
}
if ($transaction) {
return $transaction;
} else {
return false;
}
} catch (Exception $e) {
$this->log($e->getMessage());
return false;
}
}
public function verifyCredentials($gateway) {
try {
//Try API call, if no exception is thrown, the credentials are correct
if ($gateway != null) {
$client_token = $gateway->clientToken()->generate();
} else {
$client_token = Braintree_ClientToken::generate();
}
return $client_token;
} catch (Exception $e) {
$this->log($e->getMessage());
return false;
}
}
public function verifyMerchantAccount($gateway, $merchant_account_id) {
try {
//Try API call, if no exception is thrown, the above credentials are correct
if ($gateway != null) {
$merchant_account = $gateway->merchantAccount()->find($merchant_account_id);
} else {
$merchant_account = Braintree_MerchantAccount::find($merchant_account_id);
}
if ($merchant_account && $merchant_account->status == 'active') {
return $merchant_account;
} else {
return false;
}
} catch (Exception $e) {
$this->log($e->getMessage());
return false;
}
}
public function setGateway($access_token) {
return new Braintree_Gateway(array('accessToken' => $access_token));
}
public function log($data) {
if ($this->config->get('payment_pp_braintree_debug')) {
$backtrace = debug_backtrace();
$log = new Log('braintree.log');
$log->write('(' . $backtrace[1]['class'] . '::' . $backtrace[1]['function'] . ') - ' . print_r($data, true));
}
}
}
|