fix DBI connection, RT#39250
[freeside.git] / FS / bin / freeside-wipe-cvv
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Std;
5 use FS::UID qw(adminsuidsetup dbh);
6 use FS::Record qw(qsearch qsearchs);
7 use Time::Local 'timelocal';
8 use Date::Format 'time2str';
9
10 my %opt;
11 getopts('vnd:', \%opt);
12
13 my $user = shift or die &usage;
14 adminsuidsetup $user;
15 $FS::UID::AutoCommit = 0;
16 $FS::Record::nowarn_identical = 1;
17
18 my $extra_sql = FS::cust_main->cancel_sql;
19 $extra_sql = "WHERE $extra_sql 
20 AND cust_main.payby IN('CARD','DCRD','CHEK','DCHK')
21 ";
22
23 if($opt{'d'}) {
24   $opt{'d'} =~ /^(\d+)$/ or die &usage;
25   my $time = timelocal(0,0,0,(localtime(time-(86400*$1)))[3..5]);
26   print "Excluding customers canceled after ".time2str("%D",$time)."\n" 
27     if $opt{'v'};
28   $extra_sql .= ' AND 0 = (' . FS::cust_main->select_count_pkgs_sql . 
29                 " AND cust_pkg.cancel > $time)";
30 }
31
32 foreach my $cust_main ( qsearch({
33     'table'     => 'cust_main',
34     'hashref'   => {},
35     'extra_sql' => $extra_sql
36   }) ) {
37   if($opt{'v'}) {
38     print $cust_main->name, "\n";
39   }
40   if($opt{'n'}) {
41     $cust_main->payinfo('');
42     $cust_main->paydate('');
43     $cust_main->payby('BILL'); 
44 # can't have a CARD or CHEK without a valid payinfo
45   }
46   $cust_main->paycvv('');
47   my $error = $cust_main->replace;
48   if($error) {
49     dbh->rollback;
50     die "$error (changes reverted)\n";
51   }
52 }
53 dbh->commit;
54
55 sub usage {
56   "Usage:\n\n  freeside-wipe-cvv [ -v ] [ -n ] [ -d days ] user\n"
57 }
58
59 =head1 NAME
60
61 freeside-wipe-cvv - Wipe sensitive payment information from customer records.
62
63 =head1 SYNOPSIS
64
65   freeside-wipe-cvv [ -v ] [ -n ] [ -d days ] user
66
67 =head1 DESCRIPTION
68
69 freeside-wipe-cvv deletes the CVV numbers (and, optionally, credit
70 card or bank account numbers) of customers who have no non-canceled 
71 packages.  Normally CVV numbers are deleted as soon as a payment is 
72 processed; if the customer is canceled before a payment is processed, 
73 this may not happen and the CVV will remain indefinitely, violating 
74 good security practice and (possibly) your merchant agreement.
75 Running freeside-wipe-cvv will remove this data.
76
77 -v: Be verbose.
78
79 -n: Remove card and account numbers in addition to CVV numbers.  This 
80 will also set the customer's payment method to 'BILL'.
81
82 -d days: Only remove CVV/card numbers from customers who have been 
83 inactive for at least that many days.  Optional; will default to 
84 all canceled customers.
85
86 =cut
87