A QCodo powered CMS
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.

318 lines
9.6 KiB

  1. <?php
  2. if(!defined('QUINTACMS') ) die("No quinta.");
  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 <sidewalksoftware@gmail.com>
  19. *
  20. *@version 0.3
  21. *
  22. *@package Quinta
  23. * @subpackage Modules
  24. */
  25. class PaymentModule extends QPanel{
  26. /**
  27. *@var CheckOutModule a reference to the control block - expected to be CheckOutModule
  28. */
  29. protected $objControlBlock;
  30. /**
  31. *@var Order a reference to the current order
  32. */
  33. protected $objOrder;
  34. /**
  35. *@var Address a reference to the current order
  36. */
  37. protected $objAddress;
  38. /**
  39. *@var array PaymentMethods flagged active in database
  40. */
  41. protected $aryPaymentMethods;
  42. /**
  43. *@var PaymentMethod a reference to the method chosen by the user (or the default)
  44. */
  45. protected $objSelectedMethod;
  46. /**
  47. *@var PaymentAction the action for the selected payment method
  48. */
  49. protected $objPaymentAction=null;
  50. /**
  51. *@var string strDefaultServiceProvider - the default payment gateway
  52. */
  53. protected $strDefaultServiceProvider = DEFAULT_PAYMENT_PROVIDER;
  54. /**
  55. *@var string strDefaultServiceType - the default payment type for the default service
  56. */
  57. protected $strDefaultServiceType = DEFAULT_PAYMENT_SERVICE;
  58. /**
  59. *@var array aryYears - years for CC expiration selection
  60. */
  61. protected $aryYears = array(
  62. '2008',
  63. '2009',
  64. '2010',
  65. '2011',
  66. '2012',
  67. '2013',
  68. '2014',
  69. '2015',
  70. '2016',
  71. '2017',
  72. '2018',
  73. );
  74. /**
  75. *@var array aryMonths - months for CC expiration selection
  76. */
  77. protected $aryMonths = array(
  78. '01',
  79. '02',
  80. '03',
  81. '04',
  82. '05',
  83. '06',
  84. '07',
  85. '08',
  86. '09',
  87. '10',
  88. '11',
  89. '12',
  90. );
  91. protected $strErrors;
  92. protected $fltPaymentTotal;
  93. protected $blnHasActiveMethods = true;
  94. /**
  95. *@var bool blnShowCCInput - if true display Credit card input fields
  96. */
  97. protected $blnShowCCInput = false;
  98. /**
  99. *@var integer intPreviousAddressId - the id of the address before a selection signal.
  100. */
  101. protected $intPreviousAddressId;
  102. /// Display objects ..
  103. /**
  104. *@var array PaymentMethodControllers for the selection of PaymentMethods
  105. */
  106. public $aryPaymentMethodControllers;
  107. /**
  108. *@var QButton Button to complete the purchase - triggers a payment action
  109. */
  110. public $btnPurchase;
  111. /**
  112. * @todo subclass for validation ..QCCTextBox?
  113. *@var QTextBox
  114. */
  115. public $txtCCNumber;
  116. /**
  117. *@var QListBox
  118. */
  119. public $lstCCExpirationYear;
  120. /**
  121. *@var QListBox
  122. */
  123. public $lstCCExpirationMonth;
  124. /**
  125. * @todo implement CCV Check ..
  126. *@var QTextBox
  127. */
  128. public $txtCCVNumber;
  129. /**
  130. *@var AddressSelectionModule objAddressSelectionModule - handles selecting the billing address
  131. */
  132. public $objAddressSelectionModule;
  133. /**
  134. * Module constructor
  135. *@param ContentBlock - parent controller object.
  136. *@param Order - the order for which we are paying.
  137. */
  138. public function __construct(QPanel $objControlBlock, $objOrder ){
  139. try {
  140. parent::__construct($objControlBlock);
  141. } catch (QCallerException $objExc) {
  142. $objExc->IncrementOffset();
  143. throw $objExc;
  144. }
  145. $this->objOrder =& $objOrder;
  146. $this->objControlBlock =& $objControlBlock;
  147. $this->AutoRenderChildren = true;
  148. $this->strTemplate = __QUINTA_CORE_VIEWS__ . '/PaymentModule.tpl.php';
  149. $this->aryPaymentMethods = PaymentMethod::QueryArray(
  150. QQ::Equal(QQN::PaymentMethod()->Active, true),
  151. QQ::OrderBy(QQN::PaymentMethod()->SortOrder, false)
  152. );
  153. if(!empty( $this->aryPaymentMethods )){
  154. $this->objAddressSelectionModule = new AddressSelectionModule($this,
  155. 'SelectAddress',
  156. $this->objOrder->BillingAddressId
  157. );
  158. $this->objAddress = $this->objAddressSelectionModule->Address;
  159. $this->createInputOptions();
  160. }else
  161. $this->blnHasActiveMethods = false;
  162. }
  163. /**
  164. *This creates the method views for the available payment methods and the input fields for
  165. * credit card numbers (if needed)
  166. */
  167. protected function createInputOptions(){
  168. $this->blnShowCCInput = false;
  169. foreach($this->aryPaymentMethods as &$objPaymentMethod){
  170. $objPaymentMethodController = new PaymentMethodController($this, $objPaymentMethod);
  171. if( $objPaymentMethod->ServiceProvider == $this->DefaultServiceProvider
  172. && $objPaymentMethod->ServiceType == $this->DefaultServiceType )
  173. {
  174. $objPaymentMethodController->Checked = true;
  175. $this->objSelectedMethod =& $objPaymentMethod;
  176. $this->objOrder->PaymentMethodId = $objPaymentMethod->Id;
  177. }
  178. if( $objPaymentMethod->RequiresCcNumber )
  179. $this->blnShowCCInput = true;
  180. $this->aryPaymentMethodControllers[] = $objPaymentMethodController;
  181. }
  182. //credit card input fields
  183. if($this->blnShowCCInput){
  184. $this->txtCCNumber = new QTextBox($this);
  185. $this->txtCCNumber->CssClass = 'CCNumber';
  186. $this->txtCCNumber->Required = true;
  187. $this->txtCCNumber->Name = Quinta::Translate('Card Number') . ': ';
  188. $this->txtCCNumber->Text = '';
  189. $this->txtCCVNumber = new QTextBox($this);
  190. $this->txtCCVNumber->CssClass = 'CCVNumber';
  191. $this->txtCCVNumber->Required = true;
  192. $this->txtCCVNumber->Name = Quinta::Translate('CCV Number') . ': ';
  193. $this->lstCCExpirationYear = new QListBox($this);
  194. foreach($this->aryYears as $strYear)
  195. $this->lstCCExpirationYear->AddItem($strYear, $strYear);
  196. $this->lstCCExpirationYear->Required = true;
  197. $this->lstCCExpirationMonth = new QListBox($this);
  198. foreach($this->aryMonths as $strMonth)
  199. $this->lstCCExpirationMonth->AddItem($strMonth,$strMonth);
  200. $this->lstCCExpirationMonth->Required = true;
  201. }
  202. }
  203. /**
  204. * Sets the selected payment method, called when the user selects/changes a payment
  205. * method radio button.
  206. */
  207. public function SelectMethod($intMethodId){
  208. foreach($this->aryPaymentMethods as &$objMethod ){
  209. if($intMethodId == $objMethod->Id){
  210. $this->objSelectedMethod =& $objMethod;
  211. $this->objOrder->PaymentMethodId = $objMethod->Id;
  212. break;
  213. }
  214. }
  215. }
  216. public function SelectAddress($intAddressId, $strParameter=null){
  217. if( is_numeric($intAddressId) ){
  218. $this->intPreviousAddressId = $intAddressId;
  219. $this->objOrder->SetBillingAddress($this->objAddressSelectionModule->Address);
  220. $this->objAddress = $this->objAddressSelectionModule->Address;
  221. }
  222. $this->objAddressSelectionModule->RemoveChildControls(true);
  223. $this->RemoveChildControl($this->objAddressSelectionModule->ControlId, false);
  224. if( 'Edit' == $strParameter )
  225. $this->objAddressSelectionModule = new AddressSelectionModule($this, 'SelectAddress', $intAddressId, true);
  226. elseif( 'New' == $strParameter )
  227. $this->objAddressSelectionModule = new AddressSelectionModule($this, 'SelectAddress', null, true);
  228. else {//Note: includes Save and Cancel ..
  229. if($intAddressId)
  230. $this->objAddressSelectionModule = new AddressSelectionModule($this, 'SelectAddress', $intAddressId);
  231. else
  232. $this->objAddressSelectionModule = new AddressSelectionModule($this, 'SelectAddress', $this->intPreviousAddressId);
  233. }
  234. $this->objAddressSelectionModule->Visible = true;
  235. $this->AddChildControl($this->objAddressSelectionModule);
  236. }
  237. public function Validate(){ return true;}
  238. public function __get($strName){
  239. switch ($strName){
  240. case 'SelectedMethod':
  241. return $this->objSelectedMethod ;
  242. case 'Address':
  243. return $this->objAddress ;
  244. case 'HasActiveMethods':
  245. return $this->blnHasActiveMethods ;
  246. case 'ShowCCInput':
  247. return $this->blnShowCCInput ;
  248. case 'DefaultServiceProvider':
  249. return $this->strDefaultServiceProvider ;
  250. case 'DefaultServiceType':
  251. return $this->strDefaultServiceType ;
  252. case 'Errors':
  253. return $this->strErrors ;
  254. case 'Order':
  255. return $this->objOrder ;
  256. default:
  257. try {
  258. return parent::__get($strName);
  259. } catch (QCallerException $objExc) {
  260. $objExc->IncrementOffset();
  261. throw $objExc;
  262. }
  263. }
  264. }
  265. public function __set($strName, $mixValue){
  266. switch ($strName){
  267. case 'SelectedMethod':
  268. try {
  269. return ($this->objSelectedMethod = QType::Cast($mixValue, 'PaymentMethod' ));
  270. } catch (QInvalidCastException $objExc) {
  271. $objExc->IncrementOffset();
  272. throw $objExc;
  273. }
  274. case 'DefaultServiceProvider':
  275. try {
  276. return ($this->strDefaultServiceProvider = QType::Cast($mixValue, Qtype::String ));
  277. } catch (QInvalidCastException $objExc) {
  278. $objExc->IncrementOffset();
  279. throw $objExc;
  280. }
  281. case 'DefaultServiceType':
  282. try {
  283. return ($this->strDefaultServiceType = QType::Cast($mixValue, Qtype::String ));
  284. } catch (QInvalidCastException $objExc) {
  285. $objExc->IncrementOffset();
  286. throw $objExc;
  287. }
  288. default:
  289. try {
  290. return (parent::__set($strName, $mixValue));
  291. } catch (QCallerException $objExc) {
  292. $objExc->IncrementOffset();
  293. throw $objExc;
  294. }
  295. }
  296. }
  297. }//end class
  298. }//end define
  299. ?>