Revision 696
- Date:
- 2018/08/14 20:52:12
- Files:
Legend:
- Added
- Removed
- Modified
-
utf8/plugins/payments/lib/payments/Provider/Sber.pm
11 11 use JSON::XS; 12 12 use Data::Dumper; 13 13 14 use constant ( 15 REG => 0, # Заказ зарегистрирован, но не оплачен; 16 HOLD => 1, # Предавторизованная сумма захолдирована (для двухстадийных платежей); 17 PAY => 2, # Проведена полная авторизация суммы заказа; 18 CANCEL => 3, # Авторизация отменена; 19 REFUND => 4, # По транзакции была проведена операция возврата; 20 ACS => 5, # Инициирована авторизация через ACS банка-эмитента; 21 REJECT => 6, # Авторизация отклонена. 22 ); 23 24 our %STATUS = ( 25 0 => 'Ожидание оплаты', 26 1 => 'Предавторизованная сумма захолдирована', 27 2 => 'Успешная оплата', 28 3 => 'Авторизация отменена', 29 4 => 'По транзакции была проведена операция возврата', 30 5 => 'Инициирована авторизация через ACS банка-эмитента', 31 6 => 'Авторизация отклонена', 32 ); 33 14 34 sub new { 15 35 my ($proto, %params) = @_; 16 36 my $class = ref($proto) || $proto; … … 28 48 $self->{fail_url} = $params{fail_url} || $state->{payments}{$prefix."_fail_url"}; 29 49 30 50 $self->{currency} = $state->{payments}{$prefix."_currency_code"}; 51 $self->{payment_statuses} = \%STATUS; 31 52 32 53 my $host = 'https://'. ($self->{test_mode} ? '3dsec.sberbank.ru' : 'securepayments.sberbank.ru'); 33 54 $self->{api} = { … … 78 99 my $self = shift; 79 100 my $opts = shift // {}; 80 101 81 unless ( %$opts && exists $opts->{orderNumber} && exists $opts->{amount} ) { 82 $self->{result}{error} = 'Не указаны обязательные параметры: orderNumber или amount'; 102 unless ( %$opts && (exists $opts->{order} || exists $opts->{orderNumber} && exists $opts->{amount}) ) { 103 $self->{result}{error} = 'Не указаны обязательные параметры: order, orderNumber или amount'; 83 104 return $self; 84 105 } 85 106 my $method = 'init'; … … 98 119 $opts->{sessionTimeoutSecs} = $self->{session_timeout}; 99 120 } 100 121 122 my $order; 123 if ( exists $opts->{order} && ref $opts->{order} eq 'webshop::Order' ) { 124 $order = delete $opts->{order}; 125 $opts->{orderNumber} = $order->id; 126 } 127 101 128 my $uid = delete $opts->{uid}; 102 129 unless ( $uid ) { 103 $self->{result}{error} = 'Не указан user id'; 104 return $self; 130 if ( ref $order ) { 131 $uid = $order->uid; 132 } else { 133 $self->{result}{error} = 'Не указан user id'; 134 return $self; 135 } 105 136 } 106 137 107 138 ### Сумма должна быть в копейках. Если дробное (рубли.копейки) - преобразуем в копейки 108 my $sum = $opts->{amount}; 109 if ( !$sum || $sum !~ /^[\d\,\.]+$/ ) { 110 $self->{result}{error} = 'Не указана или неправильно указана сумма транзакции'; 111 return $self; 139 if ( ref $order ) { 140 $opts->{amount} = int($order->sum_total * 100); 141 } else { 142 my $sum = $opts->{amount}; 143 if ( !$sum || $sum !~ /^[\d\,\.]+$/ ) { 144 $self->{result}{error} = 'Не указана или неправильно указана сумма транзакции'; 145 return $self; 146 } 147 if ( $sum =~ /[,.]/ ) { 148 $sum =~ s/\,/\./; 149 $opts->{amount} = int($sum * 100); 150 } 112 151 } 113 if ( $sum =~ /[,.]/ ) { 114 $sum =~ s/\,/\./; 115 $opts->{amount} = int($sum * 100); 116 } 117 152 $opts->{jsonParams} = {} unless exists $opts->{jsonParams}; 118 153 $opts->{jsonParams}{uid} = $uid; 119 154 … … 127 162 return $self unless ref $operation; 128 163 129 164 my $transaction = $self->get_transaction_by_order_id( $opts->{orderNumber} ); 130 if ( ref $transaction && $transaction->name eq 'Init' ) { 165 if ( ref $transaction && $transaction->name ne 'Expired' ) { 131 166 ### Transaction already exists 132 167 $self->{result}{success} = 1; 133 168 $self->{result}{session_id} = $transaction->session_id; … … 135 170 } else { 136 171 if ( ref $transaction && $transaction->name eq 'Expired' ) { 137 172 $transaction->delete; 173 $transaction = undef; 138 174 } 139 my $req = $self->_createRequestGet( $method, $opts ); 140 my $ua = LWP::UserAgent->new; 141 $ua->agent('Mozilla/5.0'); 142 my $result = $ua->get( $req ); 143 if ( $result->code == 200 ) { 144 warn "Sberbank Init result: [".$result->decoded_content."]\n" if $DEBUG; 145 my $content = JSON::XS->new->decode( $result->decoded_content ); 146 warn Dumper $content if $DEBUG; 175 if ( !ref $transaction ) { 176 my $req = $self->_createRequestGet( $method, $opts ); 177 my $ua = LWP::UserAgent->new; 178 $ua->agent('Mozilla/5.0'); 179 my $result = $ua->get( $req ); 180 if ( $result->code == 200 ) { 181 warn "Sberbank Init result: [".$result->decoded_content."]\n" if $DEBUG; 182 my $content = JSON::XS->new->decode( $result->decoded_content ); 183 warn Dumper $content if $DEBUG; 147 184 148 if ( ref $content && exists $content->{orderId} ) { 149 my $now = Contenido::DateTime->new; 150 $transaction = payments::Transaction->new( $keeper ); 151 $transaction->dtime( $now->ymd('-').' '.$now->hms ); 152 $transaction->provider( $self->{payment_system} ); 153 $transaction->session_id( $content->{orderId} ); 154 $transaction->status( $self->{test_mode} ); 155 $transaction->order_id( $opts->{orderNumber} ); 156 $transaction->operation_id( $operation->id ); 157 $transaction->currency_code( 'RUR' ); 158 $transaction->sum( $opts->{amount} ); 159 $transaction->form_url( $content->{formUrl} ); 160 $transaction->name( 'Init' ); 161 $transaction->success( 0 ); 162 $transaction->store; 185 if ( ref $content && exists $content->{orderId} ) { 186 my $now = Contenido::DateTime->new; 187 $transaction = payments::Transaction->new( $keeper ); 188 $transaction->dtime( $now->ymd('-').' '.$now->hms ); 189 $transaction->provider( $self->{payment_system} ); 190 $transaction->session_id( $content->{orderId} ); 191 $transaction->status( $self->{test_mode} ); 192 $transaction->order_id( $opts->{orderNumber} ); 193 $transaction->operation_id( $operation->id ); 194 $transaction->currency_code( 'RUR' ); 195 $transaction->sum( $opts->{amount} ); 196 $transaction->form_url( $content->{formUrl} ); 197 $transaction->name( 'Init' ); 198 $transaction->success( 0 ); 199 $transaction->store; 163 200 164 $self->{result}{success} = 1; 165 $self->{result}{session_id} = $content->{orderId}; 166 $self->{result}{transaction} = $transaction; 167 } elsif ( ref $content && exists $content->{errorCode} && $content->{errorCode} ) { 168 $self->{result}{error} = Encode::encode('utf-8', $content->{errorMessage}); 169 warn "[".$result->decoded_content."]\n"; 201 $self->{result}{success} = 1; 202 $self->{result}{session_id} = $content->{orderId}; 203 $self->{result}{transaction} = $transaction; 204 } elsif ( ref $content && exists $content->{errorCode} && $content->{errorCode} ) { 205 $self->{result}{error} = Encode::encode('utf-8', $content->{errorMessage}); 206 warn "[".$result->decoded_content."]\n"; 207 } else { 208 $self->{result}{error} = 'Sberbank Init failed'; 209 $self->{result}{responce} = $result->decoded_content; 210 warn $self->{result}{error}."\n"; 211 warn "[".$result->decoded_content."]\n"; 212 } 170 213 } else { 171 214 $self->{result}{error} = 'Sberbank Init failed'; 172 $self->{result}{responce} = $result->decoded_content; 173 warn $self->{result}{error}."\n"; 174 warn "[".$result->decoded_content."]\n"; 215 $self->{result}{responce} = $result->status_line; 216 warn $self->{result}{error}.": ".$result->status_line."\n"; 217 warn Dumper $result; 175 218 } 176 } else { 177 $self->{result}{error} = 'Sberbank Init failed'; 178 $self->{result}{responce} = $result->status_line; 179 warn $self->{result}{error}.": ".$result->status_line."\n"; 180 warn Dumper $result; 181 219 } 182 220 } 183 221 return $self; … … 224 262 my $self = shift; 225 263 my $opts = shift // {}; 226 264 227 unless ( exists $opts->{orderNumber} || exists $self->{result} && exists $self->{result}{transaction} && ref $self->{result}{transaction} ) { 265 unless ( exists $opts->{orderNumber} 266 || exists $self->{result} && exists $self->{result}{transaction} && ref $self->{result}{transaction} 267 || exists $opts->{transaction} && ref $opts->{transaction} ) { 228 268 $self->{result}{error} = 'Не указан обязательный параметр orderNumber или не получена транзакция'; 229 269 return $self; 230 270 } 231 271 my $method = 'status'; 232 272 my $transaction; 233 if ( exists $self->{result}{transaction} ) { 273 if ( exists $opts->{transaction} ) { 274 $transaction = delete $opts->{transaction}; 275 } elsif ( exists $self->{result}{transaction} ) { 234 276 $transaction = $self->{result}{transaction}; 235 $opts->{orderNumber} = $transaction->order_id; 236 277 } else { 237 278 $transaction = $self->get_transaction_by_order_id( $opts->{orderNumber} ); 238 279 } … … 240 281 $self->{result}{error} = "Не найдена транзакция для order_id=".$opts->{orderNumber}; 241 282 return $self; 242 283 } 284 $opts->{orderNumber} = $transaction->order_id; 243 285 $opts->{orderId} = $transaction->session_id; 244 warn "Sberbank Status: ".Dumper($opts) if $DEBUG; 286 warn "Sberbank status opts: ".Dumper($opts) if $DEBUG; 245 287 246 288 my $req = $self->_createRequestGet( $method, $opts ); 247 289 my $ua = LWP::UserAgent->new;