webhooks/stripe: Fix the invoice_created event.

The previous code for this event was using a key that's not actually
a part of the payload. So here we simple remove the usage of that key
and add a (previously missing) test for this event.

Signed-off-by: Hemanth V. Alluri <hdrive1999@gmail.com>
This commit is contained in:
Hemanth V. Alluri
2020-06-03 08:59:16 +05:30
committed by Tim Abbott
parent 7dbdfe9a97
commit 9a9c1e0794
3 changed files with 151 additions and 3 deletions

View File

@@ -0,0 +1,140 @@
{
"id": "evt_1GpmuvHLwdCOCoR7Q22hCa2N",
"object": "event",
"api_version": "2020-03-02",
"created": 1591153524,
"data": {
"object": {
"id": "in_1GpmuuHLwdCOCoR7ghzQDQLW",
"object": "invoice",
"account_country": "IN",
"account_name": null,
"amount_due": 0,
"amount_paid": 0,
"amount_remaining": 0,
"application_fee_amount": null,
"attempt_count": 0,
"attempted": false,
"auto_advance": false,
"billing_reason": "manual",
"charge": null,
"collection_method": "send_invoice",
"created": 1591153524,
"currency": "inr",
"custom_fields": null,
"customer": "cus_HH97asvHvaYQYp",
"customer_address": null,
"customer_email": "zenitsu_agatsuma@mail.example.com",
"customer_name": "Zenitsu Agatsuma",
"customer_phone": null,
"customer_shipping": {
"address": {
"city": "",
"country": "",
"line1": "",
"line2": "",
"postal_code": "",
"state": ""
},
"name": "Zenitsu Agatsuma",
"phone": ""
},
"customer_tax_exempt": "none",
"customer_tax_ids": [],
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [],
"description": null,
"discount": null,
"due_date": null,
"ending_balance": null,
"footer": null,
"hosted_invoice_url": null,
"invoice_pdf": null,
"lines": {
"object": "list",
"data": [
{
"id": "il_1GpmuuHLwdCOCoR7nCRM5MXl",
"object": "line_item",
"amount": 0,
"currency": "inr",
"description": null,
"discountable": true,
"invoice_item": "ii_1GpmuuHLwdCOCoR7VOeTr2BM",
"livemode": false,
"metadata": {},
"period": {
"end": 1591153524,
"start": 1591153524
},
"plan": null,
"price": {
"id": "price_1GpmuuHLwdCOCoR7ff5A7n1i",
"object": "price",
"active": false,
"billing_scheme": "per_unit",
"created": 1591153524,
"currency": "inr",
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_HOa5MrXjZ9YBuA",
"recurring": null,
"tiers_mode": null,
"transform_quantity": null,
"type": "one_time",
"unit_amount": 0,
"unit_amount_decimal": "0"
},
"proration": false,
"quantity": 1,
"subscription": null,
"tax_amounts": [],
"tax_rates": [],
"type": "invoiceitem"
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/invoices/in_1GpmuuHLwdCOCoR7ghzQDQLW/lines"
},
"livemode": false,
"metadata": {},
"next_payment_attempt": null,
"number": null,
"paid": false,
"payment_intent": null,
"period_end": 1591153524,
"period_start": 1591153524,
"post_payment_credit_notes_amount": 0,
"pre_payment_credit_notes_amount": 0,
"receipt_number": null,
"starting_balance": 0,
"statement_descriptor": null,
"status": "draft",
"status_transitions": {
"finalized_at": null,
"marked_uncollectible_at": null,
"paid_at": null,
"voided_at": null
},
"subscription": null,
"subtotal": 0,
"tax": null,
"tax_percent": null,
"total": 0,
"total_tax_amounts": [],
"transfer_data": null,
"webhooks_delivered_at": null
}
},
"livemode": false,
"pending_webhooks": 1,
"request": {
"id": "req_cz0vh1VJ1U4MzE",
"idempotency_key": null
},
"type": "invoice.created"
}

View File

@@ -118,6 +118,15 @@ Billing method: send invoice"""
self.send_and_test_stream_message('invoice_payment_failed', expected_topic, expected_message,
content_type="application/x-www-form-urlencoded")
def test_invoice_created(self) -> None:
expected_topic = "cus_HH97asvHvaYQYp"
expected_message = """
[Invoice](https://dashboard.stripe.com/invoices/in_1GpmuuHLwdCOCoR7ghzQDQLW) created (manual)
Total: 0.00 INR
Amount due: 0.00 INR
""".strip()
self.send_and_test_stream_message("invoice_created", expected_topic, expected_message)
def test_invoiceitem_created(self) -> None:
expected_topic = "cus_00000000000000"
expected_message = "[Invoice item](https://dashboard.stripe.com/invoiceitems/ii_00000000000000) created for 10.00 CAD"

View File

@@ -166,11 +166,10 @@ def topic_and_body(payload: Dict[str, Any]) -> Tuple[str, str]:
else:
body = default_body(update_blacklist=['lines', 'description', 'number', 'finalized_at',
'status_transitions', 'payment_intent'])
if event == 'created': # nocoverage
if event == 'created':
# Could potentially add link to invoice PDF here
body += ' ({reason})\nBilling method: {method}\nTotal: {total}\nAmount due: {due}'.format(
body += ' ({reason})\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':