File Coverage

blib/lib/Business/Bitcoin/Request.pm
Criterion Covered Total %
statement 29 51 56.8
branch 6 30 20.0
condition 1 9 11.1
subroutine 6 9 66.6
pod 2 2 100.0
total 44 101 43.5


line stmt bran cond sub pod time code
1             # -*-cperl-*-
2             #
3             # Business::Bitcoin::Request - Bitcoin payment request
4             # Copyright (c) 2016 Ashish Gulhati
5             #
6             # $Id: lib/Business/Bitcoin/Request.pm v1.10 Thu Dec 29 13:32:18 2016 -0500 $
7              
8 2     2   7 use strict;
  2         2  
  2         57  
9              
10             package Business::Bitcoin::Request;
11 2     2   8 use DBI;
  2         1  
  2         50  
12 2     2   1113 use LWP::UserAgent;
  2         61329  
  2         54  
13 2     2   13 use HTTP::Request;
  2         2  
  2         54  
14              
15 2     2   8 use vars qw( $VERSION $AUTOLOAD );
  2         2  
  2         1034  
16              
17             our ( $VERSION ) = '$Revision: 1.10 $' =~ /\s+([\d\.]+)/;
18              
19             sub new {
20 1     1 1 7 my ($class, %args) = @_;
21 1 50 33     9 return undef if $args{Amount} !~ /^\d+$/; return undef if $args{StartIndex} and $args{StartIndex} =~ /\D/;
  1 50       5  
22 1         9 my $db = $args{_BizBTC}->db; my $xpub = $args{_BizBTC}->xpub; my $ku = $args{_BizBTC}->kucmd;
  1         11  
  1         8  
23 1         3 my $timestamp = time; my $index;
  1         1  
24 1 50       5 my $index = defined $args{StartIndex} ? $args{StartIndex} : 'NULL';
25 1 50       7 my $refid = defined $args{Reference} ? "'$args{Reference}'" : 'NULL';
26 1 50       14 return undef unless $db->do("INSERT INTO requests values ($index, '$args{Amount}', NULL, $refid, '$timestamp');");
27 1         9748 $index = $db->last_insert_id('%', '%', 'requests', 'reqid');
28 1         9 $ENV{PATH} = undef;
29 1 50       3963 return undef unless my $address = `$ku $xpub -s 0/$index -a`; chomp $address;
  0            
30 0           my $rows = $db->do("UPDATE requests set address='$address' where reqid='$index';");
31             bless { Address => $address,
32             Amount => $args{Amount},
33             Reference => $args{Reference},
34 0 0         Confirmations => defined $args{Confirmations} ? $args{Confirmations} : 5,
35             Created => $timestamp }, $class;
36             }
37              
38             sub verify {
39 0     0 1   my $self = shift;
40 0           my $ua = new LWP::UserAgent;
41 0           my $req = HTTP::Request->new(GET => 'https://blockchain.info/q/addressbalance/' . $self->address . '?confirmations=' . $self->confirmations);
42 0           my $res = $ua->request($req);
43 0           $res->content == $self->amount;
44             }
45              
46             sub _find {
47 0     0     my ($class, %args) = @_;
48 0 0 0       return unless defined $args{Address} or defined $args{Reference};
49 0 0 0       return if defined $args{Address} and defined $args{Reference};
50              
51             my $query = 'SELECT address,amount,refid,created from requests WHERE ' .
52 0 0         (defined $args{Address} ? "address='$args{Address}';" : "refid='$args{Reference}';");
53 0           my ($address, $amount, $refid, $created) = $args{_BizBTC}->db->selectrow_array($query);
54             bless { Address => $address,
55             Amount => $amount,
56             Reference => $refid,
57 0 0         Confirmations => defined $args{Confirmations} ? $args{Confirmations} : 5,
58             Created => $created }, $class;
59             }
60              
61             sub AUTOLOAD {
62 0     0     my $self = shift; (my $auto = $AUTOLOAD) =~ s/.*:://;
  0            
63 0 0         return if $auto eq 'DESTROY';
64 0 0         if ($auto =~ /^(confirmations)$/x) {
65 0 0         $self->{"\u$auto"} = shift if (defined $_[0]);
66             }
67 0 0         if ($auto =~ /^(amount|address|reference|version|created|confirmations)$/x) {
68 0           return $self->{"\u$auto"};
69             }
70             else {
71 0           die "Could not AUTOLOAD method $auto.";
72             }
73             }
74              
75             1; # End of Business::Bitcoin::Request
76              
77             =head1 NAME
78              
79             Business::Bitcoin::Request - Bitcoin payment request
80              
81             =head1 VERSION
82              
83             $Revision: 1.10 $
84             $Date: Thu Dec 29 13:32:18 2016 -0500 $
85              
86             =head1 SYNOPSIS
87              
88             Business::Bitcoin::Request objects represent Bitcoin payment requests
89             generated by Business::Bitcoin.
90              
91             use Business::Bitcoin;
92              
93             my $bizbtc = new Business::Bitcoin (DB => '/tmp/bizbtc.db',
94             XPUB => 'xpub...');
95              
96             my $request = $bizbtc->request(Amount => 4200);
97              
98             print ($request->verify ? "Verified\n" : "Verification failed\n");
99              
100             =head1 METHODS
101              
102             =head2 new
103              
104             Not intended to be called directly. Business::Bitcoin::Request objects
105             should be created by calling the request method on a Business::Bitcoin
106             object.
107              
108             =head2 verify
109              
110             Verify that the request has been paid. Returns true if the request has
111             been paid, false otherwise. The number of confirmations required to
112             consider a payment valid can be set via the confirmations accessor.
113              
114             =head1 ACCESSORS
115              
116             Accessors can be called with no arguments to query the value of an
117             object property, or with a single argument, to set the property to a
118             specific value (unless the property is read only).
119              
120             =head2 confirmations
121              
122             The number of confirmations needed to consider a payment valid.
123              
124             =head2 amount
125              
126             The amount of the payment request, in Satoshi. Read only.
127              
128             =head2 address
129              
130             The Bitcoin receiving address for the payment request. Read only.
131              
132             =head2 created
133              
134             The timestamp of when the request was created. Read only.
135              
136             =head2 reference
137              
138             An optional reference ID for the request, to facilitate integration
139             with existing order systems. Read only.
140              
141             =head2 version
142              
143             The version number of this module. Read only.
144              
145             =head1 AUTHOR
146              
147             Ashish Gulhati, C<< >>
148              
149             =head1 BUGS
150              
151             Please report any bugs or feature requests to C, or through
152             the web interface at L. I will be notified, and then you'll
153             automatically be notified of progress on your bug as I make changes.
154              
155             =head1 SUPPORT
156              
157             You can find documentation for this module with the perldoc command.
158              
159             perldoc Business::Bitcoin::Request
160              
161             You can also look for information at:
162              
163             =over 4
164              
165             =item * RT: CPAN's request tracker
166              
167             L
168              
169             =item * AnnoCPAN: Annotated CPAN documentation
170              
171             L
172              
173             =item * CPAN Ratings
174              
175             L
176              
177             =item * Search CPAN
178              
179             L
180              
181             =back
182              
183             =head1 LICENSE AND COPYRIGHT
184              
185             Copyright (c) 2016 Ashish Gulhati. All rights reserved.
186              
187             This program is free software; you can redistribute it and/or modify it
188             under the terms of either: the GNU General Public License as published
189             by the Free Software Foundation; or the Artistic License.
190              
191             See http://dev.perl.org/licenses/ for more information.