revbank/plugins/revspace_mollie
Juerd Waalboer eed0db7897 Cleanup: use subroutine signatures, remove deprecated methods.
The signatures feature has been "experimental" since Perl 5.20 (May 2014), but
expected to stay. After 8 years I'm ready to take the risk :)

Have added Perl v5.28 (June 2018) as the minimum requirement, even though the
current revbank should work with 5.20, to see if this bothers any users. Perl
v5.28 is in Debian "buster", which is now oldstable.
2021-12-03 18:00:34 +01:00

59 lines
1.8 KiB
Perl

#!perl
use LWP::UserAgent;
use JSON;
my $ua = LWP::UserAgent->new(agent => "revbank");
my $backend_url = "https://deposit.revspace.nl/mollie.php";
sub backend_call($hash) {
#$hash->{test} = 1; # use mollie test environment
my $response = $ua->post($backend_url, $hash);
$response->is_success
or die "HTTP request failed (" . $response->status_line . ")\n";
my $result = eval { decode_json $response->decoded_content };
defined $result and ref($result) eq "HASH"
or die "Invalid JSON from HTTP request\n";
return $result;
}
sub command($self, $cart, $command, @) {
# currently 10 characters after the underscore, but it's not documented.
my ($id) = $command =~ /^(tr_[A-Za-z0-9]{10,12})$/ or return NEXT;
my $result = eval { backend_call { id => $id } };
$@ and return REJECT, "API call failed: $@";
$result->{ok} or return REJECT, "Voucher rejected: $result->{message}.";
my $description = "Deposit (online; $id)";
my $amount = $result->{amount};
if ($result->{test_amount}) {
$description .= " TEST MODE ($result->{test_amount})";
}
$cart->add(
+$amount,
$description,
{ is_deposit => 1, method => 'online', mollie_id => $id, no_repeat => 1 }
);
return ACCEPT;
}
sub hook_abort($class, $cart, $reason, @) {
# Opportunistic; ignore failures. Can't do anything about it anyway.
my @ids = map $_->attribute('mollie_id'), $cart->entries('mollie_id');
eval { print "Reactivating $_.\n"; backend_call { id => $_, action => "abort" } }
for @ids;
}
sub hook_checkout($class, $cart, $user, $transaction_id, @) {
# Opportunistic; ignore failures. Can't do anything about it anyway.
my @ids = map $_->attribute('mollie_id'), $cart->entries('mollie_id');
eval { backend_call { id => $_, action => "finalize" } } for @ids;
}