PageRenderTime 45ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/kohana_core/modules/payment/libraries/drivers/Payment/Authorize.php

https://gitlab.com/vince.omega/mcb-nov-build
PHP | 160 lines | 104 code | 25 blank | 31 comment | 7 complexity | 836d4d24cb76bc26e8b0628dcd62d665 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Authorize.net Payment Driver
  4. *
  5. * $Id: Authorize.php 4160 2009-04-07 21:03:16Z ixmatus $
  6. *
  7. * @package Payment
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class Payment_Authorize_Driver implements Payment_Driver
  13. {
  14. // Array containing any response codes set from the gateway
  15. private $response = Null;
  16. //transaction result
  17. private $transaction = False;
  18. // Fields required to do a transaction
  19. private $required_fields = array
  20. (
  21. 'x_login' => FALSE,
  22. 'x_version' => TRUE,
  23. 'x_delim_char' => TRUE,
  24. 'x_url' => TRUE,
  25. 'x_type' => TRUE,
  26. 'x_method' => TRUE,
  27. 'x_tran_key' => FALSE,
  28. 'x_relay_response' => TRUE,
  29. 'x_card_num' => FALSE,
  30. 'x_exp_date' => FALSE,
  31. 'x_card_code' => FALSE,
  32. 'x_amount' => FALSE,
  33. );
  34. // Default required values
  35. private $authnet_values = array
  36. (
  37. 'x_version' => '3.1',
  38. 'x_delim_char' => '|',
  39. 'x_delim_data' => 'TRUE',
  40. 'x_url' => 'FALSE',
  41. 'x_type' => 'AUTH_CAPTURE',
  42. 'x_method' => 'CC',
  43. 'x_relay_response' => 'FALSE',
  44. );
  45. private $test_mode = TRUE;
  46. private $curl_config;
  47. private $lastResult = '';
  48. /**
  49. * Sets the config for the class.
  50. *
  51. * @param array config passed from the library
  52. */
  53. public function __construct($config)
  54. {
  55. $this->authnet_values['x_login'] = $config['auth_net_login_id'];
  56. $this->authnet_values['x_tran_key'] = $config['auth_net_tran_key'];
  57. $this->required_fields['x_login'] = !empty($config['auth_net_login_id']);
  58. $this->required_fields['x_tran_key']= !empty($config['auth_net_tran_key']);
  59. $this->authnet_values['x_md5_hash'] = $config['md5_hash'];
  60. $this->required_fields['x_md5_hash'] = !empty($config['md5_hash']);
  61. $this->curl_config = $config['curl_config'];
  62. $this->test_mode = $config['test_mode'];
  63. Kohana::log('debug', 'Authorize.net Payment Driver Initialized');
  64. }
  65. public function set_fields($fields)
  66. {
  67. foreach ((array) $fields as $key => $value)
  68. {
  69. $this->authnet_values['x_'.$key] = $value;
  70. if (array_key_exists('x_'.$key, $this->required_fields) and !empty($value)) $this->required_fields['x_'.$key] = TRUE;
  71. }
  72. }
  73. private function getResultResponseFull(){
  74. $response = array("", "Approved", "Declined", "Error");
  75. return $response[$this->response[0]];
  76. }
  77. public function get_response(){
  78. return $this->response[3]; //ResponseText
  79. }
  80. public function get_transaction_id(){
  81. return $this->response[6];
  82. }
  83. /**
  84. * Process a given transaction.
  85. *
  86. * @return boolean
  87. */
  88. public function process()
  89. {
  90. // Check for required fields
  91. if (in_array(FALSE, $this->required_fields))
  92. {
  93. $fields = array();
  94. foreach ($this->required_fields as $key => $field)
  95. {
  96. if (!$field) $fields[] = $key;
  97. }
  98. throw new Kohana_Exception('payment.required', implode(', ', $fields));
  99. }
  100. $fields = '';
  101. foreach ( $this->authnet_values as $key => $value )
  102. {
  103. $fields .= $key.'='.urlencode($value).'&';
  104. }
  105. $post_url = ($this->test_mode) ?
  106. 'https://test.authorize.net/gateway/transact.dll' :
  107. 'https://secure.authorize.net/gateway/transact.dll'; // Live URL
  108. //'https://certification.authorize.net/gateway/transact.dll' : // Test mode URL
  109. Kohana::log('error', '--------------------');
  110. Kohana::log('error', 'payment to:'.$post_url);
  111. Kohana::log('error', '-> parameters:'.$fields);
  112. Kohana::log('error', '--------------------');
  113. $ch = curl_init($post_url);
  114. curl_setopt_array($ch, $this->curl_config); // Set custom curl options
  115. curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim($fields, '& ')); // Set the curl POST fields
  116. $response = curl_exec($ch); //execute post and get results
  117. curl_close($ch);
  118. if (!$response)
  119. throw new Kohana_Exception('payment.gateway_connection_error');
  120. /*
  121. if(empty(trim($response)))
  122. throw new Kohana_Exception('payment.gateway_connection_error');
  123. */
  124. $this->response = explode("|", $response);
  125. if ($this->getResultResponseFull() == "Approved"){
  126. return TRUE;
  127. }elseif ($this->getResultResponseFull() == "Declined"){
  128. return FALSE;
  129. }
  130. }
  131. public function getLastError(){
  132. return $this->get_response();
  133. //return $this->getResultResponseFull();
  134. }
  135. } // End Payment_Authorize_Driver Class