Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

At the moment I'm developing a billing application which wil interface with authorize.net, due to the way I am encrypting the credit card details I will be logging into the system and running the transaction due to the fact I wont be storing the encrypt/decrypt key on the server, so I will need to enter it when a run is initiated. Account are re-billed so unfortunately cc storage is essential

The only hurdle I am facing is how to schedule which accounts are to be billed, I would really like some feedback on the best approach to doing this and would be very greatfull for any suggestions, here are my thoughts in doing this:

Have a time(); stamp, and compare it with the transaction last run, if it was > 2592000 ago (a month) run the new one (problem here being that it may also have annual billing so the numbers could get very large, and the different lengths of the months may pose a problem)

Use a simple date in the form of next-bill: 22-02-2002 - when the billing run is started it will just compare todays date with that of the next-run column, if a match is made the transaction will be processed. (problem here, is that if the billing isnt run for that day, the transactions will be missed)

Sorry for the lengthy post, any thoughts would be appreciated

Replies are listed 'Best First'.
Re: Credit Card Billing
by Trimbach (Curate) on Jan 05, 2002 at 21:03 UTC
    I run a site that does monthly credit card re-billing and our solution was to store a "last-billed" UNIX timestamp in the database, and then query the database for users that have a "last billed" date on a certain day of the month. (That is, if it's the 10th of the month, look for last billed dates that are the 10th of the last month.) This system works great except for people who originally subscribe on the 29th, 30th, or 31st of the month... to take care of them we just bill all those people on the 28th. Easy peasey. Missed transactions (or failed transactions) are easy to track because we always know the last date a successful billing occurred. No need to set a separate flag.

    I suppose I don't need to remind you that if you're going to be storing credit card numbers for God's sake don't store them anywhere within your web server's file tree... yes, I know you're encrypting them but it's always good practice to make it as hard as possible to even get to the cc numbers in the first place.

    Gary Blackburn
    Trained Killer

      Hi All,

      thanks so much for the responses

      The way I was thinking was to store the cc numbers in an sql database.

      If the encrypt/decrypt key was stored in the perl script, and then the script were to be compiled using perlcc or such, would that be secure? Or would it still be the best option to just enter the encrypt key manually whenever i deal with the cc numbers, i know that method would be very secure

      Thanks
        Obfuscating the key using perlcc is not the answer. The only setup I have seen that seems remotely comforting is a multi-tier design where the box that has the keys is running no services, and can only be passed transactions. (i.e, it can't be queried).

        Something like:

        
        Internet =====> Firewall ===> 
                DMZ Web Server ==> Firewall ==> 
                       Seriously Locked Down CC Processer 
        
        
        The 'CC Processer' box should be running no services at all, and listening on only one socket. This socket should accept inbound transactions and return an ack/nack, and nothing else. This would mean that administration, key changes, logging in, etc, would have to be done at the console. The web server should only have CC number while they are in transit, and should never write them to disk.

        The folks at VISA have a pretty decent summary of what should be done to protect machines with CC data. ( It's a bit lacking on implementation details, but still good.) See: The VISA CISP Tech Info page.

Re: Credit Card Billing
by Jazz (Curate) on Jan 06, 2002 at 00:06 UTC

    I use Class::Date for date comparisons. It facilitate comparisons with past, present and future dates.

    use Class::Date qw/ date localdate now /; # date that the billing period starts (get data from db) my $period_start = date('2001-12-31'); # date to start attempting charges (get data from db) my $charge_date = date('2001-12-27'); my $today = date( localdate( now ))->truncate; # truncate makes time m +idnight # it's late if ( $charge_date < $today ){ print "Charge date passed. Transaction previously failed. Try aga +in.\n"; } # not yet due elsif ( $charge_date > $today ) { print 'Charge to be processed in ', +( $charge_date - Class::Date->new( $today ) )->day, ' days.'; } # charge today else { print 'Today is charge date. If successful, next charge date will + be: ', date( $charge_date + '1M'); # one month # change next charge date in BillSchedule table. }

    An alternative is to use Date::Calc, which was discussed recently here. Depending on how many comparisions you'll be doing, Date::Calc may be the best choice because it's much faster.


      I have come up with a similar solution previously, initially making use of some hand-rolled calculation routines which I have since replaced using Date::Calc.

      The setting in which these calculations are used is a billing system where the billing for each customer is determined by their plan id (which in turn relates to billing information in another database table) and the activation date of their account. The following is an edited snippet of the transaction calculation code:

      # return customer id, plan id and activation date for activated acco +unts and customers my $acc = $dbh->prepare(qq/ select accounts.customerid, accounts.planid, accounts.activationd +ate from accounts, customers where accounts.customerid = customers.id and accounts.terminationdate = NULL and customers.activated = 'Y' /); $acc->execute; while (my $res = $acc->fetchrow_hashref) { # Information regarding billing fees for each plan is stored in +@plans where the array index correlates to the billing id my $plan = $plans[ $res->{'planid'} ]; # $delta is the number of days since account activation - note t +hat @date holds todays date my $delta = Delta_Days((split '-', $res->{'activationdate'}), @dat +e); if ($delta == 0) { if ($plan->{'setup'}) { # account was opened today - charge set up fee if necess +ary } } else { # Alternatively, if the plan has a regular billing period, c +alculated as number of billing periods per annum, calculate whether t +he client should be billed today if ($plan->{'fee'} && $plan->{'period'}) { my $calc = $delta / int (Days_in_Year((@date)[0..1]) / $pl +an->{'period'}); if ($calc == int($calc)) { # Client should be billed $plan->{'fee'} amount } } } } $acc->finish;

      The details of billing plans is stored in @plans and includes an unique plan id, plan setup fee (if applicable), the number of billing periods per annum and the fee per billing period. The use of number of billing periods per annum was selected as a measure for billing as it allowed the maximum in flexibility with regard to future plans - There is no fixed limitation with regard to all plans having to monthly or annual or alike. The number of billing periods per annum is converted to day period by the int (Days_in_Year((@date)[0..1]) / $plan->{'period'}) which in turn allows the date of billing to be calculated if the modulus of days since the account activation to day of transaction run ($delta) is zero.

       

      perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: Credit Card Billing
by jonjacobmoon (Pilgrim) on Jan 05, 2002 at 18:23 UTC
    I have some thoughts on this, but a question first. What does your schema look like? Clearly, you need to handle alot of this stuff a DB and using a database driven solution, I would think, would be required here.

    I realize that is not a Perl answer but I am not sure your post is a Perl questions as much as it is an algorithm question. That is is fine, but I wanted to be clear on that.

    My only other thought on it is to use Class::Date for handling the date calculation, storage, and calculation. I like the OO interface that it uses.

    I did something like this recently, and would be happy to rehash that with you directly. Feel free to email me outside PM is you want to bounce some ideas around.

Re: Credit Card Billing
by metadoktor (Hermit) on Jan 05, 2002 at 18:51 UTC
    Use a simple date in the form of next-bill: 22-02-2002 - when the billing run is started it will just compare todays date with that of the next-run column, if a match is made the transaction will be processed. (problem here, is that if the billing isnt run for that day, the transactions will be missed)

    So, you have setup a new field with the next-billing-date and you're afraid that if the program is not run that day then the transaction will be missed? How about adding a billed flag? When you establish the next-billing-date also make sure to set the billed flag to 0. That way when your program runs even if it runs after that billing date you will still bill the account because the flag has not been set. This is a simple solution but you can probably fit it to your needs with additional modifications depending upon your constraints.

    metadoktor

    "The doktor is in."

Credit Card Re-Billing
by andye (Curate) on Jan 05, 2002 at 20:19 UTC
    Account are re-billed so unfortunately cc storage is essential

    Not necessarily the case - some of the payment processors will handle scheduled re-billing for you. I know that WorldDirect will do this, although in this case they require the user to set up a username/password.

    If monks know of other payment processors who offer automatic rebilling, could they tell me (in this thread or by /msg) - it would be handy to know.

    Cheers,
    andy.