1 |
358 |
ahitrov |
package payments::Provider::Base; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings 'all'; |
5 |
|
|
use Contenido::Globals; |
6 |
|
|
use payments::Keeper; |
7 |
|
|
|
8 |
|
|
|
9 |
|
|
sub new { |
10 |
|
|
my ($proto, %params) = @_; |
11 |
|
|
my $class = ref($proto) || $proto; |
12 |
|
|
my $self = {}; |
13 |
|
|
my $prefix = $class =~ /\:\:(\w+)$/ ? lc($1) : undef; |
14 |
|
|
return unless $prefix; |
15 |
|
|
|
16 |
|
|
$self->{payment_system} = $prefix; |
17 |
|
|
$self->{app_id} = $state->{payments}->{$prefix."_app_id"}; |
18 |
|
|
$self->{secret} = $state->{payments}->{$prefix."_app_secret"}; |
19 |
|
|
$self->{currency} = $state->{payments}->{$prefix."_currency_code"}; |
20 |
|
|
$self->{test_mode} = $state->{payments}->{$prefix."_test_mode"}; |
21 |
|
|
|
22 |
|
|
bless $self, $class; |
23 |
|
|
|
24 |
|
|
return $self; |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
|
28 |
|
|
sub id { |
29 |
|
|
my $self = shift; |
30 |
|
|
return $self->{app_id}; |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
sub app_id { |
34 |
|
|
my $self = shift; |
35 |
|
|
return $self->{app_id}; |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
sub secret { |
39 |
|
|
my $self = shift; |
40 |
|
|
return $self->{secret}; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
sub test_mode { |
44 |
|
|
my $self = shift; |
45 |
|
|
return $self->{test_mode}; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
sub currency { |
49 |
|
|
my $self = shift; |
50 |
|
|
return $self->{currency}; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
sub currency_code { |
54 |
|
|
my $self = shift; |
55 |
|
|
return $self->{currency}; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
sub payment_system { |
59 |
|
|
my $self = shift; |
60 |
|
|
return $self->{payment_system}; |
61 |
|
|
} |
62 |
|
|
|
63 |
690 |
ahitrov |
################################# |
64 |
|
|
# Пытается зарегистрировать операцию по order_id. |
65 |
|
|
# В случае успеха возвращает объект payments::Operation |
66 |
|
|
# В случае неуспеха выставляет ошибку и возвращает undef. |
67 |
|
|
# Сумма заказа в копейках |
68 |
|
|
########################################################## |
69 |
|
|
sub payment_operation_register { |
70 |
|
|
my $self = shift; |
71 |
|
|
my $opts = shift // {}; |
72 |
|
|
unless ( $opts->{order_id} && $opts->{uid} && $opts->{sum} && $opts->{name} ) { |
73 |
|
|
$self->{result}{error} = 'Переданы не все обязательные параметры'; |
74 |
|
|
return undef; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
my $operation = $keeper->get_documents( |
78 |
|
|
class => 'payments::Operation', |
79 |
|
|
status => $self->{test_mode}, |
80 |
|
|
order_id => $opts->{order_id}, |
81 |
|
|
order_by => 'ctime', |
82 |
|
|
return_mode => 'array_ref', |
83 |
|
|
); |
84 |
|
|
my $new = 0; |
85 |
|
|
if ( ref $operation eq 'ARRAY' && @$operation ) { |
86 |
|
|
my $last = $operation->[-1]; |
87 |
|
|
if ( $opts->{name} eq 'create' && ($last->name eq 'suspend' || $last->name eq 'cancel' || $last->name eq 'close') ) { |
88 |
|
|
$self->{result}{error} = 'Заказ закрыт, отменен или заморожен. Оплата по нему невозможна'; |
89 |
|
|
return undef; |
90 |
|
|
} elsif ( $opts->{name} eq 'refund' && ($last->name eq 'suspend' || $last->name eq 'close') ) { |
91 |
|
|
$self->{result}{error} = 'Заказ закрыт или заморожен. Возврат средств по нему невозможен'; |
92 |
|
|
return undef; |
93 |
|
|
} elsif ( $last->name eq $opts->{name} ) { |
94 |
|
|
$operation = $last; |
95 |
|
|
} else { |
96 |
|
|
if ( $opts->{name} eq 'refund' && (grep { $_->name eq 'create' } @$operation) ) { |
97 |
|
|
$new = 1; |
98 |
|
|
} |
99 |
|
|
} |
100 |
|
|
} elsif ( $opts->{name} eq 'create' ) { |
101 |
|
|
$new = 1; |
102 |
710 |
ahitrov |
} else { |
103 |
|
|
$self->{result}{error} = 'Попытка создать операцию ['.$opts->{name}.'] без регистрации типа [create]'; |
104 |
|
|
return undef; |
105 |
690 |
ahitrov |
} |
106 |
|
|
if ( $new ) { |
107 |
|
|
$operation = payments::Operation->new( $keeper ); |
108 |
|
|
$operation->status( $self->{test_mode} ); |
109 |
|
|
$operation->name( $opts->{name} ); |
110 |
|
|
$operation->order_id( $opts->{order_id} ); |
111 |
|
|
$operation->uid( $opts->{uid} ); |
112 |
|
|
$operation->sum( $opts->{sum} ); |
113 |
|
|
$operation->store; |
114 |
|
|
} |
115 |
|
|
return $operation; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
sub get_transaction_by_order_id { |
119 |
|
|
my $self = shift; |
120 |
|
|
my $order_id = shift; |
121 |
|
|
|
122 |
|
|
my ($transaction) = $keeper->get_documents( |
123 |
|
|
class => 'payments::Transaction', |
124 |
|
|
status => $self->{test_mode}, |
125 |
|
|
limit => 1, |
126 |
|
|
order_id => $order_id, |
127 |
|
|
order_by => 'ctime desc', |
128 |
|
|
provider => $self->{payment_system}, |
129 |
|
|
); |
130 |
|
|
|
131 |
|
|
return $transaction; |
132 |
|
|
} |
133 |
|
|
|
134 |
358 |
ahitrov |
1; |