| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* class for managing form data |
| 5 |
*/ |
| 6 |
class GFEwayFormData { |
| 7 |
|
| 8 |
public $amount = 0; |
| 9 |
public $total = 0; |
| 10 |
public $ccName = ''; |
| 11 |
public $ccNumber = ''; |
| 12 |
public $ccExpMonth = ''; |
| 13 |
public $ccExpYear = ''; |
| 14 |
public $ccCVN = ''; |
| 15 |
public $namePrefix = ''; |
| 16 |
public $firstName = ''; |
| 17 |
public $lastName = ''; |
| 18 |
public $email = ''; |
| 19 |
public $address = ''; // simple address, for regular payments |
| 20 |
public $address_street = ''; // street address, for recurring payments |
| 21 |
public $address_suburb = ''; // suburb, for recurring payments |
| 22 |
public $address_state = ''; // state, for recurring payments |
| 23 |
public $address_country = ''; // country, for recurring payments |
| 24 |
public $postcode = ''; // postcode, for both regular and recurring payments |
| 25 |
public $phone = ''; // phone number, for recurring payments |
| 26 |
public $recurring = FALSE; // false, or an array of inputs from complex field |
| 27 |
public $ccField = FALSE; // handle to meta-"field" for credit card in form |
| 28 |
|
| 29 |
private $isLastPageFlag = FALSE; |
| 30 |
private $isCcHiddenFlag = FALSE; |
| 31 |
private $hasPurchaseFieldsFlag = FALSE; |
| 32 |
|
| 33 |
/** |
| 34 |
* initialise instance |
| 35 |
* @param array $form |
| 36 |
*/ |
| 37 |
public function __construct(&$form) { |
| 38 |
// check for last page |
| 39 |
$current_page = GFFormDisplay::get_source_page($form['id']); |
| 40 |
$target_page = GFFormDisplay::get_target_page($form, $current_page, rgpost('gform_field_values')); |
| 41 |
$this->isLastPageFlag = ($target_page == 0); |
| 42 |
|
| 43 |
// load the form data |
| 44 |
$this->loadForm($form); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* load the form data we care about from the form array |
| 49 |
* @param array $form |
| 50 |
*/ |
| 51 |
private function loadForm(&$form) { |
| 52 |
foreach ($form['fields'] as &$field) { |
| 53 |
$id = $field['id']; |
| 54 |
|
| 55 |
switch(RGFormsModel::get_input_type($field)){ |
| 56 |
case 'name': |
| 57 |
// only pick up the first name field (assume later ones are additional info) |
| 58 |
if (empty($this->firstName) && empty($this->lastName)) { |
| 59 |
$this->namePrefix = rgpost("input_{$id}_2"); |
| 60 |
$this->firstName = rgpost("input_{$id}_3"); |
| 61 |
$this->lastName = rgpost("input_{$id}_6"); |
| 62 |
} |
| 63 |
break; |
| 64 |
|
| 65 |
case 'email': |
| 66 |
// only pick up the first email address field (assume later ones are additional info) |
| 67 |
if (empty($this->email)) |
| 68 |
$this->email = rgpost("input_{$id}"); |
| 69 |
break; |
| 70 |
|
| 71 |
case 'phone': |
| 72 |
// only pick up the first phone number field (assume later ones are additional info) |
| 73 |
if (empty($this->phone)) |
| 74 |
$this->phone = rgpost("input_{$id}"); |
| 75 |
break; |
| 76 |
|
| 77 |
case 'address': |
| 78 |
// only pick up the first address field (assume later ones are additional info, e.g. shipping) |
| 79 |
if (empty($this->address) && empty($this->postcode)) { |
| 80 |
$this->postcode = trim(rgpost("input_{$id}_5")); |
| 81 |
$parts = array(trim(rgpost("input_{$id}_1")), trim(rgpost("input_{$id}_2"))); |
| 82 |
$this->address_street = implode(', ', array_filter($parts, 'strlen')); |
| 83 |
$this->address_suburb = trim(rgpost("input_{$id}_3")); |
| 84 |
$this->address_state = trim(rgpost("input_{$id}_4")); |
| 85 |
$this->address_country = trim(rgpost("input_{$id}_6")); |
| 86 |
|
| 87 |
// aggregate street, city, state, country into a single string (for regular one-off payments) |
| 88 |
$parts = array($this->address_street, $this->address_suburb, $this->address_state, $this->address_country); |
| 89 |
$this->address = implode(', ', array_filter($parts, 'strlen')); |
| 90 |
} |
| 91 |
break; |
| 92 |
|
| 93 |
case 'creditcard': |
| 94 |
$this->isCcHiddenFlag = RGFormsModel::is_field_hidden($form, $field, RGForms::post('gform_field_values')); |
| 95 |
$this->ccField =& $field; |
| 96 |
$this->ccName = rgpost("input_{$id}_5"); |
| 97 |
$this->ccNumber = self::cleanCcNumber(rgpost("input_{$id}_1")); |
| 98 |
$ccExp = rgpost("input_{$id}_2"); |
| 99 |
if (is_array($ccExp)) |
| 100 |
list($this->ccExpMonth, $this->ccExpYear) = $ccExp; |
| 101 |
$this->ccCVN = rgpost("input_{$id}_3"); |
| 102 |
break; |
| 103 |
|
| 104 |
case 'total': |
| 105 |
$this->total = GFCommon::to_number(rgpost("input_{$id}")); |
| 106 |
$this->hasPurchaseFieldsFlag = true; |
| 107 |
break; |
| 108 |
|
| 109 |
case GFEWAY_FIELD_RECURRING: |
| 110 |
// only pick it up if it isn't hidden |
| 111 |
if (!RGFormsModel::is_field_hidden($form, $field, RGForms::post('gform_field_values'))) { |
| 112 |
$this->recurring = GFEwayRecurringField::getPost($id); |
| 113 |
} |
| 114 |
break; |
| 115 |
|
| 116 |
default: |
| 117 |
// check for product field |
| 118 |
if (in_array($field['type'], array('option', 'donation', 'product', 'total', 'shipping', 'calculation'))) { |
| 119 |
$this->amount += self::getProductPrice($form, $field); |
| 120 |
$this->hasPurchaseFieldsFlag = true; |
| 121 |
} |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// TODO: shipping? |
| 127 |
|
| 128 |
// if form didn't pass the total, pick it up from calculated amount |
| 129 |
if ($this->total == 0) { |
| 130 |
$this->total = $this->amount; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* extract the price from a product field, and multiply it by the quantity |
| 136 |
* @return float |
| 137 |
*/ |
| 138 |
private static function getProductPrice($form, $field) { |
| 139 |
$price = $qty = 0; |
| 140 |
$isProduct = false; |
| 141 |
$id = $field['id']; |
| 142 |
|
| 143 |
if (!RGFormsModel::is_field_hidden($form, $field, array())) { |
| 144 |
$lead_value = rgpost("input_{$id}"); |
| 145 |
|
| 146 |
// look for a quantity field for product |
| 147 |
$qty_fields = GFCommon::get_product_fields_by_type($form, array('quantity'), $id); |
| 148 |
if (empty($qty_fields)) { |
| 149 |
$qty_field = false; |
| 150 |
$qty = 1; |
| 151 |
} |
| 152 |
else { |
| 153 |
$qty_field = $qty_fields[0]; |
| 154 |
$qty = (float) rgpost("input_{$qty_field['id']}"); |
| 155 |
} |
| 156 |
|
| 157 |
switch ($field["inputType"]) { |
| 158 |
case 'singleproduct': |
| 159 |
$price = GFCommon::to_number(rgpost("input_{$id}_2")); |
| 160 |
if (!$qty_field) { |
| 161 |
// no quantity field, pick it up from input |
| 162 |
$qty = (float) GFCommon::to_number(rgpost("input_{$id}_3")); |
| 163 |
} |
| 164 |
$isProduct = true; |
| 165 |
break; |
| 166 |
|
| 167 |
case 'hiddenproduct': |
| 168 |
$price = GFCommon::to_number($field["basePrice"]); |
| 169 |
$isProduct = true; |
| 170 |
break; |
| 171 |
|
| 172 |
case 'donation': |
| 173 |
case 'price': |
| 174 |
$price = GFCommon::to_number($lead_value); |
| 175 |
$isProduct = true; |
| 176 |
break; |
| 177 |
|
| 178 |
default: |
| 179 |
// handle drop-down lists |
| 180 |
if (!empty($lead_value)) { |
| 181 |
list($name, $price) = rgexplode('|', $lead_value, 2); |
| 182 |
$isProduct = true; |
| 183 |
} |
| 184 |
break; |
| 185 |
} |
| 186 |
|
| 187 |
// pick up extra costs from any options |
| 188 |
if ($isProduct) { |
| 189 |
$options = GFCommon::get_product_fields_by_type($form, array('option'), $id); |
| 190 |
foreach($options as $option){ |
| 191 |
if (!RGFormsModel::is_field_hidden($form, $option, array())) { |
| 192 |
$option_value = rgpost("input_{$option['id']}"); |
| 193 |
|
| 194 |
if (is_array(rgar($option, 'inputs'))) { |
| 195 |
foreach($option['inputs'] as $input){ |
| 196 |
$input_value = rgpost('input_' . str_replace('.', '_', $input['id'])); |
| 197 |
$option_info = GFCommon::get_option_info($input_value, $option, true); |
| 198 |
if(!empty($option_info)) |
| 199 |
$price += GFCommon::to_number(rgar($option_info, 'price')); |
| 200 |
} |
| 201 |
} |
| 202 |
elseif (!empty($option_value)){ |
| 203 |
$option_info = GFCommon::get_option_info($option_value, $option, true); |
| 204 |
$price += GFCommon::to_number(rgar($option_info, 'price')); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
$price *= $qty; |
| 210 |
} |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
return $price; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* clean up credit card number, removing spaces and dashes, so that it should only be digits if correctly submitted |
| 219 |
* @param string $ccNumber |
| 220 |
* @return string |
| 221 |
*/ |
| 222 |
private static function cleanCcNumber($ccNumber) { |
| 223 |
return strtr($ccNumber, array(' ' => '', '-' => '')); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* check whether we're on the last page of the form |
| 228 |
* @return boolean |
| 229 |
*/ |
| 230 |
public function isLastPage() { |
| 231 |
return $this->isLastPageFlag; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* check whether CC field is hidden (which indicates that payment is being made another way) |
| 236 |
* @return boolean |
| 237 |
*/ |
| 238 |
public function isCcHidden() { |
| 239 |
return $this->isCcHiddenFlag; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* check whether form has any product fields or a recurring payment field (because CC needs something to bill against) |
| 244 |
* @return boolean |
| 245 |
*/ |
| 246 |
public function hasPurchaseFields() { |
| 247 |
return $this->hasPurchaseFieldsFlag || !!$this->recurring; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* check whether form a recurring payment field |
| 252 |
* @return boolean |
| 253 |
*/ |
| 254 |
public function hasRecurringPayments() { |
| 255 |
return !!$this->recurring; |
| 256 |
} |
| 257 |
} |
| 258 |
|