Skip to content

Commit

Permalink
Merge pull request #26 from solid-software/coupon_resource_2
Browse files Browse the repository at this point in the history
Add `CouponResource`
  • Loading branch information
yurii-prykhodko-solid authored Aug 9, 2024
2 parents f2820a5 + e0dc00e commit 9013819
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 60 deletions.
2 changes: 2 additions & 0 deletions lib/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ part 'src/messages/requests/create_price.dart';
part 'src/messages/requests/create_product.dart';
part 'src/messages/requests/create_refund.dart';
part 'src/messages/requests/create_subscription_schedule.dart';
part 'src/messages/requests/created.dart';
part 'src/messages/requests/list_coupons.dart';
part 'src/messages/requests/list_prices.dart';
part 'src/messages/requests/list_products.dart';
part 'src/messages/requests/list_promotion_codes.dart';
Expand Down
79 changes: 51 additions & 28 deletions lib/messages.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions lib/src/messages/requests/created.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
part of '../../../messages.dart';

@JsonSerializable()
class CreatedRequest {
/// Minimum value to filter by (exclusive).
final int? gt;

/// Minimum value to filter by (inclusive).
final int? gte;

/// Maximum value to filter by (exclusive).
final int? lt;

/// Maximum value to filter by (inclusive).
final int? lte;

CreatedRequest({
this.gt,
this.gte,
this.lt,
this.lte,
});

factory CreatedRequest.fromJson(Map<String, dynamic> json) =>
_$CreatedRequestFromJson(json);

Map<String, dynamic> toJson() => _$CreatedRequestToJson(this);
}
40 changes: 40 additions & 0 deletions lib/src/messages/requests/list_coupons.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
part of '../../../messages.dart';

/// https://docs.stripe.com/api/coupons/list
@JsonSerializable()
class ListCouponsRequest {
/// A filter on the list, based on the object created field. The value can be
/// a string with an integer Unix timestamp, or it can be a dictionary with a
/// number of different query options.
final CreatedRequest? created;

/// A cursor for use in pagination. ending_before is an object ID that defines
/// your place in the list. For instance, if you make a list request and
/// receive 100 objects, starting with obj_bar, your subsequent call can
/// include ending_before=obj_bar in order to fetch the previous page of the
/// list.
final String? endingBefore;

/// A limit on the number of objects to be returned. Limit can range between
/// 1 and 100, and the default is 10.
final int? limit;

/// A cursor for use in pagination. starting_after is an object ID that
/// defines your place in the list. For instance, if you make a list request
/// and receive 100 objects, ending with obj_foo, your subsequent call can
/// include starting_after=obj_foo in order to fetch the next page of the
/// list.
final String? startingAfter;

ListCouponsRequest({
this.created,
this.endingBefore,
this.limit,
this.startingAfter,
});

factory ListCouponsRequest.fromJson(Map<String, dynamic> json) =>
_$ListCouponsRequestFromJson(json);

Map<String, dynamic> toJson() => _$ListCouponsRequestToJson(this);
}
32 changes: 1 addition & 31 deletions lib/src/messages/requests/list_promotion_codes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ListPromotionCodesRequest {
/// A filter on the list, based on the object created field. The value can be
/// a string with an integer Unix timestamp, or it can be a dictionary with a
/// number of different query options.
final ListPromotionCodesCreatedRequest? created;
final CreatedRequest? created;

/// Only return promotion codes that are restricted to this customer.
final String? customer;
Expand Down Expand Up @@ -54,33 +54,3 @@ class ListPromotionCodesRequest {

Map<String, dynamic> toJson() => _$ListPromotionCodesRequestToJson(this);
}

/// https://docs.stripe.com/api/promotion_codes/list#list_promotion_code-created
@JsonSerializable()
class ListPromotionCodesCreatedRequest {
/// Minimum value to filter by (exclusive).
final int? gt;

/// Minimum value to filter by (inclusive).
final int? gte;

/// Maximum value to filter by (exclusive).
final int? lt;

/// Maximum value to filter by (inclusive).
final int? lte;

ListPromotionCodesCreatedRequest({
this.gt,
this.gte,
this.lt,
this.lte,
});

factory ListPromotionCodesCreatedRequest.fromJson(
Map<String, dynamic> json) =>
_$ListPromotionCodesCreatedRequestFromJson(json);

Map<String, dynamic> toJson() =>
_$ListPromotionCodesCreatedRequestToJson(this);
}
22 changes: 22 additions & 0 deletions lib/src/resources/coupon.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'dart:async';

import 'package:stripe/messages.dart';

import '../client.dart';
import '_resource.dart';

class CouponResource extends Resource<Coupon> {
static const _resourceName = 'coupons';

CouponResource(Client client) : super(client);

Future<DataList<Coupon>> list([
ListCouponsRequest? request,
]) async {
final map = await get(_resourceName, queryParameters: request?.toJson());
return DataList<Coupon>.fromJson(
map,
(value) => Coupon.fromJson(value as Map<String, dynamic>),
);
}
}
7 changes: 6 additions & 1 deletion lib/stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'src/client.dart';
import 'src/resources/balance_transaction.dart';
import 'src/resources/charge.dart';
import 'src/resources/checkout_session.dart';
import 'src/resources/coupon.dart';
import 'src/resources/customer.dart';
import 'src/resources/payment_intent.dart';
import 'src/resources/portal_session.dart';
Expand Down Expand Up @@ -75,6 +76,9 @@ class Stripe {
/// https://docs.stripe.com/api/promotion_codes
final PromotionCodeResource promotionCode;

/// https://docs.stripe.com/api/coupons
final CouponResource coupon;

factory Stripe(String apiKey) {
final client = DioClient(apiKey: apiKey);
return Stripe.withClient(client);
Expand All @@ -93,5 +97,6 @@ class Stripe {
subscriptionSchedule = SubscriptionScheduleResource(client),
charge = ChargeResource(client),
balanceTransaction = BalanceTransactionResource(client),
promotionCode = PromotionCodeResource(client);
promotionCode = PromotionCodeResource(client),
coupon = CouponResource(client);
}

0 comments on commit 9013819

Please sign in to comment.