Skip to content

Commit

Permalink
ECP-2415 -- add extra details to charge request (#92)
Browse files Browse the repository at this point in the history
* add extra details to charge request

* add test user model to resolve test failures

* updates from change review

* changelog and version bump
  • Loading branch information
nicmollel-ro authored Jun 25, 2024
1 parent c5b26df commit 3ad5e3e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Change Log
All notable changes to this project will be documented in this file.

##[0.13.0]
### Added
- optional metadata field for member identitifer `member_uuid`
- optional metadata field for payment identifier `payment_uuid`


##[0.12.0]
### Removed
- Removed support for Python <3.9
Expand Down
2 changes: 1 addition & 1 deletion aa_stripe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
__title__ = "Ro Stripe"
__version__ = "0.12.0"
__version__ = "0.13.0"
__author__ = "Remigiusz Dymecki"
__license__ = "MIT"
__copyright__ = "Copyright 2019 Ro"
Expand Down
13 changes: 11 additions & 2 deletions aa_stripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ class StripeCharge(StripeBasicModel):
source = generic.GenericForeignKey("content_type", "object_id")
statement_descriptor = models.CharField(max_length=22, blank=True)

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

if self.is_charged:
Expand All @@ -410,7 +410,16 @@ def charge(self, idempotency_key=None):
"object_id": self.object_id,
"content_type_id": self.content_type_id,
}
idempotency_key = "{}-{}-{}".format(metadata["object_id"], metadata["content_type_id"], idempotency_key)
if settings.PAYMENT_ORIGIN:
metadata["origin"] = settings.PAYMENT_ORIGIN
if hasattr(self.user, "uuid"):
metadata["member_uuid"] = str(self.user.uuid)
if payment_uuid:
metadata["payment_uuid"] = str(payment_uuid)

idempotency_key = "{}-{}-{}".format(
metadata["object_id"], metadata["content_type_id"], idempotency_key
)

params = {
"amount": self.amount,
Expand Down
7 changes: 6 additions & 1 deletion aa_stripe/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from aa_stripe.settings import stripe_settings
stripe_settings.API_KEY # "api_key"
"""

from __future__ import unicode_literals

from django.conf import settings
Expand All @@ -20,9 +21,13 @@
"PENDING_WEBHOOKS_THRESHOLD": 20,
"API_KEY": "",
"WEBHOOK_ENDPOINT_SECRET": "",
"USER_MODEL": settings.AUTH_USER_MODEL
"USER_MODEL": settings.AUTH_USER_MODEL,
}

PAYMENT_ORIGIN = (
settings.PAYMENT_ORIGIN if hasattr(settings, "PAYMENT_ORIGIN") else None
)


class StripeSettings(object):
def __getattr__(self, attr):
Expand Down
8 changes: 5 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ def pytest_configure():
"django.contrib.admin",
"django.contrib.sites",
"rest_framework",
"aa_stripe"
"aa_stripe",
"tests",
),
AUTH_USER_MODEL="tests.TestUser",
PAYMENT_ORIGIN="test-roman_api",
ROOT_URLCONF="aa_stripe.api_urls",
TESTING=True,

ENV_PREFIX="test-env",
STRIPE_SETTINGS_API_KEY="apikey",
STRIPE_SETTINGS_WEBHOOK_ENDPOINT_SECRET="fake"
STRIPE_SETTINGS_WEBHOOK_ENDPOINT_SECRET="fake",
)
8 changes: 8 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import uuid

from django.contrib.auth.models import AbstractUser
from django.db import models


class TestUser(AbstractUser):
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
4 changes: 4 additions & 0 deletions tests/test_charge.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Test charging users through the StripeCharge model"""

import sys
from io import StringIO

import mock
import stripe
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.test import TestCase
Expand Down Expand Up @@ -144,6 +146,8 @@ def test_charge_with_idempotency_key(self, charge_create_mocked):
metadata={
"object_id": self.charge.object_id,
"content_type_id": self.charge.content_type_id,
"origin": settings.PAYMENT_ORIGIN,
"member_uuid": str(self.user.uuid),
},
)

Expand Down

0 comments on commit 3ad5e3e

Please sign in to comment.