summaryrefslogtreecommitdiff
path: root/FS/FS/ClientAPI/SGNG.pm
blob: 7f784dcd042e800eb04edb8bcf4ff020b07b9b9b (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#this stuff is SG-specific (i.e. multi-customer company username hack)

package FS::ClientAPI::SGNG;

use strict;
use vars qw( $cache $DEBUG );
use Time::Local qw(timelocal timelocal_nocheck);
use Business::CreditCard;
use FS::Record qw( qsearch qsearchs );
use FS::Conf;
use FS::cust_main;
use FS::cust_pkg;
use FS::ClientAPI::MyAccount; #qw( payment_info process_payment )

$DEBUG = 0;

sub _cache {
  $cache ||= new FS::ClientAPI_SessionCache( {
               'namespace' => 'FS::ClientAPI::MyAccount', #yes, share session_ids
             } );
}

sub ping {
  #my $p = shift;

  return { 'pong' => '1' };

}

#this might almost be general-purpose
sub decompify_pkgs {
  my $p = shift;

  my $session = _cache->get($p->{'session_id'})
    or return { 'error' => "Can't resume session" }; #better error message

  my $custnum = $session->{'custnum'};

  my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
    or return { 'error' => "unknown custnum $custnum" };

  return { 'error' => 'Not a complimentary customer' }
    unless $cust_main->payby eq 'COMP';

  my $paydate =
    $cust_main->paydate =~ /^\S+$/ ? $cust_main->paydate : '2037-12-31';

  my ($payyear,$paymonth,$payday) = split (/-/,$paydate);

  my $date = timelocal(0,0,0,$payday,--$paymonth,$payyear);

  foreach my $cust_pkg (
    qsearch({ 'table'     => 'cust_pkg',
              'hashref'   => { 'custnum' => $custnum,
                               'bill'    => '',
                             },
              'extra_sql' => ' AND '. FS::cust_pkg->active_sql,
           })
  ) {
    $cust_pkg->set('bill', $date);
    my $error = $cust_pkg->replace;
    return { 'error' => $error } if $error;
  }

  return { 'error' => '' };

}

#find old payment info
# (should work just like MyAccount::payment_info, except returns previous info
#  too)
# definitly sg-specific, no one else stores past customer records like this
sub previous_payment_info {
  my $p = shift;

  my $session = _cache->get($p->{'session_id'})
    or return { 'error' => "Can't resume session" }; #better error message

  my $payment_info = FS::ClientAPI::MyAccount::payment_info($p);

  my $custnum = $session->{'custnum'};

  my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
    or return { 'error' => "unknown custnum $custnum" };

  #?
  return $payment_info if $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/;

  foreach my $prev_cust_main (
    reverse _previous_cust_main( 'custnum'       => $custnum, 
                                 'username'      => $cust_main->company,
                                 'with_payments' => 1,
                               )
  ) {

    next unless $prev_cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/;

    if ( $prev_cust_main->payby =~ /^(CARD|DCRD)$/ ) {

      #card expired?
      my ($payyear,$paymonth,$payday) = split (/-/, $cust_main->paydate);

      my $expdate = timelocal_nocheck(0,0,0,1,$paymonth,$payyear);

      next if $expdate < time;

    } elsif ( $prev_cust_main->payby =~ /^(CHEK|DCHK)$/ ) {

      #any check?  or just skip these in favor of cards?

    }

    return { %$payment_info,
             #$prev_cust_main->payment_info
             _cust_main_payment_info( $prev_cust_main ),
             'previous_custnum' => $prev_cust_main->custnum,
           };

  }

  #still nothing?  return an error?
  return $payment_info;

}

#this is really FS::cust_main::payment_info, but here for now
sub _cust_main_payment_info {
  my $self = shift;

  my %return = ();

  $return{balance} = $self->balance;

  $return{payname} = $self->payname
                     || ( $self->first. ' '. $self->get('last') );

  $return{$_} = $self->get($_) for qw(address1 address2 city state zip);

  $return{payby} = $self->payby;
  $return{stateid_state} = $self->stateid_state;

  if ( $self->payby =~ /^(CARD|DCRD)$/ ) {
    $return{card_type} = cardtype($self->payinfo);
    $return{payinfo} = $self->paymask;

    @return{'month', 'year'} = $self->paydate_monthyear;

  }

  if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
    my ($payinfo1, $payinfo2) = split '@', $self->paymask;
    $return{payinfo1} = $payinfo1;
    $return{payinfo2} = $payinfo2;
    $return{paytype}  = $self->paytype;
    $return{paystate} = $self->paystate;

  }

  #doubleclick protection
  my $_date = time;
  $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;

  %return;

}

