Skip to content

Commit

Permalink
add synth discount stream, gets data from invoices (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
keyn4 authored Jul 17, 2024
1 parent d7f0596 commit ff37f8d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tap_stripe/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ def parse_response(self, response: requests.Response) -> Iterable[dict]:
# using prices API instead of plans API
if self.name == "plans":
url = base_url + f"/v1/prices/{record_id}"
# discounts is a synthetic stream, it uses data from invoices
if self.name == "discounts":
url = base_url + f"/v1/invoices/{record_id}"
else:
url = base_url + f"/v1/{self.name}/{record['id']}"
params = {}
Expand Down
75 changes: 75 additions & 0 deletions tap_stripe/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,3 +1379,78 @@ def path(self):
th.Property("reason", th.StringType),
th.Property("status", th.StringType),
).to_dict()


class Discounts(stripeStream):
"""Define Products stream."""

name = "discounts"
replication_key = "updated"
object = "invoice"
event_filter = "invoice.*"

@property
def path(self):
# get discounts from invoices and invoice line items
if not self.get_from_events:
return "invoices"
else:
return "events"


@property
def expand(self):
if self.get_from_events:
return ["discounts", "lines.data.discounts", "discounts.coupon.applies_to"]
else:
return ["data.discounts", "data.lines.data.discounts", "data.discounts.coupon.applies_to"]


schema = th.PropertiesList(
th.Property("id", th.StringType),
th.Property("object", th.StringType),
th.Property("checkout_session", th.StringType),
th.Property("coupon", th.ObjectType(
th.Property("id", th.StringType),
th.Property("object", th.StringType),
th.Property("amount_off", th.IntegerType),
th.Property("created", th.IntegerType),
th.Property("currency", th.StringType),
th.Property("duration", th.StringType),
th.Property("duration_in_months", th.IntegerType),
th.Property("max_redemptions", th.IntegerType),
th.Property("metadata", th.CustomType({"type": ["object", "array", "string"]})),
th.Property("name", th.StringType),
th.Property("percent_off", th.NumberType),
th.Property("redeem_by", th.IntegerType),
th.Property("times_redeemed", th.IntegerType),
th.Property("valid", th.BooleanType),
)),
th.Property("customer", th.StringType),
th.Property("end", th.DateTimeType),
th.Property("invoice", th.StringType),
th.Property("invoice_item", th.StringType),
th.Property("promotion_code", th.StringType),
th.Property("start", th.DateTimeType),
th.Property("subscription", th.BooleanType),
th.Property("subscription_item", th.StringType),
th.Property("updated", th.DateTimeType),
).to_dict()

def parse_response(self, response) -> Iterable[dict]:
response = super().parse_response(response)
discounts = []
for invoice in response:
invoice_discounts = []
updated = invoice["updated"]
# add header discounts to invoice discounts list
invoice_discounts.extend(invoice["discounts"])
# add line discounts to invoice discounts list
[invoice_discounts.extend(line["discounts"]) for line in invoice["lines"]["data"]]
# add updated rep key to all invoice discounts
[discount.update({"updated": updated}) for discount in invoice_discounts]
# add invoice discounts to discounts list
discounts.extend(invoice_discounts)
return discounts


2 changes: 2 additions & 0 deletions tap_stripe/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
RefundsStream,
PayoutReportsStream,
DisputesIssuingStream,
Discounts
)

STREAM_TYPES = [
Expand Down Expand Up @@ -61,6 +62,7 @@
RefundsStream,
PayoutReportsStream,
DisputesIssuingStream,
Discounts
]


Expand Down

0 comments on commit ff37f8d

Please sign in to comment.