A Qcodo based CMS/ecommerce framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

367 lines
14 KiB

12 years ago
  1. <?php
  2. if(!defined('QUASICMS') ) die("No quasi.");
  3. if (!defined("PAYMENTMODULE.CLASS.PHP")){
  4. define("PAYMENTMODULE.CLASS.PHP",1);
  5. /**
  6. * Class PaymentModule - a module to manage PaymentMethods
  7. *
  8. * This class iterates through the list of PaymentMethods in the payment_methods table
  9. * and for each that is flagged active it creates a QRadioButton to be rendered in the template.
  10. * When the button is clicked, it sets objSelectedMethod to the selected payment method.
  11. * When the 'Purchase' button is clicked the btn_Purchase function is called; this calls three
  12. * functions that a PaymentAction is required to implement:
  13. * - PreProcess: perform any actions to set up the transaction
  14. * - Process: perform the actual transaction, ie. connect to server and make a request in most cases.
  15. * - PostProcess: perform any validation checks and update the order_status_history table in most cases.
  16. *
  17. *
  18. *@author Erik Winn <erikwinnmail@yahoo.com>
  19. *
  20. * $Id: PaymentModule.class.php 500 2009-02-06 21:44:47Z erikwinn $
  21. *@version 0.1
  22. *
  23. *@copyright (C) 2008 by Erik Winn
  24. *@license GPL v.2
  25. This program is free software; you can redistribute it and/or modify
  26. it under the terms of the GNU General Public License as published by
  27. the Free Software Foundation; either version 2 of the License, or
  28. (at your option) any later version.
  29. This program is distributed in the hope that it will be useful,
  30. but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. GNU General Public License for more details.
  33. You should have received a copy of the GNU General Public License
  34. along with this program; if not, write to the Free Software
  35. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
  36. *
  37. *@package Quasi
  38. * @subpackage Modules
  39. */
  40. class PaymentModule extends QPanel
  41. {
  42. /**
  43. *@var CheckOutModule a reference to the control block - expected to be CheckOutModule
  44. */
  45. protected $objControlBlock;
  46. /**
  47. *@var Order a reference to the current order
  48. */
  49. protected $objOrder;
  50. /**
  51. *@var Address a reference to the current order
  52. */
  53. protected $objAddress;
  54. /**
  55. *@var array PaymentMethods flagged active in database
  56. */
  57. protected $aryPaymentMethods;
  58. /**
  59. *@var PaymentMethod a reference to the method chosen by the user (or the default)
  60. */
  61. protected $objSelectedMethod;
  62. /**
  63. *@var PaymentAction the action for the selected payment method
  64. */
  65. protected $objPaymentAction=null;
  66. /**
  67. *@var string strDefaultServiceProvider - the default payment gateway
  68. */
  69. protected $strDefaultServiceProvider = DEFAULT_PAYMENT_PROVIDER;
  70. /**
  71. *@var string strDefaultServiceType - the default payment type for the default service
  72. */
  73. protected $strDefaultServiceType = DEFAULT_PAYMENT_SERVICE;
  74. /**
  75. *@var array aryYears - years for CC expiration selection
  76. */
  77. protected $aryYears = array(
  78. '2008',
  79. '2009',
  80. '2010',
  81. '2011',
  82. '2012',
  83. '2013',
  84. '2014',
  85. '2015',
  86. '2016',
  87. '2017',
  88. '2018',
  89. );
  90. /**
  91. *@var array aryMonths - months for CC expiration selection
  92. */
  93. protected $aryMonths = array(
  94. '01',
  95. '02',
  96. '03',
  97. '04',
  98. '05',
  99. '06',
  100. '07',
  101. '08',
  102. '09',
  103. '10',
  104. '11',
  105. '12',
  106. );
  107. protected $strErrors;
  108. protected $fltPaymentTotal;
  109. protected $blnHasActiveMethods = true;
  110. /**
  111. *@var bool blnShowCCInput - if true display Credit card input fields
  112. */
  113. protected $blnShowCCInput = false;
  114. /**
  115. *@var integer intPreviousAddressId - the id of the address before a selection signal.
  116. */
  117. protected $intPreviousAddressId;
  118. /// Display objects ..
  119. /**
  120. *@var array PaymentMethodViews for the selection of PaymentMethods
  121. */
  122. public $aryPaymentMethodViews;
  123. /**
  124. *@var QButton Button to complete the purchase - triggers a payment action
  125. */
  126. public $btnPurchase;
  127. /**
  128. * @todo subclass for validation ..QCCTextBox?
  129. *@var QTextBox
  130. */
  131. public $txtCCNumber;
  132. /**
  133. *@var QListBox
  134. */
  135. public $lstCCExpirationYear;
  136. /**
  137. *@var QListBox
  138. */
  139. public $lstCCExpirationMonth;
  140. /**
  141. * @todo implement CCV Check ..
  142. *@var QTextBox
  143. */
  144. public $txtCCVNumber;
  145. /**
  146. *@var AddressSelectionModule objAddressSelectionModule - handles selecting the billing address
  147. */
  148. public $objAddressSelectionModule;
  149. /**
  150. * Module constructor
  151. *@param ContentBlock - parent controller object.
  152. *@param Order - the order for which we are paying.
  153. */
  154. public function __construct(QPanel $objControlBlock, $objOrder )
  155. {
  156. try {
  157. parent::__construct($objControlBlock);
  158. } catch (QCallerException $objExc) {
  159. $objExc->IncrementOffset();
  160. throw $objExc;
  161. }
  162. $this->objOrder =& $objOrder;
  163. $this->objControlBlock =& $objControlBlock;
  164. $this->AutoRenderChildren = true;
  165. $this->strTemplate = __QUASI_CORE_TEMPLATES__ . '/PaymentModule.tpl.php';
  166. $this->aryPaymentMethods = PaymentMethod::QueryArray(
  167. QQ::Equal(QQN::PaymentMethod()->Active, true),
  168. QQ::OrderBy(QQN::PaymentMethod()->SortOrder, false)
  169. );
  170. if(!empty( $this->aryPaymentMethods ))
  171. {
  172. $this->objAddressSelectionModule = new AddressSelectionModule($this,
  173. 'SelectAddress',
  174. $this->objOrder->BillingAddressId
  175. );
  176. $this->objAddress = $this->objAddressSelectionModule->Address;
  177. $this->createInputOptions();
  178. }
  179. else
  180. $this->blnHasActiveMethods = false;
  181. }
  182. /**
  183. *This creates the method views for the available payment methods and the input fields for
  184. * credit card numbers (if needed)
  185. */
  186. protected function createInputOptions()
  187. {
  188. $this->blnShowCCInput = false;
  189. foreach($this->aryPaymentMethods as &$objPaymentMethod)
  190. {
  191. $objPaymentMethodView = new PaymentMethodView($this, $objPaymentMethod);
  192. if( $objPaymentMethod->ServiceProvider == $this->DefaultServiceProvider
  193. && $objPaymentMethod->ServiceType == $this->DefaultServiceType )
  194. {
  195. $objPaymentMethodView->Checked = true;
  196. $this->objSelectedMethod =& $objPaymentMethod;
  197. $this->objOrder->PaymentMethodId = $objPaymentMethod->Id;
  198. }
  199. if( $objPaymentMethod->RequiresCcNumber )
  200. $this->blnShowCCInput = true;
  201. $this->aryPaymentMethodViews[] = $objPaymentMethodView;
  202. }
  203. //credit card input fields
  204. if($this->blnShowCCInput)
  205. {
  206. $this->txtCCNumber = new QTextBox($this);
  207. $this->txtCCNumber->CssClass = 'CCNumber';
  208. $this->txtCCNumber->Required = true;
  209. $this->txtCCNumber->Name = Quasi::Translate('Card Number') . ': ';
  210. $this->txtCCNumber->Text = '';
  211. $this->txtCCVNumber = new QTextBox($this);
  212. $this->txtCCVNumber->CssClass = 'CCVNumber';
  213. $this->txtCCVNumber->Required = true;
  214. $this->txtCCVNumber->Name = Quasi::Translate('CCV Number') . ': ';
  215. $this->lstCCExpirationYear = new QListBox($this);
  216. foreach($this->aryYears as $strYear)
  217. $this->lstCCExpirationYear->AddItem($strYear, $strYear);
  218. $this->lstCCExpirationYear->Required = true;
  219. $this->lstCCExpirationMonth = new QListBox($this);
  220. foreach($this->aryMonths as $strMonth)
  221. $this->lstCCExpirationMonth->AddItem($strMonth,$strMonth);
  222. $this->lstCCExpirationMonth->Required = true;
  223. }
  224. }
  225. /**
  226. * Sets the selected payment method, called when the user selects/changes a payment
  227. * method radio button.
  228. */
  229. public function SelectMethod($intMethodId)
  230. {
  231. foreach($this->aryPaymentMethods as &$objMethod )
  232. if($intMethodId == $objMethod->Id)
  233. {
  234. $this->objSelectedMethod =& $objMethod;
  235. $this->objOrder->PaymentMethodId = $objMethod->Id;
  236. break;
  237. }
  238. }
  239. public function SelectAddress($intAddressId, $strParameter=null)
  240. {
  241. if( is_numeric($intAddressId) )
  242. {
  243. $this->intPreviousAddressId = $intAddressId;
  244. $this->objOrder->SetBillingAddress($this->objAddressSelectionModule->Address);
  245. $this->objAddress = $this->objAddressSelectionModule->Address;
  246. }
  247. $this->objAddressSelectionModule->RemoveChildControls(true);
  248. $this->RemoveChildControl($this->objAddressSelectionModule->ControlId, false);
  249. if( 'Edit' == $strParameter )
  250. $this->objAddressSelectionModule = new AddressSelectionModule($this, 'SelectAddress', $intAddressId, true);
  251. elseif( 'New' == $strParameter )
  252. $this->objAddressSelectionModule = new AddressSelectionModule($this, 'SelectAddress', null, true);
  253. else//Note: includes Save and Cancel ..
  254. {
  255. if($intAddressId)
  256. $this->objAddressSelectionModule = new AddressSelectionModule($this, 'SelectAddress', $intAddressId);
  257. else
  258. $this->objAddressSelectionModule = new AddressSelectionModule($this, 'SelectAddress', $this->intPreviousAddressId);
  259. }
  260. $this->objAddressSelectionModule->Visible = true;
  261. $this->AddChildControl($this->objAddressSelectionModule);
  262. }
  263. public function Validate(){ return true;}
  264. public function __get($strName)
  265. {
  266. switch ($strName)
  267. {
  268. case 'SelectedMethod':
  269. return $this->objSelectedMethod ;
  270. case 'Address':
  271. return $this->objAddress ;
  272. case 'HasActiveMethods':
  273. return $this->blnHasActiveMethods ;
  274. case 'ShowCCInput':
  275. return $this->blnShowCCInput ;
  276. case 'DefaultServiceProvider':
  277. return $this->strDefaultServiceProvider ;
  278. case 'DefaultServiceType':
  279. return $this->strDefaultServiceType ;
  280. case 'Errors':
  281. return $this->strErrors ;
  282. case 'Order':
  283. return $this->objOrder ;
  284. default:
  285. try {
  286. return parent::__get($strName);
  287. } catch (QCallerException $objExc) {
  288. $objExc->IncrementOffset();
  289. throw $objExc;
  290. }
  291. }
  292. }
  293. public function __set($strName, $mixValue)
  294. {
  295. switch ($strName)
  296. {
  297. case 'SelectedMethod':
  298. try {
  299. return ($this->objSelectedMethod = QType::Cast($mixValue, 'PaymentMethod' ));
  300. } catch (QInvalidCastException $objExc) {
  301. $objExc->IncrementOffset();
  302. throw $objExc;
  303. }
  304. case 'DefaultServiceProvider':
  305. try {
  306. return ($this->strDefaultServiceProvider = QType::Cast($mixValue, Qtype::String ));
  307. } catch (QInvalidCastException $objExc) {
  308. $objExc->IncrementOffset();
  309. throw $objExc;
  310. }
  311. case 'DefaultServiceType':
  312. try {
  313. return ($this->strDefaultServiceType = QType::Cast($mixValue, Qtype::String ));
  314. } catch (QInvalidCastException $objExc) {
  315. $objExc->IncrementOffset();
  316. throw $objExc;
  317. }
  318. default:
  319. try {
  320. return (parent::__set($strName, $mixValue));
  321. } catch (QCallerException $objExc) {
  322. $objExc->IncrementOffset();
  323. throw $objExc;
  324. }
  325. }
  326. }
  327. }//end class
  328. }//end define
  329. ?>