Sample Code: how to automatically prepare a payment batch

Sample Code: how to automatically prepare a payment batch

Posted by Gavin Brown on Thursday, May 1, 2008 at 11:23 UTC

Here's some sample code showing what you can do with our WWW::CNic Perl library. This script uses two functions — list_outstanding_domains and submit_payment_batch — to automatically prepare a payment batch for all domain names that are 45 days or more overdue.

There are a few ways this script could be customised: for example, to send an email to your finance team to instruct them to initiate a wire transfer for the payment. In a production environment, you'd also want to log what you're doing and update the status of the domain names in your own database.

We hope to be able to publish more sample scripts in the future: watch this space!

#!/usr/bin/perl -w
# example script to automatically remit payment advice for oustanding
# domain names.
#
# Copyright (c) 2008 CentralNic Ltd. All rights reserved. This program is
# free software; you can redistribute it and/or modify it under the same
# terms as Perl itself.
#
use WWW::CNic;
use Date::Manip;
use strict;
use warnings;

#
# some basic variables:
#
my $registrar_id         = '';       # your registrar ID
my $password             = '';       # your registrar password (or toolkit password)
my $days                 = 45;       # threshold after which you want to remit payment for a domain
my $method               = 'BA';     # BA for a bank transfer, or CH for a check

#
# prepare the query to return all outstanding domains:
#
my $query = WWW::CNic->new(
        use_ssl         => 1,
        command         => 'list_outstanding_domains',
        username        => $registrar_id,
        password        => $password,
);

#
# execute the query and check the response for an error:
#
my $result = $query->execute;

if ($result->is_error) {
        printf(
                STDERR
                "Error retrieving outstanding domains list: %s\n",
                $result->message
        );
        exit 1;
}

#
# the domains to be paid off go in here:
#
my @domains;

#
# loop through the result:
#
foreach my $domain ($result->domains) {
     #
     # calculate how long the domain has been unpaid:
     #
     my $outstanding = int((time() - strtotime($domain->{expiry})) / 86400);

     #
     # if the 'batch' property is non-zero, then we've already
     # processed this domain, so ignore it:
     #
     next if (defined($domain->{batch}) && int($domain->{batch}) > 0);

     #
     # if the domain is above the threshold, add it to the list:
     #
     if ($outstanding >= $days) {
                printf(
                        "Adding domain '%s' to batch, as it is %d days outstanding\n",
                        $domain->{domain},
                        $outstanding
                );
                push(@domains, $domain->{domain});
        }
}

if (0 == scalar(@domains)) {
        print "No domains found, exiting normally.\n";
        exit 0;
}

printf("Submit payment advice for %d domains\n", scalar(@domains));

#
# prepare the query to submit payment advice:
#
$query = WWW::CNic->new(
        use_ssl         => 1,
        command         => 'submit_payment_batch',
        username        => $registrar_id,
        password        => $password,
);

$query->set('method', $method);
$query->set('domains', \@domains);

#
# execute the query and check the response for an error:
#
$result = $query->execute;

if ($result->is_error) {
        printf(
                STDERR
                "Error submitting payment batch: %s\n",
                $result->message
        );
        exit 1;
}

#
# display response info:
#
printf(
        "Payment Batch #%d created for %d items at %s%01.2f (plus %s%01.2f VAT)\n",
        $result->response('batch'),
        $result->response('items'),
        $result->response('currency'),
        $result->response('amount'),
        $result->response('currency'),
        $result->response('vat'),
);

#
# a PHP-like strtotime() function:
#
sub strtotime { UnixDate(ParseDate($_[0]), '%s') }