-
Notifications
You must be signed in to change notification settings - Fork 35
/
stripe__balance_transactions.sql
297 lines (252 loc) · 11 KB
/
stripe__balance_transactions.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
with balance_transaction as (
select *
from {{ var('balance_transaction') }}
), account as (
select *
from {{ var('account') }}
), cards as (
select *
from {{ var('card') }}
), charge as (
select *
from {{ var('charge') }}
), customer as (
select *
from {{ var('customer') }}
), dispute as (
select *
from {{ var('dispute') }}
{% if var('stripe__using_invoices', True) %}
), invoice as (
select *
from {{ var('invoice') }}
{% endif %}
), payment_intent as (
select *
from {{ var('payment_intent') }}
{% if var('stripe__using_payment_method', True) %}
), payment_method as (
select *
from {{ var('payment_method') }}
), payment_method_card as (
select *
from {{ var('payment_method_card')}}
{% endif %}
), payout as (
select *
from {{ var('payout') }}
), refund as (
select *
from {{ var('refund') }}
{% if var('stripe__using_subscriptions', True) %}
), subscription as (
select *
from {{ var('subscription') }}
{% endif %}
), transfers as (
select *
from {{ var('transfer') }}
), dispute_summary as (
/* Although rare, payments can be disputed multiple times.
Hence, we need to aggregate the disputes to get the total disputed amount.
*/
select
charge_id,
source_relation,
{{ fivetran_utils.string_agg('dispute_id', "','")}} as dispute_ids,
{{ fivetran_utils.string_agg('distinct dispute_reason', "','")}} as dispute_reasons,
count(dispute_id) as dispute_count
from dispute
group by 1,2
), order_disputes as (
select
charge_id,
source_relation,
dispute_id,
dispute_status,
dispute_amount,
row_number() over (partition by charge_id, dispute_status, source_relation order by dispute_created_at desc) = 1 as is_latest_status_dispute,
row_number() over (partition by charge_id, source_relation order by dispute_created_at desc, dispute_amount desc) = 1 as is_absolute_latest_dispute -- include dispute_amount desc in off chance of identical dispute_created_ats
from dispute
), latest_disputes as (
select
charge_id,
source_relation,
-- Iterate over each type of possible status (according https://docs.stripe.com/api/disputes/object) and pull out the dispute_amount from the latest dispute
{% for status in ['won', 'lost', 'under_review', 'needs_response', 'warning_closed', 'warning_under_review', 'warning_needs_response'] %}
sum(case when lower(dispute_status) = '{{ status }}' then dispute_amount else 0 end) as latest_dispute_amount_{{ status }},
{% endfor %}
-- For the customer_facing_amount fields, pull out the generally latest dispute_amount
sum(case when is_absolute_latest_dispute then dispute_amount else 0 end) as latest_dispute_amount
from order_disputes
where is_latest_status_dispute
group by 1,2
)
select
balance_transaction.balance_transaction_id,
balance_transaction.created_at as balance_transaction_created_at,
balance_transaction.available_on as balance_transaction_available_on,
balance_transaction.currency as balance_transaction_currency,
balance_transaction.amount as balance_transaction_amount,
balance_transaction.fee as balance_transaction_fee,
balance_transaction.net as balance_transaction_net,
balance_transaction.source as balance_transaction_source_id,
balance_transaction.description as balance_transaction_description,
balance_transaction.type as balance_transaction_type,
coalesce(balance_transaction.reporting_category,
case
when balance_transaction.type in ('charge', 'payment') then 'charge'
when balance_transaction.type in ('refund', 'payment_refund') then 'refund'
when balance_transaction.type in ('payout_cancel', 'payout_failure') then 'payout_reversal'
when balance_transaction.type in ('transfer', 'recipient_transfer') then 'transfer'
when balance_transaction.type in ('transfer_cancel', 'transfer_failure', 'recipient_transfer_cancel', 'recipient_transfer_failure') then 'transfer_reversal'
else balance_transaction.type end)
as balance_transaction_reporting_category,
case
when balance_transaction.type in ('charge', 'payment') then charge.amount
when balance_transaction.type in ('refund', 'payment_refund') then refund.amount
when dispute_ids is not null then latest_disputes.latest_dispute_amount
else null
end as customer_facing_amount,
case
when balance_transaction.type = 'charge' then charge.currency
end as customer_facing_currency,
latest_disputes.latest_dispute_amount_won,
latest_disputes.latest_dispute_amount_lost,
latest_disputes.latest_dispute_amount_under_review,
latest_disputes.latest_dispute_amount_needs_response,
latest_disputes.latest_dispute_amount_warning_closed,
latest_disputes.latest_dispute_amount_warning_under_review,
latest_disputes.latest_dispute_amount_warning_needs_response,
{{ dbt.dateadd('day', 1, 'balance_transaction.available_on') }} as effective_at,
case
when payout.is_automatic = true then payout.payout_id
else null
end as automatic_payout_id,
payout.payout_id,
payout.created_at as payout_created_at,
payout.currency as payout_currency,
payout.is_automatic as payout_is_automatic,
payout.arrival_date_at as payout_arrival_date_at,
case
when payout.is_automatic = true then payout.arrival_date_at
else null
end as automatic_payout_effective_at,
payout.type as payout_type,
payout.status as payout_status,
payout.description as payout_description,
payout.destination_bank_account_id,
payout.destination_card_id,
coalesce(charge.customer_id, refund_charge.customer_id) as customer_id,
charge.receipt_email,
customer.email as customer_email,
customer.customer_name,
customer.description as customer_description,
customer.shipping_address_line_1 as customer_shipping_address_line_1,
customer.shipping_address_line_2 as customer_shipping_address_line_2,
customer.shipping_address_city as customer_shipping_address_city,
customer.shipping_address_state as customer_shipping_address_state,
customer.shipping_address_postal_code as customer_shipping_address_postal_code,
customer.shipping_address_country as customer_shipping_address_country,
customer.customer_address_line_1,
customer.customer_address_line_2,
customer.customer_address_city,
customer.customer_address_state,
customer.customer_address_postal_code,
customer.customer_address_country,
charge.shipping_address_line_1 as charge_shipping_address_line_1,
charge.shipping_address_line_2 as charge_shipping_address_line_2,
charge.shipping_address_city as charge_shipping_address_city,
charge.shipping_address_state as charge_shipping_address_state,
charge.shipping_address_postal_code as charge_shipping_address_postal_code,
charge.shipping_address_country as charge_shipping_address_country,
cards.card_address_line_1,
cards.card_address_line_2,
cards.card_address_city,
cards.card_address_state,
cards.card_address_postal_code,
cards.card_address_country,
coalesce(charge.charge_id, refund.charge_id, dispute_summary.charge_id) as charge_id,
charge.created_at as charge_created_at,
payment_intent.payment_intent_id,
{% if var('stripe__using_invoices', True) %}
invoice.invoice_id,
invoice.number as invoice_number,
{% endif %}
{% if var('stripe__using_subscriptions', True) %}
subscription.subscription_id,
{% endif %}
{% if var('stripe__using_payment_method', True) %}
payment_method.type as payment_method_type,
payment_method_card.brand as payment_method_brand,
payment_method_card.funding as payment_method_funding,
{% endif %}
cards.brand as card_brand,
cards.funding as card_funding,
cards.country as card_country,
charge.statement_descriptor as charge_statement_descriptor ,
dispute_summary.dispute_ids,
dispute_summary.dispute_reasons,
dispute_summary.dispute_count,
refund.refund_id,
refund.reason as refund_reason,
transfers.transfer_id,
coalesce(balance_transaction.connected_account_id, charge.connected_account_id) as connected_account_id,
connected_account.country as connected_account_country,
case
when charge.connected_account_id is not null then charge.charge_id
else null
end as connected_account_direct_charge_id,
balance_transaction.source_relation
from balance_transaction
left join payout
on payout.balance_transaction_id = balance_transaction.balance_transaction_id
and payout.source_relation = balance_transaction.source_relation
left join account connected_account
on balance_transaction.connected_account_id = connected_account.account_id
and balance_transaction.source_relation = connected_account.source_relation
left join charge
on charge.balance_transaction_id = balance_transaction.balance_transaction_id
and charge.source_relation = balance_transaction.source_relation
left join customer
on charge.customer_id = customer.customer_id
and charge.source_relation = customer.source_relation
left join cards
on charge.card_id = cards.card_id
and charge.source_relation = cards.source_relation
left join payment_intent
on charge.payment_intent_id = payment_intent.payment_intent_id
and charge.source_relation = payment_intent.source_relation
{% if var('stripe__using_payment_method', True) %}
left join payment_method
on charge.payment_method_id = payment_method.payment_method_id
and charge.source_relation = payment_method.source_relation
left join payment_method_card
on payment_method_card.payment_method_id = payment_method.payment_method_id
and charge.source_relation = balance_transaction.source_relation
{% endif %}
{% if var('stripe__using_invoices', True) %}
left join invoice
on charge.invoice_id = invoice.invoice_id
and charge.source_relation = invoice.source_relation
{% endif %}
{% if var('stripe__using_subscriptions', True) %}
left join subscription
on subscription.latest_invoice_id = charge.invoice_id
and subscription.source_relation = charge.source_relation
{% endif %}
left join refund
on refund.balance_transaction_id = balance_transaction.balance_transaction_id
and refund.source_relation = balance_transaction.source_relation
left join transfers
on transfers.balance_transaction_id = balance_transaction.balance_transaction_id
and transfers.source_relation = balance_transaction.source_relation
left join charge as refund_charge
on refund.charge_id = refund_charge.charge_id
and refund.source_relation = refund_charge.source_relation
left join dispute_summary
on charge.charge_id = dispute_summary.charge_id
and charge.source_relation = dispute_summary.source_relation
left join latest_disputes
on charge.charge_id = latest_disputes.charge_id
and charge.source_relation = latest_disputes.source_relation