Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit 3ad5e3e

Browse files
authored
ECP-2415 -- add extra details to charge request (#92)
* add extra details to charge request * add test user model to resolve test failures * updates from change review * changelog and version bump
1 parent c5b26df commit 3ad5e3e

File tree

7 files changed

+41
-7
lines changed

7 files changed

+41
-7
lines changed

CHANGELOG.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
##[0.13.0]
5+
### Added
6+
- optional metadata field for member identitifer `member_uuid`
7+
- optional metadata field for payment identifier `payment_uuid`
8+
9+
410
##[0.12.0]
511
### Removed
612
- Removed support for Python <3.9

aa_stripe/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
__title__ = "Ro Stripe"
3-
__version__ = "0.12.0"
3+
__version__ = "0.13.0"
44
__author__ = "Remigiusz Dymecki"
55
__license__ = "MIT"
66
__copyright__ = "Copyright 2019 Ro"

aa_stripe/models.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ class StripeCharge(StripeBasicModel):
396396
source = generic.GenericForeignKey("content_type", "object_id")
397397
statement_descriptor = models.CharField(max_length=22, blank=True)
398398

399-
def charge(self, idempotency_key=None):
399+
def charge(self, idempotency_key=None, payment_uuid=None):
400400
self.refresh_from_db() # to minimize the chance of double charging
401401

402402
if self.is_charged:
@@ -410,7 +410,16 @@ def charge(self, idempotency_key=None):
410410
"object_id": self.object_id,
411411
"content_type_id": self.content_type_id,
412412
}
413-
idempotency_key = "{}-{}-{}".format(metadata["object_id"], metadata["content_type_id"], idempotency_key)
413+
if settings.PAYMENT_ORIGIN:
414+
metadata["origin"] = settings.PAYMENT_ORIGIN
415+
if hasattr(self.user, "uuid"):
416+
metadata["member_uuid"] = str(self.user.uuid)
417+
if payment_uuid:
418+
metadata["payment_uuid"] = str(payment_uuid)
419+
420+
idempotency_key = "{}-{}-{}".format(
421+
metadata["object_id"], metadata["content_type_id"], idempotency_key
422+
)
414423

415424
params = {
416425
"amount": self.amount,

aa_stripe/settings.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from aa_stripe.settings import stripe_settings
1212
stripe_settings.API_KEY # "api_key"
1313
"""
14+
1415
from __future__ import unicode_literals
1516

1617
from django.conf import settings
@@ -20,9 +21,13 @@
2021
"PENDING_WEBHOOKS_THRESHOLD": 20,
2122
"API_KEY": "",
2223
"WEBHOOK_ENDPOINT_SECRET": "",
23-
"USER_MODEL": settings.AUTH_USER_MODEL
24+
"USER_MODEL": settings.AUTH_USER_MODEL,
2425
}
2526

27+
PAYMENT_ORIGIN = (
28+
settings.PAYMENT_ORIGIN if hasattr(settings, "PAYMENT_ORIGIN") else None
29+
)
30+
2631

2732
class StripeSettings(object):
2833
def __getattr__(self, attr):

tests/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ def pytest_configure():
1919
"django.contrib.admin",
2020
"django.contrib.sites",
2121
"rest_framework",
22-
"aa_stripe"
22+
"aa_stripe",
23+
"tests",
2324
),
25+
AUTH_USER_MODEL="tests.TestUser",
26+
PAYMENT_ORIGIN="test-roman_api",
2427
ROOT_URLCONF="aa_stripe.api_urls",
2528
TESTING=True,
26-
2729
ENV_PREFIX="test-env",
2830
STRIPE_SETTINGS_API_KEY="apikey",
29-
STRIPE_SETTINGS_WEBHOOK_ENDPOINT_SECRET="fake"
31+
STRIPE_SETTINGS_WEBHOOK_ENDPOINT_SECRET="fake",
3032
)

tests/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import uuid
2+
3+
from django.contrib.auth.models import AbstractUser
4+
from django.db import models
5+
6+
7+
class TestUser(AbstractUser):
8+
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)

tests/test_charge.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Test charging users through the StripeCharge model"""
2+
23
import sys
34
from io import StringIO
45

56
import mock
67
import stripe
8+
from django.conf import settings
79
from django.contrib.auth import get_user_model
810
from django.core.management import call_command
911
from django.test import TestCase
@@ -144,6 +146,8 @@ def test_charge_with_idempotency_key(self, charge_create_mocked):
144146
metadata={
145147
"object_id": self.charge.object_id,
146148
"content_type_id": self.charge.content_type_id,
149+
"origin": settings.PAYMENT_ORIGIN,
150+
"member_uuid": str(self.user.uuid),
147151
},
148152
)
149153

0 commit comments

Comments
 (0)