Business-iDEAL-Mollie
view release on metacpan or search on metacpan
lib/Business/iDEAL/Mollie.pm view on Meta::CPAN
package Business::iDEAL::Mollie;
use strict;
use warnings;
use Carp;
use LWP::UserAgent;
use XML::Simple;
our $VERSION = '0.01';
sub new {
my ($class, $args) = @_;
$args ||= {};
$class->_croak("Options must be a hash reference")
if ref($args) ne 'HASH';
my $self = {};
bless $self, $class;
$self->_init($args) or return undef;
return $self;
}
sub _init {
my ($self, $args) = @_;
my $ua = LWP::UserAgent->new(
agent => __PACKAGE__." v. $VERSION",
);
my %options = (
baseurl => 'http://www.mollie.nl/xml/ideal',
ua => $ua,
%{ $args },
);
$self->{"_$_"} = $options{$_} foreach (%options);
return $self;
}
sub banklist {
my $self = shift;
my $res = $self->{'_ua'}->get($self->{'_baseurl'}.'?a=banklist');
if ($res->is_success) {
return _parse_output($res->decoded_content)->{'bank'};
} else {
$self->{'_error'} = $res->status_line;
}
}
sub fetch {
my ($self, $parms) = @_;
$parms ||= {};
$self->_croak("Parameters must be in a hash reference")
if ref($parms) ne 'HASH';
# Check for mandatory input
foreach(qw/partnerid amount bank_id description reporturl returnurl/) {
$self->_croak("Mandatory parameter $_ not found!") unless($parms->{$_});
}
# Make sure amount is in cents
$parms->{'amount'} =~ s/[\.,]//g;
# Put the action value in $parms
$parms->{'a'} = 'fetch';
my $res = $self->{'_ua'}->post($self->{'_baseurl'}, $parms);
if ($res->is_success) {
return _parse_output($res->decoded_content)->{'order'};
} else {
$self->{'_error'} = $res->status_line;
}
}
sub check {
my ($self, $parms) = @_;
$parms ||= {};
$self->_croak("Parameters must be in a hash reference")
if ref($parms) ne 'HASH';
# Check for mandatory input
foreach(qw/partnerid transaction_id/) {
$self->_croak("Mandatory parameter $_ not found!") unless($parms->{$_});
}
# Put the action value in $parms
$parms->{'a'} = 'check';
my $res = $self->{'_ua'}->post($self->{'_baseurl'}, $parms);
if ($res->is_success) {
return _parse_output($res->decoded_content)->{'order'};
} else {
$self->{'_error'} = $res->status_line;
}
}
sub is_payed {
my ($self, $parms) = @_;
my $resp = $self->check($parms);
return ($resp->{'payed'} eq 'true') ? 1 : 0;
}
sub error {
my $self = shift;
return $self->{'_error'};
}
sub _parse_output {
my $input = shift;
return unless($input);
my $xso = new XML::Simple();
return $xso->XMLin($input);
}
sub _croak {
my ($self, @error) = @_;
Carp::croak(@error);
}
1;
__END__
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
Business::iDEAL::Mollie - Backend for iDEAL payments through mollie.nl
=head1 SYNOPSIS
use strict;
use Business::iDEAL::Mollie;
my $mollie = new Business::iDEAL::Mollie;
# First you will have to grab the bank list (the list might
# change, so make sure you do this).
my $banks = $mollie->banklist;
# Then, you probably want to feed your user the URL of
# the right bank.
my $resp = $mollie->fetch({
partnerid => 'your partner id',
amount => '1250',
bank_id => $banks->[2]{'bank_id'},
description=> 'Acme Labs: order# foo-bar-123'
reporturl => 'http://your.site.tld/mollie.cgi',
returnurl => 'http://your.site.tld/thanks.html'
});
( run in 0.692 second using v1.01-cache-2.11-cpan-8dfa8b56332 )