Business-Stripe-Webhook
view release on metacpan or search on metacpan
lib/Business/Stripe/Webhook.pm view on Meta::CPAN
if (defined $self->{'error_callback'}) {
&{$self->{'error_callback'}}($message);
} else {
STDERR->print("Stripe Webhook Error: $message\n");
}
}
sub _warning {
my ($self, $message) = @_;
return if $self->{'warning'} and $self->{'warning'} =~ /^nowarn/i;
$self->{'error'} = $message;
if (defined $self->{'warning'}) {
&{$self->{'warning'}}($message);
} else {
STDERR->print("Stripe Webhook Warning: $message\n");
}
}
__END__
=pod
=encoding UTF-8
=head1 NAME
Business::Stripe::Webhook - A Perl module for handling webhooks sent by Stripe
=head1 VERSION
Version 1.14
=head1 SYNOPSIS
use Business::Stripe::Webhook;
my $payload;
read(STDIN, $payload, $ENV{'CONTENT_LENGTH'});
my $webhook = Business::Stripe::Webhook->new(
signing_secret => 'whsec_...',
api_secret => 'sk_test_...',
payload => $payload,
invoice-paid => \&update_invoice,
checkout-session-completed => \&update_session,
);
die $webhook->error unless $webhook->success;
my $result = $webhook->process();
if ($webhook->success()) {
$webhook->reply(status => 'OK');
} else {
$webhook->reply(error => $webhook->error());
}
sub update_invoice {
# Process paid invoice
...
}
sub update_session {
# Process checkout
...
}
=head1 DESCRIPTION
L<Business::Stripe::Webhook> is a Perl module that provides an interface for handling webhooks sent by Stripe. It provides a simple way to verify the signature of the webhook, and allows the user to define a number of methods for processing specific ...
This module is designed to run on a webserver as that is where Stripe webhooks would typically be sent. It reads the payload sent from Stripe from C<STDIN> because Stripe sends an HTTP C<POST> request. Ensure that no other module is reading from C<...
=head2 Workflow
The typical workflow for L<Business::Stripe::Webhook> is to initially create an instance of the module and to define one or more Stripe events to listen for. This is done by providing references to your subroutines as part of the C<new> method. Not...
my $webhook = Business::Stripe::Webhook->new(
invoice-paid => \&sub_to_handle_paid_invoice,
);
The Stripe event names have a fullstop replaced with a minus sign. So, C<invoice.paid> becomes C<invoice-paid>.
Next is to process the L<webhook|https://stripe.com/docs/webhooks> from L<Stripe|https://stripe.com/>.
my $result = $webhook->process();
This will call the subroutines that were defined when the module was created and pass them the event object from Stripe.
Finally, a reply is sent back to L<Stripe|https://stripe.com/>.
print $webhook->reply(status => 'OK');
This produces a fully formed HTTP Response complete with headers as required by Stripe.
=head2 Reply time
Stripe requires a timely reply to webhook calls. Therefore, if you need to carry out any lengthy processing after the webhook has been sent, this should be done B<after> calling the C<reply> method and flushing C<STDOUT>
use Business::Stripe::Webhook;
my $webhook = Business::Stripe::Webhook->new(
signing_secret => 'whsec_...',
payload => $payload,
invoice-paid => \&update_invoice,
);
$webhook->process();
# Send reply for unhandled webhooks
$webhook->reply();
sub invoice-paid {
# Send reply quickly and flush buffer
print $webhook->reply();
select()->flush();
# Process paid invoice which will take time then do not return
...
exit;
}
( run in 1.021 second using v1.01-cache-2.11-cpan-7fcb06a456a )