File Coverage

blib/lib/Business/Bitcoin.pm
Criterion Covered Total %
statement 51 53 96.2
branch 17 30 56.6
condition 7 10 70.0
subroutine 11 11 100.0
pod 3 3 100.0
total 89 107 83.1


line stmt bran cond sub pod time code
1             # -*-cperl-*-
2             #
3             # Business::Bitcoin - Easy and secure way to accept Bitcoin payments online
4             # Copyright (c) Ashish Gulhati
5             #
6             # $Id: lib/Business/Bitcoin.pm v1.051 Tue Oct 16 22:26:58 PDT 2018 $
7              
8 2     2   105410 use strict;
  2         10  
  2         59  
9              
10             package Business::Bitcoin;
11              
12 2     2   34 use 5.010000;
  2         5  
13 2     2   9 use warnings;
  2         4  
  2         39  
14 2     2   8 use strict;
  2         2  
  2         51  
15              
16 2     2   2475 use DBI;
  2         28145  
  2         97  
17 2     2   798 use Business::Bitcoin::Request;
  2         6  
  2         68  
18              
19 2     2   13 use vars qw( $VERSION $AUTOLOAD );
  2         3  
  2         1040  
20              
21             our ( $VERSION ) = '$Revision: 1.051 $' =~ /\s+([\d\.]+)/;
22              
23             sub new {
24 2     2 1 76 my $class = shift;
25 2         14 my %arg = @_;
26 2 50       8 return undef unless $arg{XPUB};
27 2 50 66     15 return undef if $arg{StartIndex} and $arg{StartIndex} =~ /\D/;
28 2 50 33     12 unlink $arg{DB} if $arg{Clobber} and $arg{DB} ne ':memory:';
29 2         20 my $db = DBI->connect("dbi:SQLite:dbname=$arg{DB}", undef, undef, {AutoCommit => 1});
30 2         9849 my @tables = $db->tables('%','%','requests','TABLE');
31 2 50       1334 unless ($tables[0]) {
32 2 50       8 if ($arg{Create}) {
33 2 50       11 return undef unless $db->do('CREATE TABLE requests (
34             reqid INTEGER PRIMARY KEY AUTOINCREMENT,
35             amount int NOT NULL,
36             address text,
37             reference text UNIQUE,
38             created int NOT NULL,
39             processed int,
40             status text
41             );');
42 2 50       688 return undef unless $db->do('CREATE INDEX idx_requests_address ON requests(address);');
43 2 50       387 return undef unless $db->do('CREATE INDEX idx_requests_reference ON requests(reference);');
44 2   100     368 my $startindex = $arg{StartIndex} || 0;
45 2         5 $startindex--;
46 2 50       14 return unless $db->do("INSERT INTO SQLITE_SEQUENCE values ('requests',$startindex);");
47             }
48             else {
49 0         0 return undef;
50             }
51             }
52 2   100     226 bless { XPUB => $arg{XPUB}, DB => $db, PATH => $arg{Path} || 'penultimate' }, $class;
53             }
54              
55             sub request { # Create a Bitcoin payment request
56 2     2 1 10 my ($self, %arg) = @_;
57 2 50       19 return undef if $arg{Amount} !~ /^\d+$/;
58              
59             # Workaround for SQLite not starting sequence from 0 when asked to in new()
60 2         15 my $startindex = $self->db->selectcol_arrayref("SELECT seq from SQLITE_SEQUENCE WHERE name='requests';")->[0];
61 2 100       249 my %forcezero; %forcezero = ( StartIndex => 0 ) if $startindex == -1;
  2         8  
62              
63 2         20 my $req = new Business::Bitcoin::Request (_BizBTC => $self, %arg, %forcezero);
64             }
65              
66             sub findreq { # Retrieve a previously created request
67 2     2 1 9 my ($self, %arg) = @_;
68 2         11 my $req = _find Business::Bitcoin::Request (_BizBTC => $self, %arg);
69             }
70              
71             sub AUTOLOAD {
72 18     18   39 my $self = shift; (my $auto = $AUTOLOAD) =~ s/.*:://;
  18         93  
73 18 100       283 return if $auto eq 'DESTROY';
74 16 50       64 if ($auto =~ /^(xpub|path|db)$/x) {
75 16 50       38 $self->{"\U$auto"} = shift if (defined $_[0]);
76             }
77 16 50       54 if ($auto =~ /^(xpub|path|db|version)$/x) {
78 16         127 return $self->{"\U$auto"};
79             }
80             else {
81 0           die "Could not AUTOLOAD method $auto.";
82             }
83             }
84              
85             1; # End of Business::Bitcoin
86              
87             __END__