Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
checkout_action_call_rest_resource.inc
1 <?php
17 require_once SQ_PACKAGES_PATH.'/ecommerce/checkout_action/checkout_action.inc';
18 require_once SQ_ATTRIBUTES_PATH.'/http_request/http_request.inc';
19 require_once SQ_ATTRIBUTES_PATH.'/email_format/email_format.inc';
20 
21 define('ECOM_CHECKOUT_REST_ACTION_LOG_FILE', 'ecom_rest_checkout_action');
22 
35 {
36 
41  function __construct()
42  {
43  parent::__construct();
44 
45  }//end constructor
46 
47 
54  public static function paintInlineInterface(Ecommerce_Checkout $checkout, $settings, Backend_Outputter $o, $prefix)
55  {
56  if (!isset($settings['http_request'])) {
57  $req_settings = serialize(Array());
58  } else {
59  $req_settings = $settings['http_request'];
60  }
61 
62  if (!isset($settings['email_format'])) {
63  $email_settings = serialize(Array());
64  } else {
65  $email_settings = $settings['email_format'];
66  }
67 
68  // Set up HTTP request attribute
69  $http_request = new Asset_Attribute_HTTP_Request(0, $req_settings);
70 
71  // Set up Error Response Email attribute
72  $email_format = new Asset_Attribute_Email_Format(0, $email_settings);
73 
74  echo '<p class="sq-backend-section-subheading">REST Request</p>';
75  echo '<div id="'.$prefix.'"_http_request_div" style="padding: 0.5em 0px 2em 30px;">';
76  $http_request->paint($prefix.'_hr');
77  echo '</div>';
78 
79  // Paint "order array" setup screen
80  echo '<p class="sq-backend-section-subheading">Order Array Configuration</p>';
81  echo '<div id="'.$prefix.'"_order_array_div" style="padding: 0.5em 0px 2em 30px;">';
82  self::_paintOrderArraySetup($checkout, $settings, $o, $prefix);
83  echo '</div>';
84 
85  // Paint error email response configuration screen
86  echo '<p class="sq-backend-section-subheading"> Error Response Notification Email</p>';
87  echo '<div id="'.$prefix.'"_email_format_div" style="padding: 0.5em 0px 2em 30px;">';
88  $email_format->paint($prefix.'_email');
89  echo '</div>';
90 
91  // Paint "order array" setup screen
92  echo '<p class="sq-backend-section-subheading">Available Keywords</p>';
93  echo '<div id="'.$prefix.'"_keywords_list_div" style="padding: 0.5em 0px 2em 30px;">';
94  self::_paintAvailableKeywords($prefix, $o, $settings);
95  echo '</div>';
96 
97  }//end paintInlineInterface()
98 
99 
106  private static function _paintOrderArraySetup(Ecommerce_Checkout $checkout, $settings, Backend_Outputter $o, $prefix)
107  {
108  $order_array = array_get_index($settings, 'order_array', Array());
109 
110  $current_customisation = self::_getCurrentCustomisation($prefix, $settings);
111 
112  // Customise asset types drop down name
113  $dropdown_field = $prefix.'_asset_type_customise';
114 
115  ?>
116  <table style="width:100%">
117  <tr><td><b>Customise Order Asset Types</b></td></tr>
118  <?php
119  // Configured Product asset types
120  $configured_assets = array_keys($order_array);
121  if (!empty($configured_assets)) { ?>
122  <tr><td>
123  <table class="sq-backend-table" style="width:50%">
124  <tr>
125  <td class="sq-backend-table-header">Customised Order Asset Types</td>
126  <td class="sq-backend-table-header">Remove</td>
127  </tr>
128  <?php
129 
130  foreach ($configured_assets as $key) {
131 
132  $type_name = ucwords(str_replace('_', ' ', $key));
133 
134  // when we click on a customisation, set a hidden field to the
135  // key that we are customising and submit the screen to save any options
136  // that have been changed
137 
138  $src = 'Javascript: document.getElementById(\''.$dropdown_field.'\').value = \''.$key.'\';';
139  $src .= 'main_form.submit()';
140  ?>
141  <tr>
142  <td>
143  <a href="<?php echo $src?>"><?php echo $type_name; ?></a>
144  </td>
145  <td style="width:10%">
146  <?php check_box($dropdown_field.'_remove['.$key.']', 1, FALSE); ?>
147  </td>
148  </tr>
149  <?php
150  }//end foreach
151  ?>
152  </table>
153  </td></tr>
154  <?php
155  }
156  hidden_field($dropdown_field, '');
157  echo '<tr><td>';
158  // Dropdown to select the Project asset type
159  $current_customisation = self::_getCurrentCustomisation($prefix, $settings);
160  self::_paintAssetTypeChooser($prefix.'_asset_type_selected', $current_customisation);
161  echo '</td></tr>';
162 
163  // Interface to setup the paramters for the "Order Array"
164  if (!empty($current_customisation)) {
165  ?>
166  <tr><td>&nbsp;</td></tr>
167  <tr><td><b>Parameter Setup for "<?php echo ucwords(str_replace('_', ' ', $current_customisation)); ?>"</b></td></tr>
168  <?php
169 
170  $customised_values = array_get_index($order_array, $current_customisation, Array());
171  if (!empty($customised_values)) {
172  ?>
173  <tr><td>
174  <table class="sq-backend-table" style="width:60%">
175  <tr>
176  <td class="sq-backend-table-header">Array Item Name</td>
177  <td class="sq-backend-table-header">Value</td>
178  <td class="sq-backend-table-header">Remove</td>
179  </tr>
180  <?php
181  foreach($customised_values as $param => $value) {
182  echo '<tr>';
183  echo '<td>'.$param.'</td>';
184  echo '<td>';
185  text_box($prefix.'_params_value['.$param.']', $value, 50);
186  echo '</td>';
187  echo '<td style="width:10%">';
188  check_box($prefix.'_params_remove['.$param.']', 1, FALSE);
189  echo '</td>';
190  echo '</tr>';
191  }
192  ?>
193  </table>
194  </td></tr>
195  <?php
196  }
197  echo '<tr><td>';
198  echo "Add new parameter: ";
199  text_box($prefix.'_new_param', '', 21);
200  hidden_field($prefix.'_asset_type_submitted', $current_customisation);
201  echo '&nbsp;';
202  submit_button($prefix.'_new_param_button', translate('go'), 'document.getElementById(\''.$dropdown_field.'\').value = \''.$current_customisation.'\'; main_form.submit()');
203  echo '</td></tr>';
204  }
205  ?>
206  </table><?php
207 
208  }// _paintOrderArraySetup()
209 
210 
218  private static function _paintAvailableKeywords($prefix, Backend_Outputter $o, $settings)
219  {
220  $asset_type = self::_getCurrentCustomisation($prefix, $settings);
221  ?>
222  <table style="width:100%">
223  <?php
224  if (!empty($asset_type)) {
225  $asset_type_name = ucwords(str_replace('_', ' ', $asset_type));
226  $order_array_keywords = self::_getOrderArrayKeywords($asset_type);
227  ?>
228  <tr><td><b>In Order Array for "<?php echo $asset_type_name ?>" Type Asset (Order Items Keywords)</b></td></tr>
229  <tr><td>
230  <table class="sq-backend-table" style="width:80%">
231  <tr>
232  <td class="sq-backend-table-header" style="width:30%">Keywords</td>
233  <td class="sq-backend-table-header">Description</td>
234  </tr>
235  <?php
236  foreach($order_array_keywords as $keyword => $desc) {
237  echo '<tr>';
238  echo '<td>%'.$keyword.'%</td>';
239  echo '<td>'.$desc.'</td>';
240  echo '</tr>';
241  }//end foreach
242 
243  ?>
244  <tr><td colspan="2">NOTE: Generic asset keywords (%asset_assetid%, %asset_name%, etc) and metadata keywords (%asset_metadata_<MFIELD_NAME>%) for the product item assets are also available</td></tr>
245  </table>
246  </td></tr>
247  <tr><td>&nbsp;</td></tr>
248  <?php
249  }
250 
251  $rest_keywords = self::_getRestRequestKeywords();
252  ?>
253  <tr><td><b>In REST Request (Order Keywords)</b></td></tr>
254  <tr><td>
255  <table class="sq-backend-table" style="width:80%">
256  <tr>
257  <td class="sq-backend-table-header" style="width:30%">Keywords</td>
258  <td class="sq-backend-table-header">Description</td>
259  </tr>
260  <?php
261  foreach($rest_keywords as $keyword => $desc) {
262  echo '<tr>';
263  echo '<td>%'.$keyword.'%</td>';
264  echo '<td>'.$desc.'</td>';
265  echo '</tr>';
266  }//end foreach
267 
268  ?>
269  </table>
270  </td></tr>
271  <tr><td>&nbsp;</td></tr>
272  <?php
273 
274  $email_keywords = self::_getEmailFormatKeywords();
275  ?>
276  <tr><td><b>In Error Response Notification Email</b></td></tr>
277  <tr><td>
278  <table class="sq-backend-table" style="width:80%">
279  <tr>
280  <td class="sq-backend-table-header" style="width:30%">Keywords</td>
281  <td class="sq-backend-table-header">Description</td>
282  </tr>
283  <?php
284  foreach($email_keywords as $keyword => $desc) {
285  echo '<tr>';
286  echo '<td>%'.$keyword.'%</td>';
287  echo '<td>'.$desc.'</td>';
288  echo '</tr>';
289  }//end foreach
290 
291  ?>
292  <tr><td colspan="2">NOTE: All keywords in the "Order Keywords" list are also available</td></tr>
293  </table>
294  </td></tr>
295 
296  </table>
297  <?php
298 
299  }
300 
301 
313  private static function _paintAssetTypeChooser($field_name, $selected_type_code)
314  {
315  // All "product"/"ecommerce product" type assets in the system
316  $asset_types = $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants('product', TRUE);
317  $asset_types_info = Array();
318  foreach ($asset_types as $type_code) {
319  $asset_types_info[$type_code] = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code, 'name');
320  }
321  $asset_types_info = array_unique($asset_types_info);
322  arsort($asset_types_info);
323  $asset_types_info[''] = '';
324  $asset_types_info = array_reverse($asset_types_info);
325 
326  echo "Add Product asset type to customise: ";
327  combo_box($field_name, $asset_types_info, FALSE, $selected_type_code);
328  echo '&nbsp;';
329  submit_button($field_name.'_go', translate('go'), 'main_form.submit()');
330 
331  }//end _paintAssetTypeChooser()
332 
333 
343  private static function _getCurrentCustomisation($prefix, $settings)
344  {
345  $key = array_get_index($_POST, $prefix.'_asset_type_selected', '');
346 
347  // if there was nothing selected to customise, check for a current customisation in process
348  if (empty($key)) {
349  $key = array_get_index($_POST, $prefix.'_asset_type_current', '');
350  }
351 
352  // check to see if a current customisation was selected
353  if (isset($_POST[$prefix.'_asset_type_customise']) && !empty($_POST[$prefix.'_asset_type_customise'])) {
354  $key = $_POST[$prefix.'_asset_type_customise'];
355  }
356 
357  // if still nothing, get the first one from the list of customised assets
358  if (empty($key)) {
359  $customised_assets = array_keys(array_get_index($settings, 'order_array', Array()));
360  $key = isset($customised_assets[0]) ? $customised_assets[0] : '';
361  }
362 
363  return $key;
364 
365  }//end _getCurrentCustomisation()
366 
367 
374  public static function processInlineInterface(Ecommerce_Checkout $checkout, &$settings, Backend_Outputter $o, $prefix)
375  {
376  // Process "http request" settings
377  if (!isset($settings['http_request'])) {
378  $req_settings = serialize(Array());
379  } else {
380  $req_settings = $settings['http_request'];
381  }
382  $http_request = new Asset_Attribute_HTTP_Request(0, $req_settings);
383  $http_request->process($prefix.'_hr');
384  $settings['http_request'] = $http_request->value;
385 
386  // Process "email format" settings
387  if (!isset($settings['email_format'])) {
388  $email_settings = serialize(Array());
389  } else {
390  $email_settings = $settings['email_format'];
391  }
392  $email_format = new Asset_Attribute_Email_Format(0, $email_settings);
393  $email_format->process($prefix.'_email');
394  $settings['email_format'] = $email_format->value;
395 
396  // Process "order array" data
397  self::_processOrderArraySetup($checkout, $settings, $o, $prefix);
398 
399  return TRUE;
400 
401  }//end processInlineInterface()
402 
403 
410  public static function _processOrderArraySetup(Ecommerce_Checkout $checkout, &$settings, Backend_Outputter $o, $prefix)
411  {
412  $order_array = isset($settings['order_array']) ? $settings['order_array'] : Array();
413  $selected_asset_type = isset($_POST[$prefix.'_asset_type_submitted']) ? $_POST[$prefix.'_asset_type_submitted'] : '';
414 
415 
416  if (!empty($selected_asset_type)) {
417 
418  if (!isset($order_array[$selected_asset_type])) {
419  $order_array[$selected_asset_type] = Array();
420  }
421 
422  // Add new parameter for the selected asset type
423  if (!empty($_POST[$prefix.'_new_param'])) {
424  $order_array[$selected_asset_type][$_POST[$prefix.'_new_param']] = '';
425  }
426 
427  // Update parameter data
428  $update_params = isset($_POST[$prefix.'_params_value']) ? $_POST[$prefix.'_params_value'] : Array();
429  foreach($update_params as $param_name => $param_value) {
430  $order_array[$selected_asset_type][$param_name] = $param_value;
431  }
432 
433  // Remove the selected params
434  $remove_params = isset($_POST[$prefix.'_params_remove']) ? $_POST[$prefix.'_params_remove'] : Array();
435  foreach($remove_params as $param_name => $param_value) {
436  if (isset($order_array[$selected_asset_type][$param_name])) {
437  unset($order_array[$selected_asset_type][$param_name]);
438  }
439  }//end foreach
440  }
441  // Remove the selected customised asset types
442  $remove_types = isset($_POST[$prefix.'_asset_type_customise_remove']) ? $_POST[$prefix.'_asset_type_customise_remove'] : Array();
443  foreach($remove_types as $asset_type => $val) {
444  if (isset($order_array[$asset_type])) {
445  unset($order_array[$asset_type]);
446  }
447  }//end foreach
448 
449  // Update the "order array" value
450  $settings['order_array'] = $order_array;
451 
452  return TRUE;
453 
454  }//end _processOrderArraySetup()
455 
456 
463  public static function paintSummary(Ecommerce_Checkout $checkout, $settings, Backend_Outputter $o, $prefix)
464  {
465  if (!isset($settings['http_request'])) {
466  $req_settings = Array();
467  } else {
468  $req_settings = unserialize($settings['http_request']);
469  }
470 
471  ?><table class="no-borders">
472  <colgroup>
473  <col width="80" />
474  <col/>
475  </colgroup>
476  <tbody>
477  <tr>
478  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong><?php echo translate('http_request_method'); ?></strong></p></td>
479  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
480 
481  // Get manually-entered "method" settings.
482  if (!empty($req_settings['method'])) {
483  echo $req_settings['method'];
484  } else {
485  ?><span class="sq-backend-warning">No method specified.</span><?php
486  }
487  ?></p></td>
488  </tr>
489  <tr>
490  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong><?php echo translate('http_request_urls'); ?></strong></p></td>
491  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
492  if (!empty($req_settings['urls'])) {
493  foreach ($req_settings['urls'] as $url) {
494  echo ellipsisize($url, 512).'<br />';
495  }
496  } else {
497  ?><span class="sq-backend-warning">No URLs specified.</span><?php
498  }
499  ?></p></td></tr>
500  </tbody>
501  </table>
502  <?php
503 
504  }//end paintSummary()
505 
506 
513  public static function execute(Ecommerce_Checkout $checkout, Ecommerce_Order $order, $settings)
514  {
515  $req_settings = $settings['http_request'];
516 
517  // Set up http request object
518  $http_request = new Asset_Attribute_HTTP_Request(0, $req_settings);
519  $current_value = @unserialize($http_request->value);
520 
521  // Get list of keywords in http request
522  $keywords_list = Array();
523  if (isset($current_value['urls'])) {
524  $keywords_list = array_merge(
525  $keywords_list,
526  extract_keywords(implode(', ', $current_value['urls']))
527  );
528  }
529 
530  if (isset($current_value['request_headers'])) {
531  $keywords_list = array_merge(
532  $keywords_list,
533  extract_keywords(implode(', ', $current_value['request_headers']))
534  );
535  }
536 
537  if (isset($current_value['request_body'])) {
538  $keywords_list = array_merge(
539  $keywords_list,
540  extract_keywords($current_value['request_body'])
541  );
542  }
543 
544  if (isset($current_value['http_auth_options'])) {
545  $keywords_list = array_merge(
546  $keywords_list,
547  extract_keywords(implode(', ', $current_value['http_auth_options']))
548  );
549  }
550 
551  // Setup the email format attribute
552  $email_settings = $settings['email_format'];
553  $email_format = new Asset_Attribute_Email_Format(0, $email_settings);
554  $email_val = @unserialize($email_format->value);
555 
556  // Get list of keywords in email attribute
557  $keywords_list = array_merge($keywords_list, extract_keywords($email_val['html_format'].', '.$email_val['text_format'].', '.$email_val['subject'].', '.$email_val['from'].', '.$email_val['reply_to'].', '.implode(', ', $email_val['to'])));
558 
559  // Get order data
560  $order_xml = $order->attr('order_xml');
561  // If "strip HTML tags" global preference is enabled,
562  // then we will need to decode the encoded xml data to get proper xml
563  if (strpos($order_xml, '<') === FALSE) {
564  $order_xml = htmlspecialchars_decode($order_xml, ENT_COMPAT);
565  }
566  $order_data = xml_to_array($order_xml);
567 
568  // If the order was tax exempted (i.e. for non-taxable country)
569  if (isset($order_data['cart'][0]['total_tax'][0]) && ($order_data['cart'][0]['total_tax'][0] > 0)) {
570  $order_data['taxable'] = TRUE;
571  } else {
572  $order_data['cart'][0]['total_tax'][0] = '0';
573  $order_data['taxable'] = FALSE;
574  }
575 
576  // Get all the keyword replacements
577  $keywords_list = array_unique($keywords_list);
578  $replacements = Array();
579  foreach($keywords_list as $full_keyword) {
580  // Get the root
581  $keyword = parse_keyword($full_keyword, $modifiers);
582 
583  $replacement = '%'.$keyword.'%';
584  switch ($keyword) {
585  case 'order_subtotal':
586  $replacement = @$order_data['cart'][0]['sub_total'][0];
587  break;
588 
589  case 'order_total_tax':
590  $replacement = @$order_data['cart'][0]['total_tax'][0];
591  break;
592 
593  case 'order_total_delivery':
594  $replacement = @$order_data['cart'][0]['delivery_total'][0];
595  break;
596 
597  case 'order_grand_total_excl_delivery':
598  $replacement = @$order_data['cart'][0]['grand_total_ex_delivery'][0];
599  break;
600 
601  case 'order_grand_total':
602  $replacement = @$order_data['cart'][0]['grand_total'][0];
603  break;
604 
605  case 'order_currency_symbol':
606  $replacement = @$order_data['cart'][0]['currency_symbol'][0];
607  break;
608 
609  case 'order_item_count':
610  $items = @$order_data['cart'][0]['products'][0]['product'];
611  $item_count = 0;
612  if (!is_null($items)) {
613  foreach($items as $item) {
614  if (isset($item['quantity'])) {
615  $item_count += $item['quantity'][0];
616  }//end if
617  }//end foreach
618  }//end if
619  $replacement = $item_count;
620  break;
621 
622  case 'order_item_type_count':
623  $items = @$order_data['cart'][0]['products'][0]['product'];
624  $replacement = count($items);
625  break;
626 
627  case 'order_date':
628  $timezone_offset = substr(date('O', $order->created), 0, 1).str_pad(date('Z', $order->created)/60, 3, '0', STR_PAD_LEFT);
629  $replacement = date('YmdHis000', $order->created).$timezone_offset;
630  break;
631 
632  case 'order_date_reg':
633  $replacement = date('Y-m-d H:i:s', $order->created);
634  break;
635 
636  case 'order_array':
637  $replacement = self::_evaluateOrderArrayKeywordReplacement($order_data, $settings);
638  break;
639 
640  }//end switch
641 
642  // Delivery form response keywords
643  $matches = Array();
644  if (preg_match('|order_response_q(\d+)|i', $keyword, $matches) && isset($matches[1])) {
645  $delivery_form_values = $order->attr('order_form_value');
646  if (isset($delivery_form_values['q'.$matches[1]])) {
647  $replacement = $delivery_form_values['q'.$matches[1]];
648  }
649  }
650 
651  // Get remaining replacements for the Ecomm order asset
652  if ($replacement == '%'.$keyword.'%') {
653  $replacement = $order->getKeywordReplacement($keyword);
654  }
655 
656  // If keyword been replaced, apply the modifiers
657  if (!is_null($replacement) && $replacement != '%'.$keyword.'%') {
658  apply_keyword_modifiers($replacement, $modifiers);
659  $replacements[$full_keyword] = $replacement;
660  }
661  }//end foreach
662 
663  // Set them to the http request object
664  $http_request->setKeywordReplacements($replacements);
665 
666  // Send the request
667  $res = $http_request->run();
668  $rest_request = @$res['request'];
669  $rest_response = @$res['response'];
670 
671  // Write the info in the log file
672  self::_log('-- Order Assetid --');
673  self::_log($order->id);
674  self::_log('-- REST Request --');
675  self::_log($rest_request);
676  self::_log('-- REST Response --');
677  self::_log($rest_response);
678 
679  // Send the notification email if REST resource returns non-2xx response
680  $http_response_code = @$res['response']['info']['http_code'];
681  if (strpos($http_response_code, '2') !== 0) {
682  foreach($replacements as $key => $val) {
683  $replacements[$key] = urldecode($val);
684  }
685  // Further add these additionals keyword replacements specified to the email
686  $request_headers = '';
687  foreach($rest_request['headers'] as $key => $val) {
688  $request_headers .= $key.': '.$val.'<br />';
689  }
690  $replacements['rest_request_headers'] = $request_headers;
691  $replacements['rest_request_body'] = isset($rest_request['body']) ? $rest_request['body'] : '';
692 
693  $response_headers = '';
694  foreach($rest_response['headers'] as $key => $val) {
695  $response_headers .= $key.': '.$val.'<br />';
696  }
697  $replacements['rest_response_headers'] = $response_headers;
698  $replacements['rest_response_body'] = isset($rest_response['body']) ? $rest_response['body'] : '';
699 
700  $email_format->sendMail($replacements);
701  }
702 
703  return TRUE;
704 
705  }//end execute()
706 
707 
714  private static function _evaluateOrderArrayKeywordReplacement($order_info, $settings)
715  {
716  $order_array = '';
717  $ordered_items_raw = @$order_info['cart'][0]['products'][0]['product'];
718  $params = @$settings['order_array'];
719 
720  if (empty($params) || empty($ordered_items_raw)) {
721 
722  return $order_array;
723  }
724 
725  foreach($ordered_items_raw as $item) {
726  if (isset($item['id'][0])) {
727  $ordered_items[$item['id'][0]] = $item;
728  }
729  }
730 
731  $taxable = $order_info['taxable'];
732  $items_asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_keys($ordered_items));
733  $array_index = 0;
734  foreach($items_asset_info as $item_id => $item_asset_info) {
735  $item_data = $ordered_items[$item_id];
736  $item_type = $item_asset_info['type_code'];
737 
738  // Parameters configured for this Product type
739  $item_params = isset($params[$item_type]) ? $params[$item_type] : FALSE;
740  if (!$item_params) {
741  continue;
742  }
743  $item_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($item_id);
744  foreach($item_params as $param_name => $value) {
745  // Extract and replace the keywords in the parameter value, if any
746  $keywords = extract_keywords($value);
747  if (empty($keywords)) {
748  $parm_value = $value;
749  } else {
750  $replacements = Array();
751  foreach($keywords as $full_keyword) {
752  $keyword = parse_keyword($full_keyword, $modifiers);
753  $replacement = '%'.$keyword.'%';
754  if (strpos($keyword, 'product_') !== FALSE) {
755  $item_index_name = substr($keyword, 8);
756  if (isset($item_data[$item_index_name])) {
757  switch ($item_index_name) {
758  case 'item_price_incl_tax':
759  // The product pricing data in the order will include tax value,
760  // so for tax exempted order, fix the pricings for the keyword replcements here
761  if (!$taxable) {
762  $replacement = @$item_data['item_price'][0];
763  } else {
764  $replacement = $item_data[$item_index_name][0];
765  }
766  break;
767 
768  case 'total_item_price_incl_tax':
769  if (!$taxable) {
770  $replacement = @$item_data['total_item_price'][0];
771  } else {
772  $replacement = $item_data[$item_index_name][0];
773  }
774  break;
775 
776  case 'item_tax':
777  if (!$taxable) {
778  $replacement = '0';
779  } else {
780  $replacement = $item_data[$item_index_name][0];
781  }
782  break;
783 
784  case 'total_item_tax':
785  if (!$taxable) {
786  $replacement = '0';
787  } else {
788  $replacement = $item_data[$item_index_name][0];
789  }
790  break;
791 
792  default:
793  $replacement = $item_data[$item_index_name][0];
794  break;
795  }//end switch
796  }//end if
797  } else if ($keyword == 'item_array_index') {
798  $replacement = str_pad($array_index, 4, '0', STR_PAD_LEFT);
799  }
800  if ($replacement == '%'.$keyword.'%') {
801  $replacement = $item_asset->getKeywordReplacement($keyword);
802  }
803 
804  // If keyword was replaced apply the modifiers
805  if ($replacement != '%'.$keyword.'%') {
806  apply_keyword_modifiers($replacement, $modifiers);
807  } else {
808  // Blank out the keywords not replaced
809  $replacement = '';
810  }
811  $replacements[$full_keyword] = $replacement;
812  }//end foreach
813  replace_keywords($value, $replacements);
814  }//end else
815 
816  // Add the parameter to the array
817  $order_array .= $param_name.'['.$array_index.']='.urlencode($value).'&';
818  }//end foreach
819 
820  $array_index++;
821 
822  }//end foreach
823 
824  $order_array = rtrim($order_array, '&');
825 
826  return $order_array;
827 
828  }//end _evaluateOrderArrayKeywordReplacement()
829 
830 
837  public static function isValid(Ecommerce_Checkout $checkout, $settings)
838  {
839  if (!isset($settings['http_request'])) {
840  $req_settings = Array();
841  } else {
842  $req_settings = unserialize($settings['http_request']);
843  }
844 
845  // if either no method or urls are set, won't be able to send the request...
846  if (empty($req_settings['method'])) return FALSE;
847  if (empty($req_settings['urls'])) return FALSE;
848 
849  return TRUE;
850 
851  }//end isValid
852 
853 
860  private static function _getAvailableKeywords($asset_type, $ommit_generic_keywords=TRUE)
861  {
862  $GLOBALS['SQ_SYSTEM']->am->includeAsset($asset_type);
863  $temp_asset = new $asset_type();
864  $keywords = $temp_asset->getAvailableKeywords();
865 
866  if ($ommit_generic_keywords) {
867  // Get rid of generic default keywords from the list
868  foreach($keywords as $keyword => $desc) {
869  if (strpos($keyword, 'asset_attribute_') === FALSE) {
870  unset($keywords[$keyword]);
871  } else {
872  // We have reached asset_attribute_* keyword, which means
873  // generic keywords are no more in the list
874  break;
875  }
876  }//end foreach
877  }
878 
879  return $keywords;
880 
881  }//end _getAvailableKeywords()
882 
883 
890  private static function _getOrderArrayKeywords($asset_type)
891  {
892  $keywords = Array();
893  $keywords['item_array_index'] = 'Four digit item\'s position value in the "order array"';
894  $keywords['asset_attribute_name'] = "The full name of the product item";
895  $keywords['product_quantity'] = "Total number of the items of the given type in the order";
896  $keywords['product_item_price'] = "Product item unit price excluding tax";
897  $keywords['product_item_price_incl_tax'] = "Product item unit price including tax";
898  $keywords['product_item_tax'] = "Product item unit tax amount";
899  $keywords['product_total_item_price'] = "Total price of the items of the given type in the order excluding tax";
900  $keywords['product_total_item_price_incl_tax'] = "Total price of the items of the given type in the order including tax";
901  $keywords['product_total_item_tax'] = "Total tax for the given item type in the order";
902 
903  $keywords += self::_getAvailableKeywords($asset_type);
904 
905  return $keywords;
906 
907  }//end _getOrderArrayKeywords()
908 
909 
916  private static function _getRestRequestKeywords()
917  {
918  // Keywords added for this "Checkout action"
919  $keywords = Array(
920  'order_array' => 'Array of data for the each item in the order (as configured in the "Order Array" section)',
921  'order_response_qX' => 'Response submitted for Delivery Method\'s question "X"',
922  'order_item_count' => 'Total number of quantity in the order',
923  'order_item_type_count' => 'Total number of item types in the order',
924  'order_subtotal' => 'Total transaction amount excluding tax and delivery',
925  'order_total_tax' => 'Total transaction amount including tax',
926  'order_total_delivery' => 'Total delivery amount',
927  'order_grand_total_excl_delivery' => 'Grand total transaction amount excluding delivery',
928  'order_grand_total' => 'Grand total transaction amount',
929  'order_date' => 'Order date timestamp in YYYYMMDDHHNNSSKKKs000 format',
930  'order_date_reg' => 'Order date timestamp in YYYY:MM:DD HH:MM:SS format',
931  'order_currency_symbol' => 'Currency symbol for the transaction amount',
932  );
933 
934  // Available "Ecommerce Order" keywords
935  $keywords += self::_getAvailableKeywords('ecommerce_order');
936 
937  return $keywords;
938 
939  }//end _getRestRequestKeywords
940 
941 
948  private static function _getEmailFormatKeywords()
949  {
950  // Keywords added for this "Checkout action"
951  $keywords = Array(
952  'rest_request_headers' => 'REST request headers',
953  'rest_request_body' => 'REST request body content',
954  'rest_response_headers' => 'Response headers returned by the REST resource',
955  'rest_response_body' => 'Response body returned by the REST resource',
956  );
957 
958  return $keywords;
959 
960  }//end _getEmailFormatKeywords();
961 
962 
963 
974  private static function _log($message, $level = E_USER_NOTICE, $encode=FALSE)
975  {
976  if (is_array($message)) {
977  $message = print_r($message, TRUE);
978  }
979 
980  log_write($message, ECOM_CHECKOUT_REST_ACTION_LOG_FILE, $level, $encode);
981 
982  }//end _log()
983 
984 
985 }//end class
986 
987 ?>