From 9a606053ee6d57eb8c98a7cbabde2b80fc0c89c7 Mon Sep 17 00:00:00 2001
From: Pelle Koster <pelle.koster@geant.org>
Date: Mon, 27 Jan 2025 16:09:11 +0100
Subject: [PATCH] update billing address for existings customers

---
 stripe_checkout/stripe_checkout/stripe.py |  7 +++
 test/conftest.py                          |  1 +
 test/test_stripe.py                       | 61 +++++++++++++++++++++++
 3 files changed, 69 insertions(+)
 create mode 100644 test/test_stripe.py

diff --git a/stripe_checkout/stripe_checkout/stripe.py b/stripe_checkout/stripe_checkout/stripe.py
index 67b492d..5cbe462 100644
--- a/stripe_checkout/stripe_checkout/stripe.py
+++ b/stripe_checkout/stripe_checkout/stripe.py
@@ -37,6 +37,13 @@ def get_or_create_customer(visitor: Visitor) -> Optional[str]:
             address=parse_address(visitor.billing_address),
         )
         return customer["id"]
+    customer_id = result[0]["id"]
+    stripe.Customer.modify(
+        customer_id,
+        name=visitor.full_name,
+        address=parse_address(visitor.billing_address),
+    )
+
     return result[0]["id"]
 
 
diff --git a/test/conftest.py b/test/conftest.py
index 5abb1fb..740bfd5 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -177,6 +177,7 @@ def mock_stripe(item_data):
             "stripe.Customer.search", return_value={"data": [{"id": "stripe-cus-id"}]}
         ),
         patch("stripe.Customer.create", return_value={"id": "stripe-cus-id"}),
+        patch("stripe.Customer.modify", return_value={"id": "stripe-cus-id"}),
         patch("stripe.Invoice.create", return_value={"id": "stripe-inv-id"}),
         patch("stripe.InvoiceItem.create", return_value={"id": "stripe-invitem-id"}),
         patch(
diff --git a/test/test_stripe.py b/test/test_stripe.py
new file mode 100644
index 0000000..694ea2d
--- /dev/null
+++ b/test/test_stripe.py
@@ -0,0 +1,61 @@
+import pytest
+import stripe
+from stripe_checkout.stripe_checkout.stripe import get_or_create_customer
+
+
+@pytest.mark.parametrize(
+    "visit_addresses, stripe_address",
+    [
+        ([], {}),
+        ([{"country": "NL", "type": "postal"}], {}),
+        (
+            [
+                {"country": "NL", "type": "postal"},
+                {"country": "BE", "type": "billing"},
+            ],
+            {"country": "BE"},
+        ),
+        (
+            [
+                {
+                    "address": "line 1\nline 2",
+                    "city": "cityasdf",
+                    "country": "NL",
+                    "houseNumber": "1234",
+                    "houseNumberSuffix": "A",
+                    "postalCode": "1235AF",
+                    "state": "state-asdf",
+                    "type": "billing",
+                }
+            ],
+            {
+                "line1": "line 1\nline 2 1234 A",
+                "postal_code": "1235AF",
+                "city": "cityasdf",
+                "state": "state-asdf",
+                "country": "NL",
+            },
+        ),
+    ],
+)
+def test_create_customer_with_billing_address(
+    visit_addresses, stripe_address, default_visitor, mock_stripe
+):
+
+    default_visitor["contact"]["addresses"] = visit_addresses
+    stripe.Customer.search.return_value = {"data": []}
+    get_or_create_customer(default_visitor)
+    assert stripe.Customer.create.call_count == 1
+
+    full_result = stripe.Customer.create.call_args.kwargs["address"]
+    non_empty = {key: val for key, val in full_result.items() if val}
+    assert non_empty == stripe_address
+
+
+def test_updates_visitor_with_address(default_visitor, mock_stripe):
+    default_visitor["contact"]["addresses"] = [{"country": "BE", "type": "billing"}]
+    get_or_create_customer(default_visitor)
+    assert stripe.Customer.modify.call_count == 1
+    new_address = stripe.Customer.modify.call_args.kwargs["address"]
+    non_empty = {key: val for key, val in new_address.items() if val}
+    assert non_empty == {"country": "BE"}
-- 
GitLab