Provide better diagnostics when the cust_main owning this domain does not
[freeside.git] / FS / FS / part_export / domreg_opensrs.pm
1 package FS::part_export::domreg_opensrs;
2
3 use vars qw(@ISA %info %options $conf);
4 use Tie::IxHash;
5 use FS::Record qw(qsearchs qsearch);
6 use FS::Conf;
7 use FS::part_export::null;
8 use FS::svc_domain;
9 use FS::part_pkg;
10 use Net::OpenSRS;
11
12 =head1 NAME
13
14 FS::part_export::domreg_opensrs - Register or transfer domains with Tucows OpenSRS
15
16 =head1 DESCRIPTION
17
18 This module handles registering and transferring domains using a registration service provider (RSP) account
19 at Tucows OpenSRS, an ICANN-approved domain registrar.
20
21 As a part_export, this module can be designated for use with svc_domain services.  When the svc_domain object
22 is inserted into the Freeside database, registration or transferring of the domain may be initiated, depending
23 on the setting of the svc_domain's action field.
24
25 =over 4
26
27 =item N - Register the domain
28
29 =item M - Transfer the domain
30
31 =item I - Ignore the domain for registration purposes
32
33 =back
34
35 =cut
36
37 @ISA = qw(FS::part_export::null);
38
39 my @tldlist = qw/com net org biz info name mobi at be ca cc ch cn de dk es eu fr it mx nl tv uk us/;
40
41 tie %options, 'Tie::IxHash',
42   'username'     => { label => 'Reseller user name at OpenSRS',
43                       },
44   'privatekey'   => { label => 'Private key',
45                       },
46   'password'     => { label => 'Password for management account',
47                       },
48   'masterdomain' => { label => 'Master domain at OpenSRS',
49                       },
50   'debug_level'  => { label => 'Net::OpenSRS debug level',
51                       type => 'select',
52                       options => [ 0, 1, 2, 3 ],
53                       default => 0 },
54 #  'register'     => { label => 'Use for registration',
55 #                      type => 'checkbox',
56 #                      default => '1' },
57 #  'transfer'     => { label => 'Use for transfer',
58 #                      type => 'checkbox',
59 #                      default => '1' },
60   'tlds'         => { label => 'Use this export for these top-level domains (TLDs)',
61                       type => 'select',
62                       multi => 1,
63                       size => scalar(@tldlist),
64                       options => [ @tldlist ],
65                       default => 'com net org' },
66 ;
67
68 %info = (
69   'svc'     => 'svc_domain',
70   'desc'    => 'Domain registration via Tucows OpenSRS',
71   'options' => \%options,
72   'notes'   => <<'END'
73 Registers and transfers domains via the <a href="http://opensrs.com/">Tucows OpenSRS</a> registrar (using <a href="http://search.cpan.org/dist/Net-OpenSRS">Net::OpenSRS</a>).
74 All of the Net::OpenSRS restrictions apply:
75 <UL>
76   <LI>You must have a reseller account with Tucows.
77   <LI>You must add the public IP address of the Freeside server to the 'Script API allow' list in the OpenSRS web interface.
78   <LI>You must generate an API access key in the OpenSRS web interface and enter it below.
79   <LI>All domains are managed using the same user name and password, but you can create sub-accounts for clients.
80   <LI>The user name must be the same as your OpenSRS reseller ID.
81   <LI>You must enter a master domain that all other domains are associated with.  That domain must be registered through your OpenSRS account.
82 </UL>
83 Some top-level domains offered by OpenSRS have additional business rules not supported by this export. These TLDs cannot be registered or transfered with this export.
84 <BR><BR>Use these buttons for some useful presets:
85 <UL>
86   <LI>
87     <INPUT TYPE="button" VALUE="OpenSRS Live System (rr-n1-tor.opensrs.net)" onClick='
88       document.dummy.machine.value = "rr-n1-tor.opensrs.net";
89       this.form.machine.value = "rr-n1-tor.opensrs.net";
90     '>
91   <LI>
92     <INPUT TYPE="button" VALUE="OpenSRS Test System (horizon.opensrs.net)" onClick='
93       document.dummy.machine.value = "horizon.opensrs.net";
94       this.form.machine.value = "horizon.opensrs.net";
95     '>
96 </UL>
97 END
98 );
99
100 install_callback FS::UID sub { 
101   $conf = new FS::Conf;
102 };
103
104 =head1 METHODS
105
106 =over 4
107
108 =item format_tel
109
110 Reformats a phone number according to registry rules.  Currently Freeside stores phone numbers
111 in NANPA format and the registry prefers "+CCC.NPANPXNNNN"
112
113 =cut
114
115 sub format_tel {
116   my $tel = shift;
117
118   #if ($tel =~ /^(\d{3})-(\d{3})-(\d{4})( x(\d+))?$/) {
119   if ($tel =~ /^(\d{3})-(\d{3})-(\d{4})$/) {
120     $tel = "+1.$1$2$3";
121 #    if $tel .= "$4" if $4;
122   }
123   return $tel;
124 }
125
126 sub gen_contact_info
127 {
128   my ($co)=@_;
129
130   my @invoicing_list = $co->invoicing_list_emailonly;
131   if ( $conf->exists('emailinvoiceautoalways')
132        || $conf->exists('emailinvoiceauto') && ! @invoicing_list
133        || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
134     push @invoicing_list, $co->all_emails;
135   }
136
137   my $email = ($conf->exists('business-onlinepayment-email-override'))
138               ? $conf->config('business-onlinepayment-email-override')
139               : $invoicing_list[0];
140
141   my $c = {
142     firstname => $co->first,
143     lastname  => $co->last,
144     company   => $co->company,
145     address   => $co->address1,
146     city      => $co->city(),
147     state     => $co->state(),
148     zip       => $co->zip(),
149     country   => uc($co->country()),
150     email     => $email,
151     #phone     => format_tel($co->daytime()),
152     phone     => $co->daytime() || $co->night,
153   };
154   return $c;
155 }
156
157 sub validate_contact_info {
158   my $c = shift;
159
160   my %fields = (
161     firstname => "first name",
162     lastname => "last name",
163     address => "street address",
164     city => "city", 
165     state => "state",
166     zip => "ZIP/postal code",
167     country => "country",
168     email => "email address",
169     phone => "phone number",
170   );
171   my @err = ();
172   foreach (keys %fields) {
173     if (!defined($c->{$_}) || !$c->{$_}) {
174       push @err, $fields{$_};
175     }
176   }
177   if (scalar(@err) > 0) {
178     return "Contact information needs: " . join(', ', @err);
179   }
180   undef;
181 }
182
183 sub testmode {
184   my $self = shift;
185
186   return 'live' if $self->machine eq "rr-n1-tor.opensrs.net";
187   return 'test' if $self->machine eq "horizon.opensrs.net";
188   undef;
189 }
190
191 sub _export_insert {
192   my( $self, $svc_domain ) = ( shift, shift );
193
194   return if $svc_domain->action eq 'I';  # Ignoring registration, just doing DNS
195
196   # Get the TLD of the new domain
197   my @bits = split /\./, $svc_domain->domain;
198
199   return "Can't register subdomains: " . $svc_domain->domain if scalar(@bits) != 2;
200
201   my $tld = pop @bits;
202
203   # See if it's one this export supports
204   my @tlds = split /\s+/, $self->option('tlds');
205   @tlds =  map { s/\.//; $_ } @tlds;
206   return "Can't register top-level domain $tld, restricted to: " . $self->option('tlds') if ! grep { $_ eq $tld } @tlds;
207
208   my $cust_main = $svc_domain->cust_svc->cust_pkg->cust_main;
209
210   my $c = gen_contact_info($cust_main);
211
212   my $err = validate_contact_info($c);
213   return $err if $err;
214
215   my $srs = Net::OpenSRS->new();
216
217   $srs->debug_level( $self->option('debug_level') ); # Output should be in the Apache error log
218
219   $srs->environment( $self->testmode() );
220   $srs->set_key( $self->option('privatekey') );
221
222   $srs->set_manage_auth( $self->option('username'), $self->option('password') );
223
224   my $cookie = $srs->get_cookie( $self->option('masterdomain') );
225   if (!$cookie) {
226      return "Unable to get cookie at OpenSRS: " . $srs->last_response();
227   }
228
229   if ($svc_domain->action eq 'N') {
230 #    return "Domain registration not enabled" if !$self->option('register');
231     return $srs->last_response() if !$srs->register_domain( $svc_domain->domain, $c);
232   } elsif ($svc_domain->action eq 'M') {
233 #    return "Domain transfer not enabled" if !$self->option('transfer');
234     return $srs->last_response() if !$srs->transfer_domain( $svc_domain->domain, $c);
235   } else {
236     return "Unknown domain action " . $svc_domain->action;
237   }
238
239   return ''; # Should only get here if register or transfer succeeded
240
241 }
242
243 ## Domain registration exports do nothing on replace.  Mainly because we haven't decided what they should do.
244 #sub _export_replace {
245 #  my( $self, $new, $old ) = (shift, shift, shift);
246 #
247 #  return '';
248 #
249 #}
250
251 ## Domain registration exports do nothing on delete.  You're just removing the domain from Freeside, not the registry
252 #sub _export_delete {
253 #  my( $self, $svc_domain ) = ( shift, shift );
254 #
255 #  return '';
256 #}
257
258 sub registrar {
259   return {
260         name => 'OpenSRS',
261   };
262 }
263
264 =back
265
266 =head1 SEE ALSO
267
268 L<FS::part_export_option>, L<FS::export_svc>, L<FS::svc_domain>,
269 L<FS::Record>, schema.html from the base documentation.
270
271 =cut
272
273 1;
274