Skip to content

Commit e8b157b

Browse files
authored
Merge pull request #179 from sherrardb/capture-partial-charge
capture partial charge
2 parents 49d450f + 6e931ca commit e8b157b

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

README.pod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ L<https://stripe.com/docs/api/charges/capture#capture_charge>
236236

237237
=item * charge - L<Net::Stripe::Charge> or Str - charge to capture
238238

239+
=item * amount - Int - amount to capture
240+
239241
=back
240242

241243
Returns a L<Net::Stripe::Charge>.
@@ -1086,6 +1088,13 @@ Charge->status.
10861088

10871089
Added type and client_ip attributes for L<Net::Stripe::Token>.
10881090

1091+
=item allow capture of partial charge
1092+
1093+
Passing 'amount' to capture_charge() allows capture of a partial charge.
1094+
Per the API, any remaining amount is immediately refunded. The charge object
1095+
now also has a 'refunds' attribute, representing a L<Net::Stripe::List>
1096+
of L<Net::Stripe::Refund> objects for the charge.
1097+
10891098
=back
10901099

10911100
=head1 SEE ALSO

lib/Net/Stripe.pm

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ Charge->status.
182182
183183
Added type and client_ip attributes for L<Net::Stripe::Token>.
184184
185+
=item allow capture of partial charge
186+
187+
Passing 'amount' to capture_charge() allows capture of a partial charge.
188+
Per the API, any remaining amount is immediately refunded. The charge object
189+
now also has a 'refunds' attribute, representing a L<Net::Stripe::List>
190+
of L<Net::Stripe::Refund> objects for the charge.
191+
185192
=back
186193
187194
=method new PARAMHASH
@@ -312,6 +319,8 @@ L<https://stripe.com/docs/api/charges/capture#capture_charge>
312319
313320
=item * charge - L<Net::Stripe::Charge> or Str - charge to capture
314321
322+
=item * amount - Int - amount to capture
323+
315324
=back
316325
317326
Returns a L<Net::Stripe::Charge>.
@@ -403,12 +412,18 @@ Charges: {
403412
);
404413
}
405414

406-
method capture_charge(Net::Stripe::Charge|Str :$charge) {
415+
method capture_charge(
416+
Net::Stripe::Charge|Str :$charge!,
417+
Int :$amount?,
418+
) {
407419
if (ref($charge)) {
408420
$charge = $charge->id;
409421
}
410422

411-
return $self->_post("charges/$charge/capture");
423+
my %args = (
424+
amount => $amount,
425+
);
426+
return $self->_post("charges/$charge/capture", \%args);
412427
}
413428

414429
}
@@ -1848,6 +1863,27 @@ sub _pre_2011_08_01_processing {
18481863
$hash->{$type}->{url} = "/v1/customers/$customer_id/$type";
18491864
}
18501865
}
1866+
1867+
foreach my $type ( qw/ refunds / ) {
1868+
if ( exists( $hash->{$type} ) && ref( $hash->{$type} ) eq 'ARRAY' ) {
1869+
$hash->{$type} = _array_to_list( delete( $hash->{$type} ) );
1870+
my $charge_id;
1871+
if ( exists( $hash->{object} ) && $hash->{object} eq 'charge' && exists( $hash->{id} ) ) {
1872+
$charge_id = $hash->{id};
1873+
}
1874+
# Net::Stripe::List->new() will fail without url, but we
1875+
# can make debugging easier by providing a message here
1876+
die Net::Stripe::Error->new(
1877+
type => "object coercion error",
1878+
message => sprintf(
1879+
"Could not determine charge id while coercing %s list into Net::Stripe::List.",
1880+
$type,
1881+
),
1882+
) unless $charge_id;
1883+
# mimic the url sent with standard Stripe lists
1884+
$hash->{$type}->{url} = "/v1/charges/$charge_id/$type";
1885+
}
1886+
}
18511887
return $hash;
18521888
}
18531889

lib/Net/Stripe/Charge.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ has 'receipt_email' => (is => 'ro', isa => 'Maybe[Str]');
2828
has 'status' => (is => 'ro', isa => 'Maybe[Str]');
2929
has 'capture' => (is => 'ro', isa => 'Bool', default=> 1);
3030
has 'statement_descriptor' => (is => 'ro', isa => 'Maybe[Str]');
31+
has 'refunds' => (is => 'ro', isa => 'Net::Stripe::List');
3132

3233
method form_fields {
3334
return $self->form_fields_for(

t/live.t

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,46 @@ Charges: {
493493
is $captured_charge->amount, $amount, "amount is $amount";
494494
is $captured_charge->id, $charge->id, 'Charge ids match';
495495
}
496+
497+
Auth_then_partial_capture: {
498+
my $amount = 1234;
499+
my $charge = $stripe->post_charge(
500+
amount => $amount,
501+
currency => 'usd',
502+
card => $token_id_visa,
503+
capture => 0,
504+
);
505+
isa_ok $charge, 'Net::Stripe::Charge';
506+
ok !$charge->refunded, 'charge is not refunded';
507+
ok $charge->paid, 'charge was paid';
508+
ok !$charge->captured, 'charge was not captured';
509+
ok !defined( $charge->balance_transaction ), 'balance_transaction is undef';
510+
is $charge->amount, $amount, "amount matches";
511+
my $refunds = $charge->refunds;
512+
isa_ok $refunds, "Net::Stripe::List";
513+
my @refunds = $refunds->elements;
514+
is scalar( @refunds ), 0, 'charge has no refunds';
515+
516+
my $auth_charge_id = $charge->id;
517+
my $partial = 567;
518+
my $captured_charge = $stripe->capture_charge(
519+
charge => $auth_charge_id,
520+
amount => $partial,
521+
);
522+
ok !$captured_charge->refunded, 'charge is not refunded';
523+
ok $captured_charge->paid, 'charge was paid';
524+
ok $captured_charge->captured, 'charge was captured';
525+
ok defined( $captured_charge->balance_transaction ), 'balance_transaction is defined';
526+
is $captured_charge->amount, $amount, "amount matches";
527+
is $captured_charge->id, $charge->id, 'Charge ids match';
528+
is $captured_charge->amount_refunded, $amount - $partial, "amount_refunded matches";
529+
$refunds = $captured_charge->refunds;
530+
isa_ok $refunds, "Net::Stripe::List";
531+
@refunds = $refunds->elements;
532+
is scalar( @refunds ), 1, 'charge has one refund';
533+
is $refunds[0]->amount, $amount - $partial, "refund amount matches";
534+
is $refunds[0]->status, 'succeeded', 'refund was successful';
535+
}
496536
}
497537

498538
Customers: {

0 commit comments

Comments
 (0)