Skip to content

Commit cb271ac

Browse files
committed
add metadata for Coupon
* clean up and augment unit tests * update POD
1 parent df4b926 commit cb271ac

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

lib/Net/Stripe/Coupon.pm

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ has 'duration' => (is => 'rw', isa => 'Maybe[Str]', required => 1);
1212
has 'duration_in_months' => (is => 'rw', isa => 'Maybe[Int]');
1313
has 'max_redemptions' => (is => 'rw', isa => 'Maybe[Int]');
1414
has 'redeem_by' => (is => 'rw', isa => 'Maybe[Int]');
15+
has 'metadata' => (is => 'ro', isa => 'Maybe[HashRef]');
1516

1617
method form_fields {
1718
return $self->form_fields_for(
18-
qw/id percent_off duration duration_in_months max_redemptions redeem_by/
19+
qw/id percent_off duration duration_in_months max_redemptions redeem_by
20+
metadata/
1921
);
2022
}
2123

t/live.t

+28-16
Original file line numberDiff line numberDiff line change
@@ -172,39 +172,51 @@ Plans: {
172172
Coupons: {
173173
Basic_successful_use: {
174174
my $id = "coupon-$future_ymdhms";
175-
my $coupon = $stripe->post_coupon(
175+
my %coupon_args = (
176176
id => $id,
177177
percent_off => 50,
178178
duration => 'repeating',
179179
duration_in_months => 3,
180180
max_redemptions => 5,
181181
redeem_by => time() + 100,
182+
metadata => {
183+
'somemetadata' => 'hello world',
184+
},
182185
);
183-
isa_ok $coupon, 'Net::Stripe::Coupon',
184-
'I love it when a coupon comes together';
185-
is $coupon->id, $id, 'coupon id is the same';
186+
my $coupon = $stripe->post_coupon( %coupon_args );
187+
isa_ok $coupon, 'Net::Stripe::Coupon';
188+
for my $f ( sort( keys( %coupon_args ) ) ) {
189+
is_deeply $coupon->$f, $coupon_args{$f}, "coupon $f matches";
190+
}
186191

187192
my $newcoupon = $stripe->get_coupon(coupon_id => $id);
188-
isa_ok $newcoupon, 'Net::Stripe::Coupon',
189-
'I love it when another coupon comes together';
193+
isa_ok $newcoupon, 'Net::Stripe::Coupon';
190194
is $newcoupon->id, $id, 'coupon id was encoded correctly';
191-
is($newcoupon->$_, $coupon->$_, "$_ matches")
192-
for qw/id percent_off duration duration_in_months
193-
max_redemptions redeem_by/;
195+
for my $f ( sort( keys( %coupon_args ) ) ) {
196+
is_deeply $newcoupon->$f, $coupon->$f, "$f matches for both coupon";
197+
}
194198

195199
my $coupons = $stripe->get_coupons(limit => 1);
196200
is scalar(@{$coupons->data}), 1, 'got just one coupon';
197201
is $coupons->get(0)->id, $id, 'coupon id matches';
198202

199203
my $hash = $stripe->delete_coupon($coupon);
200204
ok $hash->{deleted}, 'delete response indicates delete was successful';
201-
# swallow the expected warning rather than have it print out durring tests.
202-
close STDERR;
203-
open(STDERR, ">", "/dev/null");
204-
eval { $stripe->get_coupon(coupon_id => $id) };
205-
ok $@, "no longer can fetch deleted coupons";
206-
close STDERR;
207-
open(STDERR, ">&", STDOUT);
205+
is $hash->{id}, $id, 'deleted id is correct';
206+
eval {
207+
# swallow the expected warning rather than have it print out during tests.
208+
local $SIG{__WARN__} = sub {};
209+
$stripe->get_coupon(coupon_id => $id);
210+
};
211+
if ($@) {
212+
my $e = $@;
213+
isa_ok $e, 'Net::Stripe::Error', 'error raised is an object';
214+
is $e->type, 'invalid_request_error', 'error type';
215+
is $e->message, "No such coupon: $id", 'error message';
216+
} else {
217+
fail "no longer can fetch deleted coupons";
218+
219+
}
208220
}
209221
}
210222

0 commit comments

Comments
 (0)