Tobias Oetiker
and 1 contributors

NAME

Mojolicious::Plugin::Pingen - Print Package Send Physical letters

DESCRIPTION

Mojolicious::Plugin::Pingen is a plugin for the Mojolicious web framework which allows you to do communicate with pingen.com.

This module is EXPERIMENTAL. The API can change at any time. Let me know if you are using it.

SYNOPSIS

Production mode

    use Mojolicious::Lite;
    plugin Pingen => { apikey => $ENV{SUPER_SECRET_PINGEN_KEY} };

    post '/send' => sub {
        my $c = shift;
        $c->delay(
            sub { $c->pingen->document->upload($c->param('pdf'), shift->begin) },
            sub {
                my ($delay, $res) = @_;
                return $c->reply->exception($res->{errormessage}) if $res->{error};
                $c->pingen->document->send($res->{id},{
                    speed => 1
                },$delay->begin)
            },
            sub {
                my ($delay, $res) = @_;
                return $c->reply->exception($err) if $err;
                return $c->render(text => "Delivery of $res->{id} is scheduled!");
            }
        );
    );

Testing mode

  use Mojolicious::Lite;
  plugin Pingen => { mocked => 1 };

Setting mocked will enable this plugin to work without an actual connection to pingen.com. This is done by replicating the behavior of Pingen for those API we support. This is especially useful when writing unit tests.

The apikey for the mocked interface is: sk_test_super_secret_key

The following routes will be added to your application to mimic Pidgen:

  • POST /mocked/pingen/document/upload

  • POST /mocked/pingen/document/send

  • POST /mocked/pingen/document/delete

  • POST /mocked/pingen/send/cancel

ATTRIBUTES

base_url

  $str = $self->base_url;

This is the location to Stripe payment solution. Will be set to https://api.pingen.com/.

apikey

  $str = $self->apikey;

The value for the private API key. Available in the Stripe admin gui.

exceptions

This will cause exceptions to be thrown if there is any problem with submitting the invoice to pingen.

HELPERS

pingen.document.upload

    my $json =   $c->pingen->document->upload($asset[,\%args]);
    $c->pingen->document->upload($mojoUpload[,\%args], sub { my ($c, $json) = @_; });

Upload a Mojo::Upload object containint a (PDF file). Pingen will analyze the content of the pdf and figure out how and where to mail it.

$json is the response object from pingen.

%args can contain the following optional parameters:

  • color

    Print color: 0 = B/W, 1 = Color, 2 = Mixed (optimized)

  • duplex

    Paper hadling: 0 = simplex, 1 = duplex

  • rightaddress

    Where in the document is the address: 0 = Address left, 1 = Address right.

pingen.document.send

   my $json =   $c->pingen->document->send($id,\%args);
   $c->pingen->document->send($id,\%args, sub { my ($c, $json) = @_; });

Use the $id returned from the upload call to actually send the document.

$json is a response object from pingen.

%args can contain the following parameters:

  • speed

    Delivery Speed. Varies by country. Use https://pingen.com/en/developer/testapi-send.html to get a list of speeds for your country. In general 1 = Priority, 2 = Economy.

  • envelope

    If you have designed your own envelope in the pingen webinterface, hunt take the html inspector to the source code of the admin ui to determine the id of your envelope. You can then refer to it using that id.

pingen.document.delete

   my $json =   $c->pingen->document->delete($id);
   $c->pingen->document->delete($id, sub { my ($c, $json) = @_; });

Use the $id returned from upload call to delete the document. Note that this will not cancel any pending send tasks.

$json is a response object from pingen.

pingen.send.cancel

   my $json =   $c->pingen->send->cancel($id);
   $c->pingen->send->cancel($id, sub { my ($c, $json) = @_; });

cancel the given send task. Use the ID returned from the send call.

$err is a string describing the error. Will be empty string on success. $json is a response object from pingen.

METHODS

register

  $app->plugin(Pingen => \%config);

Called when registering this plugin in the main Mojolicious application.

SEE ALSO

ACKNOLEDGEMENT

Jan Henning Thorsen for his Mojolicious::Plugin::StripePayment from where I shamelessly copied the structure of this module. Thanks!

AUTHOR

Tobias Oetiker, <tobi@oetiker.ch>

COPYRIGHT

Copyright OETIKER+PARTNER AG 2015

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10 or, at your option, any later version of Perl 5 you may have available.

1;