1 package FS::part_export::ez_prepaid;
3 use base qw( FS::part_export );
6 use vars qw(@ISA %info $version $replace_ok_kludge $product_info);
8 use FS::Record qw( qsearchs );
11 use XML::Simple qw( xml_in );
17 my %language_id = ( English => 1, Spanish => 2 );
19 tie my %options, 'Tie::IxHash',
20 'site_id' => { label => 'Site ID' },
21 'clerk_id' => { label => 'Clerk ID' },
22 # 'product_id' => { label => 'Product ID' }, use the 'title' field
23 # 'amount' => { label => 'Purchase amount' },
24 'language' => { label => 'Language',
26 options => [ 'English', 'Spanish' ],
29 'debug' => { label => 'Debug level',
30 type => 'select', options => [0, 1, 2 ] },
34 'svc' => 'svc_external',
35 'desc' => 'Purchase EZ-Prepaid PIN',
36 'options' => \%options,
39 <P>Export to the EZ-Prepaid PIN purchase service. If the purchase is allowed,
40 the PIN will be stored as svc_external.id.</P>
41 <P>svc_external.title must contain the product ID, and should be set as a fixed
42 field in the service definition. For a list of product IDs, see the
43 "Merchant Info" tab in the EZ Prepaid reseller portal.</P>
47 $replace_ok_kludge = 0;
50 my ($self, $svc_external) = @_;
52 # the name on the certificate is 'debisys.com', for some reason
53 local $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
55 my $pin = eval { $self->ez_prepaid_PinDistSale( $svc_external->title ) };
58 local($replace_ok_kludge) = 1;
59 $svc_external->set('id', $pin);
60 $svc_external->replace;
64 $replace_ok_kludge ? '' : "can't change PIN after purchase";
68 "can't delete PIN after purchase";
71 # possibly options at some point to relate these to agentnum/usernum
72 sub site_id { $_[0]->option('site_id') }
74 sub clerk_id { $_[0]->option('clerk_id') }
76 sub ez_prepaid_PinDistSale {
78 my $product_id = shift;
79 $self->ez_prepaid_init; # populate product ID cache
80 my $info = $product_info->{$product_id};
82 if ( $self->option('debug') ) {
83 warn "Purchasing PIN product #$product_id:\n" .
84 $info->{Description}."\n".
85 $info->{CurrencyCode} . ' ' .$info->{Amount}."\n";
88 die "Unknown PIN product #$product_id.\n";
91 my $response = $self->ez_prepaid_request(
97 '', # AccountID, not used for PIN sale
98 $product_info->{$product_id}->{Amount},
100 ($language_id{ $self->option('language') } || 1),
102 if ( $self->option('debug') ) {
103 warn Dumper($response);
104 # includes serial number and transaction ID, possibly useful
105 # (but we don't have a structured place to store it--maybe in
111 sub ez_prepaid_init {
112 # returns the SOAP client object
114 my $wsdl = 'https://webservice.ez-prepaid.com/soap/webServices.wsdl';
116 if ( $self->option('debug') >= 2 ) {
117 SOAP::Lite->import(+trace => [transport => \&log_transport ]);
120 if ( !$self->client ) {
121 $self->set(client => SOAP::Lite->new->service($wsdl));
122 # I don't know if this can happen, but better to bail out here
123 # than go into recursion.
124 die "Error creating SOAP client\n" if !$self->client;
127 if ( !defined($product_info) ) {
128 # for now we only support the 'PIN' type
129 my $response = $self->ez_prepaid_request(
130 'GetTransTypeList', $version, $self->site_id, '', '', '', ''
132 my %transtype = map { $_->{Description} => $_->{TransTypeId} }
133 @{ $response->{TransType} };
135 if ( !exists $transtype{PIN} ) {
136 warn "'PIN' transaction type not available.\n";
137 # or else your site ID is wrong
141 $response = $self->ez_prepaid_request(
144 $self->option('site_id'),
151 map { $_->{ProductId} => $_ }
152 @{ $response->{Product} }
154 } #!defined $product_info
159 if ( UNIVERSAL::can($in, 'content') ) {
160 warn $in->content."\n";
164 my @ForceArray = qw(TransType Product); # add others as needed
165 sub ez_prepaid_request {
167 # takes a method name and param list,
168 # returns a hashref containing the unpacked response
171 $self->ez_prepaid_init if !$self->client;
174 my $xml = $self->client->$method(@_);
175 # All of their response data types are one part, a string, containing
176 # an encoded XML structure, containing the fields described in the docs.
177 my $response = xml_in($xml, ForceArray => \@ForceArray);
178 if ( exists($response->{ResponseCode}) && $response->{ResponseCode} > 0 ) {
179 die "[$method] ".$response->{ResponseMessage};