Skip to content

Commit

Permalink
Support Transfer Reversals (#20)
Browse files Browse the repository at this point in the history
Adds support for reversing existing transfers via a new create_reversal method.
  • Loading branch information
danschmidt5189 committed Jul 14, 2015
1 parent 9d0a6bf commit 1d7b192
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Makefile.PL
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v5.037.
# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v5.036.
use strict;
use warnings;

Expand Down Expand Up @@ -36,7 +36,7 @@ my %WriteMakefileArgs = (
"strict" => 0,
"warnings" => 0
},
"VERSION" => "0.1100",
"VERSION" => "0.1200",
"test" => {
"TESTS" => "t/*.t"
}
Expand Down
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WebService::Stripe - Stripe API bindings

# VERSION

version 0.1100
version 0.1200

# SYNOPSIS

Expand Down Expand Up @@ -243,6 +243,31 @@ Example:

cancel_transfer($id)

## create\_reversal

Reverses an existing transfer.

[Stripe Documentation](https://stripe.com/docs/api/python#transfer_reversals)

Example:

$ws_stripe->create_reversal(
# Transfer ID (required)
$xfer_id,
data => {
# POST data (optional)
refund_application_fee => 'true',
amount => 100,
description => 'Invoice Correction',
'metadata[local_reversal_id]' => 'rvrsl_123',
'metadata[requester]' => 'John Doe'
},
headers => {
# Headers (optional)
stripe_account => $account->{'id'}
}
);

## get\_balance

get_balance()
Expand Down
2 changes: 1 addition & 1 deletion dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = Perl_5
copyright_holder = Tilt, Inc
copyright_year = 2014

version = 0.1100
version = 0.1200

[@Filter]
-bundle = @Basic
Expand Down
31 changes: 31 additions & 0 deletions lib/WebService/Stripe.pm
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ method cancel_transfer(Str $id, :$headers) {
return $self->post("/v1/transfers/$id/cancel", undef, headers => $headers);
}

method create_reversal($xfer_id, HashRef :$data = {}, HashRef :$headers = {}) {
return $self->post("/v1/transfers/$xfer_id/reversals", $data,
headers => $headers,
);
}

method get_bitcoin_receivers(HashRef :$query, :$headers) {
return $self->get( "/v1/bitcoin/receivers", $query, headers => $headers );
}
Expand Down Expand Up @@ -428,6 +434,31 @@ Example:
cancel_transfer($id)
=head2 create_reversal
Reverses an existing transfer.
L<Stripe Documentation|https://stripe.com/docs/api/python#transfer_reversals>
Example:
$ws_stripe->create_reversal(
# Transfer ID (required)
$xfer_id,
data => {
# POST data (optional)
refund_application_fee => 'true',
amount => 100,
description => 'Invoice Correction',
'metadata[local_reversal_id]' => 'rvrsl_123',
'metadata[requester]' => 'John Doe'
},
headers => {
# Headers (optional)
stripe_account => $account->{'id'}
}
);
=head2 get_balance
get_balance()
Expand Down
89 changes: 78 additions & 11 deletions t/04-transfers.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Test::Modern;
use Test::Modern qw(:deeper :fatal :more);
use t::lib::Common qw(:constants skip_unless_has_secret stripe);
use JSON qw(from_json);

Expand All @@ -17,39 +17,106 @@ my $bank = stripe->add_bank(
},
account_id => $account->{id},
);
cmp_deeply $bank => TD->superhashof({ last4 => 6789 }), 'created bank';
cmp_deeply $bank => superhashof({ last4 => 6789 }), 'created bank';

subtest 'create a transfer and do stuff with it' => sub {
my $transfer = stripe->create_transfer({
amount => 100,
currency => 'cad',
destination => $account->{id},
});
cmp_deeply $transfer => TD->superhashof({
id => TD->re('^tr_'),
cmp_deeply $transfer => superhashof({
id => re('^tr_'),
amount => 100,
});
my $transfer_id = $transfer->{id};
}),
'... Created a transfer';

my $transfer_id = $transfer->{id};
$transfer = stripe->update_transfer($transfer->{id}, data => {
'metadata[foo]' => 'bar'
});
is $transfer->{id} => $transfer_id;
is $transfer->{id}, $transfer_id,
'... Updated a transfer';

$transfer = stripe->get_transfer($transfer->{id});
cmp_deeply $transfer => TD->superhashof({
cmp_deeply $transfer => superhashof({
id => $transfer_id,
amount => 100,
metadata => { foo => 'bar' },
});
}),
'... Got an existing transfer';

my $exc = exception { stripe->cancel_transfer($transfer->{id}) };
is $exc->code => 400;
# Expect failure b/c Stripe's test env doesn't support this
my $err = exception { stripe->cancel_transfer($transfer->{id}) };
like $err, qr/while they are pending/,
'... Sent cancel request to Stripe';
};

subtest 'list transfers' => sub {
my $transfers = stripe->get_transfers;
ok $transfers->{data}[0]{amount};
};

subtest 'create_reversal' => sub {
subtest "Can create a complete reversal" => sub {
my $xfer = stripe->create_transfer({
amount => 100,
currency => 'cad',
destination => $account->{'id'},
'metadata[tester]' => 'WebService::Stripe::create_reversal',
});

my $reversal = stripe->create_reversal($xfer->{'id'});
cmp_deeply $reversal, superhashof({
object => 'transfer_reversal',
amount => 100,
}),
'... Created a full reversal',
or diag explain $reversal;
};

subtest "Can create a partial reversal" => sub {
my $xfer = stripe->create_transfer({
amount => 50,
currency => 'cad',
destination => $account->{'id'},
});

my $reversal = stripe->create_reversal($xfer->{'id'},
data => {
amount => 25,
}
);
cmp_deeply $reversal, superhashof({
object => 'transfer_reversal',
amount => 25,
}),
'... Created a 50% reversal',
or diag explain $reversal;
};

subtest "Can reverse an Account-scoped bank transfer" => sub {
my $xfer = stripe->create_transfer({
amount => 50,
currency => 'cad',
destination => $bank->{'id'},
}, headers => { stripe_account => $account->{'id'} });

my $err = exception {
stripe->create_reversal($xfer->{'id'},
data => {
amount => 25,
},
headers => {
stripe_account => $account->{'id'}
}
);
};

# Expect failure b/c Stripe's test environment doesn't support this
like $err, qr/while they are pending/,
'... Sent reversal request to Stripe';
};
};

done_testing;

0 comments on commit 1d7b192

Please sign in to comment.