From 942e419b85f990199cc7758cb93eca962b4b9136 Mon Sep 17 00:00:00 2001 From: Rishi Gupta Date: Mon, 4 Feb 2019 12:18:47 -0800 Subject: [PATCH] Revert "webhooks/stripe: Handle invoice events explicitly." The original commit made a number of well-meaning but suboptimal product changes to the invoice events, such as threading them under the invoice id rather than the customer id. It also seems to be causing 500s for invoiceitem events, though I'm not sure why. This reverts commit 65489b03917f88030d3a029aa51db675cf9b326f. --- .../stripe/fixtures/invoice_created.json | 106 ---------- .../stripe/fixtures/invoice_deleted.json | 106 ---------- .../stripe/fixtures/invoice_finalized.json | 106 ---------- .../invoice_marked_uncollectible.json | 107 ---------- .../fixtures/invoice_payment_failed.json | 182 ++++++++---------- .../fixtures/invoice_payment_succeeded.json | 107 ---------- .../stripe/fixtures/invoice_sent.json | 106 ---------- .../stripe/fixtures/invoice_upcoming.json | 106 ---------- .../stripe/fixtures/invoice_updated.json | 110 ----------- .../stripe/fixtures/invoice_voided.json | 106 ---------- zerver/webhooks/stripe/tests.py | 65 +------ zerver/webhooks/stripe/view.py | 47 ++--- 12 files changed, 97 insertions(+), 1157 deletions(-) delete mode 100644 zerver/webhooks/stripe/fixtures/invoice_created.json delete mode 100644 zerver/webhooks/stripe/fixtures/invoice_deleted.json delete mode 100644 zerver/webhooks/stripe/fixtures/invoice_finalized.json delete mode 100644 zerver/webhooks/stripe/fixtures/invoice_marked_uncollectible.json delete mode 100644 zerver/webhooks/stripe/fixtures/invoice_payment_succeeded.json delete mode 100644 zerver/webhooks/stripe/fixtures/invoice_sent.json delete mode 100644 zerver/webhooks/stripe/fixtures/invoice_upcoming.json delete mode 100644 zerver/webhooks/stripe/fixtures/invoice_updated.json delete mode 100644 zerver/webhooks/stripe/fixtures/invoice_voided.json diff --git a/zerver/webhooks/stripe/fixtures/invoice_created.json b/zerver/webhooks/stripe/fixtures/invoice_created.json deleted file mode 100644 index cf1c8d03aa..0000000000 --- a/zerver/webhooks/stripe/fixtures/invoice_created.json +++ /dev/null @@ -1,106 +0,0 @@ -{ - "created": 1326853478, - "livemode": false, - "id": "invoice.created_00000000000000", - "type": "invoice.created", - "object": "event", - "request": null, - "pending_webhooks": 1, - "api_version": "2018-11-08", - "data": { - "object": { - "id": "in_00000000000000", - "object": "invoice", - "amount_due": 1000, - "amount_paid": 0, - "amount_remaining": 1000, - "application_fee": null, - "attempt_count": 0, - "attempted": false, - "auto_advance": true, - "billing": "send_invoice", - "billing_reason": "manual", - "charge": null, - "currency": "usd", - "custom_fields": null, - "customer": "cus_00000000000000", - "date": 1546862399, - "default_source": null, - "description": null, - "discount": null, - "due_date": 1549454425, - "ending_balance": 0, - "finalized_at": 1546862425, - "footer": null, - "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA", - "invoice_pdf": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA/pdf", - "lines": { - "data": [ - { - "id": "sli_00000000000000", - "object": "line_item", - "amount": 999, - "currency": "usd", - "description": "1 × Diamond Essentials (at $9.99 / month)", - "discountable": true, - "livemode": false, - "metadata": { - }, - "period": { - "end": 1551072697, - "start": 1548394297 - }, - "plan": { - "id": "plan_00000000000000", - "object": "plan", - "active": true, - "aggregate_usage": null, - "amount": 999, - "billing_scheme": "per_unit", - "created": 1548385214, - "currency": "usd", - "interval": "month", - "interval_count": 1, - "livemode": false, - "metadata": { - }, - "nickname": null, - "product": "prod_00000000000000", - "tiers": null, - "tiers_mode": null, - "transform_usage": null, - "trial_period_days": null, - "usage_type": "licensed" - }, - "proration": false, - "quantity": 1, - "subscription": "sub_00000000000000", - "subscription_item": "si_00000000000000", - "type": "subscription" - } - ], - "has_more": false, - "object": "list", - "url": "/v1/invoices/in_1DpwlvHOC2HiLzOV3LVuwMuC/lines" - }, - "livemode": false, - "metadata": { - }, - "next_payment_attempt": null, - "number": "7DBE74C-0001", - "paid": true, - "period_end": 1546862399, - "period_start": 1546862399, - "receipt_number": null, - "starting_balance": 0, - "statement_descriptor": null, - "status": "open", - "subscription": null, - "subtotal": 1000, - "tax": 0, - "tax_percent": null, - "total": 1000, - "webhooks_delivered_at": null - } - } -} diff --git a/zerver/webhooks/stripe/fixtures/invoice_deleted.json b/zerver/webhooks/stripe/fixtures/invoice_deleted.json deleted file mode 100644 index 55221f5bf9..0000000000 --- a/zerver/webhooks/stripe/fixtures/invoice_deleted.json +++ /dev/null @@ -1,106 +0,0 @@ -{ - "created": 1326853478, - "livemode": false, - "id": "invoice.deleted_00000000000000", - "type": "invoice.deleted", - "object": "event", - "request": null, - "pending_webhooks": 1, - "api_version": "2018-11-08", - "data": { - "object": { - "id": "in_00000000000000", - "object": "invoice", - "amount_due": 1000, - "amount_paid": 0, - "amount_remaining": 1000, - "application_fee": null, - "attempt_count": 0, - "attempted": false, - "auto_advance": true, - "billing": "send_invoice", - "billing_reason": "manual", - "charge": null, - "currency": "usd", - "custom_fields": null, - "customer": "cus_00000000000000", - "date": 1546862399, - "default_source": null, - "description": null, - "discount": null, - "due_date": 1549454425, - "ending_balance": 0, - "finalized_at": 1546862425, - "footer": null, - "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA", - "invoice_pdf": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA/pdf", - "lines": { - "data": [ - { - "id": "sli_00000000000000", - "object": "line_item", - "amount": 999, - "currency": "usd", - "description": "1 × Diamond Essentials (at $9.99 / month)", - "discountable": true, - "livemode": false, - "metadata": { - }, - "period": { - "end": 1551072770, - "start": 1548394370 - }, - "plan": { - "id": "plan_00000000000000", - "object": "plan", - "active": true, - "aggregate_usage": null, - "amount": 999, - "billing_scheme": "per_unit", - "created": 1548385214, - "currency": "usd", - "interval": "month", - "interval_count": 1, - "livemode": false, - "metadata": { - }, - "nickname": null, - "product": "prod_00000000000000", - "tiers": null, - "tiers_mode": null, - "transform_usage": null, - "trial_period_days": null, - "usage_type": "licensed" - }, - "proration": false, - "quantity": 1, - "subscription": "sub_00000000000000", - "subscription_item": "si_00000000000000", - "type": "subscription" - } - ], - "has_more": false, - "object": "list", - "url": "/v1/invoices/in_1DpwlvHOC2HiLzOV3LVuwMuC/lines" - }, - "livemode": false, - "metadata": { - }, - "next_payment_attempt": null, - "number": "7DBE74C-0001", - "paid": false, - "period_end": 1546862399, - "period_start": 1546862399, - "receipt_number": null, - "starting_balance": 0, - "statement_descriptor": null, - "status": "open", - "subscription": null, - "subtotal": 1000, - "tax": 0, - "tax_percent": null, - "total": 1000, - "webhooks_delivered_at": null - } - } -} diff --git a/zerver/webhooks/stripe/fixtures/invoice_finalized.json b/zerver/webhooks/stripe/fixtures/invoice_finalized.json deleted file mode 100644 index 7216e93663..0000000000 --- a/zerver/webhooks/stripe/fixtures/invoice_finalized.json +++ /dev/null @@ -1,106 +0,0 @@ -{ - "created": 1326853478, - "livemode": false, - "id": "invoice.finalized_00000000000000", - "type": "invoice.finalized", - "object": "event", - "request": null, - "pending_webhooks": 1, - "api_version": "2018-11-08", - "data": { - "object": { - "id": "in_00000000000000", - "object": "invoice", - "amount_due": 1000, - "amount_paid": 0, - "amount_remaining": 1000, - "application_fee": null, - "attempt_count": 0, - "attempted": false, - "auto_advance": true, - "billing": "send_invoice", - "billing_reason": "manual", - "charge": null, - "currency": "usd", - "custom_fields": null, - "customer": "cus_00000000000000", - "date": 1546862399, - "default_source": null, - "description": null, - "discount": null, - "due_date": 1549454425, - "ending_balance": 0, - "finalized_at": 1546862425, - "footer": null, - "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA", - "invoice_pdf": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA/pdf", - "lines": { - "data": [ - { - "id": "sli_00000000000000", - "object": "line_item", - "amount": 999, - "currency": "usd", - "description": "1 × Diamond Essentials (at $9.99 / month)", - "discountable": true, - "livemode": false, - "metadata": { - }, - "period": { - "end": 1551072823, - "start": 1548394423 - }, - "plan": { - "id": "plan_00000000000000", - "object": "plan", - "active": true, - "aggregate_usage": null, - "amount": 999, - "billing_scheme": "per_unit", - "created": 1548385214, - "currency": "usd", - "interval": "month", - "interval_count": 1, - "livemode": false, - "metadata": { - }, - "nickname": null, - "product": "prod_00000000000000", - "tiers": null, - "tiers_mode": null, - "transform_usage": null, - "trial_period_days": null, - "usage_type": "licensed" - }, - "proration": false, - "quantity": 1, - "subscription": "sub_00000000000000", - "subscription_item": "si_00000000000000", - "type": "subscription" - } - ], - "has_more": false, - "object": "list", - "url": "/v1/invoices/in_1DpwlvHOC2HiLzOV3LVuwMuC/lines" - }, - "livemode": false, - "metadata": { - }, - "next_payment_attempt": null, - "number": "7DBE74C-0001", - "paid": false, - "period_end": 1546862399, - "period_start": 1546862399, - "receipt_number": null, - "starting_balance": 0, - "statement_descriptor": null, - "status": "open", - "subscription": null, - "subtotal": 1000, - "tax": 0, - "tax_percent": null, - "total": 1000, - "webhooks_delivered_at": null - } - } -} diff --git a/zerver/webhooks/stripe/fixtures/invoice_marked_uncollectible.json b/zerver/webhooks/stripe/fixtures/invoice_marked_uncollectible.json deleted file mode 100644 index 22cf0675e7..0000000000 --- a/zerver/webhooks/stripe/fixtures/invoice_marked_uncollectible.json +++ /dev/null @@ -1,107 +0,0 @@ -{ - "created": 1326853478, - "livemode": false, - "id": "invoice.marked_00000000000000", - "type": "invoice.marked_uncollectible", - "object": "event", - "request": null, - "pending_webhooks": 1, - "api_version": "2018-11-08", - "data": { - "object": { - "id": "in_00000000000000", - "object": "invoice", - "amount_due": 1000, - "amount_paid": 0, - "amount_remaining": 1000, - "application_fee": null, - "attempt_count": 0, - "attempted": false, - "auto_advance": true, - "billing": "send_invoice", - "billing_reason": "manual", - "charge": null, - "currency": "usd", - "custom_fields": null, - "customer": "cus_00000000000000", - "date": 1546862399, - "default_source": null, - "description": null, - "discount": null, - "due_date": 1549454425, - "ending_balance": 0, - "finalized_at": 1546862425, - "footer": null, - "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA", - "invoice_pdf": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA/pdf", - "lines": { - "data": [ - { - "id": "sli_00000000000000", - "object": "line_item", - "amount": 999, - "currency": "usd", - "description": "1 × Diamond Essentials (at $9.99 / month)", - "discountable": true, - "livemode": false, - "metadata": { - }, - "period": { - "end": 1551072873, - "start": 1548394473 - }, - "plan": { - "id": "plan_00000000000000", - "object": "plan", - "active": true, - "aggregate_usage": null, - "amount": 999, - "billing_scheme": "per_unit", - "created": 1548385214, - "currency": "usd", - "interval": "month", - "interval_count": 1, - "livemode": false, - "metadata": { - }, - "nickname": null, - "product": "prod_00000000000000", - "tiers": null, - "tiers_mode": null, - "transform_usage": null, - "trial_period_days": null, - "usage_type": "licensed" - }, - "proration": false, - "quantity": 1, - "subscription": "sub_00000000000000", - "subscription_item": "si_00000000000000", - "type": "subscription" - } - ], - "has_more": false, - "object": "list", - "url": "/v1/invoices/in_1DpwlvHOC2HiLzOV3LVuwMuC/lines" - }, - "livemode": false, - "metadata": { - }, - "next_payment_attempt": null, - "number": "7DBE74C-0001", - "paid": false, - "period_end": 1546862399, - "period_start": 1546862399, - "receipt_number": null, - "starting_balance": 0, - "statement_descriptor": null, - "status": "open", - "subscription": null, - "subtotal": 1000, - "tax": 0, - "tax_percent": null, - "total": 1000, - "webhooks_delivered_at": null - } - } -} - diff --git a/zerver/webhooks/stripe/fixtures/invoice_payment_failed.json b/zerver/webhooks/stripe/fixtures/invoice_payment_failed.json index 8c07c4df44..40cd51a268 100644 --- a/zerver/webhooks/stripe/fixtures/invoice_payment_failed.json +++ b/zerver/webhooks/stripe/fixtures/invoice_payment_failed.json @@ -1,107 +1,83 @@ { - "created": 1326853478, - "livemode": false, - "id": "invoice.payment_00000000000000", - "type": "invoice.payment_failed", - "object": "event", - "request": null, - "pending_webhooks": 1, - "api_version": "2018-11-08", - "data": { - "object": { - "id": "in_00000000000000", - "object": "invoice", - "amount_due": 1000, - "amount_paid": 0, - "amount_remaining": 1000, - "application_fee": null, - "attempt_count": 0, - "attempted": true, - "auto_advance": true, - "billing": "send_invoice", - "billing_reason": "manual", - "charge": null, - "currency": "usd", - "custom_fields": null, - "customer": "cus_00000000000000", - "date": 1546862399, - "default_source": null, - "description": null, - "discount": null, - "due_date": 1549454425, - "ending_balance": 0, - "finalized_at": 1546862425, - "footer": null, - "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA", - "invoice_pdf": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA/pdf", - "lines": { - "data": [ - { - "id": "sli_00000000000000", - "object": "line_item", - "amount": 999, - "currency": "usd", - "description": "1 × Diamond Essentials (at $9.99 / month)", - "discountable": true, + "created": 1326853478, + "livemode": false, + "id": "evt_00000000000000", + "type": "invoice.payment_failed", + "object": "event", + "request": null, + "pending_webhooks": 1, + "api_version": "2016-07-06", + "data": { + "object": { + "id": "in_00000000000000", + "object": "invoice", + "amount_due": 0, + "application_fee": null, + "attempt_count": 0, + "attempted": true, + "charge": null, + "closed": false, + "currency": "aud", + "customer": "cus_00000000000000", + "date": 1480672129, + "description": null, + "discount": null, + "ending_balance": null, + "forgiven": false, + "lines": { + "data": [ + { + "id": "sub_9fYmJQOQy04yoh", + "object": "line_item", + "amount": 2000, + "currency": "aud", + "description": null, + "discountable": true, + "livemode": true, + "metadata": {}, + "period": { + "start": 1483350529, + "end": 1486028929 + }, + "plan": { + "id": "gold", + "object": "plan", + "amount": 2000, + "created": 1480672129, + "currency": "aud", + "interval": "month", + "interval_count": 1, + "livemode": false, + "metadata": {}, + "name": "Gold Special", + "statement_descriptor": null, + "trial_period_days": null + }, + "proration": false, + "quantity": 1, + "subscription": null, + "type": "subscription" + } + ], + "total_count": 1, + "object": "list", + "url": "/v1/invoices/in_19MDevCV4wXizEw4B4lY7YPR/lines" + }, "livemode": false, - "metadata": { - }, - "period": { - "end": 1551072969, - "start": 1548394569 - }, - "plan": { - "id": "plan_00000000000000", - "object": "plan", - "active": true, - "aggregate_usage": null, - "amount": 999, - "billing_scheme": "per_unit", - "created": 1548385214, - "currency": "usd", - "interval": "month", - "interval_count": 1, - "livemode": false, - "metadata": { - }, - "nickname": null, - "product": "prod_00000000000000", - "tiers": null, - "tiers_mode": null, - "transform_usage": null, - "trial_period_days": null, - "usage_type": "licensed" - }, - "proration": false, - "quantity": 1, - "subscription": "sub_00000000000000", - "subscription_item": "si_00000000000000", - "type": "subscription" - } - ], - "has_more": false, - "object": "list", - "url": "/v1/invoices/in_1DpwlvHOC2HiLzOV3LVuwMuC/lines" - }, - "livemode": false, - "metadata": { - }, - "next_payment_attempt": null, - "number": "7DBE74C-0001", - "paid": false, - "period_end": 1546862399, - "period_start": 1546862399, - "receipt_number": null, - "starting_balance": 0, - "statement_descriptor": null, - "status": "open", - "subscription": null, - "subtotal": 1000, - "tax": 0, - "tax_percent": null, - "total": 1000, - "webhooks_delivered_at": null, - "closed": false + "metadata": {}, + "next_payment_attempt": 1480675729, + "paid": false, + "period_end": 1480672129, + "period_start": 1480672129, + "receipt_number": null, + "starting_balance": 0, + "statement_descriptor": null, + "subscription": null, + "subtotal": 0, + "tax": null, + "tax_percent": null, + "total": 0, + "webhooks_delivered_at": null + } } - } } diff --git a/zerver/webhooks/stripe/fixtures/invoice_payment_succeeded.json b/zerver/webhooks/stripe/fixtures/invoice_payment_succeeded.json deleted file mode 100644 index 83919b9e49..0000000000 --- a/zerver/webhooks/stripe/fixtures/invoice_payment_succeeded.json +++ /dev/null @@ -1,107 +0,0 @@ -{ - "created": 1326853478, - "livemode": false, - "id": "invoice.payment_00000000000000", - "type": "invoice.payment_succeeded", - "object": "event", - "request": null, - "pending_webhooks": 1, - "api_version": "2018-11-08", - "data": { - "object": { - "id": "in_00000000000000", - "object": "invoice", - "amount_due": 1000, - "amount_paid": 0, - "amount_remaining": 1000, - "application_fee": null, - "attempt_count": 0, - "attempted": true, - "auto_advance": true, - "billing": "send_invoice", - "billing_reason": "manual", - "charge": "_00000000000000", - "currency": "usd", - "custom_fields": null, - "customer": "cus_00000000000000", - "date": 1546862399, - "default_source": null, - "description": null, - "discount": null, - "due_date": 1549454425, - "ending_balance": 0, - "finalized_at": 1546862425, - "footer": null, - "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA", - "invoice_pdf": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA/pdf", - "lines": { - "data": [ - { - "id": "sli_00000000000000", - "object": "line_item", - "amount": 999, - "currency": "usd", - "description": "1 × Diamond Essentials (at $9.99 / month)", - "discountable": true, - "livemode": false, - "metadata": { - }, - "period": { - "end": 1551073031, - "start": 1548394631 - }, - "plan": { - "id": "plan_00000000000000", - "object": "plan", - "active": true, - "aggregate_usage": null, - "amount": 999, - "billing_scheme": "per_unit", - "created": 1548385214, - "currency": "usd", - "interval": "month", - "interval_count": 1, - "livemode": false, - "metadata": { - }, - "nickname": null, - "product": "prod_00000000000000", - "tiers": null, - "tiers_mode": null, - "transform_usage": null, - "trial_period_days": null, - "usage_type": "licensed" - }, - "proration": false, - "quantity": 1, - "subscription": "sub_00000000000000", - "subscription_item": "si_00000000000000", - "type": "subscription" - } - ], - "has_more": false, - "object": "list", - "url": "/v1/invoices/in_1DpwlvHOC2HiLzOV3LVuwMuC/lines" - }, - "livemode": false, - "metadata": { - }, - "next_payment_attempt": null, - "number": "7DBE74C-0001", - "paid": true, - "period_end": 1546862399, - "period_start": 1546862399, - "receipt_number": null, - "starting_balance": 0, - "statement_descriptor": null, - "status": "open", - "subscription": null, - "subtotal": 1000, - "tax": 0, - "tax_percent": null, - "total": 1000, - "webhooks_delivered_at": null, - "closed": true - } - } -} diff --git a/zerver/webhooks/stripe/fixtures/invoice_sent.json b/zerver/webhooks/stripe/fixtures/invoice_sent.json deleted file mode 100644 index 0787d0a7b8..0000000000 --- a/zerver/webhooks/stripe/fixtures/invoice_sent.json +++ /dev/null @@ -1,106 +0,0 @@ -{ - "created": 1326853478, - "livemode": false, - "id": "invoice.sent_00000000000000", - "type": "invoice.sent", - "object": "event", - "request": null, - "pending_webhooks": 1, - "api_version": "2018-11-08", - "data": { - "object": { - "id": "in_00000000000000", - "object": "invoice", - "amount_due": 1000, - "amount_paid": 0, - "amount_remaining": 1000, - "application_fee": null, - "attempt_count": 0, - "attempted": false, - "auto_advance": true, - "billing": "send_invoice", - "billing_reason": "manual", - "charge": null, - "currency": "usd", - "custom_fields": null, - "customer": "cus_00000000000000", - "date": 1546862399, - "default_source": null, - "description": null, - "discount": null, - "due_date": 1549454425, - "ending_balance": 0, - "finalized_at": 1546862425, - "footer": null, - "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA", - "invoice_pdf": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA/pdf", - "lines": { - "data": [ - { - "id": "sli_00000000000000", - "object": "line_item", - "amount": 999, - "currency": "usd", - "description": "1 × Diamond Essentials (at $9.99 / month)", - "discountable": true, - "livemode": false, - "metadata": { - }, - "period": { - "end": 1551073074, - "start": 1548394674 - }, - "plan": { - "id": "plan_00000000000000", - "object": "plan", - "active": true, - "aggregate_usage": null, - "amount": 999, - "billing_scheme": "per_unit", - "created": 1548385214, - "currency": "usd", - "interval": "month", - "interval_count": 1, - "livemode": false, - "metadata": { - }, - "nickname": null, - "product": "prod_00000000000000", - "tiers": null, - "tiers_mode": null, - "transform_usage": null, - "trial_period_days": null, - "usage_type": "licensed" - }, - "proration": false, - "quantity": 1, - "subscription": "sub_00000000000000", - "subscription_item": "si_00000000000000", - "type": "subscription" - } - ], - "has_more": false, - "object": "list", - "url": "/v1/invoices/in_1DpwlvHOC2HiLzOV3LVuwMuC/lines" - }, - "livemode": false, - "metadata": { - }, - "next_payment_attempt": null, - "number": "7DBE74C-0001", - "paid": false, - "period_end": 1546862399, - "period_start": 1546862399, - "receipt_number": null, - "starting_balance": 0, - "statement_descriptor": null, - "status": "open", - "subscription": null, - "subtotal": 1000, - "tax": 0, - "tax_percent": null, - "total": 1000, - "webhooks_delivered_at": null - } - } -} diff --git a/zerver/webhooks/stripe/fixtures/invoice_upcoming.json b/zerver/webhooks/stripe/fixtures/invoice_upcoming.json deleted file mode 100644 index b6140f07cf..0000000000 --- a/zerver/webhooks/stripe/fixtures/invoice_upcoming.json +++ /dev/null @@ -1,106 +0,0 @@ -{ - "created": 1326853478, - "livemode": false, - "id": "invoice.upcoming_00000000000000", - "type": "invoice.upcoming", - "object": "event", - "request": null, - "pending_webhooks": 1, - "api_version": "2018-11-08", - "data": { - "object": { - "id": null, - "object": "invoice", - "amount_due": 1000, - "amount_paid": 0, - "amount_remaining": 1000, - "application_fee": null, - "attempt_count": 0, - "attempted": false, - "auto_advance": true, - "billing": "send_invoice", - "billing_reason": "manual", - "charge": null, - "currency": "usd", - "custom_fields": null, - "customer": "cus_00000000000000", - "date": 1546862399, - "default_source": null, - "description": null, - "discount": null, - "due_date": 1549454425, - "ending_balance": 0, - "finalized_at": 1546862425, - "footer": null, - "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA", - "invoice_pdf": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA/pdf", - "lines": { - "data": [ - { - "id": "sli_00000000000000", - "object": "line_item", - "amount": 999, - "currency": "usd", - "description": "1 × Titanium Corporate (at $9.99 / month)", - "discountable": true, - "livemode": false, - "metadata": { - }, - "period": { - "end": 1551113860, - "start": 1548435460 - }, - "plan": { - "id": "plan_00000000000000", - "object": "plan", - "active": true, - "aggregate_usage": null, - "amount": 999, - "billing_scheme": "per_unit", - "created": 1548434720, - "currency": "usd", - "interval": "month", - "interval_count": 1, - "livemode": false, - "metadata": { - }, - "nickname": null, - "product": "prod_00000000000000", - "tiers": null, - "tiers_mode": null, - "transform_usage": null, - "trial_period_days": null, - "usage_type": "licensed" - }, - "proration": false, - "quantity": 1, - "subscription": "sub_00000000000000", - "subscription_item": "si_00000000000000", - "type": "subscription" - } - ], - "has_more": false, - "object": "list", - "url": "/v1/invoices/in_1DpwlvHOC2HiLzOV3LVuwMuC/lines" - }, - "livemode": false, - "metadata": { - }, - "next_payment_attempt": null, - "number": "7DBE74C-0001", - "paid": false, - "period_end": 1546862399, - "period_start": 1546862399, - "receipt_number": null, - "starting_balance": 0, - "statement_descriptor": null, - "status": "open", - "subscription": null, - "subtotal": 1000, - "tax": 0, - "tax_percent": null, - "total": 1000, - "webhooks_delivered_at": null - } - } -} diff --git a/zerver/webhooks/stripe/fixtures/invoice_updated.json b/zerver/webhooks/stripe/fixtures/invoice_updated.json deleted file mode 100644 index 3aac0fdac4..0000000000 --- a/zerver/webhooks/stripe/fixtures/invoice_updated.json +++ /dev/null @@ -1,110 +0,0 @@ -{ - "created": 1326853478, - "livemode": false, - "id": "invoice.updated_00000000000000", - "type": "invoice.updated", - "object": "event", - "request": null, - "pending_webhooks": 1, - "api_version": "2018-11-08", - "data": { - "object": { - "id": "in_00000000000000", - "object": "invoice", - "amount_due": 1000, - "amount_paid": 0, - "amount_remaining": 1000, - "application_fee": null, - "attempt_count": 0, - "attempted": false, - "auto_advance": true, - "billing": "send_invoice", - "billing_reason": "manual", - "charge": null, - "currency": "usd", - "custom_fields": null, - "customer": "cus_00000000000000", - "date": 1546862399, - "default_source": null, - "description": null, - "discount": null, - "due_date": 1549454425, - "ending_balance": 0, - "finalized_at": 1546862425, - "footer": null, - "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA", - "invoice_pdf": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA/pdf", - "lines": { - "data": [ - { - "id": "sli_00000000000000", - "object": "line_item", - "amount": 999, - "currency": "usd", - "description": "1 × Titanium Corporate (at $9.99 / month)", - "discountable": true, - "livemode": false, - "metadata": { - }, - "period": { - "end": 1551114237, - "start": 1548435837 - }, - "plan": { - "id": "plan_00000000000000", - "object": "plan", - "active": true, - "aggregate_usage": null, - "amount": 999, - "billing_scheme": "per_unit", - "created": 1548434720, - "currency": "usd", - "interval": "month", - "interval_count": 1, - "livemode": false, - "metadata": { - }, - "nickname": null, - "product": "prod_00000000000000", - "tiers": null, - "tiers_mode": null, - "transform_usage": null, - "trial_period_days": null, - "usage_type": "licensed" - }, - "proration": false, - "quantity": 1, - "subscription": "sub_00000000000000", - "subscription_item": "si_00000000000000", - "type": "subscription" - } - ], - "has_more": false, - "object": "list", - "url": "/v1/invoices/in_1DpwlvHOC2HiLzOV3LVuwMuC/lines" - }, - "livemode": false, - "metadata": { - }, - "next_payment_attempt": null, - "number": "7DBE74C-0001", - "paid": false, - "period_end": 1546862399, - "period_start": 1546862399, - "receipt_number": null, - "starting_balance": 0, - "statement_descriptor": null, - "status": "open", - "subscription": null, - "subtotal": 1000, - "tax": 0, - "tax_percent": null, - "total": 1000, - "webhooks_delivered_at": null - }, - "previous_attributes": { - "lines": [ - ] - } - } -} diff --git a/zerver/webhooks/stripe/fixtures/invoice_voided.json b/zerver/webhooks/stripe/fixtures/invoice_voided.json deleted file mode 100644 index fd0a313b3b..0000000000 --- a/zerver/webhooks/stripe/fixtures/invoice_voided.json +++ /dev/null @@ -1,106 +0,0 @@ -{ - "created": 1326853478, - "livemode": false, - "id": "invoice.voided_00000000000000", - "type": "invoice.voided", - "object": "event", - "request": null, - "pending_webhooks": 1, - "api_version": "2018-11-08", - "data": { - "object": { - "id": "in_00000000000000", - "object": "invoice", - "amount_due": 1000, - "amount_paid": 0, - "amount_remaining": 1000, - "application_fee": null, - "attempt_count": 0, - "attempted": false, - "auto_advance": true, - "billing": "send_invoice", - "billing_reason": "manual", - "charge": null, - "currency": "usd", - "custom_fields": null, - "customer": "cus_00000000000000", - "date": 1546862399, - "default_source": null, - "description": null, - "discount": null, - "due_date": 1549454425, - "ending_balance": 0, - "finalized_at": 1546862425, - "footer": null, - "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA", - "invoice_pdf": "https://pay.stripe.com/invoice/invst_IH3jVDl7ovgEn6FgamLGRz0uiA/pdf", - "lines": { - "data": [ - { - "id": "sli_00000000000000", - "object": "line_item", - "amount": 999, - "currency": "usd", - "description": "1 × Diamond Essentials (at $9.99 / month)", - "discountable": true, - "livemode": false, - "metadata": { - }, - "period": { - "end": 1551073198, - "start": 1548394798 - }, - "plan": { - "id": "plan_00000000000000", - "object": "plan", - "active": true, - "aggregate_usage": null, - "amount": 999, - "billing_scheme": "per_unit", - "created": 1548385214, - "currency": "usd", - "interval": "month", - "interval_count": 1, - "livemode": false, - "metadata": { - }, - "nickname": null, - "product": "prod_00000000000000", - "tiers": null, - "tiers_mode": null, - "transform_usage": null, - "trial_period_days": null, - "usage_type": "licensed" - }, - "proration": false, - "quantity": 1, - "subscription": "sub_00000000000000", - "subscription_item": "si_00000000000000", - "type": "subscription" - } - ], - "has_more": false, - "object": "list", - "url": "/v1/invoices/in_1DpwlvHOC2HiLzOV3LVuwMuC/lines" - }, - "livemode": false, - "metadata": { - }, - "next_payment_attempt": null, - "number": "7DBE74C-0001", - "paid": false, - "period_end": 1546862399, - "period_start": 1546862399, - "receipt_number": null, - "starting_balance": 0, - "statement_descriptor": null, - "status": "open", - "subscription": null, - "subtotal": 1000, - "tax": 0, - "tax_percent": null, - "total": 1000, - "webhooks_delivered_at": null - } - } -} diff --git a/zerver/webhooks/stripe/tests.py b/zerver/webhooks/stripe/tests.py index 95edd61a1f..ad8796df98 100644 --- a/zerver/webhooks/stripe/tests.py +++ b/zerver/webhooks/stripe/tests.py @@ -112,6 +112,12 @@ Billing method: send invoice""" self.send_and_test_stream_message('customer_discount_created', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") + def test_invoice_payment_failed(self) -> None: + expected_topic = u"cus_00000000000000" + expected_message = u"[Invoice](https://dashboard.stripe.com/invoices/in_00000000000000) payment failed" + self.send_and_test_stream_message('invoice_payment_failed', expected_topic, expected_message, + content_type="application/x-www-form-urlencoded") + def test_payout_canceled(self) -> None: expected_topic = u"Payout po_00000000000000" expected_message = u"**[Payout](https://dashboard.stripe.com/payout/po_00000000000000)** for amount **$11.00** has been canceled." @@ -146,62 +152,3 @@ Billing method: send invoice""" self.send_and_test_stream_message('payout_updated', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") - - def test_invoice_created(self) -> None: - expected_topic = u"invoice in_00000000000000" - expected_message = u"An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been created." - self.send_and_test_stream_message('invoice_created', expected_topic, expected_message, - content_type="application/x-www-form-urlencoded") - - def test_invoice_deleted(self) -> None: - expected_topic = u"invoice in_00000000000000" - expected_message = u"An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been deleted." - self.send_and_test_stream_message('invoice_deleted', expected_topic, expected_message, - content_type="application/x-www-form-urlencoded") - - def test_invoice_finalized(self) -> None: - expected_topic = u"invoice in_00000000000000" - expected_message = u"An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been finalized." - self.send_and_test_stream_message('invoice_finalized', expected_topic, expected_message, - content_type="application/x-www-form-urlencoded") - - def test_invoice_marked_uncollectible(self) -> None: - expected_topic = u"invoice in_00000000000000" - expected_message = u"An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been marked as uncollectible." - self.send_and_test_stream_message('invoice_marked_uncollectible', expected_topic, expected_message, content_type="application/x-www-form-urlencoded") - - def test_invoice_payment_failed(self) -> None: - expected_topic = u"invoice in_00000000000000" - expected_message = u"An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has failed." - self.send_and_test_stream_message('invoice_payment_failed', expected_topic, expected_message, - content_type="application/x-www-form-urlencoded") - - def test_invoice_payment_succeeded(self) -> None: - expected_topic = u"invoice in_00000000000000" - expected_message = u"An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has succeeded." - self.send_and_test_stream_message('invoice_payment_succeeded', expected_topic, expected_message, - content_type="application/x-www-form-urlencoded") - - def test_invoice_sent(self) -> None: - expected_topic = u"invoice in_00000000000000" - expected_message = u"An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been sent." - self.send_and_test_stream_message('invoice_sent', expected_topic, expected_message, - content_type="application/x-www-form-urlencoded") - - def test_invoice_upcoming(self) -> None: - expected_topic = u"invoice None" - expected_message = u"An upcoming **[invoice](https://dashboard.stripe.com/invoices/None)** for the payment of amount **$10.00** has been created." - self.send_and_test_stream_message('invoice_upcoming', expected_topic, expected_message, - content_type="application/x-www-form-urlencoded") - - def test_invoice_updated(self) -> None: - expected_topic = u"invoice in_00000000000000" - expected_message = u"An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been updated." - self.send_and_test_stream_message('invoice_updated', expected_topic, expected_message, - content_type="application/x-www-form-urlencoded") - - def test_invoice_voided(self) -> None: - expected_topic = u"invoice in_00000000000000" - expected_message = u"An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been voided." - self.send_and_test_stream_message('invoice_voided', expected_topic, expected_message, - content_type="application/x-www-form-urlencoded") diff --git a/zerver/webhooks/stripe/view.py b/zerver/webhooks/stripe/view.py index b523573893..73ccf4bf5a 100644 --- a/zerver/webhooks/stripe/view.py +++ b/zerver/webhooks/stripe/view.py @@ -12,19 +12,6 @@ from zerver.lib.webhooks.common import check_send_webhook_message, \ UnexpectedWebhookEventType from zerver.models import UserProfile -INVOICE_INFO = { - "created": "has been created", - "deleted": "has been deleted", - "finalized": "has been finalized", - "marked_uncollectible": "has been marked as uncollectible", - "payment_failed": "has failed", - "payment_succeeded": "has succeeded", - "sent": "has been sent", - "upcoming": "has been created", - "updated": "has been updated", - "voided": "has been voided" -} - class SuppressedEvent(Exception): pass @@ -159,6 +146,18 @@ def topic_and_body(payload: Dict[str, Any]) -> Tuple[str, str]: topic = 'files' body = default_body() + ' ({purpose}). \nTitle: {title}'.format( purpose=object_['purpose'].replace('_', ' '), title=object_['title']) + if category == 'invoice': + if event == 'upcoming': # nocoverage + body = 'Upcoming invoice created' + else: + body = default_body(update_blacklist=['lines', 'description', 'number', 'finalized_at']) + if event == 'created': # nocoverage + # Could potentially add link to invoice PDF here + body += ' ({reason})\nBilling method: {method}\nTotal: {total}\nAmount due: {due}'.format( + reason=object_['billing_reason'].replace('_', ' '), + method=object_['billing'].replace('_', ' '), + total=amount_string(object_['total'], object_['currency']), + due=amount_string(object_['amount_due'], object_['currency'])) if category == 'invoiceitem': # nocoverage body = default_body(update_blacklist=['description']) if event == 'created': @@ -197,28 +196,6 @@ def topic_and_body(payload: Dict[str, Any]) -> Tuple[str, str]: amount=amount, end=end ) - - if category.startswith('invoice'): - object_id = object_['id'] - link = "https://dashboard.stripe.com/invoices/{}".format(object_id) - amount = amount_string(object_["amount_due"], object_["currency"]) - body_t = "An {invoice_type} **[invoice]({link})** for the payment of amount **{amount}** {end}." - - end = INVOICE_INFO.get(event) # type: ignore # expression has type Optional[str] - - if event == "upcoming": - invoice_type = "upcoming" - else: - invoice_type = "" - - topic = "invoice {}".format(object_id) - body = body_t.format( - invoice_type = invoice_type, - link=link, - amount=amount, - end=end - ) - if category in ['payment_intent', 'plan', 'product', 'recipient', 'reporting', 'review', 'sigma', 'sku', 'source', 'subscription_schedule', 'topup', 'transfer']: # nocoverage