Business-GoCardless

 view release on metacpan or  search on metacpan

t/business/gocardless/billingrequest.t  view on Meta::CPAN

#!perl

use strict;
use warnings;

use Test::Most;
use Test::Deep;
use Test::Exception;
use Test::MockObject;
use JSON ();

use Business::GoCardless::Client;

use_ok( 'Business::GoCardless::BillingRequest' );
isa_ok(
    my $BillingRequest = Business::GoCardless::BillingRequest->new(
        client => Business::GoCardless::Client->new(
            token       => 'foo',
            api_version => 2,
            base_url    => 'https://api.gocardless.com',
        ),
        id     => 'BRQ123',
        links  => {
            customer                                        => 'CU123',
            instalment_schedule_request_instalment_schedule => 'IS123',
            mandate_request_mandate                         => 'MD123',
            payment_request_payment                         => 'PM123',
            subscription_request_subscription               => 'SB123',
        },
    ),
    'Business::GoCardless::BillingRequest'
);

can_ok(
    $BillingRequest,
    qw/
        endpoint
        actions
        auto_fulfil
        created_at
        creditor_address
        creditor_identifier
        creditor_name
        creditor_scheme_identifier
        experimentation
        fallback_enabled
        fallback_occurred
        id
        instalment_schedule_request
        links
        mandate_request
        metadata
        payment_context_code
        payment_purpose_code
        payment_request
        purpose_code
        resources
        sign_flow_url
        status
        subscription_request

        collect_customer_details
        collect_bank_account
        confirm_payer_details
        fulfil
        cancel
        notify
        fallback
        choose_currency
        select_institution

        pending
        ready_to_fulfil
        fulfilling
        fulfilled
        cancelled

        customer_id
        instalment_schedule_id
        mandate_id
        payment_id
        subscription_id

        create_flow
        institutions
    /,
);

is( $BillingRequest->endpoint,'/billing_requests/%s','endpoint' );
is( $BillingRequest->uri,'https://api.gocardless.com/billing_requests/BRQ123','->uri' );

note( "status checks" );

ok( ! $BillingRequest->pending,'! ->pending when no status' );

my @statuses = qw/ pending ready_to_fulfil fulfilling fulfilled cancelled /;

foreach my $status ( @statuses ) {
    $BillingRequest->status( $status );
    ok( $BillingRequest->$status,"->$status" );
    ok( ! $BillingRequest->$_,"! ->$_ when $status" )
        for grep { $_ ne $status } @statuses;
}

note( "links" );

is( $BillingRequest->customer_id,'CU123','->customer_id' );
is( $BillingRequest->instalment_schedule_id,'IS123','->instalment_schedule_id' );
is( $BillingRequest->mandate_id,'MD123','->mandate_id' );
is( $BillingRequest->payment_id,'PM123','->payment_id' );
is( $BillingRequest->subscription_id,'SB123','->subscription_id' );

$BillingRequest->links( undef );
ok( ! $BillingRequest->customer_id,'! ->customer_id when no links' );

note( "operations" );

# monkey patching LWP here to make this test work without
# having to actually hit the endpoints or use credentials
no warnings 'redefine';
no warnings 'once';
my $mock = Test::MockObject->new;
my $request;
$mock->mock( 'is_success',sub { 1 } );
$mock->mock( 'header',sub {} );
*LWP::UserAgent::request = sub { $request = $_[1]; $mock };

$mock->mock( 'content',sub { '{"billing_requests":{"id":"BRQ123","status":"after collect_customer_details"}}' } );

isa_ok(
    $BillingRequest->collect_customer_details( customer => { email => 'lee@example.com' },customer_billing_detail => { country_code => 'GB' } ),
    'Business::GoCardless::BillingRequest',
    '->collect_customer_details'
);
is( $request->method,'POST','->collect_customer_details POSTs' );
like( $request->uri,qr!/billing_requests/BRQ123/actions/collect_customer_details$!,'->collect_customer_details request path' );
cmp_deeply(
    JSON->new->decode( $request->content ),
    { data => { customer => { email => 'lee@example.com' },customer_billing_detail => { country_code => 'GB' } } },
    '->collect_customer_details request body'
);
is( $BillingRequest->status,'after collect_customer_details','->collect_customer_details updates the object' );

$mock->mock( 'content',sub { '{"billing_requests":{"id":"BRQ123","status":"after collect_bank_account"}}' } );

isa_ok(
    $BillingRequest->collect_bank_account( account_holder_name => 'Lee Johnson',iban => 'GB60BARC20000055779911' ),
    'Business::GoCardless::BillingRequest',
    '->collect_bank_account'
);
is( $request->method,'POST','->collect_bank_account POSTs' );
like( $request->uri,qr!/billing_requests/BRQ123/actions/collect_bank_account$!,'->collect_bank_account request path' );
cmp_deeply(
    JSON->new->decode( $request->content ),
    { data => { account_holder_name => 'Lee Johnson',iban => 'GB60BARC20000055779911' } },
    '->collect_bank_account request body'
);
is( $BillingRequest->status,'after collect_bank_account','->collect_bank_account updates the object' );

$mock->mock( 'content',sub { '{"billing_requests":{"id":"BRQ123","status":"after confirm_payer_details"}}' } );

isa_ok(
    $BillingRequest->confirm_payer_details,
    'Business::GoCardless::BillingRequest',
    '->confirm_payer_details'
);
is( $request->method,'POST','->confirm_payer_details POSTs' );
like( $request->uri,qr!/billing_requests/BRQ123/actions/confirm_payer_details$!,'->confirm_payer_details request path' );
ok( ! length( $request->content ),'->confirm_payer_details has no request body' );
is( $BillingRequest->status,'after confirm_payer_details','->confirm_payer_details updates the object' );

$mock->mock( 'content',sub { '{"billing_requests":{"id":"BRQ123","status":"after fulfil"}}' } );

isa_ok(
    $BillingRequest->fulfil,
    'Business::GoCardless::BillingRequest',
    '->fulfil'
);
is( $request->method,'POST','->fulfil POSTs' );



( run in 0.703 second using v1.01-cache-2.11-cpan-99fa8243a42 )