PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Admin/Form/PaymentProcessor.php

https://github.com/michaelmcandrew/th
PHP | 349 lines | 226 code | 67 blank | 56 comment | 29 complexity | 0fbc5421cf2117b5a9b3db55a0a909c6 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.0 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2011
  31. * $Id: PaymentProcessor.php 9702 2007-05-29 23:57:16Z lobo $
  32. *
  33. */
  34. require_once 'CRM/Admin/Form.php';
  35. /**
  36. * This class generates form components for Location Type
  37. *
  38. */
  39. class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form
  40. {
  41. protected $_id = null;
  42. protected $_testID = null;
  43. protected $_fields = null;
  44. protected $_ppDAO;
  45. function preProcess( ) {
  46. parent::preProcess( );
  47. CRM_Utils_System::setTitle(ts('Settings - Payment Processor'));
  48. // get the payment processor meta information
  49. if ( $this->_id ) {
  50. $this->_ppType = CRM_Utils_Request::retrieve( 'pp', 'String', $this, false, null );
  51. if ( ! $this->_ppType ) {
  52. $this->_ppType = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_PaymentProcessor',
  53. $this->_id,
  54. 'payment_processor_type' );
  55. }
  56. $this->set( 'pp', $this->_ppType );
  57. } else {
  58. $this->_ppType = CRM_Utils_Request::retrieve( 'pp', 'String', $this, true, null );
  59. }
  60. $this->assign( 'ppType', $this->_ppType );
  61. require_once 'CRM/Core/DAO/PaymentProcessorType.php';
  62. $this->_ppDAO = new CRM_Core_DAO_PaymentProcessorType( );
  63. $this->_ppDAO->name = $this->_ppType;
  64. if ( ! $this->_ppDAO->find( true ) ) {
  65. CRM_Core_Error::fatal( ts( 'Could not find payment processor meta information' ) );
  66. }
  67. if ( $this->_id ) {
  68. $refreshURL = CRM_Utils_System::url( 'civicrm/admin/paymentProcessor',
  69. "reset=1&action=update&id={$this->_id}",
  70. false, null, false );
  71. } else {
  72. $refreshURL = CRM_Utils_System::url( 'civicrm/admin/paymentProcessor',
  73. "reset=1&action=add",
  74. false, null, false );
  75. }
  76. //CRM-4129
  77. $destination = CRM_Utils_Request::retrieve( 'civicrmDestination', 'String', $this );
  78. if ( $destination ) {
  79. $destination = urlencode( $destination );
  80. $refreshURL .= "&civicrmDestination=$destination";
  81. }
  82. $this->assign( 'refreshURL', $refreshURL );
  83. $this->assign( 'is_recur', $this->_ppDAO->is_recur );
  84. $this->_fields = array(
  85. array( 'name' => 'user_name',
  86. 'label' => $this->_ppDAO->user_name_label ),
  87. array( 'name' => 'password',
  88. 'label' => $this->_ppDAO->password_label ),
  89. array( 'name' => 'signature',
  90. 'label' => $this->_ppDAO->signature_label ),
  91. array( 'name' => 'subject',
  92. 'label' => $this->_ppDAO->subject_label ),
  93. array( 'name' => 'url_site',
  94. 'label' => ts( 'Site URL' ),
  95. 'rule' => 'url',
  96. 'msg' => ts( 'Enter a valid URL' ) ),
  97. );
  98. if ( $this->_ppDAO->is_recur ) {
  99. $this->_fields[] = array( 'name' => 'url_recur',
  100. 'label' => ts( 'Recurring Payments URL' ),
  101. 'rule' => 'url',
  102. 'msg' => ts( 'Enter a valid URL' ) );
  103. }
  104. if ( ! empty( $this->_ppDAO->url_button_default ) ) {
  105. $this->_fields[] = array( 'name' => 'url_button',
  106. 'label' => ts( 'Button URL' ),
  107. 'rule' => 'url',
  108. 'msg' => ts( 'Enter a valid URL' ) );
  109. }
  110. if ( ! empty( $this->_ppDAO->url_api_default ) ) {
  111. $this->_fields[] = array( 'name' => 'url_api',
  112. 'label' => ts( 'API URL' ),
  113. 'rule' => 'url',
  114. 'msg' => ts( 'Enter a valid URL' ) );
  115. }
  116. }
  117. /**
  118. * Function to build the form
  119. *
  120. * @return None
  121. * @access public
  122. */
  123. public function buildQuickForm( $check = false )
  124. {
  125. parent::buildQuickForm( );
  126. if ($this->_action & CRM_Core_Action::DELETE ) {
  127. return;
  128. }
  129. $attributes = CRM_Core_DAO::getAttribute( 'CRM_Core_DAO_PaymentProcessor' );
  130. $this->add( 'text', 'name', ts( 'Name' ),
  131. $attributes['name'], true );
  132. $this->addRule( 'name', ts('Name already exists in Database.'), 'objectExists', array( 'CRM_Core_DAO_PaymentProcessor', $this->_id ) );
  133. $this->add( 'text', 'description', ts( 'Description' ),
  134. $attributes['description'] );
  135. $types = CRM_Core_PseudoConstant::paymentProcessorType( );
  136. $this->add( 'select', 'payment_processor_type', ts( 'Payment Processor Type' ), $types, true,
  137. array('onchange' => "reload(true)") );
  138. // is this processor active ?
  139. $this->add('checkbox', 'is_active' , ts('Is this Payment Processor active?') );
  140. $this->add('checkbox', 'is_default', ts('Is this Payment Processor the default?') );
  141. foreach ( $this->_fields as $field ) {
  142. if ( empty( $field['label'] ) ) {
  143. continue;
  144. }
  145. $this->add( 'text', $field['name'],
  146. $field['label'], $attributes[$field['name']] );
  147. $this->add( 'text', "test_{$field['name']}",
  148. $field['label'], $attributes[$field['name']] );
  149. if ( CRM_Utils_Array::value( 'rule', $field ) ) {
  150. $this->addRule( $field['name'] , $field['msg'], $field['rule'] );
  151. $this->addRule( "test_{$field['name']}", $field['msg'], $field['rule'] );
  152. }
  153. }
  154. $this->addFormRule( array( 'CRM_Admin_Form_PaymentProcessor', 'formRule' ) );
  155. }
  156. static function formRule( $fields ) {
  157. // make sure that at least one of live or test is present
  158. // and we have at least name and url_site
  159. // would be good to make this processor specific
  160. $errors = array( );
  161. if ( ! ( self::checkSection( $fields, $errors ) ||
  162. self::checkSection( $fields, $errors, 'test' ) ) ) {
  163. $errors['_qf_default'] = ts( 'You must have at least the test or live section filled' );
  164. }
  165. if ( ! empty( $errors ) ) {
  166. return $errors;
  167. }
  168. return empty( $errors ) ? true : $errors;
  169. }
  170. static function checkSection( &$fields, &$errors, $section = null ) {
  171. $names = array( 'user_name' );
  172. $present = false;
  173. $allPresent = true;
  174. foreach ( $names as $name ) {
  175. if ( $section ) {
  176. $name = "{$section}_$name";
  177. }
  178. if ( ! empty( $fields[$name] ) ) {
  179. $present = true;
  180. } else {
  181. $allPresent = false;
  182. }
  183. }
  184. if ( $present ) {
  185. if ( ! $allPresent ) {
  186. $errors['_qf_default'] = ts( 'You must have at least the user_name specified' );
  187. }
  188. }
  189. return $present;
  190. }
  191. function setDefaultValues( ) {
  192. $defaults = array( );
  193. $defaults['payment_processor_type'] = $this->_ppType;
  194. if ( ! $this->_id ) {
  195. $defaults['is_active'] = $defaults['is_default'] = 1;
  196. $defaults['url_site'] = $this->_ppDAO->url_site_default;
  197. $defaults['url_api'] = $this->_ppDAO->url_api_default;
  198. $defaults['url_recur'] = $this->_ppDAO->url_recur_default;
  199. $defaults['url_button'] = $this->_ppDAO->url_button_default;
  200. $defaults['test_url_site'] = $this->_ppDAO->url_site_test_default;
  201. $defaults['test_url_api'] = $this->_ppDAO->url_api_test_default;
  202. $defaults['test_url_recur'] = $this->_ppDAO->url_recur_test_default;
  203. $defaults['test_url_button'] = $this->_ppDAO->url_button_test_default;
  204. return $defaults;
  205. }
  206. $domainID = CRM_Core_Config::domainID( );
  207. $dao = new CRM_Core_DAO_PaymentProcessor( );
  208. $dao->id = $this->_id;
  209. $dao->domain_id = $domainID;
  210. if ( ! $dao->find( true ) ) {
  211. return $defaults;
  212. }
  213. CRM_Core_DAO::storeValues( $dao, $defaults );
  214. // now get testID
  215. $testDAO = new CRM_Core_DAO_PaymentProcessor( );
  216. $testDAO->name = $dao->name;
  217. $testDAO->is_test = 1;
  218. $testDAO->domain_id = $domainID;
  219. if ( $testDAO->find( true ) ) {
  220. $this->_testID = $testDAO->id;
  221. foreach ( $this->_fields as $field ) {
  222. $testName = "test_{$field['name']}";
  223. $defaults[$testName] = $testDAO->{$field['name']};
  224. }
  225. }
  226. if ( $this->_ppType ) {
  227. $defaults['payment_processor_type'] = $this->_ppType;
  228. }
  229. return $defaults;
  230. }
  231. /**
  232. * Function to process the form
  233. *
  234. * @access public
  235. * @return None
  236. */
  237. public function postProcess()
  238. {
  239. CRM_Utils_System::flushCache( 'CRM_Core_DAO_PaymentProcessor' );
  240. if ( $this->_action & CRM_Core_Action::DELETE ) {
  241. CRM_Core_BAO_PaymentProcessor::del( $this->_id );
  242. CRM_Core_Session::setStatus( ts('Selected Payment Processor has been deleted.') );
  243. return;
  244. }
  245. $values = $this->controller->exportValues( $this->_name );
  246. $domainID = CRM_Core_Config::domainID( );
  247. if ( CRM_Utils_Array::value( 'is_default', $values ) ) {
  248. $query = "UPDATE civicrm_payment_processor SET is_default = 0 WHERE domain_id = $domainID";
  249. CRM_Core_DAO::executeQuery( $query, CRM_Core_DAO::$_nullArray );
  250. }
  251. $this->updatePaymentProcessor( $values, $domainID, false );
  252. $this->updatePaymentProcessor( $values, $domainID, true );
  253. }//end of function
  254. function updatePaymentProcessor( &$values, $domainID, $test ) {
  255. $dao = new CRM_Core_DAO_PaymentProcessor( );
  256. $dao->id = $test ? $this->_testID : $this->_id;
  257. $dao->domain_id = $domainID;
  258. $dao->is_test = $test;
  259. if ( ! $test ) {
  260. $dao->is_default = CRM_Utils_Array::value( 'is_default', $values, 0 );
  261. } else {
  262. $dao->is_default = 0;
  263. }
  264. $dao->is_active = CRM_Utils_Array::value( 'is_active' , $values, 0 );
  265. $dao->name = $values['name'];
  266. $dao->description = $values['description'];
  267. $dao->payment_processor_type = $values['payment_processor_type'];
  268. foreach ( $this->_fields as $field ) {
  269. $fieldName = $test ? "test_{$field['name']}" : $field['name'];
  270. $dao->{$field['name']} = trim( CRM_Utils_Array::value( $fieldName, $values ) );
  271. if ( empty( $dao->{$field['name']} ) ) {
  272. $dao->{$field['name']} = 'null';
  273. }
  274. }
  275. // also copy meta fields from the info DAO
  276. $dao->is_recur = $this->_ppDAO->is_recur;
  277. $dao->billing_mode = $this->_ppDAO->billing_mode;
  278. $dao->class_name = $this->_ppDAO->class_name;
  279. $dao->payment_type = $this->_ppDAO->payment_type;
  280. $dao->save( );
  281. }
  282. }