|
| 1 | + |
| 2 | +=head1 NAME |
| 3 | +
|
| 4 | +Open311::Endpoint::Integration::Agile - An integration with the Agile backend |
| 5 | +
|
| 6 | +=head1 SYNOPSIS |
| 7 | +
|
| 8 | +This integration lets us fetch & post Green Garden Waste data to and from |
| 9 | +Agile |
| 10 | +
|
| 11 | +=cut |
| 12 | + |
| 13 | +package Open311::Endpoint::Integration::Agile; |
| 14 | + |
| 15 | +use v5.14; |
| 16 | + |
| 17 | +use Moo; |
| 18 | +use Integrations::Agile; |
| 19 | +use Open311::Endpoint::Service::UKCouncil::Agile; |
| 20 | +use JSON::MaybeXS; |
| 21 | + |
| 22 | +extends 'Open311::Endpoint'; |
| 23 | +with 'Open311::Endpoint::Role::mySociety'; |
| 24 | +with 'Open311::Endpoint::Role::ConfigFile'; |
| 25 | +with 'Role::EndpointConfig'; |
| 26 | +with 'Role::Logger'; |
| 27 | + |
| 28 | +has jurisdiction_id => ( is => 'ro' ); |
| 29 | + |
| 30 | +has category_mapping => ( |
| 31 | + is => 'lazy', |
| 32 | + default => sub { $_[0]->endpoint_config->{category_mapping} } |
| 33 | +); |
| 34 | + |
| 35 | +has service_class => ( |
| 36 | + is => 'ro', |
| 37 | + default => 'Open311::Endpoint::Service::UKCouncil::Agile', |
| 38 | +); |
| 39 | + |
| 40 | +has integration_class => ( is => 'ro', default => 'Integrations::Agile' ); |
| 41 | + |
| 42 | +has agile => ( |
| 43 | + is => 'lazy', |
| 44 | + default => sub { |
| 45 | + $_[0]->integration_class->new( |
| 46 | + config_filename => $_[0]->jurisdiction_id ); |
| 47 | + }, |
| 48 | +); |
| 49 | + |
| 50 | +use constant PAYMENT_METHOD_MAPPING => { |
| 51 | + credit_card => 'CREDITCARD', |
| 52 | + direct_debit => 'DIRECTDEBIT', |
| 53 | + csc => 'CREDITCARD', |
| 54 | +}; |
| 55 | + |
| 56 | +sub get_integration { |
| 57 | + $_[0]->log_identifier( $_[0]->jurisdiction_id ); |
| 58 | + return $_[0]->agile; |
| 59 | +} |
| 60 | + |
| 61 | +sub services { |
| 62 | + my ($self) = @_; |
| 63 | + |
| 64 | + my $services = $self->category_mapping; |
| 65 | + |
| 66 | + my @services = map { |
| 67 | + my $service = $services->{$_}; |
| 68 | + my $name = $service->{name}; |
| 69 | + |
| 70 | + $self->service_class->new( |
| 71 | + service_name => $name, |
| 72 | + service_code => $_, |
| 73 | + description => $name, |
| 74 | + keywords => ['waste_only'], |
| 75 | + $service->{group} ? ( group => $service->{group} ) : (), |
| 76 | + $service->{groups} ? ( groups => $service->{groups} ) : (), |
| 77 | + ); |
| 78 | + } keys %$services; |
| 79 | + |
| 80 | + return @services; |
| 81 | +} |
| 82 | + |
| 83 | +sub post_service_request { |
| 84 | + my ( $self, $service, $args ) = @_; |
| 85 | + |
| 86 | + $self->logger->info( |
| 87 | + "post_service_request(" . $service->service_code . ")" ); |
| 88 | + $self->logger->debug( |
| 89 | + "post_service_request arguments: " . encode_json($args) ); |
| 90 | + |
| 91 | + my $integration = $self->get_integration; |
| 92 | + |
| 93 | + my $is_free = $integration->IsAddressFree( $args->{attributes}{uprn} ); |
| 94 | + |
| 95 | + if ( $is_free->{IsFree} eq 'True' ) { |
| 96 | + my $res = $integration->SignUp( { |
| 97 | + Firstname => $args->{first_name}, |
| 98 | + Surname => $args->{last_name}, |
| 99 | + Email => $args->{email}, |
| 100 | + TelNumber => $args->{phone} || '', |
| 101 | + TitleCode => 'Default', |
| 102 | + CustomerExternalReference => '', |
| 103 | + ServiceContractUPRN => $args->{attributes}{uprn}, |
| 104 | + WasteContainerQuantity => int( $args->{attributes}{new_containers} ) || 1, |
| 105 | + AlreadyHasBinQuantity => int( $args->{attributes}{current_containers} ) || 0, |
| 106 | + PaymentReference => $args->{attributes}{PaymentCode}, |
| 107 | + PaymentMethodCode => |
| 108 | + PAYMENT_METHOD_MAPPING->{ $args->{attributes}{payment_method} }, |
| 109 | + |
| 110 | + # Used for FMS report ID |
| 111 | + ActionReference => $args->{attributes}{fixmystreet_id}, |
| 112 | + |
| 113 | + # TODO |
| 114 | + DirectDebitDate => '', |
| 115 | + DirectDebitReference => '', |
| 116 | + } ); |
| 117 | + |
| 118 | + # Expected response: |
| 119 | + # { |
| 120 | + # CustomerExternalReference: string, |
| 121 | + # CustomerReference: string, |
| 122 | + # ServiceContractReference: string |
| 123 | + # } |
| 124 | + my $request = $self->new_request( |
| 125 | + service_request_id => $res->{ServiceContractReference}, |
| 126 | + ); |
| 127 | + |
| 128 | + return $request; |
| 129 | + |
| 130 | + } else { |
| 131 | + die 'UPRN ' |
| 132 | + . $args->{attributes}{uprn} |
| 133 | + . ' already has a subscription or is invalid'; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +1; |
0 commit comments