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

/include/Modules/UDM/AMPPayment/Save.inc.php

https://github.com/radicalsuz/amp
PHP | 264 lines | 200 code | 58 blank | 6 comment | 28 complexity | ae5a12e15438052ba891c1d2831841ac MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-2.0
  1. <?php
  2. require_once ('Modules/Payment/Payment.php');
  3. require_once ('Modules/Payment/Item.inc.php');
  4. require_once ('AMP/UserData/Plugin/Save.inc.php');
  5. require_once ('AMP/Form/ElementSwapScript.inc.php');
  6. class UserDataPlugin_Save_AMPPayment extends UserDataPlugin_Save {
  7. var $short_name = "AMPPayment";
  8. var $available = true;
  9. var $options = array(
  10. 'merchant_ID'=> array('label'=>'Merchant',
  11. 'type'=>'select',
  12. 'available'=>true,
  13. 'default'=>1,
  14. 'values'=>'Lookup(payment_merchants,id,Merchant)'),
  15. 'item_IDs' => array( 'label'=>'Items for Purchase',
  16. 'type'=>'multiselect',
  17. 'available'=>true,
  18. 'values'=>'Lookup(payment_items,id,name)'),
  19. 'purchase_description' => array( 'label'=>'Label for Purchase Field',
  20. 'type'=>'text',
  21. 'available'=>true,
  22. 'default'=>'Purchase Description'),
  23. 'email_receipt' => array( 'label'=>'Send Receipt Email',
  24. 'type'=>'checkbox',
  25. 'available'=>true,
  26. 'default'=>true),
  27. 'email_receipt_template' => array( 'label' => 'Template For Receipt',
  28. 'type' => 'select',
  29. 'available' => true),
  30. 'allowed_payment_types' => array( 'label' => 'Allowed Payment Options',
  31. 'type' => 'multiselect',
  32. 'values'=> array('CreditCard'=>'Credit Card','Check'=>'Check'),
  33. 'default'=>'CreditCard,Check',
  34. 'available' => true),
  35. 'secure_server' => array( 'label' => 'Secure Server Name',
  36. 'type' => 'text',
  37. 'available' => true ),
  38. 'check_payable' => array( 'label' => 'Check Payment Instructions',
  39. 'type' => 'textarea',
  40. 'available' => true )
  41. );
  42. var $_field_prefix = 'plugin_AMPPayment';
  43. var $fieldswap_object_id = 'plugin_AMPPayment_Swap';
  44. var $item_info;
  45. function UserDataPlugin_Save_AMPPayment (&$udm, $plugin_instance=null) {
  46. $this->init($udm, $plugin_instance);
  47. $this->confirmSSL();
  48. }
  49. function confirmSSL() {
  50. if ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) return true;
  51. $options = $this->getOptions( array('secure_server') );
  52. if ( !isset($options['secure_server']) ) return false;
  53. header ('Location: https://'.$options['secure_server'].$_SERVER['REQUEST_URI']);
  54. }
  55. function save($data) {
  56. $options = $this->getOptions();
  57. if (!isset($data['item_ID'])) return false;
  58. if (empty($data)) return true;
  59. if (!$this->confirmSSL()) trigger_error( "Payment Save operating without secure server" );
  60. $data['user_ID'] = $this->udm->uid;
  61. $data['merchant_ID'] = $options['merchant_ID'];
  62. $item = $this->getItems( $data['item_ID'] ) ;
  63. $this->processor->setData( $data );
  64. if ($this->processor->execute( $item->getData('Amount'), $item->getData('name'))) {
  65. $this->sendReceipt( $options );
  66. return true;
  67. }
  68. //in case of failure
  69. $this->_pass_errors_to_UDM();
  70. return false;
  71. }
  72. function sendReceipt( $options ) {
  73. if ((!isset($options['email_receipt'])) || $options['email_receipt']==FALSE) return false;
  74. $email_options['intro_text'] = $options['email_receipt_template'];
  75. $email_options['_payment_ID'] = $this->processor->id;
  76. $this->udm->doPlugin ('AMPPayment', 'EmailReceipt', $email_options);
  77. }
  78. function setProcessor( $type = null ) {
  79. if (!isset($this->processor)) {
  80. $this->processor =& new Payment ( $this->dbcon, $type );
  81. }
  82. }
  83. function getSaveFields() {
  84. $save_fields = array();
  85. $types_to_avoid = array ("html", "static", "header");
  86. foreach ($this->fields as $fname => $fdef) {
  87. if ( array_search($this->fields[$fname]['type'], $types_to_avoid)!==FALSE ) continue;
  88. $save_fields[] = $fname;
  89. }
  90. return $save_fields;
  91. }
  92. function getPaymentType() {
  93. if ( isset($_REQUEST[$this->addPrefix('Payment_Type')]) ) {
  94. return $_REQUEST[$this->addPrefix('Payment_Type')];
  95. }
  96. return false;
  97. }
  98. function _pass_errors_to_UDM () {
  99. if (!isset($this->processor->errors)) return false;
  100. foreach ($this->processor->errors as $error_message) {
  101. $this->udm->errorMessage( $error_message );
  102. }
  103. }
  104. function _register_fields_dynamic() {
  105. $options = $this->getOptions();
  106. $fields = & $this->fields;
  107. $fields = array();
  108. //Grab the item data
  109. $fields['item_ID'] = $this->setupPaymentItems( $options );
  110. if (!isset($fields['item_ID'])) return;
  111. //Get fields from the Payment object
  112. $fields = array_merge( $fields, $this->setupPaymentTypes($options) );
  113. }
  114. function setupPaymentItems( $options ) {
  115. if (!isset($options['item_IDs'])) return;
  116. $item_set = split("[ ]?,[ ]?", $options['item_IDs']);
  117. if (!is_array($item_set)) return;
  118. foreach ($item_set as $item_id) {
  119. $this->item_info[$item_id] = & new PaymentItem ( $this->dbcon, $item_id );
  120. }
  121. return array( 'label'=>$options['purchase_description'],
  122. 'type'=>'select',
  123. 'required'=>true,
  124. 'values'=>$this->getItemOptions(),
  125. 'public'=>true,
  126. 'enabled'=>true);
  127. }
  128. function &getItems( $item_id = null ) {
  129. if (!isset($this->item_info)) return false;
  130. if (!isset($item_id)) return $this->item_info;
  131. if (isset($this->item_info[$item_id])) return $this->item_info[$item_id];
  132. return false;
  133. }
  134. function getItemOptions() {
  135. if (!$this->getItems()) return false;
  136. $itemOptions = array();
  137. foreach ($this->getItems() as $item) {
  138. $itemOptions[$item->id] = $item->optionValue();
  139. }
  140. return $itemOptions;
  141. }
  142. function setupPaymentTypes( $options = array( )) {
  143. //if the payment type is already set
  144. //return only the fields from the relevent processor
  145. if ($selected_type = $this->getPaymentType()) {
  146. $this->setProcessor( $selected_type );
  147. $selector_field['Payment_Type'] = $this->getPaymentSelect( $options, $allow_select = false );
  148. $selector_field['Payment_Type']['default'] = $selected_type;
  149. return ($selector_field + $this->processor->fields);
  150. }
  151. //Otherwise Return fields from all processor types
  152. $selector_field['Payment_Type'] = $this->getPaymentSelect($options);
  153. $fieldswapper = &ElementSwapScript::instance();
  154. $fieldswapper->addSwapper($this->fieldswap_object_id );
  155. $fieldswapper->setForm( $this->udm->name, $this->fieldswap_object_id );
  156. $paymentType_fields = array();
  157. foreach ($this->getAllowedPaymentTypes( $options ) as $payment_type => $description) {
  158. $current = &new Payment ($this->dbcon, $payment_type);
  159. if ($payment_type == 'Check') $this->_setupCheck( $current, $options );
  160. $fieldswapper->addSet( $payment_type, $this->convertFieldDefstoDOM($current->fields), $this->fieldswap_object_id) ;
  161. $paymentType_fields = array_merge($paymentType_fields, $current->fields);
  162. }
  163. $this->_register_javascript ($fieldswapper->output());
  164. return ($selector_field + $paymentType_fields);
  165. }
  166. function _setupCheck( &$payment, $options = array( )) {
  167. if (!isset($options['check_payable'])) return false;
  168. $payment->paymentType->setPayable( $options['check_payable'] );
  169. }
  170. function getPaymentSelect( $options, $allow_select=true ) {
  171. $payment_options = $this->getAllowedPaymentTypes( $options );
  172. $type = $allow_select?'select':'hidden';
  173. $new_select = array('type' => $type,
  174. 'label' => 'Payment Method',
  175. 'enabled' => true,
  176. 'public' => true,
  177. 'required' => true
  178. );
  179. if (!$allow_select) return $new_select;
  180. $new_select['values'] = $payment_options;
  181. $new_select['attr'] = array( 'onChange'=>
  182. 'ActivateSwap( window.'.$this->fieldswap_object_id.', this.value );');
  183. return $new_select;
  184. }
  185. function getAllowedPaymentTypes( $options ) {
  186. $allowed_types = split("[ ]?,[ ]?", $options['allowed_payment_types']);
  187. return array_combine_key( $allowed_types, $this->options['allowed_payment_types']['values']);
  188. }
  189. function _register_options_dynamic () {
  190. if ($this->udm->admin) {
  191. $udm_mod_id = $this->dbcon->qstr( $this->udm->instance );
  192. $modlist_sql = "SELECT moduletext.id, moduletext.name FROM moduletext, modules
  193. WHERE modules.id = moduletext.modid
  194. AND modules.userdatamodid = $udm_mod_id
  195. ORDER BY name ASC";
  196. $modlist_rs = $this->dbcon->CacheExecute( $modlist_sql )
  197. or die( "Error fetching module information: " . $this->dbcon->ErrorMsg() );
  198. $modules[ '' ] = '--';
  199. while ( $row = $modlist_rs->FetchRow() ) {
  200. $modules[ $row['id'] ] = $row['name'];
  201. }
  202. $this->options['Email_Receipt_Template']['values']=$modules;
  203. }
  204. }
  205. }
  206. ?>