pp_braintree.php (4933B)
1 <?php 2 class ModelExtensionPaymentPPBraintree extends Model { 3 public function generateToken($gateway, $data) { 4 try { 5 if ($gateway != null) { 6 $client_token = $gateway->clientToken()->generate($data); 7 } else { 8 $client_token = Braintree_ClientToken::generate($data); 9 } 10 11 return $client_token; 12 } catch (Exception $e) { 13 $this->log($e->getMessage()); 14 15 return false; 16 } 17 } 18 19 public function addTransaction($gateway, $data) { 20 try { 21 if ($gateway != null) { 22 $transaction = $gateway->transaction()->sale($data); 23 } else { 24 $transaction = Braintree_Transaction::sale($data); 25 } 26 27 return $transaction; 28 } catch (Exception $e) { 29 $this->log($e->getMessage()); 30 31 return false; 32 } 33 } 34 35 public function getCustomer($gateway, $customer_id, $log = true) { 36 try { 37 if ($gateway != null) { 38 $customer = $gateway->customer()->find($customer_id); 39 } else { 40 $customer = Braintree_Customer::find($customer_id); 41 } 42 43 return $customer; 44 } catch (Exception $e) { 45 if ($log) { 46 $this->log($e->getMessage()); 47 } 48 49 return false; 50 } 51 } 52 53 public function getPaymentMethod($gateway, $token) { 54 try { 55 if ($gateway != null) { 56 $payment_method = $gateway->paymentMethod()->find($token); 57 } else { 58 $payment_method = Braintree_PaymentMethod::find($token); 59 } 60 61 return $payment_method; 62 } catch (Exception $e) { 63 $this->log($e->getMessage()); 64 65 return false; 66 } 67 } 68 69 public function addPaymentMethod($gateway, $data) { 70 try { 71 if ($gateway != null) { 72 $payment_method = $gateway->paymentMethod()->create($data); 73 } else { 74 $payment_method = Braintree_PaymentMethod::create($data); 75 } 76 77 return $payment_method; 78 } catch (Exception $e) { 79 $this->log($e->getMessage()); 80 81 return false; 82 } 83 } 84 85 public function deletePaymentMethod($gateway, $token) { 86 try { 87 if ($gateway != null) { 88 $gateway->paymentMethod()->delete($token); 89 } else { 90 Braintree_PaymentMethod::delete($token); 91 } 92 93 return true; 94 } catch (Exception $e) { 95 $this->log($e->getMessage()); 96 97 return false; 98 } 99 } 100 101 public function getPaymentMethodNonce($gateway, $token) { 102 try { 103 if ($gateway != null) { 104 $response = $gateway->paymentMethodNonce()->find($token); 105 } else { 106 $response = Braintree_PaymentMethodNonce::find($token); 107 } 108 109 return $response; 110 } catch (Exception $e) { 111 $this->log($e->getMessage()); 112 113 return false; 114 } 115 } 116 117 public function createPaymentMethodNonce($gateway, $token) { 118 try { 119 if ($gateway != null) { 120 $response = $gateway->paymentMethodNonce()->create($token); 121 } else { 122 $response = Braintree_PaymentMethodNonce::create($token); 123 } 124 125 return $response; 126 } catch (Exception $e) { 127 $this->log($e->getMessage()); 128 129 return false; 130 } 131 } 132 133 public function setCredentials() { 134 Braintree_Configuration::environment($this->config->get('payment_pp_braintree_environment')); 135 Braintree_Configuration::merchantId($this->config->get('payment_pp_braintree_merchant_id')); 136 Braintree_Configuration::publicKey($this->config->get('payment_pp_braintree_public_key')); 137 Braintree_Configuration::privateKey($this->config->get('payment_pp_braintree_private_key')); 138 } 139 140 public function setGateway($access_token) { 141 return new Braintree_Gateway(array('accessToken' => $access_token)); 142 } 143 144 public function getMethod($address, $total) { 145 $this->load->language('extension/payment/pp_braintree'); 146 147 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('payment_pp_braintree_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')"); 148 149 if ($this->config->get('payment_pp_braintree_total') > 0 && $this->config->get('payment_pp_braintree_total') > $total) { 150 $status = false; 151 } elseif (!$this->config->get('payment_pp_braintree_geo_zone_id')) { 152 $status = true; 153 } elseif ($query->num_rows) { 154 $status = true; 155 } else { 156 $status = false; 157 } 158 159 $method_data = array(); 160 161 if ($status) { 162 $method_data = array( 163 'code' => 'pp_braintree', 164 'title' => $this->language->get('text_title'), 165 'terms' => '', 166 'sort_order' => $this->config->get('payment_pp_braintree_sort_order') 167 ); 168 } 169 170 return $method_data; 171 } 172 173 public function getSupportedCurrencies() { 174 $currencies = array(); 175 176 foreach ($this->config->get('payment_pp_braintree_account') as $currency => $account) { 177 if ($account['status']) { 178 $currencies[] = $currency; 179 } 180 } 181 182 return $currencies; 183 } 184 185 public function log($data) { 186 if ($this->config->get('payment_pp_braintree_debug')) { 187 $log = new Log('braintree.log'); 188 $log->write(print_r($data, true)); 189 } 190 } 191 }