Business-Stripe-Webhook

 view release on metacpan or  search on metacpan

t/04-reply.t  view on Meta::CPAN

my $webhook = Business::Stripe::Webhook->new(
    payload       => $payload,
    'invoice-paid' => sub { $handled = 1; },
);

$webhook->process();

ok( $handled, 'callback ran' );

my $output = capture_reply($webhook);
ok( $output =~ /\AContent-type: application\/json\n\n/s, 'reply includes content-type header' );

my (undef, $body) = split(/\n\n/, $output, 2);
my $data = decode_json($body);

is( $data->{'status'}, 'noaction', 'default status is noaction' );
is_deeply( $data->{'sent_to'}, ['invoice-paid'], 'sent_to includes handler' );
is( $data->{'sent_to_all'}, 'false', 'sent_to_all defaults to false' );
like( $data->{'timestamp'}, qr/\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\z/, 'timestamp present' );

my $webhook_nohandler = Business::Stripe::Webhook->new(
    payload => $payload,
);

$webhook_nohandler->process();

my $output_nohandler = capture_reply($webhook_nohandler);
ok( $output_nohandler =~ /\AContent-type: application\/json\n\n/s, 'reply includes content-type header without args' );

my (undef, $body_nohandler) = split(/\n\n/, $output_nohandler, 2);
my $data_nohandler = decode_json($body_nohandler);

is( $data_nohandler->{'status'}, 'noaction', 'default status is noaction' );
is_deeply( $data_nohandler->{'sent_to'}, [], 'sent_to empty when no handler' );
is( $data_nohandler->{'sent_to_all'}, 'false', 'sent_to_all remains false' );
like( $data_nohandler->{'timestamp'}, qr/\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\z/, 'timestamp present without args' );

my $sig_handled = 0;

t/08-get-subscription.t  view on Meta::CPAN

    request => sub {
        my ($self, $method, $url, $args) = @_;
        $called{method}  = $method;
        $called{url}     = $url;
        $called{headers} = $args->{headers};
        return {
            success => 1,
            status  => 200,
            reason  => 'OK',
            content => '{"id":"sub_123","status":"active"}',
            headers => { 'content-type' => 'application/json' },
        };
    },
);

my $response = $webhook->get_subscription('sub_123');

ok( $response->{success}, 'Successful response' );
is( $called{method}, 'GET', 'Uses GET method' );
is( $called{url}, 'https://api.stripe.com/v1/subscriptions/sub_123', 'Uses subscription endpoint' );
is( $called{headers}->{Authorization}, 'Bearer sk_test_123', 'Uses API secret header' );

t/08-get-subscription.t  view on Meta::CPAN

is( $data->{id}, 'sub_123', 'Parsed JSON subscription id' );
is( $data->{status}, 'active', 'Parsed JSON subscription status' );

$mock->redefine(
    request => sub {
        return {
            success => 0,
            status  => 400,
            reason  => 'Bad Request',
            content => '{"error":"invalid subscription"}',
            headers => { 'content-type' => 'application/json' },
        };
    },
);

my $fail_response = $webhook->get_subscription('sub_bad');

ok( !$fail_response->{success}, 'Failure response' );
like( $fail_response->{content}, qr/invalid subscription/, 'Failure surfaces error content' );

done_testing();



( run in 1.776 second using v1.01-cache-2.11-cpan-524268b4103 )