Business-Stripe-Webhook

 view release on metacpan or  search on metacpan

lib/Business/Stripe/Webhook.pm  view on Meta::CPAN

      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;
  }
      

=head2 Errors and Warnings

By default, any errors or warnings are sent to C<STDERR>.  These can be altered to instead go to your own subroutine to handle errors and/or warnings by defining these when create the object.

  my $webhook = Business::Stripe::Webhook->new(
      invoice-paid => \&sub_to_handle_paid_invoice,
      error        => \&my_error_handler,
      warning      => \&my_warning_handler,
  );

Additionally, warnings can be turned off by setting the C<warning> parameter to C<nowarn>.  Errors cannot be turned off.

=head1 METHODS

=head2 new

Creates a new Business::Stripe::Webhook object.

  my $webhook = Business::Stripe::Webhook->new(
      signing_secret => 'whsec_...',
      payload        => $payload,
  );

This method takes one or more parameters:

=over

=item *

B<signing_secret>: The webhook signing secret provided by Stripe. If omitted, the Stripe Signature will not be checked.

=item *

B<payload>: A JSON string.  Required (I<see below>). The JSON object from Stripe.

=item *

B<api_secret>: The Stripe secret API Key - see L<https://stripe.com/docs/keys>. Optional but will be required if the C<get_subscription> method is needed.

=item *

B<I<stripe-event>>: One or more callbacks to the subroutines to handle the webhooks events sent by Stripe.  See L<https://stripe.com/docs/api/events/list>.

To listen for an event, change the fullstop in the Stripe event name to a minus sign and use that as the parameter.  The events you define should match the events you ask Stripe to send.  Any events Stripe sends that do not have a callback defined wi...

Stripe event C<invoice.paid> becomes C<invoice-paid>
Stripe event C<invoice.payment_failed> becomes C<invoice-payment_failed>

=item *

B<all-webhooks>: A callback subroutine which will be called for every event received from Stripe even if a callback subroutine for that event has not been defined.

=item *

B<error>: A callback subroutine to handle errors.  If not defined, errors are sent to C<STDERR>.

=item *

B<warning>: A callback subroutine to handle warnings.  If not defined, warnings are sent to C<STDERR>.  If set to C<nowarn>, warnings are ignored.


=back

Previous versions on L<Business::Stripe::Webhook> allowed the B<payload> parameter to be omitted.  In this case, the module would read C<STDIN> to obtain the JSON string.  This continues to work for backward compatibility only but will be removed fro...

=head2 success

Returns true if the last operation was successful, or false otherwise.

  if ($webhook->success()) {
      ...
  }

=head2 error

Returns the error message from the last operation, or an empty string if there was no error.

  my $error = $webhook->error();

=head2 process

This method processes the webhook sent from Stripe.  It checks the Stripe Signature if a C<signing_secret> parameter has been included and calls the defined subroutine to handle the Stripe event.  Each subroutine is passed a JSON decoded Event Object...

  my $result = $webhook->process();

This method takes no parameters.

Normally, the return value can be ignored.  Returns C<undef> if there was an error or warning.

If the C<v1> parameter is missing an C<error> is set and the method returns C<undef>.  Otherwise it returns true if the signature has been verified or false if not.

=head2 check_signature

Checks the signature of the webhook to verify that it was sent by Stripe.

  my $sig_ok = $webhook->check_signature();

This method takes no parameters.

Normally, this method does not need to be called.  It is called by the C<process> method if a C<signing_secret> parameter was included when the object was created.

=head2 reply



( run in 0.856 second using v1.01-cache-2.11-cpan-f56aa216473 )