Skip to content

Commit ea3d1af

Browse files
committed
Merge pull request #23 from andrewsolomon/master
add $customer->discount->coupon attributes
2 parents e2e82db + b0a1b43 commit ea3d1af

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

lib/Net/Stripe.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use Net::Stripe::Plan;
1414
use Net::Stripe::Coupon;
1515
use Net::Stripe::Charge;
1616
use Net::Stripe::Customer;
17+
use Net::Stripe::Discount;
1718
use Net::Stripe::Subscription;
1819
use Net::Stripe::SubscriptionList;
1920
use Net::Stripe::Error;

lib/Net/Stripe/Customer.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ has 'trial_end' => (is => 'rw', isa => 'Maybe[Int|Str]');
1010
has 'card' => (is => 'rw', isa => 'Maybe[StripeCard]');
1111
has 'plan' => (is => 'rw', isa => 'Maybe[StripePlan|Str]');
1212
has 'coupon' => (is => 'rw', isa => 'Maybe[StripeCoupon]');
13+
has 'discount' => (is => 'rw', isa => 'Maybe[Net::Stripe::Discount]');
1314
has 'metadata' => (is => 'rw', isa => 'HashRef');
1415

1516
# API object args

lib/Net/Stripe/Discount.pm

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package Net::Stripe::Discount;
2+
use Moose;
3+
use Moose::Util::TypeConstraints;
4+
use methods;
5+
extends 'Net::Stripe::Resource';
6+
7+
has 'coupon' => (is => 'rw', isa => 'Maybe[Net::Stripe::Coupon]');
8+
9+
10+
=head1 NAME
11+
12+
Net::Stripe::Discount
13+
14+
=head1 SEE ALSO
15+
16+
L<https://stripe.com>, L<https://stripe.com/docs/api>
17+
18+
=head1 AUTHORS
19+
20+
Luke Closs
21+
22+
=head1 LICENSE
23+
24+
Net-Stripe is Copyright 2011 Prime Radiant, Inc.
25+
Net-Stripe is distributed under the same terms as Perl itself.
26+
27+
=cut
28+
29+
__PACKAGE__->meta->make_immutable;
30+
1;

lib/Net/Stripe/Resource.pm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ around BUILDARGS => sub {
3434
$args{subscription} = Net::Stripe::Subscription->new($s);
3535
}
3636
}
37+
if (my $s = $args{coupon}) {
38+
if (ref($s) eq 'HASH') {
39+
$args{coupon} = Net::Stripe::Coupon->new($s);
40+
}
41+
}
42+
if (my $s = $args{discount}) {
43+
if (ref($s) eq 'HASH') {
44+
$args{discount} = Net::Stripe::Discount->new($s);
45+
}
46+
}
3747
if (my $p = $args{plan}) {
3848
if (ref($p) eq 'HASH') {
3949
$args{plan} = Net::Stripe::Plan->new($p);

t/live.t

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,41 @@ Customers: {
371371
is $other_dsubs->status, 'active', 'subscription is still active';
372372
ok $other_dsubs->canceled_at, 'has canceled_at';
373373
ok !$other_dsubs->ended_at, 'does not have ended_at (not at period end yet)';
374+
375+
my $priceyplan = $stripe->post_plan(
376+
id => "pricey-$future_ymdhms",
377+
amount => 1000,
378+
currency => 'usd',
379+
interval => 'year',
380+
name => "Priceyplan $future_ymdhms",
381+
);
382+
ok $priceyplan->id, 'priceyplan created';
383+
my $coupon_id = "priceycoupon-$future_ymdhms";
384+
my $coupon = $stripe->post_coupon(
385+
id => $coupon_id,
386+
percent_off => 100,
387+
duration => 'once',
388+
max_redemptions => 1,
389+
redeem_by => time() + 100,
390+
);
391+
isa_ok $coupon, 'Net::Stripe::Coupon',
392+
'I love it when a coupon pays for the first month';
393+
is $coupon->id, $coupon_id, 'coupon id is the same';
394+
$customer->coupon($coupon->id);
395+
$stripe->post_customer($customer);
396+
$customer = $stripe->get_customer($customer->id);
397+
is $customer->discount->coupon->id, $coupon_id,
398+
'got the coupon';
399+
my $priceysubs = $stripe->post_subscription(
400+
customer_id => $customer->id,
401+
plan => $priceyplan->id,
402+
);
403+
isa_ok $priceysubs, 'Net::Stripe::Subscription',
404+
'got a subscription back';
405+
is $priceysubs->plan->id, $priceyplan->id;
406+
$customer = $stripe->get_customer($customer->id);
407+
is $customer->subscriptions->data->[0]->plan->id,
408+
$priceyplan->id, 'subscribed without a creditcard';
374409
}
375410
}
376411
}

0 commit comments

Comments
 (0)