Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Credit Card Billing

by Jazz (Curate)
on Jan 06, 2002 at 00:06 UTC ( #136588=note: print w/ replies, xml ) Need Help??


in reply to Credit Card Billing

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.



Comment on Re: Credit Card Billing
Download Code
Re: Re: Credit Card Billing
by rob_au (Abbot) on Jan 06, 2002 at 08:32 UTC
    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'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://136588]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2014-07-11 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (220 votes), past polls