| 1 | package plenigo::CustomersManager; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | plenigo::CustomersManager - A utility class to handle customers. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use plenigo::CustomersManager; | ||
| 10 | |||
| 11 | # Prepare configuration | ||
| 12 | |||
| 13 | my $use_stage = 0; # set if stage system should be used | ||
| 14 | my $configuration = plenigo::Configuration->new(access_token => 'ACCESS_TOKEN', use_stage => $use_stage); | ||
| 15 | |||
| 16 | # Instantiate customers manager | ||
| 17 | |||
| 18 | my $customers_manager = plenigo::CustomersManager->new(configuration => $configuration); | ||
| 19 | |||
| 20 | # Register a new external customer | ||
| 21 | |||
| 22 | my %customer_details = $customers_manager->registerCustomer('newuser@example.com', 'DE', '123456789', 'MR', 'Mike', 'Miller'); | ||
| 23 | |||
| 24 | # Change email address of an exiting customer | ||
| 25 | |||
| 26 | $customers_manager->editCustomer('CUSTOMER_ID', 'newmail@example.com') | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | plenigo::CustomersManager provides functionality to manage customers. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | use Moo; | ||
| 35 | use Carp qw(confess); | ||
| 36 | use plenigo::Ex; | ||
| 37 | use plenigo::RestClient; | ||
| 38 | |||
| 39 | our $VERSION = '3.0002'; | ||
| 40 | |||
| 41 | has configuration => ( | ||
| 42 | is => 'ro', | ||
| 43 | required => 1 | ||
| 44 | ); | ||
| 45 | |||
| 46 | =head2 registerCustomer($email, $language, $customer_id, $salutation, $first_name, $last_name) | ||
| 47 | |||
| 48 | Register a new customer in the plenigo system. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub registerCustomer { | ||
| 53 | my ($self, $email, $language, $customer_id, $salutation, $first_name, $last_name) = @_; | ||
| 54 | |||
| 55 | my $rest_client = plenigo::RestClient->new(configuration => $self->configuration); | ||
| 56 | my %result = $rest_client->post('customers', {}, ( | ||
| 57 | customerId => $customer_id, | ||
| 58 | email => $email, | ||
| 59 | language => $language, | ||
| 60 | gender => $salutation, | ||
| 61 | addresses => ( | ||
| 62 | "type" => "INVOICE", | ||
| 63 | "preferred" => 1, | ||
| 64 | firstName => $first_name, | ||
| 65 | lastName => $last_name, | ||
| 66 | ) | ||
| 67 | )); | ||
| 68 | if ($result{'response_code'} == 409) { | ||
| 69 | plenigo::Ex->throw({ code => 409, message => 'A customer with the given email already exists. If you want to avoid this error, set the fail_by_existing_email flag to false.' }); | ||
| 70 | } | ||
| 71 | if ($result{'response_code'} == 429) { | ||
| 72 | plenigo::Ex->throw({ code => 429, message => 'A customer registration with the same email address can only be done once every 5 seconds.' }); | ||
| 73 | } | ||
| 74 | else { | ||
| 75 | return %{$result{'response_content'}}; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | =head2 editCustomer($customer_id, $email) | ||
| 80 | |||
| 81 | Edit a customer in the plenigo system. | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub editCustomer { | ||
| 86 | my ($self, $customer_id, $email) = @_; | ||
| 87 | |||
| 88 | my $rest_client = plenigo::RestClient->new(configuration => $self->configuration); | ||
| 89 | my %result = $rest_client->put('customers/' . $customer_id, (email => $email)); | ||
| 90 | if ($result{'response_code'} == 404) { | ||
| 91 | plenigo::Ex->throw({ code => 404, message => 'There is no customer for the customer id passed.' }); | ||
| 92 | } | ||
| 93 | |||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | 1; |