pp_braintree.php (4145B)
1 <?php 2 class ModelExtensionPaymentPPBraintree extends Model { 3 public function generateToken($gateway, $data = array()) { 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 getTransaction($gateway, $transaction_id) { 20 try { 21 if ($gateway != null) { 22 $transaction = $gateway->transaction()->find($transaction_id); 23 } else { 24 $transaction = Braintree_Transaction::find($transaction_id); 25 } 26 27 if ($transaction) { 28 return $transaction; 29 } else { 30 return false; 31 } 32 } catch (Exception $e) { 33 $this->log($e->getMessage()); 34 35 return false; 36 } 37 } 38 39 public function getTransactions($gateway, $data = array()) { 40 try { 41 if ($gateway != null) { 42 $transactions = $gateway->transaction()->search($data); 43 } else { 44 $transactions = Braintree_Transaction::search($data); 45 } 46 47 if ($transactions) { 48 return $transactions; 49 } else { 50 return false; 51 } 52 } catch (Exception $e) { 53 $this->log($e->getMessage()); 54 55 return false; 56 } 57 } 58 59 public function voidTransaction($gateway, $transaction_id) { 60 try { 61 if ($gateway != null) { 62 $transaction = $gateway->transaction()->void($transaction_id); 63 } else { 64 $transaction = Braintree_Transaction::void($transaction_id); 65 } 66 67 if ($transaction) { 68 return $transaction; 69 } else { 70 return false; 71 } 72 } catch (Exception $e) { 73 $this->log($e->getMessage()); 74 75 return false; 76 } 77 } 78 79 public function settleTransaction($gateway, $transaction_id, $amount) { 80 try { 81 if ($gateway != null) { 82 $transaction = $gateway->transaction()->submitForSettlement($transaction_id, $amount); 83 } else { 84 $transaction = Braintree_Transaction::submitForSettlement($transaction_id, $amount); 85 } 86 87 if ($transaction) { 88 return $transaction; 89 } else { 90 return false; 91 } 92 } catch (Exception $e) { 93 $this->log($e->getMessage()); 94 95 return false; 96 } 97 } 98 99 public function refundTransaction($gateway, $transaction_id, $amount) { 100 try { 101 if ($gateway != null) { 102 $transaction = $gateway->transaction()->refund($transaction_id, $amount); 103 } else { 104 $transaction = Braintree_Transaction::refund($transaction_id, $amount); 105 } 106 107 if ($transaction) { 108 return $transaction; 109 } else { 110 return false; 111 } 112 } catch (Exception $e) { 113 $this->log($e->getMessage()); 114 115 return false; 116 } 117 } 118 119 public function verifyCredentials($gateway) { 120 try { 121 //Try API call, if no exception is thrown, the credentials are correct 122 if ($gateway != null) { 123 $client_token = $gateway->clientToken()->generate(); 124 } else { 125 $client_token = Braintree_ClientToken::generate(); 126 } 127 128 return $client_token; 129 } catch (Exception $e) { 130 $this->log($e->getMessage()); 131 132 return false; 133 } 134 } 135 136 public function verifyMerchantAccount($gateway, $merchant_account_id) { 137 try { 138 //Try API call, if no exception is thrown, the above credentials are correct 139 if ($gateway != null) { 140 $merchant_account = $gateway->merchantAccount()->find($merchant_account_id); 141 } else { 142 $merchant_account = Braintree_MerchantAccount::find($merchant_account_id); 143 } 144 145 if ($merchant_account && $merchant_account->status == 'active') { 146 return $merchant_account; 147 } else { 148 return false; 149 } 150 } catch (Exception $e) { 151 $this->log($e->getMessage()); 152 153 return false; 154 } 155 } 156 157 public function setGateway($access_token) { 158 return new Braintree_Gateway(array('accessToken' => $access_token)); 159 } 160 161 public function log($data) { 162 if ($this->config->get('payment_pp_braintree_debug')) { 163 $backtrace = debug_backtrace(); 164 $log = new Log('braintree.log'); 165 $log->write('(' . $backtrace[1]['class'] . '::' . $backtrace[1]['function'] . ') - ' . print_r($data, true)); 166 } 167 } 168 }