#find old cust_main records (with payments)
sub _previous_cust_main {

  #safety check!  return nothing unless we're enabled explicitly
  return () unless FS::Conf->new->exists('sg-multicustomer_hack');

  my %opt = @_;
  my $custnum  = $opt{'custnum'};
  my $username = $opt{'username'};
  
  my %search = ();
  if ( $opt{'with_payments'} ) {
    $search{'extra_sql'} =
      ' AND 0 < ( SELECT COUNT(*) FROM cust_pay
                    WHERE cust_pay.custnum = cust_main.custnum
                )
      ';
  }

  qsearch( {
    'table'    => 'cust_main', 
    'hashref'  => { 'company' => { op => 'ILIKE', value => $opt{'username'} },
                    'custnum' => { op => '!=',    value => $opt{'custnum'}  },
                  },
    'order_by' => 'ORDER BY custnum',
    %search,
  } );

}

#since we could be passing masked old CC data, need to look that up and
#replace it (like regular process_payment does) w/info from old customer record
sub previous_process_payment {
  my $p = shift;

  return FS::ClientAPI::MyAccount::process_payment($p)
    unless $p->{'previous_custnum'}
        && (    ( $p->{'payby'} =~ /^(CARD|DCRD)$/ && $p->{'payinfo'}  =~ /x/i )
             || ( $p->{'payby'} =~ /^(CHEK|DCHK)$/ && $p->{'payinfo1'} =~ /x/i )
           );

  my $session = _cache->get($p->{'session_id'})
    or return { 'error' => "Can't resume session" }; #better error message

  my $custnum = $session->{'custnum'};

  my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
    or return { 'error' => "unknown custnum $custnum" };

  #make sure this is really a previous custnum of this customer
  my @previous_cust_main =
    grep { $_->custnum == $p->{'previous_custnum'} }
         _previous_cust_main( 'custnum'       => $custnum, 
                              'username'      => $cust_main->company,
                              'with_payments' => 1,
                            );

  my $previous_cust_main = $previous_cust_main[0];

  #causes problems with old data w/old masking method
  #if $previous_cust_main->paymask eq $payinfo;
  
  if ( $p->{'payby'} =~ /^(CHEK|DCHK)$/ && $p->{'payinfo1'} =~ /x/i ) {
    ( $p->{'payinfo1'}, $p->{'payinfo2'} ) =
      split('@', $previous_cust_main->payinfo);
  } elsif ( $p->{'payby'} =~ /^(CARD|DCRD)$/ && $p->{'payinfo'} =~ /x/i ) {
    $p->{'payinfo'} = $previous_cust_main->payinfo;
  }

  FS::ClientAPI::MyAccount::process_payment($p);

}

sub previous_payment_info_renew_info {
  my $p = shift;
  my $renew_info   = renew_info($p);
  my $payment_info = previous_payment_info($p);
  return { %$renew_info,
           %$payment_info,
         };
}

sub previous_process_payment_order_pkg {
  my $p = shift;

  my $hr = previous_process_payment($p);
  return $hr if $hr->{'error'};

  order_pkg($p);
}

sub previous_process_payment_change_pkg {
  my $p = shift;

  my $hr = previous_process_payment($p);
  return $hr if $hr->{'error'};

  change_pkg($p);
}

sub previous_process_payment_order_renew {
  my $p = shift;

  my $hr = previous_process_payment($p);
  return $hr if $hr->{'error'};

  order_renew($p);
}

1;