File Coverage

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