1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
| public function SendMoney( $iQiwiAccount, $dAmount, $sCurrency, $sComment ) {
return $this->payment( null, array( 'account' => '+'.$iQiwiAccount, 'comment' => $sComment ), $dAmount, $sCurrency );
}
# Проверка отчета, если обнаружен идет выполнение скрипт. Отчет с переменной заданной в index.php у нас "%"
public function GetHistory( $sStartDate, $sFinishDate ) {
$sResult = $this->curl( 'user/report/list.action?daterange=true&start='.$sStartDate.'&finish='.$sFinishDate );
$aTransactions = array();
foreach( explode( '</div><div class="reportsLine ', str_replace( '> <', '><', preg_replace( '!\s+!u', ' ', $sResult ) ) ) as $iKey => $sValue ) {
if( $iKey == 0 )
continue;
# Проверка даты и времени платежа
$aData = array();
$aData['iID'] = explode( '<span class="value">', $sValue );
if( count( $aData['iID'] ) < 2 )
continue;
$aData['iID'] = explode( '</', $aData['iID'][1] );
$aData['iID'] = trim( $aData['iID'][0] );
$aData['sDate'] = explode( 'class="date">', $sValue );
$aData['sDate'] = explode( '</', $aData['sDate'][1] );
$aData['sDate'] = trim( $aData['sDate'][0] );
$aData['sTime'] = explode( 'class="time">', $sValue );
$aData['sTime'] = explode( '</', $aData['sTime'][1] );
$aData['sTime'] = trim( $aData['sTime'][0] );
$aData['sAmount'] = explode( 'class="originalExpense"><span>', $sValue );
$aData['sAmount'] = explode( '</', $aData['sAmount'][1] );
$aData['sAmount'] = trim( $aData['sAmount'][0] );
$aData['dAmount'] = preg_replace( '/[^0-9\.]+/', '', str_replace( ',', '.', $aData['sAmount'] ) ) - 0;
$aData['sCurrency'] = mb_strpos( $aData['sAmount'], 'руб.' ) !== false ? 'RUB' : (mb_strpos( $aData['sAmount'], 'долл.' ) !== false ? 'USD' : (mb_strpos( $aData['sAmount'], 'тенге.' ) !== false ? 'KZT' : 'NAN'));
$aData['sWithExpend'] = explode( 'WithExpend', $sValue );
$aData['sWithExpend'] = explode( '</div>', $aData['sWithExpend'][1] );
$aData['sWithExpend'] = explode( '<div class="cash">', $aData['sWithExpend'][0] );
$aData['sWithExpend'] = trim( $aData['sWithExpend'][1] );
$aData['dWithExpend'] = preg_replace( '/[^0-9\.]+/', '', str_replace( ',', '.', $aData['sWithExpend'] ) ) - 0;
$aData['iOpponentPhone'] = explode( 'class="opNumber">', $sValue );
$aData['iOpponentPhone'] = explode( '</', $aData['iOpponentPhone'][1] );
$aData['iOpponentPhone'] = trim( str_replace( '+', '', $aData['iOpponentPhone'][0] ) );
$aData['sComment'] = explode( 'class="comment">', $sValue );
$aData['sComment'] = explode( '</', $aData['sComment'][1] );
$aData['sComment'] = html_entity_decode( trim( $aData['sComment'][0] ), ENT_QUOTES, 'UTF-8' );
$aData['sProvider'] = explode( '<div class="provider"><span>', $sValue );
$aData['sProvider'] = explode( '</span>', $aData['sProvider'][1] );
$aData['sProvider'] = trim( $aData['sProvider'][0] );
$aData['sType'] = mb_strpos( $sValue, 'IncomeWithExpend expenditure' ) !== false ? 'EXPENDITURE' : (mb_strpos( $sValue, 'IncomeWithExpend income' ) !== false ? 'INCOME' : 'NAN');
$aData['sStatus'] = explode( '"', $sValue );
$aData['sStatus'] = trim( $aData['sStatus'][0] );
if( $aData['sStatus'] == 'ERROR' ) {
$aData['sError'] = explode( '{"message":"', $sValue );
$aData['sError'] = explode( '"', $aData['sError'][1] );
$aData['sError'] = trim( $aData['sError'][0] );
}
if( $aData['iID'] == false ) {
$aData['iID'] = explode( '{"txn":', $sValue );
$aData['iID'] = explode( '}', $aData['iID'][1] );
$aData['iID'] = $aData['iID'][0];
}
$aTransactions['ID-'.$aData['iID']] = $aData;
}
return $aTransactions;
}
public function payment( $iProvider, array $aExtra, $dAmount, $sCurrency = 'RUB', $sPayCurrency = 'RUB' ) {
$dAmount = intval( str_replace( ',', '.', $dAmount ) * 100 ) / 100;
$aAmount = explode( '.', $dAmount );
if( !isset( $aAmount[1] ) )
$aAmount[1] = '00';
else if( strlen( $aAmount[1] ) != 2 )
$aAmount[1] .= '0';
foreach( $aExtra as $sKey => $sValue ) {
$aExtra["extra['".$sKey."']"] = $sValue;
unset( $aExtra[$sKey] );
}
# Ничинаем перевод, переход на кнопку "Перевести"
$this->curl( is_null( $iProvider ) ? 'transfer/form.action' : 'transfer.action?provider='.$iProvider );
# Вводим номер указанный и сумму
if( ($aResponse = @json_decode( $this->curl(
'payment/state.action?'.http_build_query(
array_merge( array(
'amountInteger' => $aAmount[0],
'amountFraction' => $aAmount[1],
'arg_num' => '',
'currency' => $sCurrency,
'protected' => 'true',
'source' => 'qiwi_'.$sPayCurrency,
'state' => 'CONFIRM'
), $aExtra )
)
), true )) === false )
throw new Exception( 'internal error, step 1' );
if( !isset( $aResponse['data'] ) || !isset( $aResponse['data']['token'] ) )
throw new Exception( isset( $aResponse['message'] ) ? $aResponse['message'] : 'internal error, step 2' );
$sResponse = $this->curl(
'payment/state.action'.http_build_query(
array_merge( array(
'amountInteger' => $aAmount[0],
'amountFraction' => $aAmount[1],
'arg_num' => '',
'currency' => $sCurrency,
'protected' => 'true',
'source' => 'qiwi_'.$sPayCurrency,
'state' => 'CONFIRM',
'token' => $aResponse['data']['token'],
), $aExtra )
)
);
if( ($aResponse = @json_decode( $this->curl( 'payment/state.action?state=PAY' ), true )) === false )
throw new Exception( 'internal error, step 3' );
if( !isset( $aResponse['data'] ) || !isset( $aResponse['data']['token'] ) )
throw new Exception( isset( $aResponse['message'] ) ? $aResponse['message'] : 'internal error, step 4' );
$sResponse = $this->curl(
'payment/state.action?state=CONFIRM&protected=true',
array(
'token' => $aResponse['data']['token'],
'state' => 'PAY'
)
);
if( mb_strpos( $sResponse, 'transaction":"' ) === false ) {
if( mb_strpos( $sResponse, 'class="errorElement"' ) !== false ) {
$aExplode = explode( 'class="errorElement">', $sResponse );
$aExplode = explode( '</', $aExplode[1] );
throw new Exception( trim( $aExplode[0] ) );
}
else {
if( count( $sMessage = explode( '<p>', $sResponse ) ) < 2 )
throw new Exception( 'unknown error' );
$sMessage = explode( '</p>', $sMessage[1] );
throw new Exception( $sMessage[0] );
}
}
if( $iProvider == 22496 ) {
if( count( $aExplode = explode( 'Код ваучера:', $sResponse ) ) < 2 )
throw new Exception( 'error parse egg' );
$aExplode = explode( '</', $aExplode[1] );
return trim( $aExplode[0] );
}
$aHistory = $this->GetHistory( date( 'd.m.Y', strtotime( '-1 day' ) ), date( 'd.m.Y', strtotime( '+1 day' ) ) );
$aTransfer = array_shift( $aHistory );
if( $aTransfer === false || $aTransfer['dAmount'] != $dAmount || $aTransfer['sCurrency'] != $sCurrency )
throw new Exception( 'transfer not found in history' );
return $aTransfer['iID'];
}
public function curl( $sPath, $mPOST = null, array $aOptions = null ) {
$oCURL = curl_init( 'https://w.qiwi.com/'.$sPath );
curl_setopt_array( $oCURL, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_REFERER => "https://w.qiwi.com/payment/main.action",
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0',
CURLOPT_HTTPHEADER => array(
'Accept: application/json, text/javascript, */*; q=0.01',
'X-Requested-With: XMLHttpRequest',
'Content-Type' => 'application/x-www-form-urlencoded; charset=windows-1251'
),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_COOKIEJAR => $cookie_file,
CURLOPT_COOKIEFILE => $cookie_file
) );
if( !is_null( $mPOST ) )
curl_setopt_array( $oCURL, array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => is_array( $mPOST ) ? http_build_query( $mPOST ) : $mPOST,
) );
if( is_array( $aOptions ) && count( $aOptions ) )
curl_setopt_array( $oCURL, $aOptions );
$sResponse = curl_exec( $oCURL );
if( $sResponse === false || curl_errno( $oCURL ) != 0 )
throw new Exception( curl_error( $oCURL ), curl_errno( $oCURL ) );
curl_close( $oCURL );
return $sResponse;
}
}
?> |