diff --git a/perllib/Integrations/Agile.pm b/perllib/Integrations/Agile.pm index 9f833a58d..e948cda66 100644 --- a/perllib/Integrations/Agile.pm +++ b/perllib/Integrations/Agile.pm @@ -90,6 +90,16 @@ sub IsAddressFree { ); } +sub Renew { + my ( $self, $params ) = @_; + + return $self->api_call( + action => 'renewal', + controller => 'servicecontract', + data => $params, + ); +} + sub SignUp { my ( $self, $params ) = @_; diff --git a/perllib/Open311/Endpoint/Integration/Agile.pm b/perllib/Open311/Endpoint/Integration/Agile.pm index 2c984f3bd..d7f38679e 100644 --- a/perllib/Open311/Endpoint/Integration/Agile.pm +++ b/perllib/Open311/Endpoint/Integration/Agile.pm @@ -49,6 +49,7 @@ has agile => ( use constant SERVICE_TO_SUB_MAPPING => { garden_subscription => \&_garden_subscription, + garden_subscription_renew => \&_garden_subscription_renew, garden_subscription_cancel => \&_garden_subscription_cancel, }; @@ -93,7 +94,11 @@ sub post_service_request { $self->logger->debug( "post_service_request arguments: " . encode_json($args) ); - my $sub = SERVICE_TO_SUB_MAPPING->{$service->service_code}; + # Garden Subscription may be a 'renew' type + my $lookup = $service->service_code; + $lookup .= "_$args->{attributes}{type}" if $args->{attributes}{type}; + + my $sub = SERVICE_TO_SUB_MAPPING->{$lookup}; die 'Service \'' . $service->service_code . '\' not handled' unless $sub; @@ -116,7 +121,7 @@ sub _garden_subscription { TitleCode => 'Default', CustomerExternalReference => '', ServiceContractUPRN => $args->{attributes}{uprn}, - WasteContainerQuantity => int( $args->{attributes}{new_containers} ) || 1, + WasteContainerQuantity => int( $args->{attributes}{total_containers} ) || 1, AlreadyHasBinQuantity => int( $args->{attributes}{current_containers} ) || 0, PaymentReference => $args->{attributes}{PaymentCode}, PaymentMethodCode => @@ -125,9 +130,8 @@ sub _garden_subscription { # Used for FMS report ID ActionReference => $args->{attributes}{fixmystreet_id}, - # TODO - DirectDebitDate => '', - DirectDebitReference => '', + DirectDebitDate => $args->{attributes}{direct_debit_start_date} // '', + DirectDebitReference => $args->{attributes}{direct_debit_reference} // '', } ); # Expected response: @@ -149,6 +153,51 @@ sub _garden_subscription { } } +sub _garden_subscription_renew { + my ( $self, $args ) = @_; + + my $integration = $self->get_integration; + + my $is_free = $integration->IsAddressFree( $args->{attributes}{uprn} ); + + if ( $is_free->{IsFree} eq 'False' ) { + my $res = $integration->Renew( { + CustomerExternalReference => $args->{attributes}{customer_external_ref}, + ServiceContractUPRN => $args->{attributes}{uprn}, + WasteContainerQuantity => int( $args->{attributes}{total_containers} ) || 1, + AlreadyHasBinQuantity => int( $args->{attributes}{current_containers} ) || 0, + PaymentReference => $args->{attributes}{PaymentCode}, + PaymentMethodCode => + PAYMENT_METHOD_MAPPING->{ $args->{attributes}{payment_method} }, + + DirectDebitDate => $args->{attributes}{direct_debit_start_date} // '', + DirectDebitReference => $args->{attributes}{direct_debit_reference} // '', + } ); + + # Expected response: + # { + # Id: int, + # Address: string, + # ServiceContractStatus: string, + # WasteContainerType: string, + # WasteContainerQuantity: int, + # StartDate: string, + # EndDate: string, + # ReminderDate: string, + # } + my $request = $self->new_request( + service_request_id => $res->{Id}, # TODO Is this correct? + ); + + return $request; + + } else { + die 'UPRN ' + . $args->{attributes}{uprn} + . ' does not have a subscription to be renewed, or is invalid'; + } +} + sub _garden_subscription_cancel { my ( $self, $args ) = @_; diff --git a/perllib/Open311/Endpoint/Service/UKCouncil/Agile.pm b/perllib/Open311/Endpoint/Service/UKCouncil/Agile.pm index e30946dff..8bcde058f 100644 --- a/perllib/Open311/Endpoint/Service/UKCouncil/Agile.pm +++ b/perllib/Open311/Endpoint/Service/UKCouncil/Agile.pm @@ -33,6 +33,13 @@ sub _build_attributes { required => 0, automated => 'hidden_field', ), + Open311::Endpoint::Service::Attribute->new( + code => 'total_containers', + description => 'Total number of requested containers', + datatype => 'string', + required => 0, + automated => 'hidden_field', + ), Open311::Endpoint::Service::Attribute->new( code => 'current_containers', description => 'Number of current containers', @@ -42,7 +49,7 @@ sub _build_attributes { ), Open311::Endpoint::Service::Attribute->new( code => 'new_containers', - description => 'Number of new containers', + description => 'Number of new containers (total requested minus current)', datatype => 'string', required => 0, automated => 'hidden_field', @@ -84,6 +91,31 @@ sub _build_attributes { required => 0, automated => 'hidden_field', ), + + # For direct debit payments + Open311::Endpoint::Service::Attribute->new( + code => 'direct_debit_reference', + description => 'Direct debit reference', + datatype => 'string', + required => 0, + automated => 'hidden_field', + ), + Open311::Endpoint::Service::Attribute->new( + code => 'direct_debit_start_date', + description => 'Direct debit initial payment date', + datatype => 'string', + required => 0, + automated => 'hidden_field', + ), + + Open311::Endpoint::Service::Attribute->new( + code => 'type', + description => 'Denotes whether subscription request is a renewal or not', + datatype => 'string', + required => 0, + automated => 'hidden_field', + ), + ); return \@attributes; diff --git a/t/open311/endpoint/agile.t b/t/open311/endpoint/agile.t index 271aa84bc..9ec7e0582 100644 --- a/t/open311/endpoint/agile.t +++ b/t/open311/endpoint/agile.t @@ -135,40 +135,60 @@ subtest 'GET service' => sub { description => 'Property ID', order => 3, }, + { %defaults, + code => 'total_containers', + description => 'Total number of requested containers', + order => 4, + }, { %defaults, code => 'current_containers', description => 'Number of current containers', - order => 4, + order => 5, }, { %defaults, code => 'new_containers', - description => 'Number of new containers', - order => 5, + description => 'Number of new containers (total requested minus current)', + order => 6, }, { %defaults, code => 'payment_method', description => 'Payment method: credit card or direct debit', - order => 6, + order => 7, }, { %defaults, code => 'payment', description => 'Payment amount in pence', - order => 7, + order => 8, }, { %defaults, code => 'reason', description => 'Cancellation reason', - order => 8, + order => 9, }, { %defaults, code => 'due_date', description => 'Cancellation date', - order => 9, + order => 10, }, { %defaults, code => 'customer_external_ref', description => 'Customer external ref', - order => 10, + order => 11, + }, + { %defaults, + code => 'direct_debit_reference', + description => 'Direct debit reference', + order => 12, + }, + { %defaults, + code => 'direct_debit_start_date', + description => 'Direct debit initial payment date', + order => 13, + }, + { %defaults, + code => 'type', + description => 'Denotes whether subscription request is a renewal or not', + order => 14, }, ], }, 'correct json returned'; @@ -187,7 +207,7 @@ subtest 'successfully subscribe to garden waste' => sub { 'attribute[fixmystreet_id]' => 2000001, 'attribute[uprn]' => '123_no_sub', 'attribute[current_containers]' => 1, - 'attribute[new_containers]' => 2, + 'attribute[total_containers]' => 2, 'attribute[payment_method]' => 'credit_card', 'attribute[PaymentCode]' => 'payment_123', ); @@ -213,7 +233,7 @@ subtest 'try to subscribe to garden waste when already subscribed' => sub { 'attribute[fixmystreet_id]' => 2000001, 'attribute[uprn]' => '234_has_sub', 'attribute[current_containers]' => 1, - 'attribute[new_containers]' => 2, + 'attribute[total_containers]' => 2, 'attribute[payment_method]' => 'credit_card', 'attribute[PaymentCode]' => 'payment_123', ); @@ -237,7 +257,7 @@ subtest 'handle unknown error' => sub { 'attribute[fixmystreet_id]' => 'bad_data', 'attribute[uprn]' => '123_no_sub', 'attribute[current_containers]' => 1, - 'attribute[new_containers]' => 2, + 'attribute[total_containers]' => 2, 'attribute[payment_method]' => 'credit_card', 'attribute[PaymentCode]' => 'payment_123', );