GFPaystationFormData

The Gravity Forms Paystation (3 party hosted) GFPaystationFormData class.

Defined (1)

The class is defined in the following location(s).

/class.GFPaystationFormData.php  
  1. class GFPaystationFormData { 
  2.  
  3. public $amount = 0; 
  4. public $total = 0; 
  5. public $formID = 0; 
  6.  
  7. // field mappings to GF form 
  8. public $MerchantReference; // merchant reference 
  9. public $CustomerDetails; // Optional 255 char string. 
  10. public $OrderDetails; // Optional 255 char string. 
  11. public $PaystationOverrideId; // Optional for overriding the paystation id in the main settings. 
  12.  
  13. private $isLastPageFlag = FALSE; 
  14. private $isCcHiddenFlag = FALSE; 
  15. private $hasPurchaseFieldsFlag = FALSE; 
  16.  
  17. /** 
  18. * initialise instance 
  19. * @param array $form 
  20. * @param GFPaystationFeed $feed 
  21. */ 
  22. public function __construct(&$form, $feed) { 
  23. // check for last page 
  24. $current_page = GFFormDisplay::get_source_page($form['id']); 
  25. $target_page = GFFormDisplay::get_target_page($form, $current_page, rgpost('gform_field_values')); 
  26. $this->isLastPageFlag = ($target_page == 0); 
  27.  
  28. // load the form data 
  29. $this->formID = $form['id']; 
  30. $this->loadForm($form, $feed); 
  31.  
  32. /** 
  33. * load the form data we care about from the form array 
  34. * @param array $form 
  35. * @param GFPaystationFeed $feed 
  36. */ 
  37. private function loadForm(&$form, $feed) { 
  38. // pick up feed mappings, set special mappings (form ID, title) 
  39. $inverseMap = $feed->getGfFieldMap(); 
  40. if (isset($inverseMap['title'])) { 
  41. $this->$inverseMap['title'] = $form['title']; 
  42. if (isset($inverseMap['form'])) { 
  43. $this->$inverseMap['form'] = $form['id']; 
  44.  
  45. // iterate over fields to collect data 
  46. foreach ($form['fields'] as &$field) { 
  47. $fieldName = empty($field['adminLabel']) ? $field['label'] : $field['adminLabel']; 
  48. $id = (string) $field['id']; 
  49.  
  50. switch(GFFormsModel::get_input_type($field)) { 
  51. case 'total': 
  52. $this->total = GFCommon::to_number(rgpost("input_{$id}")); 
  53. $this->hasPurchaseFieldsFlag = true; 
  54. break; 
  55.  
  56. default: 
  57. // check for product field 
  58. if (GFCommon::is_product_field($field['type']) || $field['type'] == 'donation') { 
  59. $this->amount += self::getProductPrice($form, $field); 
  60. $this->hasPurchaseFieldsFlag = true; 
  61. break; 
  62.  
  63. // check for feed mapping 
  64. if (isset($field['inputs']) && is_array($field['inputs'])) { 
  65. // compound field, see if want whole field 
  66. if (isset($inverseMap[$id])) { 
  67. // want whole field, concatenate values 
  68. $values = array(); 
  69. foreach($field['inputs'] as $input) { 
  70. $subID = strtr($input['id'], '.', '_'); 
  71. $values[] = trim(rgpost("input_{$subID}")); 
  72. $this->$inverseMap[$id] = implode(' ', array_filter($values, 'strlen')); 
  73. else { 
  74. // see if want any part-field 
  75. foreach($field['inputs'] as $input) { 
  76. $key = (string) $input['id']; 
  77. if (isset($inverseMap[$key])) { 
  78. $subID = strtr($input['id'], '.', '_'); 
  79. $this->$inverseMap[$key] = rgpost("input_{$subID}"); 
  80. else { 
  81. // simple field, just take value 
  82. if (isset($inverseMap[$id])) { 
  83. $this->$inverseMap[$id] = rgpost("input_{$id}"); 
  84.  
  85. // TODO: shipping? 
  86.  
  87. // if form didn't pass the total, pick it up from calculated amount 
  88. if ($this->total == 0) 
  89. $this->total = $this->amount; 
  90.  
  91. /** 
  92. * extract the price from a product field, and multiply it by the quantity 
  93. * @return float 
  94. */ 
  95. private static function getProductPrice($form, $field) { 
  96. $price = $qty = 0; 
  97. $isProduct = false; 
  98. $id = $field['id']; 
  99.  
  100. if (!GFFormsModel::is_field_hidden($form, $field, array())) { 
  101. $lead_value = rgpost("input_{$id}"); 
  102.  
  103. $qty_field = GFCommon::get_product_fields_by_type($form, array('quantity'), $id); 
  104. $qty = sizeof($qty_field) > 0 ? rgpost("input_{$qty_field[0]['id']}") : 1; 
  105.  
  106. switch ($field["inputType"]) { 
  107. case 'singleproduct': 
  108. $price = GFCommon::to_number(rgpost("input_{$id}_2")); 
  109. $qty = GFCommon::to_number(rgpost("input_{$id}_3")); 
  110. $isProduct = true; 
  111. break; 
  112.  
  113. case 'hiddenproduct': 
  114. $price = GFCommon::to_number($field["basePrice"]); 
  115. $isProduct = true; 
  116. break; 
  117.  
  118. case 'donation': 
  119. case 'price': 
  120. $price = GFCommon::to_number($lead_value); 
  121. $isProduct = true; 
  122. break; 
  123.  
  124. default: 
  125. // handle drop-down lists 
  126. if (!empty($lead_value)) { 
  127. list($name, $price) = rgexplode('|', $lead_value, 2); 
  128. $isProduct = true; 
  129. break; 
  130.  
  131. // pick up extra costs from any options 
  132. if ($isProduct) { 
  133. $options = GFCommon::get_product_fields_by_type($form, array('option'), $id); 
  134. foreach($options as $option) { 
  135. if (!GFFormsModel::is_field_hidden($form, $option, array())) { 
  136. $option_value = rgpost("input_{$option['id']}"); 
  137.  
  138. if (is_array(rgar($option, 'inputs'))) { 
  139. foreach($option['inputs'] as $input) { 
  140. $input_value = rgpost('input_' . str_replace('.', '_', $input['id'])); 
  141. $option_info = GFCommon::get_option_info($input_value, $option, true); 
  142. if(!empty($option_info)) 
  143. $price += GFCommon::to_number(rgar($option_info, 'price')); 
  144. elseif (!empty($option_value)) { 
  145. $option_info = GFCommon::get_option_info($option_value, $option, true); 
  146. $price += GFCommon::to_number(rgar($option_info, 'price')); 
  147.  
  148. $price *= $qty; 
  149.  
  150. return $price; 
  151.  
  152. /** 
  153. * check whether we're on the last page of the form 
  154. * @return boolean 
  155. */ 
  156. public function isLastPage() { 
  157. return $this->isLastPageFlag; 
  158.  
  159. /** 
  160. * check whether CC field is hidden (which indicates that payment is being made another way) 
  161. * @return boolean 
  162. */ 
  163. public function isCcHidden() { 
  164. return $this->isCcHiddenFlag; 
  165.  
  166. /** 
  167. * check whether form has any product fields (because CC needs something to bill against) 
  168. * @return boolean 
  169. */ 
  170. public function hasPurchaseFields() { 
  171. return $this->hasPurchaseFieldsFlag;