Skip to content

Commit

Permalink
chore: Updated dj-stripe (2.6.3 -> 2.8.1) and stripe-mock (0.110.0 ->…
Browse files Browse the repository at this point in the history
… 0.170.0) versions. (closes #191).
  • Loading branch information
kailip committed Aug 28, 2023
1 parent 7917fb1 commit 3ec8d7e
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ services:
- db

stripemock:
image: stripemock/stripe-mock:v0.103.0
image: stripe/stripe-mock:v0.170.0
ports:
- "12111:12111"
- "12112:12112"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# dj-stripe in version 2.8.0 dropped support for text-based JSON fields and hasn't provided migration to work it out.
#
# The migration below is based on the discussion by dj-stripe team available under those links:
# - https://github.com/dj-stripe/dj-stripe/issues/1566
# - https://github.com/dj-stripe/dj-stripe/issues/517

from django.core.management.base import BaseCommand
from django.db import transaction, connection

AFFECTED_FIELDS = [
("djstripe_account", "business_profile"),
("djstripe_account", "company"),
("djstripe_account", "individual"),
("djstripe_account", "requirements"),
("djstripe_account", "settings"),
("djstripe_account", "tos_acceptance"),
("djstripe_invoice", "customer_address"),
("djstripe_invoice", "customer_shipping"),
("djstripe_invoice", "discount"),
("djstripe_invoice", "status_transitions"),
("djstripe_invoice", "threshold_reason"),
("djstripe_upcominginvoice", "customer_address"),
("djstripe_upcominginvoice", "customer_shipping"),
("djstripe_upcominginvoice", "discount"),
("djstripe_upcominginvoice", "status_transitions"),
("djstripe_upcominginvoice", "threshold_reason"),
("djstripe_invoiceitem", "period"),
("djstripe_plan", "tiers"),
("djstripe_plan", "transform_usage"),
("djstripe_subscription", "billing_thresholds"),
("djstripe_subscription", "discount"),
("djstripe_subscription", "pending_invoice_item_interval"),
("djstripe_subscription", "pending_update"),
("djstripe_subscriptionitem", "billing_thresholds"),
("djstripe_subscriptionschedule", "current_phase"),
("djstripe_subscriptionschedule", "default_settings"),
("djstripe_subscriptionschedule", "phases"),
("djstripe_taxid", "verification"),
("djstripe_usagerecordsummary", "period"),
("djstripe_session", "display_items"),
("djstripe_session", "payment_method_types"),
("djstripe_countryspec", "supported_bank_account_currencies"),
("djstripe_countryspec", "supported_payment_currencies"),
("djstripe_countryspec", "supported_payment_methods"),
("djstripe_countryspec", "supported_transfer_countries"),
("djstripe_countryspec", "verification_fields"),
("djstripe_balancetransaction", "fee_details"),
("djstripe_charge", "billing_details"),
("djstripe_charge", "fraud_details"),
("djstripe_charge", "outcome"),
("djstripe_charge", "payment_method_details"),
("djstripe_charge", "shipping"),
("djstripe_charge", "transfer_data"),
("djstripe_mandate", "customer_acceptance"),
("djstripe_mandate", "payment_method_details"),
("djstripe_mandate", "multi_use"),
("djstripe_mandate", "single_use"),
("djstripe_product", "attributes"),
("djstripe_product", "deactivate_on"),
("djstripe_product", "images"),
("djstripe_product", "package_dimensions"),
("djstripe_customer", "address"),
("djstripe_customer", "invoice_settings"),
("djstripe_customer", "preferred_locales"),
("djstripe_customer", "shipping"),
("djstripe_dispute", "balance_transactions"),
("djstripe_dispute", "evidence"),
("djstripe_dispute", "evidence_details"),
("djstripe_event", "data"),
("djstripe_paymentintent", "last_payment_error"),
("djstripe_paymentintent", "next_action"),
("djstripe_paymentintent", "payment_method_types"),
("djstripe_paymentintent", "shipping"),
("djstripe_paymentintent", "transfer_data"),
("djstripe_setupintent", "last_setup_error"),
("djstripe_setupintent", "next_action"),
("djstripe_setupintent", "payment_method_types"),
("djstripe_price", "recurring"),
("djstripe_price", "tiers"),
("djstripe_price", "transform_quantity"),
("djstripe_source", "owner"),
("djstripe_source", "code_verification"),
("djstripe_source", "receiver"),
("djstripe_source", "redirect"),
("djstripe_source", "source_data"),
("djstripe_paymentmethod", "billing_details"),
("djstripe_paymentmethod", "acss_debit"),
("djstripe_paymentmethod", "afterpay_clearpay"),
("djstripe_paymentmethod", "alipay"),
("djstripe_paymentmethod", "au_becs_debit"),
("djstripe_paymentmethod", "bacs_debit"),
("djstripe_paymentmethod", "bancontact"),
("djstripe_paymentmethod", "boleto"),
("djstripe_paymentmethod", "card"),
("djstripe_paymentmethod", "card_present"),
("djstripe_paymentmethod", "eps"),
("djstripe_paymentmethod", "fpx"),
("djstripe_paymentmethod", "giropay"),
("djstripe_paymentmethod", "grabpay"),
("djstripe_paymentmethod", "ideal"),
("djstripe_paymentmethod", "interac_present"),
("djstripe_paymentmethod", "oxxo"),
("djstripe_paymentmethod", "p24"),
("djstripe_paymentmethod", "sepa_debit"),
("djstripe_paymentmethod", "sofort"),
("djstripe_paymentmethod", "wechat_pay"),
("djstripe_scheduledqueryrun", "error"),
("djstripe_webhookendpoint", "enabled_events"),
("djstripe_webhookeventtrigger", "headers"),
("djstripe_account", "metadata"),
("djstripe_coupon", "metadata"),
("djstripe_invoice", "metadata"),
("djstripe_upcominginvoice", "metadata"),
("djstripe_invoiceitem", "metadata"),
("djstripe_plan", "metadata"),
("djstripe_subscription", "metadata"),
("djstripe_subscriptionitem", "metadata"),
("djstripe_subscriptionschedule", "metadata"),
("djstripe_taxrate", "metadata"),
("djstripe_session", "metadata"),
("djstripe_applicationfee", "metadata"),
("djstripe_applicationfeerefund", "metadata"),
("djstripe_transfer", "metadata"),
("djstripe_transferreversal", "metadata"),
("djstripe_balancetransaction", "metadata"),
("djstripe_charge", "metadata"),
("djstripe_mandate", "metadata"),
("djstripe_product", "metadata"),
("djstripe_customer", "metadata"),
("djstripe_dispute", "metadata"),
("djstripe_event", "metadata"),
("djstripe_file", "metadata"),
("djstripe_filelink", "metadata"),
("djstripe_paymentintent", "metadata"),
("djstripe_setupintent", "metadata"),
("djstripe_payout", "metadata"),
("djstripe_price", "metadata"),
("djstripe_refund", "metadata"),
("djstripe_bankaccount", "metadata"),
("djstripe_card", "metadata"),
("djstripe_source", "metadata"),
("djstripe_paymentmethod", "metadata"),
("djstripe_scheduledqueryrun", "metadata"),
("djstripe_webhookendpoint", "metadata"),
]


class Command(BaseCommand):
help = 'Migrate text-based JSON fields to json-based ones to support dj-stripe v2.8.0'

@transaction.atomic
def handle(self, *args, **options):
with connection.cursor() as cursor:
for af in AFFECTED_FIELDS:
self.stdout.write(f'Migrating field "{af[1]}" of "{af[0]}" table..')
raw_sql = f'ALTER TABLE {af[0]} ALTER COLUMN "{af[1]}" TYPE jsonb USING "{af[1]}"::text::jsonb;'
cursor.execute(raw_sql)
self.stdout.write(self.style.SUCCESS("Migration successful!"))
25 changes: 25 additions & 0 deletions packages/backend/apps/finances/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest_factoryboy
import stripe
from django.contrib.auth import get_user_model
from djstripe.models import Account

from . import factories
from .. import constants
Expand Down Expand Up @@ -88,6 +89,30 @@ def yearly_plan_price(price_factory, plan_factory):
return price


@pytest.fixture(scope='function', autouse=True)
def stripe_mock_fixture_product_default_price(price_factory):
"""
stripe-mock v0.128.0 introduces "default_price" field in returned product fixture. In some cases the tests may fail,
if the Price object with this id does not exist in database. Adding it manually to avoid that issue. The hardcoded
ID may need to be changed when the Stripe-mock is updated to the newer version.
"""
return price_factory(id="price_1NapVeJr3d0nrouD2gX5mHOY")


@pytest.fixture(scope='function', autouse=True)
def stripe_find_owner_account_monkey_patch():
"""
dj-stripe v2.7.0 restored checking for owner of an Account object. Stripe-mock returns different account ID on
every call, what leads to infinite loop of fetching accounts from Stripe-mock. This monkey patch restores old
_find_owner_account functionality of the Account model.
"""

def _find_owner_account(cls, data, api_key):
return None

Account._find_owner_account = classmethod(_find_owner_account)


@pytest.fixture(scope='function', autouse=True)
def stripe_proxy():
stripe.api_base = "http://stripemock:12111"
Expand Down
7 changes: 3 additions & 4 deletions packages/backend/apps/finances/tests/test_webhooks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import datetime

from djstripe.enums import RefundStatus, RefundFailureReason

import calleee
import pytest

from djstripe import models as djstripe_models
from djstripe.enums import RefundStatus, RefundFailureReason

from .utils import stripe_encode
from .. import notifications

Expand All @@ -25,7 +24,7 @@ def test_previously_trialing_subscription_is_canceled(
# The hardcoded ID is equal to the one returned from stripe-mock
# If the test fails after stripe-mock update you most likely need to change this to match their
# fixtures
'id': 'si_IyZyeAN1KSAd6Z',
'id': 'si_OQqOMhX37fUC5o',
'price': monthly_plan_price.id,
}
],
Expand Down
1 change: 0 additions & 1 deletion packages/backend/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@
STRIPE_CHECKS_ENABLED = env.bool("STRIPE_CHECKS_ENABLED", default=True)
if not STRIPE_CHECKS_ENABLED:
SILENCED_SYSTEM_CHECKS.append("djstripe.C001")
DJSTRIPE_USE_NATIVE_JSONFIELD = False

SUBSCRIPTION_TRIAL_PERIOD_DAYS = env("SUBSCRIPTION_TRIAL_PERIOD_DAYS", default=7)

Expand Down
30 changes: 8 additions & 22 deletions packages/backend/pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ dependencies = [
"django-hashid-field~=3.3",
"django-hosts~=5.2",
"drf-access-policy~=1.1",
"dj-stripe==2.6.3",
"dj-stripe==2.8.1",
"psycopg2-binary~=2.9",
"requests~=2.28",
"sentry-sdk~=1.14",
Expand Down

0 comments on commit 3ec8d7e

Please sign in to comment.