Index: trunk/extensions/DonationInterface/globalcollect_gateway/scripts/orphans.php |
— | — | @@ -1,4 +1,6 @@ |
2 | 2 | <?php |
| 3 | +//If you want to use this script, you will have to add the following line to LocalSettings.php: |
| 4 | +//$wgAutoloadClasses['GlobalCollectOrphanAdapter'] = $IP . '/extensions/DonationInterface/globalcollect_gateway/scripts/orphan_adapter.php'; |
3 | 5 | |
4 | 6 | //TODO: Something that is not specific to anybody's install, here. |
5 | 7 | global $IP; |
— | — | @@ -8,41 +10,163 @@ |
9 | 11 | require_once( "$IP/maintenance/Maintenance.php" ); |
10 | 12 | |
11 | 13 | class GlobalCollectOrphanRectifier extends Maintenance { |
| 14 | + |
| 15 | + protected $killfiles = array(); |
| 16 | + protected $order_ids = array(); |
| 17 | + protected $max_per_execute = 5; |
| 18 | + |
| 19 | + |
12 | 20 | function execute(){ |
13 | 21 | |
14 | | - $max_per_execute = 50; |
| 22 | + $order_ids = file('orphanlogs/order_ids.txt', FILE_SKIP_EMPTY_LINES); |
| 23 | + foreach ($order_ids as $key=>$val){ |
| 24 | + $order_ids[$key] = trim($val); |
| 25 | + } |
| 26 | + foreach ($order_ids as $id){ |
| 27 | + $this->order_ids[$id] = $id; //easier to unset this way. |
| 28 | + } |
| 29 | + echo "Order ID count: " . count($this->order_ids) . "\n"; |
15 | 30 | |
| 31 | + $files = $this->getAllLogFileNames(); |
| 32 | + foreach ($files as $file){ |
| 33 | + $file_array = file($file, FILE_SKIP_EMPTY_LINES); |
| 34 | + $payments = $this->findTransactionLines($file_array); |
| 35 | + if (count($payments) === 0){ |
| 36 | + $this->killfiles[] = $file; |
| 37 | + } |
| 38 | + } |
16 | 39 | |
| 40 | + $data = array( |
| 41 | + 'wheeee' => 'yes' |
| 42 | +// 'order_id' => '1052864192', |
| 43 | +// 'i_order_id' => '1052864192', |
| 44 | +// 'city' => '', |
| 45 | +// 'state' => '', |
| 46 | +// 'zip' => '', |
| 47 | +// 'country' => 'US', |
| 48 | +// 'email' => '', |
| 49 | +// 'card_num' => '', |
| 50 | + |
| 51 | + ); |
| 52 | + |
| 53 | + $class_name = 'GlobalCollectOrphanAdapter'; |
| 54 | + |
| 55 | + $adapter = new $class_name(array('external_data' => $data)); |
| 56 | + $adapter->setCurrentTransaction('INSERT_ORDERWITHPAYMENT'); |
| 57 | + $var_map = $adapter->defineVarMap(); |
| 58 | + |
| 59 | + $xml = new DomDocument; |
| 60 | + |
| 61 | + foreach ($payments as $key => $payment_data){ |
| 62 | + $xml->loadXML($payment_data['xml']); |
| 63 | + $parsed = $adapter->getResponseData($xml); |
| 64 | + $payments[$key]['parsed'] = $parsed; |
| 65 | + $payments[$key]['unstaged'] = $adapter->unstage_data($parsed); |
| 66 | + $payments[$key]['unstaged']['contribution_tracking_id'] = $payments['contribution_tracking_id']; |
| 67 | + $payments[$key]['unstaged']['i_order_id'] = $payments[$key]['unstaged']['order_id']; |
| 68 | + } |
| 69 | + //setCurrentTransaction |
| 70 | + //then load the XML into a DomDocument, and run getResponseData. |
| 71 | + |
| 72 | + echo print_r($payments, true); |
17 | 73 | |
18 | | - //load in and chunk the list of XML. Phase 1: From a file |
19 | | - //MUST include contribution_tracking id! |
| 74 | + //careful after this bit. |
| 75 | + die(); |
20 | 76 | |
21 | | - //for each chunk, load it into a GC adapter, and have it parse as if it were a response. |
22 | 77 | |
23 | | - //load that response into DonationData, and run the thing that completes the transaction, |
24 | | - //as if we were coming from resultSwitcher. |
25 | | - //(Note: This should only work if we're sitting at one of the designated statuses) |
26 | 78 | |
27 | | - $data = array( |
28 | | - 'order_id' => '1052864192', |
29 | | - 'i_order_id' => '1052864192', |
30 | | - 'city' => '', |
31 | | - 'state' => '', |
32 | | - 'zip' => '', |
33 | | - 'country' => 'US', |
34 | | - 'email' => '', |
35 | | - 'card_num' => '', |
36 | | - |
37 | | - ); |
38 | | - |
39 | 79 | //we may need to unset some hooks out here. Like... recaptcha. Makes no sense. |
40 | 80 | $adapter = new GlobalCollectAdapter(array('external_data' => $data)); |
41 | 81 | error_log("\n\n\n"); |
42 | 82 | $results = $adapter->do_transaction('Confirm_CreditCard'); |
| 83 | + |
| 84 | + |
| 85 | + //$this->rewriteOrderIds(); |
| 86 | + //and don't forget to kill the logs we don't care about anymore. |
43 | 87 | } |
| 88 | + |
| 89 | + function getAllLogFileNames(){ |
| 90 | + $files = array(); |
| 91 | + if ($handle = opendir(dirname(__FILE__) . '/orphanlogs/')){ |
| 92 | + while ( ($file = readdir($handle)) !== false ){ |
| 93 | + if (trim($file, '.') != '' && $file != 'order_ids.txt'){ |
| 94 | + $files[] = dirname(__FILE__) . '/orphanlogs/' . $file; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + closedir($handle); |
| 99 | + return $files; |
| 100 | + } |
| 101 | + |
| 102 | + function findTransactionLines($file){ |
| 103 | + $lines = array(); |
| 104 | + $orders = array(); |
| 105 | + $contrib_id_finder = array(); |
| 106 | + foreach ($file as $line_no=>$line_data){ |
| 107 | + if (strpos($line_data, '<XML><REQUEST><ACTION>INSERT_ORDERWITHPAYMENT') === 0){ |
| 108 | + $lines[$line_no] = $line_data; |
| 109 | + } elseif (strpos($line_data, 'Raw XML Response')){ |
| 110 | + $contrib_id_finder[] = $line_data; |
| 111 | + } elseif (strpos(trim($line_data), '<ORDERID>') === 0){ |
| 112 | + $contrib_id_finder[] = trim($line_data); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + foreach ($lines as $line_no=>$line_data){ |
| 117 | + if (count($orders) >= $this->max_per_execute){ |
| 118 | + continue; |
| 119 | + } |
| 120 | + $pos1 = strpos($line_data, '<ORDERID>') + 9; |
| 121 | + $pos2 = strpos($line_data, '</ORDERID>'); |
| 122 | + if ($pos2 > $pos1){ |
| 123 | + $tmp = substr($line_data, $pos1, $pos2-$pos1); |
| 124 | + echo "$tmp\n"; |
| 125 | + if (isset($this->order_ids[$tmp])){ |
| 126 | + $orders[$tmp] = trim($line_data); |
| 127 | + unset($this->order_ids[$tmp]); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + //reverse the array, so we find the last instance first. |
| 133 | + $contrib_id_finder = array_reverse($contrib_id_finder); |
| 134 | + foreach ($orders as $order_id => $xml){ |
| 135 | + $contribution_tracking_id = ''; |
| 136 | + $finder = array_search("<ORDERID>$order_id</ORDERID>", $contrib_id_finder); |
| 137 | + |
| 138 | + //now search forward (which is actually backward) to the "Raw XML" line, so we can get the contribution_tracking_id |
| 139 | + //TODO: Some kind of (in)sanity check for this. Just because we've found it one step backward doesn't mean... |
| 140 | + //...but it's kind of good. For now. |
| 141 | + $explode_me = false; |
| 142 | + while (!$explode_me){ |
| 143 | + ++$finder; |
| 144 | + if (strpos($contrib_id_finder[$finder], "Raw XML Response")){ |
| 145 | + $explode_me = $contrib_id_finder[$finder]; |
| 146 | + } |
| 147 | + } |
| 148 | + if (strlen($explode_me)){ |
| 149 | + $explode_me = explode(': ', $explode_me); |
| 150 | + $contribution_tracking_id = trim($explode_me[1]); |
| 151 | + $orders[$order_id] = array( |
| 152 | + 'xml' => $xml, |
| 153 | + 'contribution_tracking_id' => $contribution_tracking_id, |
| 154 | + ); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return $orders; |
| 159 | + } |
| 160 | + |
| 161 | + function rewriteOrderIds() { |
| 162 | + $file = fopen('orphanlogs/order_ids.txt', 'w'); |
| 163 | + $outstanding_orders = implode("\n", $this->order_ids); |
| 164 | + fwrite($file, $outstanding_orders); |
| 165 | + fclose($file); |
| 166 | + } |
| 167 | + |
44 | 168 | } |
45 | 169 | |
| 170 | +$maintClass = "GlobalCollectOrphanRectifier"; |
| 171 | +require_once( "$IP/maintenance/doMaintenance.php" ); |
46 | 172 | |
47 | 173 | |
48 | | -$maintClass = "GlobalCollectOrphanRectifier"; |
49 | | -require_once( "$IP/maintenance/doMaintenance.php" ); |
Property changes on: trunk/extensions/DonationInterface/globalcollect_gateway/scripts/orphanlogs |
___________________________________________________________________ |
Added: svn:ignore |
50 | 174 | + order_ids.txt |
2011-11-11:17:59:48-globalcollect |
Index: trunk/extensions/DonationInterface/globalcollect_gateway/scripts/orphan_adapter.php |
— | — | @@ -0,0 +1,28 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class GlobalCollectOrphanAdapter extends GlobalCollectAdapter { |
| 5 | + public function unstage_data( $data = array(), $final = true ){ |
| 6 | + $unstaged = array(); |
| 7 | + foreach ( $data as $key=>$val ){ |
| 8 | + if (is_array($val)){ |
| 9 | + $unstaged += $this->unstage_data( $val, false ); |
| 10 | + } else { |
| 11 | + if (array_key_exists($key, $this->var_map)){ |
| 12 | + //run the unstage data functions. |
| 13 | + $unstaged[$this->var_map[$key]] = $val; |
| 14 | + //this would be EXTREMELY bad to put in the regular adapter. |
| 15 | + $this->staged_data[$this->var_map[$key]] = $val; |
| 16 | + } else { |
| 17 | + //$unstaged[$key] = $val; |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + if ($final){ |
| 22 | + $this->stageData('response'); |
| 23 | + } |
| 24 | + foreach ($unstaged as $key=>$val){ |
| 25 | + $unstaged[$key] = $this->staged_data[$key]; |
| 26 | + } |
| 27 | + return $unstaged; |
| 28 | + } |
| 29 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/DonationInterface/globalcollect_gateway/scripts/orphan_adapter.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 30 | + native |
Index: trunk/extensions/DonationInterface/globalcollect_gateway/globalcollect.adapter.php |
— | — | @@ -1049,6 +1049,7 @@ |
1050 | 1050 | |
1051 | 1051 | if ( count( $addme ) ){ |
1052 | 1052 | $this->addData( $addme ); |
| 1053 | + $this->staged_data['order_id'] = $this->staged_data['i_order_id']; |
1053 | 1054 | $logmsg = $this->getData_Raw( 'contribution_tracking_id' ) . ': '; |
1054 | 1055 | $logmsg .= 'CVV Result: ' . $this->getData_Raw( 'cvv_result' ); |
1055 | 1056 | $logmsg .= ', AVS Result: ' . $this->getData_Raw( 'avs_result' ); |
— | — | @@ -1596,23 +1597,34 @@ |
1597 | 1598 | protected function stage_language( $type = 'request' ) { |
1598 | 1599 | $language = strtolower( $this->staged_data['language'] ); |
1599 | 1600 | |
1600 | | - $count = 0; |
1601 | | - //Count's just there making sure we don't get stuck here. |
1602 | | - while ( !in_array( $language, $this->getAvailableLanguages() ) && $count < 3 ){ |
1603 | | - // Get the fallback language |
1604 | | - $language = Language::getFallbackFor( $language ); |
1605 | | - $count += 1; |
| 1601 | + switch ( $type ) { |
| 1602 | + case 'request': |
| 1603 | + $count = 0; |
| 1604 | + //Count's just there making sure we don't get stuck here. |
| 1605 | + while ( !in_array( $language, $this->getAvailableLanguages() ) && $count < 3 ){ |
| 1606 | + // Get the fallback language |
| 1607 | + $language = Language::getFallbackFor( $language ); |
| 1608 | + $count += 1; |
| 1609 | + } |
| 1610 | + |
| 1611 | + if ( !in_array( $language, $this->getAvailableLanguages() ) ){ |
| 1612 | + $language = 'en'; |
| 1613 | + } |
| 1614 | + |
| 1615 | + if ( $language === 'zh' ) { //Handles GC's mutant Chinese code. |
| 1616 | + $language = 'sc'; |
| 1617 | + } |
| 1618 | + |
| 1619 | + break; |
| 1620 | + case 'response': |
| 1621 | + if ( $language === 'sc' ){ |
| 1622 | + $language = 'zh'; |
| 1623 | + } |
| 1624 | + break; |
1606 | 1625 | } |
1607 | 1626 | |
1608 | | - if ( !in_array( $language, $this->getAvailableLanguages() ) ){ |
1609 | | - $language = 'en'; |
1610 | | - } |
1611 | | - |
1612 | | - if ( $language === 'zh' ) { //Handles GC's mutant Chinese code. |
1613 | | - $language = 'sc'; |
1614 | | - } |
1615 | | - |
1616 | 1627 | $this->staged_data['language'] = $language; |
| 1628 | + |
1617 | 1629 | } |
1618 | 1630 | |
1619 | 1631 | /** |
Index: trunk/extensions/DonationInterface/gateway_common/gateway.adapter.php |
— | — | @@ -1530,7 +1530,9 @@ |
1531 | 1531 | } |
1532 | 1532 | |
1533 | 1533 | // Format the staged data |
1534 | | - $this->formatStagedData(); |
| 1534 | + if ($type === 'request'){ |
| 1535 | + $this->formatStagedData(); |
| 1536 | + } |
1535 | 1537 | |
1536 | 1538 | } |
1537 | 1539 | |
— | — | @@ -1667,7 +1669,7 @@ |
1668 | 1670 | * @return mixed Transaction results status, or false if not set. |
1669 | 1671 | */ |
1670 | 1672 | public function getTransactionStatus() { |
1671 | | - if ( array_key_exists( 'status', $this->transaction_results ) ) { |
| 1673 | + if ( is_array( $this->transaction_results ) && array_key_exists( 'status', $this->transaction_results ) ) { |
1672 | 1674 | return $this->transaction_results['status']; |
1673 | 1675 | } else { |
1674 | 1676 | return false; |